Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/act365/net/ip/IP4Reader.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.ip ;
28  
29  import com.act365.net.*;
30  
31  import java.io.* ;
32  
33  /**
34   IP4Reader reads IP4Message objects from a data buffer.
35  */
36  
37  public class IP4Reader {
38  
39    /**
40     read() constructs an IP4 message from a buffer. IP6 is not   
41     supported.
42    */
43  
44    public static IP4Message read( byte[] buffer , int length , boolean testchecksum ) throws IOException {
45  
46      if( length < 20 ) {
47        throw new IOException("IP4 messages must be at least twenty bytes long");
48      }
49  
50      IP4Message message = new IP4Message();
51  
52      if( ( message.version = (byte)( buffer[0] >>> 4 ) ) != 4 ){
53        throw new IOException("Only IP v4 is supported");
54      }
55  
56      message.headerlength = (byte)( buffer[0] & 0xf );
57      message.typeofservice = buffer[1];
58      message.length = SocketUtils.shortFromBytes( buffer , 2 );
59      message.identifier = SocketUtils.shortFromBytes( buffer , 4 );
60      message.flags = (byte)( buffer[6] & 0x60 );
61  
62      if( ( message.flags & IP4.MUST_FRAGMENT ) != 0 ){
63        throw new IOException("Fragmentation is not supported");
64      }
65  
66      message.offset = (short)( SocketUtils.shortFromBytes( buffer , 6 ) & 0x1fff );
67      message.timetolive = buffer[8];
68      message.protocol = buffer[9];
69      message.checksum = SocketUtils.shortFromBytes( buffer , 10 );
70  
71      message.source = new byte[4];
72      message.source[0] = buffer[12];
73      message.source[1] = buffer[13];
74      message.source[2] = buffer[14];
75      message.source[3] = buffer[15];
76  
77      message.destination = new byte[4];
78      message.destination[0] = buffer[16];
79      message.destination[1] = buffer[17];
80      message.destination[2] = buffer[18];
81      message.destination[3] = buffer[19];
82  
83      message.options = new int[ message.headerlength - 5 ];
84  
85      byte b = 4 ;
86  
87      while ( ++ b < message.headerlength ){
88        message.options[ b - 5 ] = SocketUtils.intFromBytes( buffer , 4 * b );
89      } 
90  
91      short checksum ;
92  
93      if( testchecksum && ( checksum = SocketUtils.checksum( buffer , 4 * message.headerlength , 0 ) ) != 0 ){
94        throw new IOException("Checksum error: " + checksum );
95      }
96  
97      message.data = new byte[ message.length - 4 * message.headerlength ];
98  
99      int i = 4 * message.headerlength - 1 ;
100 
101     while( ++ i < message.length ){
102       message.data[ i - 4 * message.headerlength ] = buffer[ i ];
103     }
104         
105     return message ;
106   }
107 }