1 | #include "libtrace.h" |
---|
2 | #include "protocols.h" |
---|
3 | #include <assert.h> |
---|
4 | |
---|
5 | DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet, |
---|
6 | uint8_t *proto, |
---|
7 | uint32_t *remaining |
---|
8 | ) |
---|
9 | { |
---|
10 | uint8_t dummy_proto; |
---|
11 | uint16_t ethertype; |
---|
12 | uint32_t dummy_remaining; |
---|
13 | void *transport; |
---|
14 | |
---|
15 | if (!proto) proto=&dummy_proto; |
---|
16 | |
---|
17 | if (!remaining) remaining=&dummy_remaining; |
---|
18 | |
---|
19 | transport = trace_get_layer3(packet,ðertype,remaining); |
---|
20 | |
---|
21 | if (!transport || *remaining == 0) |
---|
22 | return NULL; |
---|
23 | |
---|
24 | switch (ethertype) { |
---|
25 | case 0x0800: /* IPv4 */ |
---|
26 | transport=trace_get_payload_from_ip( |
---|
27 | (libtrace_ip_t*)transport, proto, remaining); |
---|
28 | /* IPv6 */ |
---|
29 | if (transport && *proto == 41) { |
---|
30 | transport=trace_get_payload_from_ip6( |
---|
31 | (libtrace_ip6_t*)transport, proto,remaining); |
---|
32 | } |
---|
33 | return transport; |
---|
34 | case 0x86DD: /* IPv6 */ |
---|
35 | return trace_get_payload_from_ip6( |
---|
36 | (libtrace_ip6_t*)transport, proto, remaining); |
---|
37 | |
---|
38 | default: |
---|
39 | *proto=0; |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | DLLEXPORT libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet) { |
---|
46 | uint8_t proto; |
---|
47 | libtrace_tcp_t *tcp; |
---|
48 | |
---|
49 | tcp=(libtrace_tcp_t*)trace_get_transport(packet,&proto,NULL); |
---|
50 | |
---|
51 | if (!tcp || proto != 6) |
---|
52 | return NULL; |
---|
53 | |
---|
54 | return (libtrace_tcp_t*)tcp; |
---|
55 | } |
---|
56 | |
---|
57 | DLLEXPORT libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining) |
---|
58 | { |
---|
59 | libtrace_tcp_t *tcpptr = 0; |
---|
60 | |
---|
61 | if (ip->ip_p == 6) { |
---|
62 | tcpptr = (libtrace_tcp_t *) |
---|
63 | trace_get_payload_from_ip(ip, NULL, remaining); |
---|
64 | } |
---|
65 | |
---|
66 | return tcpptr; |
---|
67 | } |
---|
68 | |
---|
69 | DLLEXPORT libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet) { |
---|
70 | uint8_t proto; |
---|
71 | libtrace_udp_t *udp; |
---|
72 | |
---|
73 | udp=(libtrace_udp_t*)trace_get_transport(packet,&proto,NULL); |
---|
74 | |
---|
75 | if (!udp || proto != 17) |
---|
76 | return NULL; |
---|
77 | |
---|
78 | return udp; |
---|
79 | } |
---|
80 | |
---|
81 | DLLEXPORT libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip, uint32_t *remaining) |
---|
82 | { |
---|
83 | libtrace_udp_t *udpptr = 0; |
---|
84 | |
---|
85 | if (ip->ip_p == 17) { |
---|
86 | udpptr = (libtrace_udp_t *) |
---|
87 | trace_get_payload_from_ip(ip, NULL, remaining); |
---|
88 | } |
---|
89 | |
---|
90 | return udpptr; |
---|
91 | } |
---|
92 | |
---|
93 | DLLEXPORT libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet) { |
---|
94 | uint8_t proto; |
---|
95 | libtrace_icmp_t *icmp; |
---|
96 | |
---|
97 | icmp=(libtrace_icmp_t*)trace_get_transport(packet,&proto,NULL); |
---|
98 | |
---|
99 | if (!icmp || proto != 1) |
---|
100 | return NULL; |
---|
101 | |
---|
102 | return icmp; |
---|
103 | } |
---|
104 | |
---|
105 | DLLEXPORT libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip, uint32_t *remaining) |
---|
106 | { |
---|
107 | libtrace_icmp_t *icmpptr = 0; |
---|
108 | |
---|
109 | if (ip->ip_p == 1) { |
---|
110 | icmpptr = (libtrace_icmp_t *)trace_get_payload_from_ip(ip, |
---|
111 | NULL, remaining); |
---|
112 | } |
---|
113 | |
---|
114 | return icmpptr; |
---|
115 | } |
---|
116 | |
---|
117 | DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining) |
---|
118 | { |
---|
119 | if (remaining) { |
---|
120 | if (*remaining <= sizeof(libtrace_udp_t)) { |
---|
121 | *remaining = 0; |
---|
122 | return NULL; |
---|
123 | } |
---|
124 | *remaining-=sizeof(libtrace_udp_t); |
---|
125 | } |
---|
126 | return (void*)((char*)udp+sizeof(libtrace_udp_t)); |
---|
127 | } |
---|
128 | |
---|
129 | DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp, uint32_t *remaining) |
---|
130 | { |
---|
131 | unsigned int dlen = tcp->doff*4; |
---|
132 | if (remaining) { |
---|
133 | if (*remaining <= dlen) { |
---|
134 | *remaining = 0; |
---|
135 | return NULL; |
---|
136 | } |
---|
137 | *remaining-=dlen; |
---|
138 | } |
---|
139 | return (void *)((char *)tcp+dlen); |
---|
140 | } |
---|
141 | |
---|
142 | DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp, uint32_t *remaining) |
---|
143 | { |
---|
144 | if (remaining) { |
---|
145 | if (*remaining <= sizeof(libtrace_icmp_t)) { |
---|
146 | *remaining = 0; |
---|
147 | return NULL; |
---|
148 | } |
---|
149 | *remaining-=sizeof(libtrace_icmp_t); |
---|
150 | } |
---|
151 | return (char*)icmp+sizeof(libtrace_icmp_t); |
---|
152 | } |
---|
153 | |
---|
154 | /* Return the client port |
---|
155 | */ |
---|
156 | DLLEXPORT uint16_t trace_get_source_port(const libtrace_packet_t *packet) |
---|
157 | { |
---|
158 | uint32_t remaining; |
---|
159 | const struct ports_t *port = |
---|
160 | (const struct ports_t*)trace_get_transport((libtrace_packet_t*)packet, |
---|
161 | NULL, &remaining); |
---|
162 | |
---|
163 | /* snapped too early */ |
---|
164 | if (remaining<2) |
---|
165 | return 0; |
---|
166 | |
---|
167 | if (port) |
---|
168 | return ntohs(port->src); |
---|
169 | else |
---|
170 | return 0; |
---|
171 | } |
---|
172 | |
---|
173 | /* Same as get_source_port except use the destination port */ |
---|
174 | DLLEXPORT uint16_t trace_get_destination_port(const libtrace_packet_t *packet) |
---|
175 | { |
---|
176 | uint32_t remaining; |
---|
177 | struct ports_t *port = |
---|
178 | (struct ports_t*)trace_get_transport((libtrace_packet_t*)packet, |
---|
179 | NULL, &remaining); |
---|
180 | /* snapped to early */ |
---|
181 | if (remaining<4) |
---|
182 | return 0; |
---|
183 | |
---|
184 | if (port) |
---|
185 | return ntohs(port->dst); |
---|
186 | else |
---|
187 | return 0; |
---|
188 | } |
---|
189 | |
---|
190 | |
---|