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

Quick Search    Search Deep

Source code: com/act365/net/ip/IP4Writer.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  /**
32   IP4Writer writes IP4Message objects to a DatagramPacket.
33   Fragmentation isn't supported - all messages are created
34   with the DONT_FRAGMENT bit set and with offset set to 0.
35   However, the identified is increased in order to identify
36   packets uniquely.
37  */
38  
39  public class IP4Writer {
40  
41    static short identifier = 0 ;
42  
43    static byte[] write( IP4Message message ){
44  
45      byte[] buffer = new byte[ message.length ];
46  
47      buffer[0] = (byte)( message.version << 4 | message.headerlength ); 
48      buffer[1] = message.typeofservice ;
49  
50      SocketUtils.shortToBytes( message.length , buffer , 2 );
51      SocketUtils.shortToBytes( message.identifier , buffer , 4 );
52      SocketUtils.shortToBytes( message.offset , buffer , 6 );
53  
54      buffer[6] |= message.flags ;
55  
56      buffer[8] = (byte) message.timetolive ;
57      buffer[9] = message.protocol ;
58  
59      SocketUtils.shortToBytes( message.checksum , buffer , 10 );
60  
61      buffer[12] = message.source[0];
62      buffer[13] = message.source[1];
63      buffer[14] = message.source[2];
64      buffer[15] = message.source[3];
65  
66      buffer[16] = message.destination[0];
67      buffer[17] = message.destination[1];
68      buffer[18] = message.destination[2];
69      buffer[19] = message.destination[3];
70    
71      byte b = 4 ;
72  
73      while( ++ b < message.headerlength ){
74        SocketUtils.intToBytes( message.options[ b - 5 ] , buffer , 4 * b );
75      }
76  
77      int i = 4 * b - 1 ;
78      
79      while( ++ i < message.length ){
80        buffer[i] = message.data[ i - 4 * b ];
81      }
82      
83      return buffer ;
84    }
85  
86    /**
87     Builds a full IP4Message object and writes it to a buffer.
88     It is assumed that no options will be specified.
89    */
90  
91    public static byte[] write( byte typeofservice ,
92                                short timetolive ,
93                                byte protocol ,
94                                byte[] source ,
95                                byte[] destination ,
96                                byte[] data ) 
97    {
98      IP4Message message = new IP4Message();
99  
100     message.version = 4 ;
101     message.headerlength = 5 ;
102     message.typeofservice = typeofservice ;
103     message.length = (short)( 20 + data.length );
104     message.identifier = identifier ++ ;
105     message.flags = IP4.DONT_FRAGMENT ;
106     message.offset = (short) 0 ;
107     message.timetolive = timetolive ;
108     message.protocol = protocol ;
109     message.checksum = 0 ;
110     message.source = source ;
111     message.destination = destination ;
112     message.options = new int[0];
113     message.data = data ;
114 
115     message.checksum = SocketUtils.checksum( write( message ) , 20 , 0 );
116        
117     return write( message );
118   }
119 }
120