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 // SSrvMsg.java: Message class for SLP service request. 287c478bd9Sstevel@tonic-gate // Author: James Kempf 297c478bd9Sstevel@tonic-gate // Created On: Thu Oct 9 13:40:16 1997 307c478bd9Sstevel@tonic-gate // Last Modified By: James Kempf 317c478bd9Sstevel@tonic-gate // Last Modified On: Tue Oct 27 10:57:38 1998 327c478bd9Sstevel@tonic-gate // Update Count: 112 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 SSrvMsg class models the SLP service (request, reply) message 437c478bd9Sstevel@tonic-gate * server side. Subclasses for other versions can specialize the 447c478bd9Sstevel@tonic-gate * initialize() and makeReply() methods. 457c478bd9Sstevel@tonic-gate * 467c478bd9Sstevel@tonic-gate * @author James Kempf 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate class SSrvMsg extends SrvLocMsgImpl { 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate String serviceType = ""; // service type and naming authority 527c478bd9Sstevel@tonic-gate String query = ""; // the query 537c478bd9Sstevel@tonic-gate String spi = ""; 547c478bd9Sstevel@tonic-gate SSrvMsg()557c478bd9Sstevel@tonic-gate protected SSrvMsg() {} 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate // Construct a SSrvMsg from the byte input stream. 587c478bd9Sstevel@tonic-gate SSrvMsg(SrvLocHeader hdr, DataInputStream dis)597c478bd9Sstevel@tonic-gate SSrvMsg(SrvLocHeader hdr, DataInputStream dis) 607c478bd9Sstevel@tonic-gate throws ServiceLocationException, IOException { 617c478bd9Sstevel@tonic-gate super(hdr, SrvLocHeader.SrvReq); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate this.initialize(dis); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate // Initialize the message from the input stream. 687c478bd9Sstevel@tonic-gate initialize(DataInputStream dis)697c478bd9Sstevel@tonic-gate void initialize(DataInputStream dis) 707c478bd9Sstevel@tonic-gate throws ServiceLocationException, IOException { 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate SLPServerHeaderV2 hdr = (SLPServerHeaderV2)getHeader(); 737c478bd9Sstevel@tonic-gate StringBuffer buf = new StringBuffer(); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate // First get the previous responder. 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate hdr.parsePreviousRespondersIn(dis); 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate // Get the service type. 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate hdr.getString(buf, dis); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate serviceType = buf.toString(); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if (serviceType.length() <= 0) { 867c478bd9Sstevel@tonic-gate throw 877c478bd9Sstevel@tonic-gate new ServiceLocationException( 887c478bd9Sstevel@tonic-gate ServiceLocationException.PARSE_ERROR, 897c478bd9Sstevel@tonic-gate "srq_stype_missing", 907c478bd9Sstevel@tonic-gate new Object[0]); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate ServiceType t = new ServiceType(serviceType); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate serviceType = t.toString(); 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate // Get vector of scopes. 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate hdr.getString(buf, dis); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate hdr.scopes = hdr.parseCommaSeparatedListIn(buf.toString(), true); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate // Validate, but check for empty if solicitation for DAAdvert 1047c478bd9Sstevel@tonic-gate // or SAAdvert. 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate if (hdr.scopes.size() <= 0) { 1077c478bd9Sstevel@tonic-gate if (!t.equals(Defaults.DA_SERVICE_TYPE) && 1087c478bd9Sstevel@tonic-gate !t.equals(Defaults.SA_SERVICE_TYPE)) { 1097c478bd9Sstevel@tonic-gate throw 1107c478bd9Sstevel@tonic-gate new ServiceLocationException( 1117c478bd9Sstevel@tonic-gate ServiceLocationException.PARSE_ERROR, 1127c478bd9Sstevel@tonic-gate "no_scope_vector", 1137c478bd9Sstevel@tonic-gate new Object[0]); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate } else { 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate // Unescape scope strings. 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate hdr.unescapeScopeStrings(hdr.scopes); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate DATable.validateScopes(hdr.scopes, hdr.locale); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate // Get the query. 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate hdr.getString(buf, dis); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate query = buf.toString(); 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate // Get the SPI 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate hdr.getString(buf, dis); 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate spi = buf.toString(); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate hdr.constructDescription("SrvRqst", 1387c478bd9Sstevel@tonic-gate " service type=``" + 1397c478bd9Sstevel@tonic-gate serviceType + "''\n" + 1407c478bd9Sstevel@tonic-gate " query=``" + 1417c478bd9Sstevel@tonic-gate query + "''\n" + 1427c478bd9Sstevel@tonic-gate " spi=``" + 1437c478bd9Sstevel@tonic-gate spi + "''"); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate // Construct a SSrvMsg from the arguments. This will be a SrvRply 1477c478bd9Sstevel@tonic-gate // for transmission to the client. 1487c478bd9Sstevel@tonic-gate makeReply(Hashtable urls, Hashtable URLSignatures)1497c478bd9Sstevel@tonic-gate SrvLocMsg makeReply(Hashtable urls, Hashtable URLSignatures) 1507c478bd9Sstevel@tonic-gate throws ServiceLocationException { 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate SLPServerHeaderV2 hdr = 1537c478bd9Sstevel@tonic-gate ((SLPServerHeaderV2)getHeader()).makeReplyHeader(); 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate hdr.iNumReplies = urls.size(); 1567c478bd9Sstevel@tonic-gate // keep this info so SAs can drop 0 replies 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate int n = urls.size(); 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate String authDesc = "\n"; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate // Write out size. 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate hdr.putInt(n, baos); 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate Enumeration en = urls.keys(); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate int nurls = 0; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate // Write out the members of the list, including the lifetime. 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate while (en.hasMoreElements()) { 1757c478bd9Sstevel@tonic-gate ServiceURL surl = (ServiceURL)en.nextElement(); 1767c478bd9Sstevel@tonic-gate Hashtable auth = null; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate if (URLSignatures != null) { 1797c478bd9Sstevel@tonic-gate auth = (Hashtable)URLSignatures.get(surl); 1807c478bd9Sstevel@tonic-gate AuthBlock selectedAuth = 1817c478bd9Sstevel@tonic-gate AuthBlock.getEquivalentAuth(spi, auth); 1827c478bd9Sstevel@tonic-gate auth = null; 1837c478bd9Sstevel@tonic-gate if (selectedAuth != null) { 1847c478bd9Sstevel@tonic-gate auth = new Hashtable(); 1857c478bd9Sstevel@tonic-gate auth.put(spi, selectedAuth); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate authDesc = 1887c478bd9Sstevel@tonic-gate authDesc + " " + surl.toString() + ": " + 1897c478bd9Sstevel@tonic-gate (auth != null ? 1907c478bd9Sstevel@tonic-gate selectedAuth.toString() : 1917c478bd9Sstevel@tonic-gate "No Auth Block\n"); 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate // Parse out a URL entry. Check overflow. If the packet has filled 1957c478bd9Sstevel@tonic-gate // up, then break out of the loop. 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if (hdr.parseServiceURLOut(surl, 1987c478bd9Sstevel@tonic-gate (auth != null), 1997c478bd9Sstevel@tonic-gate auth, 2007c478bd9Sstevel@tonic-gate baos, 2017c478bd9Sstevel@tonic-gate true) == false) { 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate // Note that we set overflow here because there are additional 2047c478bd9Sstevel@tonic-gate // URL's, but we don't have to truncate the packet. 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate hdr.overflow = true; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate // We need to rewrite the size to what it should be. 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate byte[] bytes = baos.toByteArray(); 2117c478bd9Sstevel@tonic-gate baos.reset(); 2127c478bd9Sstevel@tonic-gate SrvLocHeader.putInteger(nurls, baos); 2137c478bd9Sstevel@tonic-gate baos.write(bytes, 2, bytes.length - 2); 2147c478bd9Sstevel@tonic-gate break; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate nurls++; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate hdr.payload = baos.toByteArray(); 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate // Construct description. 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate hdr.constructDescription("SrvRply", 2277c478bd9Sstevel@tonic-gate " service URLs=``" + urls + "''\n" + 2287c478bd9Sstevel@tonic-gate " auth block=" + authDesc + "\n"); 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate return hdr; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate } 234