Source code: com/voytechs/jnetstream/protocol/layer3/IPv4.java
1 /*
2 * File: IPv4.java
3 * Auth: Mark Bednarczyk
4 * Date: DATE
5 * Id: $Id: IPv4.java,v 1.1.1.1 2003/09/22 16:32:12 voytechs Exp $
6 ********************************************
7 Copyright (C) 2003 Mark Bednarczyk
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 ********************************************
23 * $Log: IPv4.java,v $
24 * Revision 1.1.1.1 2003/09/22 16:32:12 voytechs
25 * Initial import.
26 *
27 */
28 package com.voytechs.jnetstream.protocol.layer3;
29
30 import com.voytechs.jnetstream.protocol.*;
31 import com.voytechs.jnetstream.io.*;
32 import com.voytechs.jnetstream.primitive.*;
33 import com.voytechs.jnetstream.primitive.address.*;
34 import com.voytechs.jnetstream.codec.*;
35
36
37 import java.lang.*;
38 import java.util.*;
39 import java.io.IOException;
40
41 /**
42 *
43 */
44 public class IPv4
45 extends HeaderImpl {
46 /* Internal attributes */
47 private static final boolean debug = false;
48
49 public static final String NAME = "ip-v4";
50 private static Hashtable perm = new Hashtable();
51
52 /* IP Version */
53 public int ver;
54
55 /* Header Len * 4 */
56 public int hlen;
57
58 /* Type Of Server */
59 public int tos;
60
61 /* length of IP dgram */
62 public int len;
63
64 /* id of the packet */
65 public int id;
66
67 /* flags */
68 public int flags;
69
70 /* offset of the fragment */
71 public int offset;
72
73 /* time to live (ttl) */
74 public int ttl;
75
76 /* next protocol */
77 public int proto;
78
79 /* crc */
80 public int crc;
81
82
83 /* SRC IP address */
84 public IpAddress src;
85
86 /* DST IP Address */
87 public IpAddress dst;
88
89
90 /**
91 *
92 * @param
93 * @exception
94 */
95 public IPv4(PacketInputStream in)
96 throws IOException, EOPacket, EOPacketStream {
97
98 super(NAME, perm);
99
100 ver = in.readBits(4);
101 hlen = in.readBits(4);
102 tos = in.readUnsignedByte();
103 len = in.readUnsignedShort();
104 id = in.readUnsignedShort();
105 flags = in.readBits(3);
106 offset = in.readBits(13);
107 ttl = in.readUnsignedByte();
108 proto = in.readUnsignedByte();
109 crc = in.readUnsignedShort();
110 src = (IpAddress)(new IpAddressPrimitive(in)).getValue();
111 dst = (IpAddress)(new IpAddressPrimitive(in)).getValue();
112
113 }
114
115
116 /**
117 * Returns the length of the header
118 */
119 public int getHeaderLength() {
120 return(hlen *4);
121 }
122
123 /**
124 * Conventience routing to print directory from stream.
125 */
126 public static String toString(PacketInputStream in)
127 throws IOException, EOPacket, EOPacketStream {
128 String s = "";
129
130 s += "[IPv4=>";
131 s += " ver=" + in.readBits(4);
132 s += " hlen=" + in.readBits(4);
133 s += " tos=0x" + Integer.toHexString(in.readUnsignedByte());
134 s += " len=" + in.readUnsignedShort();
135 s += " id=" + in.readUnsignedShort();
136 s += " flags=0x" + Integer.toHexString(in.readBits(3));
137 s += " offset=" + in.readBits(13);
138 s += " ttl=" + in.readUnsignedByte();
139 s += " proto=" + in.readUnsignedByte();
140 s += " crc=0x" + Integer.toHexString(in.readUnsignedShort());
141 s += " src=" + IpAddressPrimitive.toString(in);
142 s += " dst=" + IpAddressPrimitive.toString(in);
143
144 s += "]";
145
146 return(s);
147 }
148
149
150 /**
151 * Test function for IPv4
152 * @param args command line arguments
153 */
154 public static void main(String [] args) {
155 }
156
157 } /* END OF: IPv4 */