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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
|
/*
* 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 2001-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
// SLPHeaderV1.java: SLPv1 Header.
// Author: James Kempf
// Created On: Thu Sep 10 15:12:14 1998
// Last Modified By: James Kempf
// Last Modified On: Wed Jan 20 15:38:07 1999
// Update Count: 59
//
package com.sun.slp;
import java.util.*;
import java.io.*;
/**
* The SLPHeaderV1 class models the SLPv1 server side header.
*
* @author James Kempf
*/
class SLPHeaderV1 extends SrvLocHeader implements Cloneable {
// Version number.
static int VERSION = 1;
// Function code for message reply.
int replyFunctionCode = SrvLocHeader.SrvAck;
// Various header flags.
protected static final int NOFLAG = 0x00;
protected static final int OVERFLOW = 0x80;
protected static final int MONOLING = 0x40;
protected static final int URLSIG = 0x20;
protected static final int ATTRSIG = 0x10;
protected static final int FRESH = 0x08;
protected static int LANG_CODE_BYTES = 2;
protected static int HEADER_BYTES =
VERSION_FUNCTION_BYTES + LANG_CODE_BYTES + 8;
// Characters to escape.
final private static String UNESCAPABLE_CHARS = ",=!></*()";
final private static String ESCAPABLE_CHARS =
UNESCAPABLE_CHARS + "&#;";
String charCode = IANACharCode.UTF8; // character encoding.
boolean monolingual = false; // monolingual flag.
// Used to construct a header in SrvLocHeader.newInstance().
SLPHeaderV1() {
super();
version = VERSION;
}
// Assign reply code based on function code type, then use superclass
// method to parse header.
void parseHeader(int functionCode, DataInputStream dis)
throws ServiceLocationException, IOException {
this.functionCode = functionCode;
// We ignore the error case here.
switch (functionCode) {
case SrvLocHeader.SrvReq:
replyFunctionCode = SrvLocHeader.SrvRply;
break;
case SrvLocHeader.AttrRqst:
replyFunctionCode = SrvLocHeader.AttrRply;
break;
case SrvLocHeader.SrvTypeRqst:
replyFunctionCode = SrvLocHeader.SrvTypeRply;
break;
}
length = getInt(dis);
byte flags = (byte) ((char)dis.read() & 0xFF);
nbytes++;
overflow = ((flags & OVERFLOW) != 0x00);
fresh = false; // fresh gets set on output in SLPv1
monolingual = ((flags & MONOLING) != 0x00);
boolean urlAuth = ((flags & URLSIG) != 0x00);
boolean attrAuth = ((flags & ATTRSIG) != 0x00);
// Security not handled for SLPv1.
if (urlAuth || attrAuth) {
throw
new ServiceLocationException(
ServiceLocationException.AUTHENTICATION_FAILED,
"v1_no_security",
new Object[0]);
}
int dialect = (int) ((char)dis.read() & 0xFF);
nbytes++;
// Dialect must be zero.
if (dialect != 0) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_nonzero_dialect",
new Object[0]);
}
byte a_bTemp[] = new byte[LANG_CODE_BYTES];
a_bTemp[0] = (byte) dis.read();
a_bTemp[1] = (byte) dis.read();
nbytes += 2;
try {
locale = new Locale(new String(a_bTemp, IANACharCode.ASCII), "");
} catch (UnsupportedEncodingException ex) {
}
int intCharCode = getInt(dis);
charCode = IANACharCode.decodeCharacterEncoding(intCharCode);
xid = (short)getInt(dis);
errCode = ServiceLocationException.OK;
}
// Parse an incoming V1 message and return the SrvLocMsg object.
SrvLocMsg parseMsg(DataInputStream dis)
throws ServiceLocationException,
IOException,
IllegalArgumentException {
SrvLocMsg msg = null;
// If this is a *multicast* request, we reject it except for DAAdvert.
// Multicast requests are only taken by SA servers.
if (mcast && (functionCode != SrvLocHeader.DAAdvert)) {
return null;
}
// Switch and convert according to function code.
switch (functionCode) {
case SrvLocHeader.SrvReq:
msg = new SLPV1SSrvMsg(this, dis);
break;
case SrvLocHeader.SrvReg:
msg = new SLPV1SSrvReg(this, dis);
break;
case SrvLocHeader.SrvDereg:
msg = new SLPV1SSrvDereg(this, dis);
break;
case SrvLocHeader.AttrRqst:
msg = new SLPV1SAttrMsg(this, dis);
break;
case SrvLocHeader.SrvTypeRqst:
msg = new SLPV1SSrvTypeMsg(this, dis);
break;
case SrvLocHeader.DAAdvert:
msg = new SLPV1CDAAdvert(this, dis);
break;
default:
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"function_code_error",
new Object[] {
new Integer(functionCode)});
}
// Check for size overflow.
if (nbytes > length) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"length_overflow",
new Object[] {
new Integer(nbytes), new Integer(length)});
}
return msg;
}
// Externalize the message by converting it to bytes and writing
// it to the output stream.
public void
externalize(ByteArrayOutputStream baos, boolean mcast, boolean isTCP)
throws ServiceLocationException {
// Need to put in the error code or previous responders.
ByteArrayOutputStream fin = new ByteArrayOutputStream();
if (functionCode == SrvLocHeader.SrvAck ||
functionCode == SrvLocHeader.SrvTypeRply ||
functionCode == SrvLocHeader.SrvRply ||
functionCode == SrvLocHeader.AttrRply ||
functionCode == SrvLocHeader.DAAdvert) {
putInt(errCode, fin);
} else {
// Parse out previous responders. Note there will only be some
// if the error code is not put out.
if (previousResponders != null) {
parseCommaSeparatedListOut(previousResponders, fin);
}
}
// Parse payload out if error code is OK and payload is nonnull.
if (payload != null && errCode == ServiceLocationException.OK) {
fin.write(payload, 0, payload.length);
}
// Don't touch payload here, somebody may put in a previousResponder
// and resend the message.
byte[] npayload = fin.toByteArray();
// Set overflow flag if buffer is too large and this isn't going out
// via TCP.
if (((npayload.length + 12) > SLPConfig.getSLPConfig().getMTU()) &&
!isTCP) {
overflow = true;
}
baos.write((byte) (0xFF & version));
nbytes++;
baos.write((byte) (0xFF & functionCode));
nbytes++;
length = npayload.length +12; // the 12 is the length of this header!
putInt(length, baos); // what about overflow???
byte flags = 0X00;
if (overflow) {
flags = (byte)(flags | OVERFLOW);
} else {
flags = (byte)(flags & ~OVERFLOW);
}
if (monolingual) {
flags = (byte)(flags | MONOLING);
} else {
flags = (byte)(flags & ~MONOLING);
}
if (fresh) {
flags = (byte)((flags | FRESH) & 0XFF);
} else {
flags = (byte)((flags & ~FRESH) & 0XFF);
}
baos.write((byte) (0xFF & flags));
nbytes++;
baos.write((byte) (0xFF & 0)); // dialect...
nbytes++;
String language = locale.getLanguage();
baos.write((byte) (0xFF & language.charAt(0)));
baos.write((byte) (0xFF & language.charAt(1)));
nbytes += 2;
int intCharCode = 0;
try {
intCharCode = IANACharCode.encodeCharacterEncoding(charCode);
} catch (ServiceLocationException ex) {
Assert.slpassert(false,
"v1_unsupported_encoding",
new Object[] {charCode});
}
putInt(intCharCode, baos);
putInt(xid, baos);
// Write the body.
baos.write(npayload, 0, npayload.length);
nbytes += npayload.length;
}
// Create an error reply using the reply code. Calculate the
// error code using the exception.
SrvLocMsg makeErrorReply(Exception ex) {
// If this is a DAAdvert, then no error reply is returned
// because V1 doesn't support unicast SrvRqst for DAAdvert.
if (functionCode == SrvLocHeader.DAAdvert) {
return null;
}
// Clone the header to make sure that everything else is the same.
// We don't want to use the same header because it may be tested
// elsewhere.
SLPHeaderV1 hdr = null;
try {
hdr = (SLPHeaderV1)this.clone();
} catch (CloneNotSupportedException exx) {
// We know we support it.
}
hdr.fresh = false;
hdr.overflow = false;
hdr.mcast = false;
hdr.functionCode = replyFunctionCode;
// We should *not* be getting a null exception down this path!
Assert.slpassert(ex != null,
"null_parameter",
new Object[] {ex});
if (ex instanceof ServiceLocationException) {
hdr.errCode = ((ServiceLocationException)ex).getErrorCode();
// Handle monolingual bit here. If the exception is
// LANGUAGE_NOT_SUPPORTED and the message type is
// either SrvRqst or AttrRqst, then we simply return an
// empty message unless the monolingual flag is on.
if (hdr.errCode ==
ServiceLocationException.LANGUAGE_NOT_SUPPORTED) {
try {
if (!hdr.monolingual) {
if (hdr.functionCode == SrvLocHeader.SrvReq) {
return SLPV1SSrvMsg.makeEmptyReply(hdr);
} else if (hdr.functionCode == SrvLocHeader.AttrRqst) {
return SLPV1SAttrMsg.makeEmptyReply(hdr);
}
}
} catch (ServiceLocationException exx) {
hdr.monolingual = true;
hdr.makeErrorReply(exx);
}
// Otherwise, we just ignore it.
}
// Anything over AUTHENTICATION_FAILED is an internal error in V1.
if (hdr.errCode > ServiceLocationException.AUTHENTICATION_FAILED) {
hdr.errCode = ServiceLocationException.PARSE_ERROR;
}
} else if (ex instanceof IllegalArgumentException ||
ex instanceof IOException) {
hdr.errCode = ServiceLocationException.PARSE_ERROR;
} else {
hdr.errCode = ServiceLocationException.PARSE_ERROR;
}
// Construct header description.
hdr.constructDescription("SrvLocMsg", "");
return hdr;
}
// Return a reply header with flags properly set.
SLPHeaderV1 makeReplyHeader() {
SLPHeaderV1 hdr = null;
try {
hdr = (SLPHeaderV1)this.clone();
} catch (CloneNotSupportedException ex) {
// We know that we support it.
}
hdr.functionCode = replyFunctionCode;
hdr.length = 0;
hdr.previousResponders = null;
hdr.scopes = null;
hdr.overflow = false;
hdr.fresh = false;
hdr.mcast = false;
hdr.nbytes = 0;
return hdr;
}
// Return display string.
public String toString() {
return
getMsgType() + ":version=``" + version + "''\n" +
" functionCode=``" + functionCode + "''\n" +
" length=``" + length + "''\n" +
" overflow=``" + overflow + "''\n" +
" mcast = ``" + mcast + "''\n" +
" fresh=``" + fresh + "''\n" +
" monolingual=``" + monolingual + "''\n" +
" charCode=``" + charCode + "''\n" +
" locale = ``" + locale + "''\n" +
" xid=``0x" + Integer.toHexString(xid) + "''\n" +
" errCode=``" + errCode + "''\n" +
" previousResponders=``" + previousResponders + "''\n" +
" scopes=``" + scopes + "''\n" +
getMsgDescription();
}
//
// Validation Utilities.
//
/**
* Validate the scope name to be sure it doesn't contain forbidden
* chars and isn't one of the reserved scope names.
*/
static void validateScope(String scope)
throws ServiceLocationException
{
if (scope.indexOf('/') != -1 || scope.indexOf(',') != -1 ||
scope.indexOf(':') != -1) {
throw new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_scope_char_res",
new Object[] {scope});
}
// Check against reserved scope names.
if (scope.equalsIgnoreCase("local") ||
scope.equalsIgnoreCase("remote")) {
throw new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_scope_name_res",
new Object[] {scope});
}
}
/**
* Remove IANA from the service type name.
*
* @param serviceType The service type and naming authority.
* @return The service type name with IANA removed.
*/
static String removeIANA(String serviceType) {
// Substitute null string for IANA.
int idx = 0;
serviceType = serviceType.toLowerCase();
if ((idx = serviceType.indexOf("." + ServiceType.IANA)) != -1) {
serviceType = serviceType.substring(0, idx);
}
return serviceType;
}
// Check whether this is a vaild SLPv1 service type. Also remove
// IANA.
static String checkServiceType(String stype)
throws ServiceLocationException {
// Check for trailing colon and remove it.
if (!stype.endsWith(":")) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_service_type_format",
new Object[] {stype});
}
String type = stype.substring(0, stype.length()-1);
// Remove IANA.
type = removeIANA(type);
// Check syntax.
ServiceType st = new ServiceType(type);
// Reject if abstract type. SLPv1 doesn't handle
// abstract types.
if (st.isAbstractType()) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_abstract_type",
new Object[0]);
}
// Reject if not a service: type. SLPv1 doesn't handle
// nonservice: types.
if (!st.isServiceURL()) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_not_surl",
new Object[0]);
}
return type;
}
//
// Parsing Utilities.
//
// Parse string, bump byte count.
byte[] getString(StringBuffer buf, DataInputStream dis)
throws ServiceLocationException, IOException {
int i, n = 0;
// Get length.
n = getInteger(dis);
byte[] bytes = new byte[n];
// Read bytes.
dis.readFully(bytes, 0, n);
// If the encoding type is Unicode, then figure out first
// whether it's big or little endian from byte header. Future
// calls won't have to go through this grief unless the byte header
// is missing.
if (this.charCode == IANACharCode.UNICODE) {
this.charCode = IANACharCode.getUnicodeEndianess(bytes);
}
String charCode = this.charCode;
// If we are still just Unicode by this point, then we need to
// add the big endian bytes to the beginning of the array.
// Otherwise, Java won't parse it. Note that we don't change
// the flag in the header, since we will need to convert the
// next time around as well.
if (charCode == IANACharCode.UNICODE) {
charCode = IANACharCode.UNICODE_BIG;
bytes = IANACharCode.addBigEndianFlag(bytes);
}
// Convert the bytes into a string.
buf.setLength(0);
buf.append(getBytesString(bytes, charCode));
return bytes;
}
// Parse out string, bump byte count. Use header encoding.
byte[] putString(String string, ByteArrayOutputStream baos) {
// If the charCode is UNICODE, arbirtarily change to big or little,
// while Java will parse.
if (charCode == IANACharCode.UNICODE) {
charCode = IANACharCode.UNICODE_BIG;
}
byte[] bytes = putStringField(string, baos, charCode);
nbytes += bytes.length;
return bytes;
}
// Parse in a service URL including lifetime if necessary.
protected ServiceURL
parseServiceURLIn(DataInputStream dis,
boolean lifeTimeToo,
short errCode)
throws ServiceLocationException, IOException {
int lifetime = 0;
StringBuffer buf = new StringBuffer();
if (lifeTimeToo) {
lifetime = getInt(dis);
}
getString(buf, dis);
ServiceURL url = null;
try {
url = new ServiceURLV1(buf.toString(), lifetime);
} catch (IllegalArgumentException ex) {
throw
new ServiceLocationException(errCode,
"malformed_url",
new Object[] {ex});
}
return url;
}
// Parse out a service URL including lifetime if required.
void
parseServiceURLOut(ServiceURL surl,
boolean lifetimeToo,
ByteArrayOutputStream baos)
throws ServiceLocationException {
String ssurl = surl.toString();
if (lifetimeToo) {
putInt(surl.getLifetime(), baos);
}
putString(ssurl, baos);
}
// Parse in a list of attributes, returing a vector of
// ServiceLocationAttribute objects.
protected Vector parseAttributeVectorIn(DataInputStream dis)
throws ServiceLocationException, IOException {
StringBuffer buf = new StringBuffer();
getString(buf, dis);
SLPConfig config = SLPConfig.getSLPConfig();
// Parse the list into ServiceLocationAttribute objects.
Vector attrForms = parseCommaSeparatedListIn(buf.toString(), false);
int i, n = attrForms.size();
for (i = 0; i < n; i++) {
String attrForm =
(String)attrForms.elementAt(i);
attrForms.setElementAt(new ServiceLocationAttributeV1(attrForm,
charCode,
false),
i);
}
return attrForms;
}
// Parse out a V1 attribute vector.
void
parseAttributeVectorOut(Vector attrs,
ByteArrayOutputStream baos)
throws ServiceLocationException {
Enumeration en = attrs.elements();
Vector strings = new Vector();
// Convert the attributes to strings, escaping characters to
// escape.
while (en.hasMoreElements()) {
ServiceLocationAttribute attr =
(ServiceLocationAttribute)en.nextElement();
// Make an SLPv1 attribute out of it, so we can
// externalize it with the v1 encoding scheme.
ServiceLocationAttributeV1 attrv1 =
new ServiceLocationAttributeV1(attr);
attrv1.charCode = charCode;
String out = attrv1.externalize();
strings.addElement(out);
}
// Parse it out.
parseCommaSeparatedListOut(strings, baos);
}
// Parse in previous responders.
void parsePreviousRespondersIn(DataInputStream dis)
throws ServiceLocationException, IOException {
StringBuffer buf = new StringBuffer();
getString(buf, dis);
previousResponders =
parseCommaSeparatedListIn(buf.toString(), true);
}
// Put out a vector of strings.
void putStringVector(Vector v, ByteArrayOutputStream baos) {
int i, n = v.size();
// Put out the total number of strings.
putInt(n, baos);
// Put out the strings.
for (i = 0; i < n; i++) {
putString((String)v.elementAt(i), baos);
}
}
// Return an SLPv1 DAAdvert.
SDAAdvert
getDAAdvert(short xid,
long timestamp,
ServiceURL url,
Vector scopes,
Vector attrs)
throws ServiceLocationException {
// If scopes vector is null, then return all scopes for this
// DA.
if (scopes.size() <= 0) {
scopes = SLPConfig.getSLPConfig().getSAConfiguredScopes();
}
return new SLPV1SDAAdvert(this, xid, timestamp, url, scopes, attrs);
}
// Reimplement clone() to get the header size right.
public Object clone()
throws CloneNotSupportedException {
SLPHeaderV1 hdr = (SLPHeaderV1)super.clone();
hdr.nbytes = HEADER_BYTES + 2; // for error code...
return hdr;
}
}
|