KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > netbios > SessionServicePacket


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package com.knowgate.jcifs.netbios;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 abstract class SessionServicePacket {
25
26     // session service packet types
27
static final int SESSION_MESSAGE = 0x00;
28     static final int SESSION_REQUEST = 0x81;
29     static final int POSITIVE_SESSION_RESPONSE = 0x82;
30     static final int NEGATIVE_SESSION_RESPONSE = 0x83;
31     static final int SESSION_RETARGET_RESPONSE = 0x84;
32     static final int SESSION_KEEP_ALIVE = 0x85;
33
34     static final int MAX_MESSAGE_SIZE = 0x0001FFFF;
35     static final int HEADER_LENGTH = 4;
36
37     static void writeInt2( int val, byte[] dst, int dstIndex ) {
38         dst[dstIndex++] = (byte)(( val >> 8 ) & 0xFF );
39         dst[dstIndex] = (byte)( val & 0xFF );
40     }
41     static void writeInt4( int val, byte[] dst, int dstIndex ) {
42         dst[dstIndex++] = (byte)(( val >> 24 ) & 0xFF );
43         dst[dstIndex++] = (byte)(( val >> 16 ) & 0xFF );
44         dst[dstIndex++] = (byte)(( val >> 8 ) & 0xFF );
45         dst[dstIndex] = (byte)( val & 0xFF );
46     }
47     static int readInt2( byte[] src, int srcIndex ) {
48         return (( src[srcIndex] & 0xFF ) << 8 ) +
49                 ( src[srcIndex + 1] & 0xFF );
50     }
51     static int readInt4( byte[] src, int srcIndex ) {
52         return (( src[srcIndex] & 0xFF ) << 24 ) +
53                 (( src[srcIndex + 1] & 0xFF ) << 16 ) +
54                 (( src[srcIndex + 2] & 0xFF ) << 8 ) +
55                 ( src[srcIndex + 3] & 0xFF );
56     }
57     static int readLength( byte[] src, int srcIndex ) {
58         srcIndex++;
59         return (( src[srcIndex++] & 0x01 ) << 16 ) +
60                 (( src[srcIndex++] & 0xFF ) << 8 ) +
61                 ( src[srcIndex++] & 0xFF );
62     }
63     static int readPacketType( InputStream JavaDoc in,
64                                     byte[] buffer,
65                                     int bufferIndex )
66                                     throws IOException JavaDoc {
67         int n;
68         if(( n = in.read( buffer, bufferIndex, HEADER_LENGTH )) != HEADER_LENGTH ) {
69             if( n == -1 ) {
70                 return -1;
71             }
72             throw new IOException JavaDoc( "unexpected EOF reading netbios session header" );
73         }
74         int t = buffer[bufferIndex] & 0xFF;
75         return t;
76     }
77
78     int type, length;
79
80     int writeWireFormat( byte[] dst, int dstIndex ) {
81         length = writeTrailerWireFormat( dst, dstIndex + HEADER_LENGTH );
82         writeHeaderWireFormat( dst, dstIndex );
83         return HEADER_LENGTH + length;
84     }
85     int readWireFormat( InputStream JavaDoc in, byte[] buffer, int bufferIndex ) throws IOException JavaDoc {
86         readHeaderWireFormat( in, buffer, bufferIndex );
87         return HEADER_LENGTH + readTrailerWireFormat( in, buffer, bufferIndex );
88     }
89     int writeHeaderWireFormat( byte[] dst, int dstIndex ) {
90         dst[dstIndex++] = (byte)type;
91         if( length > 0x0000FFFF ) {
92             dst[dstIndex] = (byte)0x01;
93         }
94         dstIndex++;
95         writeInt2( length, dst, dstIndex );
96         return HEADER_LENGTH;
97     }
98     int readHeaderWireFormat( InputStream JavaDoc in,
99                                 byte[] buffer,
100                                 int bufferIndex )
101                                 throws IOException JavaDoc {
102         type = buffer[bufferIndex++] & 0xFF;
103         length = (( buffer[bufferIndex] & 0x01 ) << 16 ) + readInt2( buffer, bufferIndex + 1 );
104         return HEADER_LENGTH;
105     }
106
107     abstract int writeTrailerWireFormat( byte[] dst, int dstIndex );
108     abstract int readTrailerWireFormat( InputStream JavaDoc in,
109                                 byte[] buffer,
110                                 int bufferIndex )
111                                 throws IOException JavaDoc;
112 }
113
Popular Tags