Branch data Line data Source code
1 : : // $Id: IP.h 6219 2008-10-01 05:39:07Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #ifndef ip_h
6 : : #define ip_h
7 : :
8 : : #include "config.h"
9 : :
10 : : #include <net_util.h>
11 : :
12 : : class IP_Hdr {
13 : : public:
14 : 1 : IP_Hdr(struct ip* arg_ip4)
15 : : {
16 : 1 : ip4 = arg_ip4;
17 : 1 : ip6 = 0;
18 : 1 : del = 1;
19 : :
20 : : #ifdef BROv6
21 : : src_addr[0] = src_addr[1] = src_addr[2] = 0;
22 : : dst_addr[0] = dst_addr[1] = dst_addr[2] = 0;
23 : :
24 : : src_addr[3] = ip4->ip_src.s_addr;
25 : : dst_addr[3] = ip4->ip_dst.s_addr;
26 : : #endif
27 : 1 : }
28 : :
29 : 20378 : IP_Hdr(const struct ip* arg_ip4)
30 : : {
31 : 20378 : ip4 = arg_ip4;
32 : 20378 : ip6 = 0;
33 : 20378 : del = 0;
34 : :
35 : : #ifdef BROv6
36 : : src_addr[0] = src_addr[1] = src_addr[2] = 0;
37 : : dst_addr[0] = dst_addr[1] = dst_addr[2] = 0;
38 : :
39 : : src_addr[3] = ip4->ip_src.s_addr;
40 : : dst_addr[3] = ip4->ip_dst.s_addr;
41 : : #endif
42 : 20378 : }
43 : :
44 : : IP_Hdr(struct ip6_hdr* arg_ip6)
45 : : {
46 : : ip4 = 0;
47 : : ip6 = arg_ip6;
48 : : del = 1;
49 : :
50 : : #ifdef BROv6
51 : : memcpy(src_addr, ip6->ip6_src.s6_addr, 16);
52 : : memcpy(dst_addr, ip6->ip6_dst.s6_addr, 16);
53 : : #endif
54 : : }
55 : :
56 : 0 : IP_Hdr(const struct ip6_hdr* arg_ip6)
57 : : {
58 : 0 : ip4 = 0;
59 : 0 : ip6 = arg_ip6;
60 : 0 : del = 0;
61 : :
62 : : #ifdef BROv6
63 : : memcpy(src_addr, ip6->ip6_src.s6_addr, 16);
64 : : memcpy(dst_addr, ip6->ip6_dst.s6_addr, 16);
65 : : #endif
66 : 0 : }
67 : :
68 : 20378 : ~IP_Hdr()
69 : : {
70 [ - + ]: 20378 : if ( del )
71 : : {
72 [ # # ]: 0 : if ( ip4 )
73 [ # # ]: 0 : delete [] (struct ip*) ip4;
74 : : else
75 [ # # ]: 0 : delete [] (struct ip6_hdr*) ip6;
76 : : }
77 : 20378 : }
78 : :
79 : 23744 : const struct ip* IP4_Hdr() const { return ip4; }
80 : : const struct ip6_hdr* IP6_Hdr() const { return ip6; }
81 : :
82 : : #ifdef BROv6
83 : : const uint32* SrcAddr() const { return src_addr; }
84 : : const uint32* DstAddr() const { return dst_addr; }
85 : : #else
86 : 22726 : const uint32* SrcAddr() const
87 [ + - ]: 22726 : { return ip4 ? &(ip4->ip_src.s_addr) : 0; }
88 : 21098 : const uint32* DstAddr() const
89 [ + - ]: 21098 : { return ip4 ? &(ip4->ip_dst.s_addr) : 0; }
90 : : #endif
91 : :
92 : 0 : uint32 SrcAddr4() const { return ip4->ip_src.s_addr; }
93 : 0 : uint32 DstAddr4() const { return ip4->ip_dst.s_addr; }
94 : :
95 [ # # ]: 0 : uint16 ID4() const { return ip4 ? ip4->ip_id : 0; }
96 : :
97 : 57644 : const u_char* Payload() const
98 : : {
99 [ + - ]: 57644 : if ( ip4 )
100 : 57644 : return ((const u_char*) ip4) + ip4->ip_hl * 4;
101 : : else
102 : 57644 : return ((const u_char*) ip6) + 40;
103 : : }
104 : :
105 : 1048 : uint16 PayloadLen() const
106 : : {
107 [ + - ]: 1048 : if ( ip4 )
108 : 1048 : return ntohs(ip4->ip_len) - ip4->ip_hl * 4;
109 : : else
110 : 1048 : return ntohs(ip6->ip6_plen);
111 : : }
112 : :
113 : 40583 : uint16 TotalLen() const
114 : : {
115 [ + - ]: 40583 : if ( ip4 )
116 : 40583 : return ntohs(ip4->ip_len);
117 : : else
118 : 40583 : return ntohs(ip6->ip6_plen) + 40;
119 : : }
120 : :
121 [ + - ]: 39541 : uint16 HdrLen() const { return ip4 ? ip4->ip_hl * 4 : 40; }
122 : 20378 : unsigned char NextProto() const
123 [ + - ]: 20378 : { return ip4 ? ip4->ip_p : ip6->ip6_nxt; }
124 : 1042 : unsigned char TTL() const
125 [ + - ]: 1042 : { return ip4 ? ip4->ip_ttl : ip6->ip6_hlim; }
126 : 20270 : uint16 FragField() const
127 [ + - ]: 20270 : { return ntohs(ip4 ? ip4->ip_off : 0); }
128 : 1042 : int DF() const
129 [ + - ][ + + ]: 1042 : { return ip4 ? ((ntohs(ip4->ip_off) & IP_DF) != 0) : 0; }
130 : 0 : uint16 IP_ID() const
131 [ # # ]: 0 : { return ip4 ? (ntohs(ip4->ip_id)) : 0; }
132 : :
133 : : private:
134 : : const struct ip* ip4;
135 : : const struct ip6_hdr* ip6;
136 : : #ifdef BROv6
137 : : uint32 src_addr[NUM_ADDR_WORDS];
138 : : uint32 dst_addr[NUM_ADDR_WORDS];
139 : : #endif
140 : : int del;
141 : : };
142 : :
143 : : #endif
|