KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Tcpip SMB Packet Handler Class
27  */

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

37     public TcpipSMBPacketHandler(Socket JavaDoc sock) throws IOException JavaDoc
38     {
39         super(sock, SMBSrvPacket.PROTOCOL_TCPIP, "TCP-SMB", "T");
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 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 header
66

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

72         int typ = (int) (buf[0] & 0xFF);
73         int dlen = (int) DataPacker.getShort(buf, 2);
74
75         // Check for a large packet, add to the data length
76

77         if (buf[1] != 0)
78         {
79             int llen = (int) buf[1];
80             dlen += (llen << 16);
81         }
82
83         // Check if the packet buffer is large enough to hold the data + header
84

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

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

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

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

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

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

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

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

130         // Return the received packet length
131

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

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

148         byte[] buf = pkt.getBuffer();
149         DataPacker.putInt(len, buf, 0);
150
151         // Output the data packet
152

153         int bufSiz = len + RFCNetBIOSProtocol.HEADER_LEN;
154         writePacket(buf, 0, bufSiz);
155     }
156 }
157
Popular Tags