1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2001 by Sun Microsystems, Inc.
* All rights reserved.
*
*/
// SLPV1Manager.java: Manages V1 Compatibility
// Author: James Kempf
// Created On: Wed Sep 9 09:51:40 1998
// Last Modified By: James Kempf
// Last Modified On: Thu Mar 4 10:39:11 1999
// Update Count: 46
//
package com.sun.slp;
import java.io.*;
import java.util.*;
import java.net.*;
/**
* The SLPV1Manager manages access between the DA and the V1 compatibility
* framework. The DA calls into the SLPV1Manager to initialize
* active and passive DA advertising, and to decode an incoming V1
* message. However, the ServiceTable does *not* call into SLPV1Manager
* to handle an outgoing message, since each individual message type is
* handled separately. SLPV1Manager also handles V1 defaults.
*
* @author James Kempf
*/
abstract class SLPV1Manager extends Object {
// V1 Header class.
static final String V1_HEADER_CLASS = "com.sun.slp.SLPHeaderV1";
// V1 multicast addresses.
static final String sGeneralSLPMCAddress = "224.0.1.22";
static final String sDADiscSLPMCAddress = "224.0.1.35";
static InetAddress v1SLPGSAddr = null;
static InetAddress v1SLPDAAddr = null;
/**
* The SLPV1Advertiser implements the SLPv1 DAAdvert xid incrementing
* algorithm. In SLPv1, the xid of an unsolicited DAAdvert is only
* 0 if it came up stateless. If it comes up with preexisting state,
* it sets the counter to 0x100. Also, when the xid counter wraps,
* it must wrap to 0x100 and not 0x0.
*/
static class SLPV1Advertiser extends DAAdvertiser {
// For implementing the V1 xid algorithm.
private short xid = 0;
private static final short STATEFUL_XID = 0x100;
private static final long STATEFUL_TIME_BOUND = 300L;
// Service table.
private ServiceTable table = null;
// Scopes to use. We need to map from V2, so default corresponds to
// the empty scope.
Vector useScopes = new Vector();
// Create an SLPv1 Advertiser and start it running.
SLPV1Advertiser(InetAddress interfac,
InetAddress maddr,
ServiceTable table)
throws ServiceLocationException {
super();
this.table = table;
initialize();
// There will be NO listener on this multicast address,
// so the superclass will simply create a scoket for it.
// We don't want to create a new Listener
// because we are not interested in multicast requests since
// only SAs answer multicast requests.
initializeNetworking(interfac, maddr);
}
// Initialize the xid for passive advertising. We need to determine
// whether we came up stateless or not. We do this by asking the
// the service store for the stateless reboot time. If the
// stateless reboot time is within the last 5 minutes, we
// assume we came up stateless. Otherwise, we're stateful.
// We also initialize the URL and scopes.
private void initialize() throws ServiceLocationException {
// Initialize the xid.
ServiceStore store = ServiceTable.getServiceTable().store;
long timestamp = store.getStateTimestamp();
long currentTime = SLPConfig.currentSLPTime();
if ((currentTime - timestamp) > STATEFUL_TIME_BOUND) {
xid = STATEFUL_XID;
}
// Initialize the scopes.
useScopes = config.getSAConfiguredScopes();
}
// Return the output buffer for a passive advert. We need to create
// the advert, rolling over the xid if necessary for the next one.
protected byte[] getOutbuf() {
SDAAdvert daadvert = null;
try {
SLPHeaderV1 hdr = new SLPHeaderV1();
hdr.functionCode = SrvLocHeader.DAAdvert;
hdr.locale = config.getLocale();
daadvert = (SDAAdvert)table.makeDAAdvert(hdr,
interfac,
xid,
useScopes,
config);
hdr = (SLPHeaderV1)daadvert.getHeader();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
hdr.externalize(baos, true, false);
byte[] outbuf = baos.toByteArray();
bumpXid();
return outbuf;
} catch (ServiceLocationException ex) {
Assert.slpassert(false,
"v1_advert_error",
new Object[0]);
}
return null;
}
private void bumpXid() {
int newXID = (int)xid + 1;
if (newXID > Short.MAX_VALUE) {
xid = STATEFUL_XID;
} else {
xid = (short)newXID;
}
}
}
// Start up listener, active and passive listeners for SLPv1.
static public void
start(SLPConfig config, ServerDATable table, ServiceTable stable) {
// We do not handle SLPv1 if security is enabled, because SLPv1
// security is not implemented.
if (config.getHasSecurity()) {
if (config.regTest() ||
config.traceMsg() ||
config.traceDrop() ||
config.traceDATraffic()) {
config.writeLog("v1_security_enabled",
new Object[0]);
}
return;
}
Vector interfaces = config.getInterfaces();
int i = 0, n = interfaces.size();
Vector advs = new Vector();
try {
InetAddress v1SLPDAAddr = null;
// Get address for DA discovery multicast.
v1SLPDAAddr = InetAddress.getByName(sDADiscSLPMCAddress);
v1SLPGSAddr = InetAddress.getByName(sGeneralSLPMCAddress);
// Add all listeners onto the SLPv1 DA multicast address and
// create a DAAdvertiser on all network interfaces for the
// general multicast group.
for (i = 0; i < n; i++) {
InetAddress interfac = (InetAddress)interfaces.elementAt(i);
// Listen for SLPv1 multicast DA service requests. Only DA
// service requests are multicast on this address.
Listener.addListenerToMulticastGroup(interfac, v1SLPDAAddr);
// We don't need to listen to the SLPv1 general multicast
// address because we never want any multicast service
// requests. But we do need to advertise as an SLPv1 DA.
// So we have a special DAAdvertiser subclass to do it.
DAAdvertiser ad =
new SLPV1Advertiser(interfac, v1SLPGSAddr, stable);
ad.start();
advs.addElement(ad);
}
// Let admin know we are running in SLPv1 compatibility mode
// if tracing is on
if (config.regTest() ||
config.traceMsg() ||
config.traceDrop() ||
config.traceDATraffic()) {
config.writeLog("v1_hello",
new Object[] {config.getSAConfiguredScopes()});
}
return;
} catch (ServiceLocationException ex) {
config.writeLog("v1_init_error",
new Object[] {ex.getMessage()});
} catch (UnknownHostException ex) {
config.writeLog("v1_init_error",
new Object[] {ex.getMessage()});
}
// Remove Listeners from multicast group, stop DAAdvertisers.
// An error occured.
int j;
for (j = 0; j < i; i++) {
InetAddress interfac = (InetAddress)interfaces.elementAt(i);
DatagramSocket dss =
Listener.returnListenerSocketOnInterface(interfac);
if (dss instanceof MulticastSocket) {
MulticastSocket mss = (MulticastSocket)dss;
try {
mss.leaveGroup(v1SLPDAAddr);
} catch (IOException ex) {
// Ignore it.
}
DAAdvertiser ad = (DAAdvertiser)advs.elementAt(j);
ad.stopThread();
}
}
}
// Initialize CSrvReg, CSrvDereg, CSrvMsg, and SDAAdvert classes for SLPv1,
// also V1 header class.
static {
SrvLocHeader.addHeaderClass(V1_HEADER_CLASS, SLPHeaderV1.VERSION);
}
}
|