| 1 | %module libtrace |
|---|
| 2 | %{ |
|---|
| 3 | #include <arpa/inet.h> |
|---|
| 4 | #include "libtrace.h" |
|---|
| 5 | %} |
|---|
| 6 | %include "carrays.i" |
|---|
| 7 | %include "cmalloc.i" |
|---|
| 8 | |
|---|
| 9 | %nodefault; |
|---|
| 10 | typedef unsigned char uint8_t; |
|---|
| 11 | typedef unsigned short uint16_t; |
|---|
| 12 | typedef unsigned int uint32_t; |
|---|
| 13 | |
|---|
| 14 | struct in_addr { |
|---|
| 15 | int s_addr; |
|---|
| 16 | }; |
|---|
| 17 | |
|---|
| 18 | struct libtrace_ip |
|---|
| 19 | { |
|---|
| 20 | unsigned int ip_hl:4; /**< header length */ |
|---|
| 21 | unsigned int ip_v:4; /**< version */ |
|---|
| 22 | uint8_t ip_tos; /**< type of service */ |
|---|
| 23 | #define IP_RF 0x8000 /**< reserved fragment flag */ |
|---|
| 24 | #define IP_DF 0x4000 /**< dont fragment flag */ |
|---|
| 25 | #define IP_MF 0x2000 /**< more fragments flag */ |
|---|
| 26 | #define IP_OFFMASK 0x1fff /**< mask for fragmenting bits */ |
|---|
| 27 | uint8_t ip_ttl; /**< time to live */ |
|---|
| 28 | uint8_t ip_p; /**< protocol */ |
|---|
| 29 | %extend { |
|---|
| 30 | // Needs ntohs |
|---|
| 31 | const uint16_t ip_sum; /**< checksum */ |
|---|
| 32 | const uint16_t ip_len; /**< total length */ |
|---|
| 33 | const uint16_t ip_id; /**< identification */ |
|---|
| 34 | const uint16_t ip_off; /**< fragment offset field */ |
|---|
| 35 | // Needs ntoha |
|---|
| 36 | %newobject ip_src; |
|---|
| 37 | %newobject ip_dst; |
|---|
| 38 | const char *const ip_src; |
|---|
| 39 | const char *const ip_dst; |
|---|
| 40 | } |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | %{ |
|---|
| 47 | #define MAKE_NTOHS(class,member) \ |
|---|
| 48 | uint16_t class ## _ ## member ## _get (struct class *self) { \ |
|---|
| 49 | return ntohs(self->member); \ |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | #define MAKE_NTOHL(class,member) \ |
|---|
| 53 | uint32_t class ## _ ## member ## _get (struct class *self) { \ |
|---|
| 54 | return ntohl(self->member); \ |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | MAKE_NTOHS(libtrace_ip,ip_sum); |
|---|
| 58 | MAKE_NTOHS(libtrace_ip,ip_len); |
|---|
| 59 | MAKE_NTOHS(libtrace_ip,ip_id); |
|---|
| 60 | MAKE_NTOHS(libtrace_ip,ip_off); |
|---|
| 61 | char *libtrace_ip_ip_src_get(struct libtrace_ip *self) { |
|---|
| 62 | return strdup(inet_ntoa(self->ip_src)); |
|---|
| 63 | } |
|---|
| 64 | char *libtrace_ip_ip_dst_get(struct libtrace_ip *self) { |
|---|
| 65 | return strdup(inet_ntoa(self->ip_dst)); |
|---|
| 66 | } |
|---|
| 67 | %}; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | struct libtrace_tcp |
|---|
| 71 | { |
|---|
| 72 | uint16_t res1:4; /**< Reserved bits */ |
|---|
| 73 | uint16_t doff:4; |
|---|
| 74 | uint16_t fin:1; /**< FIN */ |
|---|
| 75 | uint16_t syn:1; /**< SYN flag */ |
|---|
| 76 | uint16_t rst:1; /**< RST flag */ |
|---|
| 77 | uint16_t psh:1; /**< PuSH flag */ |
|---|
| 78 | uint16_t ack:1; /**< ACK flag */ |
|---|
| 79 | uint16_t urg:1; /**< URG flag */ |
|---|
| 80 | uint16_t res2:2; /**< Reserved */ |
|---|
| 81 | %extend { |
|---|
| 82 | // needs ntohs |
|---|
| 83 | const uint16_t source; /**< Source Port */ |
|---|
| 84 | const uint16_t dest; /**< Destination port */ |
|---|
| 85 | const uint16_t window; /**< Window Size */ |
|---|
| 86 | const uint16_t check; /**< Checksum */ |
|---|
| 87 | const uint16_t urg_ptr; /**< Urgent Pointer */ |
|---|
| 88 | // needs ntohl |
|---|
| 89 | const uint32_t seq; /**< Sequence number */ |
|---|
| 90 | const uint32_t ack_seq; /**< Acknowledgement Number */ |
|---|
| 91 | } |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 94 | %{ |
|---|
| 95 | MAKE_NTOHS(libtrace_tcp,source) |
|---|
| 96 | MAKE_NTOHS(libtrace_tcp,dest) |
|---|
| 97 | MAKE_NTOHS(libtrace_tcp,window) |
|---|
| 98 | MAKE_NTOHS(libtrace_tcp,check) |
|---|
| 99 | MAKE_NTOHS(libtrace_tcp,urg_ptr) |
|---|
| 100 | |
|---|
| 101 | MAKE_NTOHL(libtrace_tcp,seq) |
|---|
| 102 | MAKE_NTOHL(libtrace_tcp,ack_seq) |
|---|
| 103 | %} |
|---|
| 104 | |
|---|
| 105 | /** UDP Header for dealing with UDP packets */ |
|---|
| 106 | struct libtrace_udp { |
|---|
| 107 | %extend { |
|---|
| 108 | // Needs ntohs |
|---|
| 109 | const uint16_t source; /**< Source port */ |
|---|
| 110 | const uint16_t dest; /**< Destination port */ |
|---|
| 111 | const uint16_t len; /**< Length */ |
|---|
| 112 | const uint16_t check; /**< Checksum */ |
|---|
| 113 | } |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | %{ |
|---|
| 117 | MAKE_NTOHS(libtrace_udp,source) |
|---|
| 118 | MAKE_NTOHS(libtrace_udp,dest) |
|---|
| 119 | MAKE_NTOHS(libtrace_udp,len) |
|---|
| 120 | MAKE_NTOHS(libtrace_udp,check) |
|---|
| 121 | %} |
|---|
| 122 | |
|---|
| 123 | struct libtrace_icmp |
|---|
| 124 | { |
|---|
| 125 | uint8_t type; /* message type */ |
|---|
| 126 | uint8_t code; /* type sub-code */ |
|---|
| 127 | uint16_t checksum; |
|---|
| 128 | union |
|---|
| 129 | { |
|---|
| 130 | struct |
|---|
| 131 | { |
|---|
| 132 | uint16_t id; |
|---|
| 133 | uint16_t sequence; |
|---|
| 134 | } echo; /* echo datagram */ |
|---|
| 135 | uint32_t gateway; /* gateway address */ |
|---|
| 136 | struct |
|---|
| 137 | { |
|---|
| 138 | uint16_t __unused; |
|---|
| 139 | uint16_t mtu; |
|---|
| 140 | } frag; /* path mtu discovery */ |
|---|
| 141 | } un; |
|---|
| 142 | }; |
|---|
| 143 | |
|---|
| 144 | %rename (Packet) libtrace_packet_t; |
|---|
| 145 | struct libtrace_packet_t {}; |
|---|
| 146 | |
|---|
| 147 | %extend libtrace_packet_t { |
|---|
| 148 | libtrace_packet_t() { |
|---|
| 149 | struct libtrace_packet_t *packet = trace_create_packet(); |
|---|
| 150 | return packet; |
|---|
| 151 | } |
|---|
| 152 | ~libtrace_packet_t() { |
|---|
| 153 | trace_destroy_packet(self); |
|---|
| 154 | } |
|---|
| 155 | libtrace_packet_t *copy_packet() { |
|---|
| 156 | return trace_copy_packet(self); |
|---|
| 157 | } |
|---|
| 158 | void *get_link() { |
|---|
| 159 | return trace_get_link(self); |
|---|
| 160 | } |
|---|
| 161 | void *get_transport(uint8_t *proto, uint32_t *remaining) { |
|---|
| 162 | return trace_get_transport(self, proto, remaining); |
|---|
| 163 | } |
|---|
| 164 | struct libtrace_ip *get_ip() { |
|---|
| 165 | return trace_get_ip(self); |
|---|
| 166 | } |
|---|
| 167 | struct libtrace_tcp *get_tcp() { |
|---|
| 168 | return trace_get_tcp(self); |
|---|
| 169 | } |
|---|
| 170 | struct libtrace_udp *get_udp() { |
|---|
| 171 | return trace_get_udp(self); |
|---|
| 172 | } |
|---|
| 173 | struct libtrace_icmp *get_icmp() { |
|---|
| 174 | return trace_get_icmp(self); |
|---|
| 175 | } |
|---|
| 176 | char *get_destination_mac() { |
|---|
| 177 | return trace_ether_ntoa(trace_get_destination_mac(self),0); |
|---|
| 178 | } |
|---|
| 179 | char *get_source_mac() { |
|---|
| 180 | return trace_ether_ntoa(trace_get_source_mac(self),0); |
|---|
| 181 | } |
|---|
| 182 | char *ether_ntoa(uint8_t *mac) { |
|---|
| 183 | return trace_ether_ntoa(mac, 0); |
|---|
| 184 | } |
|---|
| 185 | uint16_t get_source_port() { |
|---|
| 186 | return trace_get_source_port(self); |
|---|
| 187 | } |
|---|
| 188 | uint16_t get_destination_port() { |
|---|
| 189 | return trace_get_destination_port(self); |
|---|
| 190 | } |
|---|
| 191 | double get_seconds() { |
|---|
| 192 | return trace_get_seconds(self); |
|---|
| 193 | } |
|---|
| 194 | uint64_t get_erf_timestamp() { |
|---|
| 195 | return trace_get_erf_timestamp(self); |
|---|
| 196 | } |
|---|
| 197 | struct timeval get_timeval() { |
|---|
| 198 | return trace_get_timeval(self); |
|---|
| 199 | } |
|---|
| 200 | int get_capture_length() { |
|---|
| 201 | return trace_get_capture_length(self); |
|---|
| 202 | } |
|---|
| 203 | size_t set_capture_length(size_t size) { |
|---|
| 204 | return trace_set_capture_length(self,size); |
|---|
| 205 | } |
|---|
| 206 | int get_wire_lenth() { |
|---|
| 207 | return trace_get_wire_length(self); |
|---|
| 208 | } |
|---|
| 209 | int get_framing_length() { |
|---|
| 210 | return trace_get_framing_length(self); |
|---|
| 211 | } |
|---|
| 212 | int get_wire_length() { |
|---|
| 213 | return trace_get_wire_length(self); |
|---|
| 214 | } |
|---|
| 215 | libtrace_linktype_t get_link_type() { |
|---|
| 216 | return trace_get_link_type(self); |
|---|
| 217 | } |
|---|
| 218 | int8_t get_direction() { |
|---|
| 219 | return trace_get_direction(self); |
|---|
| 220 | } |
|---|
| 221 | int8_t set_direction(int8_t direction) { |
|---|
| 222 | return trace_set_direction(self,direction); |
|---|
| 223 | } |
|---|
| 224 | int apply_filter(struct libtrace_filter_t *filter) { |
|---|
| 225 | return trace_apply_filter(filter,self); |
|---|
| 226 | } |
|---|
| 227 | uint8_t get_server_port(uint8_t protocol, uint16_t source, |
|---|
| 228 | uint16_t dest) { |
|---|
| 229 | return trace_get_server_port(protocol,source,dest); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | }; |
|---|
| 233 | |
|---|
| 234 | %rename (Filter) libtrace_filter_t; |
|---|
| 235 | struct libtrace_filter_t {}; |
|---|
| 236 | |
|---|
| 237 | %extend libtrace_filter_t { |
|---|
| 238 | libtrace_filter_t(char *filterstring) { |
|---|
| 239 | return trace_create_filter(filterstring); |
|---|
| 240 | }; |
|---|
| 241 | ~libtrace_filter_t() { |
|---|
| 242 | trace_destroy_filter(self); |
|---|
| 243 | }; |
|---|
| 244 | int apply_filter(struct libtrace_packet_t *packet) { |
|---|
| 245 | return trace_apply_filter(self,packet); |
|---|
| 246 | } |
|---|
| 247 | }; |
|---|
| 248 | |
|---|
| 249 | %rename (Trace) libtrace_t; |
|---|
| 250 | struct libtrace_t {}; |
|---|
| 251 | |
|---|
| 252 | %extend libtrace_t { |
|---|
| 253 | libtrace_t(char *uri) { return trace_create(uri); }; |
|---|
| 254 | ~libtrace_t() { trace_destroy(self); } |
|---|
| 255 | int read_packet(struct libtrace_packet_t *packet) { |
|---|
| 256 | return trace_read_packet(self,packet); |
|---|
| 257 | } |
|---|
| 258 | int start() { |
|---|
| 259 | return trace_start(self); |
|---|
| 260 | } |
|---|
| 261 | int pause() { |
|---|
| 262 | return trace_pause(self); |
|---|
| 263 | } |
|---|
| 264 | void help() { |
|---|
| 265 | trace_help(); |
|---|
| 266 | } |
|---|
| 267 | int config(trace_option_t option, void *value) { |
|---|
| 268 | return trace_config(self, option, value); |
|---|
| 269 | } |
|---|
| 270 | libtrace_err_t get_err() { |
|---|
| 271 | return trace_get_err(self); |
|---|
| 272 | } |
|---|
| 273 | bool is_err() { |
|---|
| 274 | return trace_is_err(self); |
|---|
| 275 | } |
|---|
| 276 | }; |
|---|
| 277 | |
|---|
| 278 | %rename (OutputTrace) libtrace_out_t; |
|---|
| 279 | struct libtrace_out_t {}; |
|---|
| 280 | |
|---|
| 281 | %extend libtrace_out_t { |
|---|
| 282 | libtrace_out_t(char *uri) { return trace_create_output(uri); }; |
|---|
| 283 | ~libtrace_t() { trace_destroy_output(self); } |
|---|
| 284 | int start_output() { |
|---|
| 285 | return trace_start_output(self); |
|---|
| 286 | } |
|---|
| 287 | int config_output(trace_option_output_t option, void *value) { |
|---|
| 288 | return trace_config_output(self, option, value); |
|---|
| 289 | } |
|---|
| 290 | libtrace_err_t get_err_output() { |
|---|
| 291 | return trace_get_err_output(self); |
|---|
| 292 | } |
|---|
| 293 | bool is_err_output() { |
|---|
| 294 | return trace_is_err_output(self); |
|---|
| 295 | } |
|---|
| 296 | int write_packet(libtrace_packet_t *packet) { |
|---|
| 297 | return trace_write_packet(self, packet); |
|---|
| 298 | } |
|---|
| 299 | }; |
|---|
| 300 | |
|---|