1 18 19 package com.knowgate.jcifs.netbios; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 abstract class SessionServicePacket { 25 26 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 in, 64 byte[] buffer, 65 int bufferIndex ) 66 throws IOException { 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 ( "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 in, byte[] buffer, int bufferIndex ) throws IOException { 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 in, 99 byte[] buffer, 100 int bufferIndex ) 101 throws IOException { 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 in, 109 byte[] buffer, 110 int bufferIndex ) 111 throws IOException ; 112 } 113 | Popular Tags |