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