PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/TestTools/src/jpcap/packet/IPPacket.java

http://ravi-project-iptest.googlecode.com/
Java | 293 lines | 144 code | 45 blank | 104 comment | 9 complexity | a665c9fa3d752aeb58d4ffc2e2c07b71 MD5 | raw file
  1. package jpcap.packet;
  2. import java.net.Inet4Address;
  3. import java.net.Inet6Address;
  4. import java.net.InetAddress;
  5. import java.net.UnknownHostException;
  6. /**
  7. * This class represents an IP packet.
  8. * <P>
  9. * Both IPv4 and IPv6 are supported.
  10. */
  11. public class IPPacket extends Packet {
  12. private static final long serialVersionUID = 6427030004078082099L;
  13. /** IP version (v4/v6) */
  14. public byte version=4;
  15. /** Priority (class) (v4/v6) */
  16. public byte priority;
  17. /** IP flag bit: [D]elay (v4) */
  18. public boolean d_flag;
  19. /** IP flag bit: [T]hrough (v4) */
  20. public boolean t_flag;
  21. /** IP flag bit: [R]eliability (v4) */
  22. public boolean r_flag;
  23. // added by Damien Daspit 5/7/01
  24. /** Type of Service (TOS) (v4/v6) */
  25. public byte rsv_tos;
  26. // *****************************
  27. /** Packet length (v4/v6) */
  28. public short length;
  29. /** Fragmentation reservation flag (v4) */
  30. public boolean rsv_frag;
  31. /** Don't fragment flag (v4) */
  32. public boolean dont_frag;
  33. /** More fragment flag (v4) */
  34. public boolean more_frag;
  35. /** Fragment offset (v4) */
  36. public short offset;
  37. /** Hop Limit, Time To Live (TTL) (v4/v6) */
  38. public short hop_limit=64;
  39. /** Protocol (v4/v6) */
  40. public short protocol;
  41. /** Protocol number for ICMP */
  42. public static final short IPPROTO_ICMP = 1;
  43. /** Protocol number for IGMP */
  44. public static final short IPPROTO_IGMP = 2;
  45. /** Protocol number for IP in IP */
  46. public static final short IPPROTO_IP = 4;
  47. /** Protocol number for TCP */
  48. public static final short IPPROTO_TCP = 6;
  49. /** Protocol number for UDP */
  50. public static final short IPPROTO_UDP = 17;
  51. /** Protocol number for IPv6 */
  52. public static final short IPPROTO_IPv6 = 41;
  53. /** Protocol number for IPv6 hop-by-hop option */
  54. public static final short IPPROTO_HOPOPT = 0;
  55. /** Protocol number for routing header for IPv6 */
  56. public static final short IPPROTO_IPv6_Route = 43;
  57. /** Protocol number for fragment header for IPv6 */
  58. public static final short IPPROTO_IPv6_Frag = 44;
  59. /** Protocol number for IPv6 ICMP */
  60. public static final short IPPROTO_IPv6_ICMP = 58;
  61. /** Protocol number for no next header header for IPv6 */
  62. public static final short IPPROTO_IPv6_NoNxt = 59;
  63. /** Protocol number for destination option for IPv6 */
  64. public static final short IPPROTO_IPv6_Opts = 60;
  65. /** IDENTIFICATION (v4) */
  66. public int ident;
  67. /** Flow label (v6) */
  68. public int flow_label;
  69. /** Source IP address */
  70. public InetAddress src_ip;
  71. /** Destination IP address */
  72. public InetAddress dst_ip;
  73. /** Option in IPv4 header (v4) */
  74. public byte[] option;
  75. /** Option headers in IPv6Option (v6) */
  76. public java.util.List options = null;
  77. public IPPacket(String sIP,String dIP)throws Exception{
  78. this.src_ip=InetAddress.getByName(sIP);
  79. this.dst_ip=InetAddress.getByName(dIP);
  80. }
  81. /**
  82. * Sets the IPv4 parameters
  83. *
  84. * @param d_flag
  85. * IP flag bit: [D]elay
  86. * @param t_flag
  87. * IP flag bit: [T]hrough
  88. * @param r_flag
  89. * IP flag bit: [R]eliability
  90. * @param rsv_tos
  91. * Type of Service (TOS)
  92. * @param priority
  93. * Priority
  94. * @param rsv_frag
  95. * Fragmentation Reservation flag
  96. * @param dont_frag
  97. * Don't fragment flag
  98. * @param more_frag
  99. * More fragment flag
  100. * @param offset
  101. * Offset
  102. * @param ident
  103. * Identifier
  104. * @param ttl
  105. * Time To Live
  106. * @param protocol
  107. * Protocol <BR>
  108. * This value is ignored when this packets inherits a higher
  109. * layer protocol(e.g. TCPPacket)
  110. * @param src
  111. * Source IP address
  112. * @param dst
  113. * Destination IP address
  114. */
  115. public void setIPv4Parameter(int priority, boolean d_flag, boolean t_flag,
  116. boolean r_flag, int rsv_tos, boolean rsv_frag, boolean dont_frag,
  117. boolean more_frag, int offset, int ident, int ttl, int protocol,
  118. InetAddress src, InetAddress dst) {
  119. this.version = 4;
  120. this.priority = (byte) priority;
  121. this.d_flag = d_flag;
  122. this.t_flag = t_flag;
  123. this.r_flag = r_flag;
  124. // added by Damien Daspit 5/7/01
  125. this.rsv_tos = (byte) rsv_tos;
  126. // *****************************
  127. this.rsv_frag = rsv_frag;
  128. this.dont_frag = dont_frag;
  129. this.more_frag = more_frag;
  130. offset = (short) offset;
  131. this.ident = ident;
  132. this.hop_limit = (short) ttl;
  133. this.protocol = (short) protocol;
  134. if(src instanceof Inet6Address || dst instanceof Inet6Address)
  135. throw new IllegalArgumentException("Address must be Inet4Address");
  136. this.src_ip = src;
  137. this.dst_ip = dst;
  138. }
  139. /**
  140. * Sets the IPv6 parameters
  141. *
  142. * @param cls
  143. * class
  144. * @param flowlabel
  145. * flow label
  146. * @param nxt_hdr
  147. * next header
  148. * @param hop_limit
  149. * hop limit
  150. * @param src
  151. * source address
  152. * @param dst
  153. * destination address
  154. */
  155. public void setIPv6Parameter(int cls, int flowlabel, int nxt_hdr,
  156. int hop_limit, InetAddress src, InetAddress dst) {
  157. this.version = 6;
  158. this.priority = (byte) cls;
  159. this.flow_label = flowlabel;
  160. this.protocol = (short) nxt_hdr;
  161. this.hop_limit = (short) hop_limit;
  162. if(src instanceof Inet4Address || dst instanceof Inet4Address)
  163. throw new IllegalArgumentException("Address must be Inet6Address");
  164. this.src_ip = src;
  165. this.dst_ip = dst;
  166. }
  167. void setIPv4Value(byte ver, byte pri, boolean d, boolean t, boolean r,
  168. byte rsv_tos, boolean rf, boolean df, boolean mf, short offset,
  169. short len, short ident, short ttl, short proto, byte[] src,
  170. byte[] dst) {
  171. this.version = ver;
  172. this.priority = pri;
  173. d_flag = d;
  174. t_flag = t;
  175. r_flag = r;
  176. // added by Damien Daspit 5/7/01
  177. this.rsv_tos = rsv_tos;
  178. // *****************************
  179. rsv_frag = rf;
  180. dont_frag = df;
  181. more_frag = mf;
  182. this.offset = offset;
  183. this.length = len;
  184. this.ident = ident;
  185. this.hop_limit = ttl;
  186. this.protocol = proto;
  187. try {
  188. this.src_ip = InetAddress.getByAddress(src);
  189. this.dst_ip = InetAddress.getByAddress(dst);
  190. } catch (UnknownHostException e) {
  191. }
  192. }
  193. void setOption(byte[] option) {
  194. this.option = option;
  195. }
  196. void setIPv6Value(byte ver, byte v6class, int flow, short payload,
  197. byte nxt, short hlim, byte[] src, byte[] dst) {
  198. this.version = ver;
  199. this.priority = v6class;
  200. this.flow_label = flow;
  201. this.length = payload;
  202. this.protocol = nxt;
  203. this.hop_limit = hlim;
  204. try {
  205. this.src_ip = InetAddress.getByAddress(src);
  206. this.dst_ip = InetAddress.getByAddress(dst);
  207. } catch (UnknownHostException e) {
  208. }
  209. }
  210. void addOptionHeader(IPv6Option header) {
  211. if (options == null)
  212. options = new java.util.ArrayList();
  213. options.add(header);
  214. }
  215. byte[] getSourceAddress() {
  216. return src_ip.getAddress();
  217. }
  218. byte[] getDestinationAddress() {
  219. return dst_ip.getAddress();
  220. }
  221. /**
  222. * Returns a string represenation of this packet.
  223. * <P>
  224. * Format(IPv4): src_ip->dst_ip protocol(protocol) priority(priority)
  225. * [D][T][R] hop(hop_limit) [RF/][DF/][MF] offset(offset) ident(ident)
  226. * <P>
  227. *
  228. * Format(IPv6): src_ip->dst_ip protocol(protocol) priority(priority)
  229. * flowlabel(flow_label) hop(hop_limit)
  230. *
  231. * @return a string represenation of this packet
  232. */
  233. public String toString() {
  234. if (version == 4) {
  235. return super.toString() + " " + src_ip + "->" + dst_ip
  236. + " protocol(" + protocol + ") priority(" + priority + ") "
  237. + (d_flag ? "D" : "") + (t_flag ? "T" : "")
  238. + (r_flag ? "R" : "") + " hop(" + hop_limit + ") "
  239. + (rsv_frag ? "RF/" : "") + (dont_frag ? "DF/" : "")
  240. + (more_frag ? "MF" : "") + " offset(" + offset
  241. + ") ident(" + ident + ")";
  242. } else {
  243. return super.toString() + " " + src_ip + "->" + dst_ip
  244. + " protocol(" + protocol + ") priority(" + priority
  245. + ") flowlabel(" + flow_label + ") hop(" + hop_limit + ")";
  246. }
  247. }
  248. }