1 17 package org.alfresco.filesys.smb.server; 18 19 import java.io.IOException ; 20 import java.net.Socket ; 21 22 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol; 23 import org.alfresco.filesys.util.DataPacker; 24 25 28 public class NetBIOSPacketHandler extends PacketHandler 29 { 30 31 37 public NetBIOSPacketHandler(Socket sock) throws IOException 38 { 39 super(sock, SMBSrvPacket.PROTOCOL_NETBIOS, "NetBIOS", "NB"); 40 } 41 42 49 public final int readPacket(SMBSrvPacket pkt) throws IOException 50 { 51 52 54 byte[] buf = pkt.getBuffer(); 55 int len = 0; 56 57 while (len < RFCNetBIOSProtocol.HEADER_LEN && len != -1) 58 len = readPacket(buf, len, RFCNetBIOSProtocol.HEADER_LEN - len); 59 60 62 if (len == -1) 63 return len; 64 65 67 if (len < RFCNetBIOSProtocol.HEADER_LEN) 68 throw new IOException ("Invalid NetBIOS header, len=" + len); 69 70 72 int typ = (int) (buf[0] & 0xFF); 73 int flags = (int) buf[1]; 74 int dlen = (int) DataPacker.getShort(buf, 2); 75 76 if ((flags & 0x01) != 0) 77 dlen += 0x10000; 78 79 81 if (typ == RFCNetBIOSProtocol.SESSION_KEEPALIVE) 82 return 0; 83 84 86 if (buf.length < (dlen + RFCNetBIOSProtocol.HEADER_LEN)) 87 { 88 89 91 byte[] newBuf = new byte[dlen + RFCNetBIOSProtocol.HEADER_LEN]; 92 for (int i = 0; i < 4; i++) 93 newBuf[i] = buf[i]; 94 95 97 pkt.setBuffer(newBuf); 98 buf = newBuf; 99 } 100 101 104 int offset = RFCNetBIOSProtocol.HEADER_LEN; 105 int totlen = offset; 106 107 while (dlen > 0) 108 { 109 110 112 len = readPacket(buf, offset, dlen); 113 114 116 if (len == -1) 117 return -1; 118 119 121 totlen += len; 122 dlen -= len; 123 124 127 offset += len; 128 129 } 131 133 return totlen; 134 } 135 136 143 public final void writePacket(SMBSrvPacket pkt, int len) throws IOException 144 { 145 146 149 byte[] buf = pkt.getBuffer(); 150 buf[0] = (byte) RFCNetBIOSProtocol.SESSION_MESSAGE; 151 buf[1] = (byte) 0; 152 153 if (len > 0xFFFF) 154 { 155 156 158 buf[1] = (byte) 0x01; 159 160 162 DataPacker.putShort((short) (len & 0xFFFF), buf, 2); 163 } 164 else 165 { 166 167 169 DataPacker.putShort((short) len, buf, 2); 170 } 171 172 174 int bufSiz = len + RFCNetBIOSProtocol.HEADER_LEN; 175 writePacket(buf, 0, bufSiz); 176 } 177 } 178 | Popular Tags |