Source code: com/act365/net/tcp/TCPReader.java
1 /*
2 * JSocket Wrench
3 *
4 * Copyright (C) act365.com October 2003
5 *
6 * Web site: http://www.act365.com/wrench
7 * E-mail: developers@act365.com
8 *
9 * The JSocket Wrench library adds support for low-level Internet protocols
10 * to the Java programming language.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 * Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 package com.act365.net.tcp ;
28
29 import com.act365.net.* ;
30
31 import java.io.IOException ;
32
33 /**
34 * Reads TCP messages.
35 */
36
37 public class TCPReader {
38
39 /**
40 * Reads a TCP message from a buffer without a checksum test.
41 */
42
43 public static TCPMessage read( byte[] buffer , int offset , int count ) throws IOException {
44 return read( buffer , offset , count , false , new byte[0] , new byte[0] );
45 }
46
47 /**
48 * Reads a TCP message from a buffer and performs a checksum test
49 * @param buffer buffer to be read from
50 * @param offset position within buffer to read from
51 * @param count number of bytes to read
52 * @param testchecksum whether to test the checksum in the message
53 * @param source message source (used in checksum test)
54 * @param destination message destination (used in checksum test)
55 * @return the decoded message
56 * @throws IOException checksum error
57 */
58
59 public static TCPMessage read( byte[] buffer ,
60 int offset ,
61 int count ,
62 boolean testchecksum ,
63 byte[] source ,
64 byte[] destination ) throws IOException {
65
66 TCPMessage message = new TCPMessage();
67
68 message.sourceport = SocketUtils.shortFromBytes( buffer , offset );
69 message.destinationport = SocketUtils.shortFromBytes( buffer , offset + 2 );
70 message.sequencenumber = SocketUtils.intFromBytes( buffer , offset + 4 );
71 message.acknowledgementnumber = SocketUtils.intFromBytes( buffer , offset + 8 );
72
73 int headerlength = buffer[ offset + 12 ] >= 0 ? buffer[ offset + 12 ] : 0xffffff00 ^ buffer[ offset + 12 ];
74
75 message.headerlength = headerlength >> 4 ;
76 message.urg = ( buffer[ offset + 13 ] & TCP.URG ) != 0 ;
77 message.ack = ( buffer[ offset + 13 ] & TCP.ACK ) != 0 ;
78 message.psh = ( buffer[ offset + 13 ] & TCP.PSH ) != 0 ;
79 message.rst = ( buffer[ offset + 13 ] & TCP.RST ) != 0 ;
80 message.syn = ( buffer[ offset + 13 ] & TCP.SYN ) != 0 ;
81 message.fin = ( buffer[ offset + 13 ] & TCP.FIN ) != 0 ;
82 message.windowsize = SocketUtils.shortFromBytes( buffer , offset + 14 );
83 message.checksum = SocketUtils.shortFromBytes( buffer , offset + 16 );
84 message.urgentpointer = SocketUtils.shortFromBytes( buffer , offset + 18 );
85
86 if( count < 4 * message.headerlength ){
87 throw new IOException("TCP message should be longer");
88 }
89
90 message.options = new byte[ 4 * message.headerlength - 20 ];
91
92 int i = offset + 19 ;
93
94 while( ++ i < offset + 4 * message.headerlength ){
95 message.options[ i - offset - 20 ] = buffer[ i ];
96 }
97
98 message.data = new byte[ count - 4 * message.headerlength ];
99
100 while( i < offset + count ){
101 message.data[ i - offset - 4 * message.headerlength ] = buffer[ i ];
102 ++ i ;
103 }
104
105 if( testchecksum ){
106
107 short checksum = SocketUtils.checksum( source ,
108 destination ,
109 (byte) SocketConstants.IPPROTO_TCP ,
110 (short) count ,
111 buffer ,
112 offset );
113
114 if( checksum != 0 ){
115 throw new IOException("Checksum error: " + checksum );
116 }
117 }
118
119 return message ;
120 }
121 }
122
123