xref: /onnv-gate/usr/src/lib/libslp/javalib/com/sun/slp/SDAAdvert.java (revision 7298:b69e27387f74)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7298SMark.J.Nelson@Sun.COM  * Common Development and Distribution License (the "License").
6*7298SMark.J.Nelson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
230Sstevel@tonic-gate  * All rights reserved.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate //  SDAAdvert.java:   Server Side DAAdvert Message.
280Sstevel@tonic-gate //  Author:           James Kempf
290Sstevel@tonic-gate //  Created On:       Tue Feb 10 15:00:39 1998
300Sstevel@tonic-gate //  Last Modified By: James Kempf
310Sstevel@tonic-gate //  Last Modified On: Tue Nov 17 12:12:18 1998
320Sstevel@tonic-gate //  Update Count:     82
330Sstevel@tonic-gate //
340Sstevel@tonic-gate 
350Sstevel@tonic-gate package com.sun.slp;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate import java.util.*;
380Sstevel@tonic-gate import java.io.*;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /**
420Sstevel@tonic-gate  * The SDAAdvert class models the SLP DAAdvert message.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * @author James Kempf
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate class SDAAdvert extends SrvLocMsgImpl {
480Sstevel@tonic-gate 
SDAAdvert(SrvLocHeader hdr, short xid, long timestamp, ServiceURL url, Vector scopes, Vector attrs)490Sstevel@tonic-gate     SDAAdvert(SrvLocHeader hdr,
500Sstevel@tonic-gate 	      short xid,
510Sstevel@tonic-gate 	      long timestamp,
520Sstevel@tonic-gate 	      ServiceURL url,
530Sstevel@tonic-gate 	      Vector scopes,
540Sstevel@tonic-gate 	      Vector attrs)
550Sstevel@tonic-gate 	throws ServiceLocationException {
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	// Note that we don't need a server side header here because
580Sstevel@tonic-gate 	//  we will not be parsing anything in.
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	try {
610Sstevel@tonic-gate 	    this.hdr = (SrvLocHeader)hdr.clone();
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	} catch (CloneNotSupportedException ex) {
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	    // We know it's supported.
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	this.hdr.xid = xid;
700Sstevel@tonic-gate 	this.hdr.functionCode = SrvLocHeader.DAAdvert;
710Sstevel@tonic-gate 	this.hdr.mcast = false;
720Sstevel@tonic-gate 	this.hdr.previousResponders = null;  // we don't want this around.
730Sstevel@tonic-gate 	this.hdr.errCode = ServiceLocationException.OK;
740Sstevel@tonic-gate 	this.hdr.overflow = false;
750Sstevel@tonic-gate 	this.hdr.length = 0;
760Sstevel@tonic-gate 	this.hdr.fresh = false;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	this.initialize(timestamp, url, scopes, attrs);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate     }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 
830Sstevel@tonic-gate     // Initialize the message.
840Sstevel@tonic-gate 
850Sstevel@tonic-gate     void
initialize(long timestamp, ServiceURL url, Vector scopes, Vector attrs)860Sstevel@tonic-gate 	initialize(long timestamp,
870Sstevel@tonic-gate 		   ServiceURL url,
880Sstevel@tonic-gate 		   Vector scopes,
890Sstevel@tonic-gate 		   Vector attrs)
900Sstevel@tonic-gate 	throws ServiceLocationException {
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	SLPServerHeaderV2 hdr = (SLPServerHeaderV2)this.hdr;
930Sstevel@tonic-gate 	hdr.scopes = (Vector)scopes.clone();
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	ServiceType serviceType = url.getServiceType();
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if (!serviceType.equals(Defaults.DA_SERVICE_TYPE)) {
980Sstevel@tonic-gate 	    throw
990Sstevel@tonic-gate 		new ServiceLocationException(
1000Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
1010Sstevel@tonic-gate 				"sdaadv_nondaurl",
1020Sstevel@tonic-gate 				new Object[] {serviceType});
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (timestamp < 0) {
1070Sstevel@tonic-gate 	    throw
1080Sstevel@tonic-gate 		new ServiceLocationException(
1090Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
1100Sstevel@tonic-gate 				"sdaadv_neg",
1110Sstevel@tonic-gate 				new Object[0]);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	// Validate scope list.
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	DATable.validateScopes(scopes, hdr.locale);
1170Sstevel@tonic-gate 	hdr.scopes = (Vector)scopes;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	// Escape scope strings.
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	hdr.escapeScopeStrings(scopes);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	// Parse out the payload.
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	String surl = url.toString();
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	// Parse out the timestamp
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	putInt32(hdr, timestamp, baos);
1320Sstevel@tonic-gate 	byte[] timestampBytes = baos.toByteArray();
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	// Parse out the URL.
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	byte[] urlBytes = hdr.putString(surl, baos);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	// Parse out the scope list.
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	byte[] scopeBytes =
1410Sstevel@tonic-gate 	    hdr.parseCommaSeparatedListOut(scopes, baos);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	// Parse out the attributes.
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	byte[] attrBytes = hdr.parseAttributeVectorOut(attrs,
1460Sstevel@tonic-gate 						       url.getLifetime(),
1470Sstevel@tonic-gate 						       false,
1480Sstevel@tonic-gate 						       null,
1490Sstevel@tonic-gate 						       baos,
1500Sstevel@tonic-gate 						       false);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	// Parse out SPI list
1530Sstevel@tonic-gate 	String spisString = "";
1540Sstevel@tonic-gate 	// First get DA SPIs from DA SPIs property
1550Sstevel@tonic-gate 	LinkedList spiList = AuthBlock.getSPIList("sun.net.slp.SPIs");
1560Sstevel@tonic-gate 	if (spiList != null && !spiList.isEmpty()) {
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	    StringBuffer spiBuf = new StringBuffer();
1590Sstevel@tonic-gate 	    spiBuf.append(spiList.getFirst().toString());
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	    for (int i = 1; i < spiList.size(); i++) {
1620Sstevel@tonic-gate 		spiBuf.append(',');
1630Sstevel@tonic-gate 		spiBuf.append(spiList.get(i).toString());
1640Sstevel@tonic-gate 	    }
1650Sstevel@tonic-gate 	    spisString = spiBuf.toString();
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	byte[] spiBytes = hdr.putString(spisString, baos);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	// Parse out auth block, if necessary.
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	Hashtable auth = null;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if (SLPConfig.getSLPConfig().getHasSecurity()) {
1750Sstevel@tonic-gate 	    Object[] message = new Object[9];
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	    // None of the strings have leading length fields, so add them here
1780Sstevel@tonic-gate 	    message[0] = timestampBytes;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	    ByteArrayOutputStream abaos = new ByteArrayOutputStream();
1810Sstevel@tonic-gate 	    hdr.putInteger(urlBytes.length, abaos);
1820Sstevel@tonic-gate 	    message[1] = abaos.toByteArray();
1830Sstevel@tonic-gate 	    message[2] = urlBytes;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	    abaos = new ByteArrayOutputStream();
1860Sstevel@tonic-gate 	    hdr.putInteger(attrBytes.length, abaos);
1870Sstevel@tonic-gate 	    message[3] = abaos.toByteArray();
1880Sstevel@tonic-gate 	    message[4] = attrBytes;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	    abaos = new ByteArrayOutputStream();
1910Sstevel@tonic-gate 	    hdr.putInteger(scopeBytes.length, abaos);
1920Sstevel@tonic-gate 	    message[5] = abaos.toByteArray();
1930Sstevel@tonic-gate 	    message[6] = scopeBytes;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	    abaos = new ByteArrayOutputStream();
1960Sstevel@tonic-gate 	    hdr.putInteger(spiBytes.length, abaos);
1970Sstevel@tonic-gate 	    message[7] = abaos.toByteArray();
1980Sstevel@tonic-gate 	    message[8] = spiBytes;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	    auth =
2010Sstevel@tonic-gate 		hdr.getCheckedAuthBlockList(message,
2020Sstevel@tonic-gate 			SLPConfig.getSLPConfig().getAdvertHeartbeatTime());
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	    // Parse out auth blocks.
2050Sstevel@tonic-gate 	    baos.write((byte)(auth.size() & 0xFF));	// auth block count
2060Sstevel@tonic-gate 	    hdr.nbytes += 1;
2070Sstevel@tonic-gate 	    AuthBlock.externalizeAll(hdr, auth, baos);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	} else {
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	    baos.write((byte)0);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	// Save bytes.
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	hdr.payload = baos.toByteArray();
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	hdr.constructDescription("DAAdvert",
2200Sstevel@tonic-gate 				 "        timestamp="+timestamp+"\n"+
2210Sstevel@tonic-gate 				 "        URL="+url+"\n"+
2220Sstevel@tonic-gate 				 "        attrs="+attrs+"\n"+
2230Sstevel@tonic-gate 				 "        SPIs="+spisString+"\n"+
2240Sstevel@tonic-gate 				 "        auth block="+AuthBlock.desc(auth) +
2250Sstevel@tonic-gate 				 "\n");
2260Sstevel@tonic-gate     }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate     // Put out the lower 32 bits of the timestamp.
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate     static private void
putInt32(SrvLocHeader hdr, long i, ByteArrayOutputStream baos)2310Sstevel@tonic-gate 	putInt32(SrvLocHeader hdr, long i,  ByteArrayOutputStream baos) {
2320Sstevel@tonic-gate 	baos.write((byte) ((i >> 24) & 0xFF));
2330Sstevel@tonic-gate 	baos.write((byte) ((i >> 16) & 0xFF));
2340Sstevel@tonic-gate 	baos.write((byte) ((i >> 8)  & 0xFF));
2350Sstevel@tonic-gate 	baos.write((byte) (i & 0XFF));
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	hdr.nbytes += 4;
2380Sstevel@tonic-gate     }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate }
241