KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > NetBIOSPacketHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.server;
18
19 import java.io.IOException JavaDoc;
20 import java.net.Socket JavaDoc;
21
22 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol;
23 import org.alfresco.filesys.util.DataPacker;
24
25 /**
26  * NetBIOS Protocol Packet Handler Class
27  */

28 public class NetBIOSPacketHandler extends PacketHandler
29 {
30
31     /**
32      * Class constructor
33      *
34      * @param sock Socket
35      * @exception IOException If a network error occurs
36      */

37     public NetBIOSPacketHandler(Socket JavaDoc sock) throws IOException JavaDoc
38     {
39         super(sock, SMBSrvPacket.PROTOCOL_NETBIOS, "NetBIOS", "NB");
40     }
41
42     /**
43      * Read a packet from the input stream
44      *
45      * @param pkt SMBSrvPacket
46      * @return int
47      * @exception IOexception If a network error occurs
48      */

49     public final int readPacket(SMBSrvPacket pkt) throws IOException JavaDoc
50     {
51
52         // Read the packet header
53

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         // Check if the connection has been closed, read length equals -1
61

62         if (len == -1)
63             return len;
64
65         // Check if we received a valid NetBIOS header
66

67         if (len < RFCNetBIOSProtocol.HEADER_LEN)
68             throw new IOException JavaDoc("Invalid NetBIOS header, len=" + len);
69
70         // Get the packet type from the header
71

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         // Check for a session keep alive type message
80

81         if (typ == RFCNetBIOSProtocol.SESSION_KEEPALIVE)
82             return 0;
83
84         // Check if the packet buffer is large enough to hold the data + header
85

86         if (buf.length < (dlen + RFCNetBIOSProtocol.HEADER_LEN))
87         {
88
89             // Allocate a new buffer to hold the data and copy the existing header
90

91             byte[] newBuf = new byte[dlen + RFCNetBIOSProtocol.HEADER_LEN];
92             for (int i = 0; i < 4; i++)
93                 newBuf[i] = buf[i];
94
95             // Attach the new buffer to the SMB packet
96

97             pkt.setBuffer(newBuf);
98             buf = newBuf;
99         }
100
101         // Read the data part of the packet into the users buffer, this may take
102
// several reads
103

104         int offset = RFCNetBIOSProtocol.HEADER_LEN;
105         int totlen = offset;
106
107         while (dlen > 0)
108         {
109
110             // Read the data
111

112             len = readPacket(buf, offset, dlen);
113
114             // Check if the connection has been closed
115

116             if (len == -1)
117                 return -1;
118
119             // Update the received length and remaining data length
120

121             totlen += len;
122             dlen -= len;
123
124             // Update the user buffer offset as more reads will be required
125
// to complete the data read
126

127             offset += len;
128
129         } // end while reading data
130

131         // Return the received packet length
132

133         return totlen;
134     }
135
136     /**
137      * Send a packet to the output stream
138      *
139      * @param pkt SMBSrvPacket
140      * @param len int
141      * @exception IOexception If a network error occurs
142      */

143     public final void writePacket(SMBSrvPacket pkt, int len) throws IOException JavaDoc
144     {
145
146         // Fill in the NetBIOS message header, this is already allocated as
147
// part of the users buffer.
148

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             // Set the >64K flag
157

158             buf[1] = (byte) 0x01;
159
160             // Set the low word of the data length
161

162             DataPacker.putShort((short) (len & 0xFFFF), buf, 2);
163         }
164         else
165         {
166
167             // Set the data length
168

169             DataPacker.putShort((short) len, buf, 2);
170         }
171
172         // Output the data packet
173

174         int bufSiz = len + RFCNetBIOSProtocol.HEADER_LEN;
175         writePacket(buf, 0, bufSiz);
176     }
177 }
178
Popular Tags