17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9a70fc3bSMark J. Nelson  * Common Development and Distribution License (the "License").
6*9a70fc3bSMark J. Nelson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
237c478bd9Sstevel@tonic-gate  * All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate //  SDAAdvert.java:   Server Side DAAdvert Message.
287c478bd9Sstevel@tonic-gate //  Author:           James Kempf
297c478bd9Sstevel@tonic-gate //  Created On:       Tue Feb 10 15:00:39 1998
307c478bd9Sstevel@tonic-gate //  Last Modified By: James Kempf
317c478bd9Sstevel@tonic-gate //  Last Modified On: Tue Nov 17 12:12:18 1998
327c478bd9Sstevel@tonic-gate //  Update Count:     82
337c478bd9Sstevel@tonic-gate //
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate package com.sun.slp;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate import java.util.*;
387c478bd9Sstevel@tonic-gate import java.io.*;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /**
427c478bd9Sstevel@tonic-gate  * The SDAAdvert class models the SLP DAAdvert message.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * @author James Kempf
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate class SDAAdvert extends SrvLocMsgImpl {
487c478bd9Sstevel@tonic-gate 
SDAAdvert(SrvLocHeader hdr, short xid, long timestamp, ServiceURL url, Vector scopes, Vector attrs)497c478bd9Sstevel@tonic-gate     SDAAdvert(SrvLocHeader hdr,
507c478bd9Sstevel@tonic-gate 	      short xid,
517c478bd9Sstevel@tonic-gate 	      long timestamp,
527c478bd9Sstevel@tonic-gate 	      ServiceURL url,
537c478bd9Sstevel@tonic-gate 	      Vector scopes,
547c478bd9Sstevel@tonic-gate 	      Vector attrs)
557c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	// Note that we don't need a server side header here because
587c478bd9Sstevel@tonic-gate 	//  we will not be parsing anything in.
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	try {
617c478bd9Sstevel@tonic-gate 	    this.hdr = (SrvLocHeader)hdr.clone();
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	} catch (CloneNotSupportedException ex) {
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	    // We know it's supported.
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	this.hdr.xid = xid;
707c478bd9Sstevel@tonic-gate 	this.hdr.functionCode = SrvLocHeader.DAAdvert;
717c478bd9Sstevel@tonic-gate 	this.hdr.mcast = false;
727c478bd9Sstevel@tonic-gate 	this.hdr.previousResponders = null;  // we don't want this around.
737c478bd9Sstevel@tonic-gate 	this.hdr.errCode = ServiceLocationException.OK;
747c478bd9Sstevel@tonic-gate 	this.hdr.overflow = false;
757c478bd9Sstevel@tonic-gate 	this.hdr.length = 0;
767c478bd9Sstevel@tonic-gate 	this.hdr.fresh = false;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	this.initialize(timestamp, url, scopes, attrs);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate     }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate     // Initialize the message.
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate     void
initialize(long timestamp, ServiceURL url, Vector scopes, Vector attrs)867c478bd9Sstevel@tonic-gate 	initialize(long timestamp,
877c478bd9Sstevel@tonic-gate 		   ServiceURL url,
887c478bd9Sstevel@tonic-gate 		   Vector scopes,
897c478bd9Sstevel@tonic-gate 		   Vector attrs)
907c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	SLPServerHeaderV2 hdr = (SLPServerHeaderV2)this.hdr;
937c478bd9Sstevel@tonic-gate 	hdr.scopes = (Vector)scopes.clone();
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	ServiceType serviceType = url.getServiceType();
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (!serviceType.equals(Defaults.DA_SERVICE_TYPE)) {
987c478bd9Sstevel@tonic-gate 	    throw
997c478bd9Sstevel@tonic-gate 		new ServiceLocationException(
1007c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
1017c478bd9Sstevel@tonic-gate 				"sdaadv_nondaurl",
1027c478bd9Sstevel@tonic-gate 				new Object[] {serviceType});
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (timestamp < 0) {
1077c478bd9Sstevel@tonic-gate 	    throw
1087c478bd9Sstevel@tonic-gate 		new ServiceLocationException(
1097c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
1107c478bd9Sstevel@tonic-gate 				"sdaadv_neg",
1117c478bd9Sstevel@tonic-gate 				new Object[0]);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	// Validate scope list.
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	DATable.validateScopes(scopes, hdr.locale);
1177c478bd9Sstevel@tonic-gate 	hdr.scopes = (Vector)scopes;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	// Escape scope strings.
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	hdr.escapeScopeStrings(scopes);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	// Parse out the payload.
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	String surl = url.toString();
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	// Parse out the timestamp
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	putInt32(hdr, timestamp, baos);
1327c478bd9Sstevel@tonic-gate 	byte[] timestampBytes = baos.toByteArray();
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	// Parse out the URL.
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	byte[] urlBytes = hdr.putString(surl, baos);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	// Parse out the scope list.
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	byte[] scopeBytes =
1417c478bd9Sstevel@tonic-gate 	    hdr.parseCommaSeparatedListOut(scopes, baos);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	// Parse out the attributes.
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	byte[] attrBytes = hdr.parseAttributeVectorOut(attrs,
1467c478bd9Sstevel@tonic-gate 						       url.getLifetime(),
1477c478bd9Sstevel@tonic-gate 						       false,
1487c478bd9Sstevel@tonic-gate 						       null,
1497c478bd9Sstevel@tonic-gate 						       baos,
1507c478bd9Sstevel@tonic-gate 						       false);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	// Parse out SPI list
1537c478bd9Sstevel@tonic-gate 	String spisString = "";
1547c478bd9Sstevel@tonic-gate 	// First get DA SPIs from DA SPIs property
1557c478bd9Sstevel@tonic-gate 	LinkedList spiList = AuthBlock.getSPIList("sun.net.slp.SPIs");
1567c478bd9Sstevel@tonic-gate 	if (spiList != null && !spiList.isEmpty()) {
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	    StringBuffer spiBuf = new StringBuffer();
1597c478bd9Sstevel@tonic-gate 	    spiBuf.append(spiList.getFirst().toString());
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	    for (int i = 1; i < spiList.size(); i++) {
1627c478bd9Sstevel@tonic-gate 		spiBuf.append(',');
1637c478bd9Sstevel@tonic-gate 		spiBuf.append(spiList.get(i).toString());
1647c478bd9Sstevel@tonic-gate 	    }
1657c478bd9Sstevel@tonic-gate 	    spisString = spiBuf.toString();
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	byte[] spiBytes = hdr.putString(spisString, baos);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	// Parse out auth block, if necessary.
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	Hashtable auth = null;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (SLPConfig.getSLPConfig().getHasSecurity()) {
1757c478bd9Sstevel@tonic-gate 	    Object[] message = new Object[9];
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	    // None of the strings have leading length fields, so add them here
1787c478bd9Sstevel@tonic-gate 	    message[0] = timestampBytes;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	    ByteArrayOutputStream abaos = new ByteArrayOutputStream();
1817c478bd9Sstevel@tonic-gate 	    hdr.putInteger(urlBytes.length, abaos);
1827c478bd9Sstevel@tonic-gate 	    message[1] = abaos.toByteArray();
1837c478bd9Sstevel@tonic-gate 	    message[2] = urlBytes;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	    abaos = new ByteArrayOutputStream();
1867c478bd9Sstevel@tonic-gate 	    hdr.putInteger(attrBytes.length, abaos);
1877c478bd9Sstevel@tonic-gate 	    message[3] = abaos.toByteArray();
1887c478bd9Sstevel@tonic-gate 	    message[4] = attrBytes;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	    abaos = new ByteArrayOutputStream();
1917c478bd9Sstevel@tonic-gate 	    hdr.putInteger(scopeBytes.length, abaos);
1927c478bd9Sstevel@tonic-gate 	    message[5] = abaos.toByteArray();
1937c478bd9Sstevel@tonic-gate 	    message[6] = scopeBytes;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	    abaos = new ByteArrayOutputStream();
1967c478bd9Sstevel@tonic-gate 	    hdr.putInteger(spiBytes.length, abaos);
1977c478bd9Sstevel@tonic-gate 	    message[7] = abaos.toByteArray();
1987c478bd9Sstevel@tonic-gate 	    message[8] = spiBytes;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	    auth =
2017c478bd9Sstevel@tonic-gate 		hdr.getCheckedAuthBlockList(message,
2027c478bd9Sstevel@tonic-gate 			SLPConfig.getSLPConfig().getAdvertHeartbeatTime());
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	    // Parse out auth blocks.
2057c478bd9Sstevel@tonic-gate 	    baos.write((byte)(auth.size() & 0xFF));	// auth block count
2067c478bd9Sstevel@tonic-gate 	    hdr.nbytes += 1;
2077c478bd9Sstevel@tonic-gate 	    AuthBlock.externalizeAll(hdr, auth, baos);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	} else {
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	    baos.write((byte)0);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	// Save bytes.
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	hdr.payload = baos.toByteArray();
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	hdr.constructDescription("DAAdvert",
2207c478bd9Sstevel@tonic-gate 				 "        timestamp="+timestamp+"\n"+
2217c478bd9Sstevel@tonic-gate 				 "        URL="+url+"\n"+
2227c478bd9Sstevel@tonic-gate 				 "        attrs="+attrs+"\n"+
2237c478bd9Sstevel@tonic-gate 				 "        SPIs="+spisString+"\n"+
2247c478bd9Sstevel@tonic-gate 				 "        auth block="+AuthBlock.desc(auth) +
2257c478bd9Sstevel@tonic-gate 				 "\n");
2267c478bd9Sstevel@tonic-gate     }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate     // Put out the lower 32 bits of the timestamp.
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate     static private void
putInt32(SrvLocHeader hdr, long i, ByteArrayOutputStream baos)2317c478bd9Sstevel@tonic-gate 	putInt32(SrvLocHeader hdr, long i,  ByteArrayOutputStream baos) {
2327c478bd9Sstevel@tonic-gate 	baos.write((byte) ((i >> 24) & 0xFF));
2337c478bd9Sstevel@tonic-gate 	baos.write((byte) ((i >> 16) & 0xFF));
2347c478bd9Sstevel@tonic-gate 	baos.write((byte) ((i >> 8)  & 0xFF));
2357c478bd9Sstevel@tonic-gate 	baos.write((byte) (i & 0XFF));
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	hdr.nbytes += 4;
2387c478bd9Sstevel@tonic-gate     }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate }
241