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 = malloc(sizeof(struct libtrace_packet_t)); |
---|
150 | return packet; |
---|
151 | } |
---|
152 | ~libtrace_packet_t() { free(self);} |
---|
153 | struct libtrace_ip *trace_get_ip() { |
---|
154 | return trace_get_ip(self); |
---|
155 | } |
---|
156 | struct libtrace_tcp *trace_get_tcp() { |
---|
157 | return trace_get_tcp(self); |
---|
158 | } |
---|
159 | struct libtrace_udp *trace_get_udp() { |
---|
160 | return trace_get_udp(self); |
---|
161 | } |
---|
162 | struct libtrace_icmp *trace_get_icmp() { |
---|
163 | return trace_get_icmp(self); |
---|
164 | } |
---|
165 | void *trace_get_link() { |
---|
166 | return trace_get_link(self); |
---|
167 | } |
---|
168 | double trace_get_seconds() { |
---|
169 | return trace_get_seconds(self); |
---|
170 | } |
---|
171 | uint64_t trace_get_erf_timestamp() { |
---|
172 | return trace_get_erf_timestamp(self); |
---|
173 | } |
---|
174 | struct timeval trace_get_timeval() { |
---|
175 | return trace_get_timeval(self); |
---|
176 | } |
---|
177 | int trace_get_capture_length() { |
---|
178 | return trace_get_capture_length(self); |
---|
179 | } |
---|
180 | int trace_get_wire_lenth() { |
---|
181 | return trace_get_wire_length(self); |
---|
182 | } |
---|
183 | libtrace_linktype_t trace_get_link_type() { |
---|
184 | return trace_get_link_type(self); |
---|
185 | } |
---|
186 | uint8_t trace_get_direction() { |
---|
187 | return trace_get_direction(self); |
---|
188 | } |
---|
189 | int trace_bpf_filter(struct libtrace_filter_t *filter) { |
---|
190 | return trace_bpf_filter(filter,self); |
---|
191 | } |
---|
192 | |
---|
193 | }; |
---|
194 | |
---|
195 | %rename (Filter) libtrace_filter_t; |
---|
196 | struct libtrace_filter_t {}; |
---|
197 | |
---|
198 | %extend libtrace_filter_t { |
---|
199 | libtrace_filter_t(char *filterstring) { |
---|
200 | return trace_bpf_setfilter(filterstring); |
---|
201 | }; |
---|
202 | int trace_bpf_filter(struct libtrace_packet_t *packet) { |
---|
203 | return trace_bpf_filter(self,packet); |
---|
204 | } |
---|
205 | }; |
---|
206 | |
---|
207 | %rename (Trace) libtrace_t; |
---|
208 | struct libtrace_t {}; |
---|
209 | |
---|
210 | %extend libtrace_t { |
---|
211 | libtrace_t(char *uri) { return trace_create(uri); }; |
---|
212 | ~libtrace_t() { trace_destroy(self); } |
---|
213 | int trace_read_packet(struct libtrace_packet_t *packet) { |
---|
214 | return trace_read_packet(self,packet); |
---|
215 | } |
---|
216 | }; |
---|
217 | |
---|