| 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 | return NULL; |
|---|
| 122 | *remaining-=sizeof(libtrace_udp_t); |
|---|
| 123 | } |
|---|
| 124 | return (void*)((char*)udp+sizeof(libtrace_udp_t)); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp, uint32_t *remaining) |
|---|
| 128 | { |
|---|
| 129 | unsigned int dlen = tcp->doff*4; |
|---|
| 130 | if (remaining) { |
|---|
| 131 | if (*remaining <= dlen) |
|---|
| 132 | return NULL; |
|---|
| 133 | *remaining-=dlen; |
|---|
| 134 | } |
|---|
| 135 | return (void *)((char *)tcp+dlen); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp, uint32_t *remaining) |
|---|
| 139 | { |
|---|
| 140 | if (remaining) { |
|---|
| 141 | if (*remaining <= sizeof(libtrace_icmp_t)) |
|---|
| 142 | return NULL; |
|---|
| 143 | *remaining-=sizeof(libtrace_icmp_t); |
|---|
| 144 | } |
|---|
| 145 | return (char*)icmp+sizeof(libtrace_icmp_t); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /* Return the client port |
|---|
| 149 | */ |
|---|
| 150 | DLLEXPORT uint16_t trace_get_source_port(const libtrace_packet_t *packet) |
|---|
| 151 | { |
|---|
| 152 | uint32_t remaining; |
|---|
| 153 | const struct ports_t *port = |
|---|
| 154 | (const struct ports_t*)trace_get_transport((libtrace_packet_t*)packet, |
|---|
| 155 | NULL, &remaining); |
|---|
| 156 | |
|---|
| 157 | /* snapped too early */ |
|---|
| 158 | if (remaining<2) |
|---|
| 159 | return 0; |
|---|
| 160 | |
|---|
| 161 | if (port) |
|---|
| 162 | return ntohs(port->src); |
|---|
| 163 | else |
|---|
| 164 | return 0; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /* Same as get_source_port except use the destination port */ |
|---|
| 168 | DLLEXPORT uint16_t trace_get_destination_port(const libtrace_packet_t *packet) |
|---|
| 169 | { |
|---|
| 170 | uint32_t remaining; |
|---|
| 171 | struct ports_t *port = |
|---|
| 172 | (struct ports_t*)trace_get_transport((libtrace_packet_t*)packet, |
|---|
| 173 | NULL, &remaining); |
|---|
| 174 | /* snapped to early */ |
|---|
| 175 | if (remaining<4) |
|---|
| 176 | return 0; |
|---|
| 177 | |
|---|
| 178 | if (port) |
|---|
| 179 | return ntohs(port->dst); |
|---|
| 180 | else |
|---|
| 181 | return 0; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|