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 TcpipSMBPacketHandler extends PacketHandler 29 { 30 31 37 public TcpipSMBPacketHandler(Socket sock) throws IOException 38 { 39 super(sock, SMBSrvPacket.PROTOCOL_TCPIP, "TCP-SMB", "T"); 40 } 41 42 49 public 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 header, len=" + len); 69 70 72 int typ = (int) (buf[0] & 0xFF); 73 int dlen = (int) DataPacker.getShort(buf, 2); 74 75 77 if (buf[1] != 0) 78 { 79 int llen = (int) buf[1]; 80 dlen += (llen << 16); 81 } 82 83 85 if (buf.length < (dlen + RFCNetBIOSProtocol.HEADER_LEN)) 86 { 87 88 90 byte[] newBuf = new byte[dlen + RFCNetBIOSProtocol.HEADER_LEN]; 91 for (int i = 0; i < 4; i++) 92 newBuf[i] = buf[i]; 93 94 96 pkt.setBuffer(newBuf); 97 buf = newBuf; 98 } 99 100 103 int offset = RFCNetBIOSProtocol.HEADER_LEN; 104 int totlen = offset; 105 106 while (dlen > 0) 107 { 108 109 111 len = readPacket(buf, offset, dlen); 112 113 115 if (len == -1) 116 return -1; 117 118 120 totlen += len; 121 dlen -= len; 122 123 126 offset += len; 127 128 } 130 132 return totlen; 133 } 134 135 142 public void writePacket(SMBSrvPacket pkt, int len) throws IOException 143 { 144 145 148 byte[] buf = pkt.getBuffer(); 149 DataPacker.putInt(len, buf, 0); 150 151 153 int bufSiz = len + RFCNetBIOSProtocol.HEADER_LEN; 154 writePacket(buf, 0, bufSiz); 155 } 156 } 157 | Popular Tags |