1 18 19 package jcifs.netbios; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 public abstract class SessionServicePacket { 25 26 static final int SESSION_MESSAGE = 0x00; 28 static final int SESSION_REQUEST = 0x81; 29 public static final int POSITIVE_SESSION_RESPONSE = 0x82; 30 public 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 readn( InputStream in, 64 byte[] b, 65 int off, 66 int len ) throws IOException { 67 int i = 0, n; 68 69 while (i < len) { 70 n = in.read( b, off + i, len - i ); 71 if (n <= 0) { 72 break; 73 } 74 i += n; 75 } 76 77 return i; 78 } 79 static int readPacketType( InputStream in, 80 byte[] buffer, 81 int bufferIndex ) 82 throws IOException { 83 int n; 84 if(( n = readn( in, buffer, bufferIndex, HEADER_LENGTH )) != HEADER_LENGTH ) { 85 if( n == -1 ) { 86 return -1; 87 } 88 throw new IOException ( "unexpected EOF reading netbios session header" ); 89 } 90 int t = buffer[bufferIndex] & 0xFF; 91 return t; 92 } 93 94 int type, length; 95 96 public int writeWireFormat( byte[] dst, int dstIndex ) { 97 length = writeTrailerWireFormat( dst, dstIndex + HEADER_LENGTH ); 98 writeHeaderWireFormat( dst, dstIndex ); 99 return HEADER_LENGTH + length; 100 } 101 int readWireFormat( InputStream in, byte[] buffer, int bufferIndex ) throws IOException { 102 readHeaderWireFormat( in, buffer, bufferIndex ); 103 return HEADER_LENGTH + readTrailerWireFormat( in, buffer, bufferIndex ); 104 } 105 int writeHeaderWireFormat( byte[] dst, int dstIndex ) { 106 dst[dstIndex++] = (byte)type; 107 if( length > 0x0000FFFF ) { 108 dst[dstIndex] = (byte)0x01; 109 } 110 dstIndex++; 111 writeInt2( length, dst, dstIndex ); 112 return HEADER_LENGTH; 113 } 114 int readHeaderWireFormat( InputStream in, 115 byte[] buffer, 116 int bufferIndex ) 117 throws IOException { 118 type = buffer[bufferIndex++] & 0xFF; 119 length = (( buffer[bufferIndex] & 0x01 ) << 16 ) + readInt2( buffer, bufferIndex + 1 ); 120 return HEADER_LENGTH; 121 } 122 123 abstract int writeTrailerWireFormat( byte[] dst, int dstIndex ); 124 abstract int readTrailerWireFormat( InputStream in, 125 byte[] buffer, 126 int bufferIndex ) 127 throws IOException ; 128 } 129 | Popular Tags |