1 | %module libtrace |
---|
2 | %{ |
---|
3 | #include <arpa/inet.h> |
---|
4 | #include "libtrace.h" |
---|
5 | %} |
---|
6 | |
---|
7 | %nodefault; |
---|
8 | typedef unsigned char uint8_t; |
---|
9 | typedef unsigned short uint16_t; |
---|
10 | typedef unsigned int uint32_t; |
---|
11 | |
---|
12 | struct in_addr { |
---|
13 | int s_addr; |
---|
14 | }; |
---|
15 | |
---|
16 | %rename (libtrace_ip) IP; |
---|
17 | struct libtrace_ip |
---|
18 | { |
---|
19 | unsigned int ip_hl:4; /**< header length */ |
---|
20 | unsigned int ip_v:4; /**< version */ |
---|
21 | uint8_t ip_tos; /**< type of service */ |
---|
22 | #define IP_RF 0x8000 /**< reserved fragment flag */ |
---|
23 | #define IP_DF 0x4000 /**< dont fragment flag */ |
---|
24 | #define IP_MF 0x2000 /**< more fragments flag */ |
---|
25 | #define IP_OFFMASK 0x1fff /**< mask for fragmenting bits */ |
---|
26 | uint8_t ip_ttl; /**< time to live */ |
---|
27 | uint8_t ip_p; /**< protocol */ |
---|
28 | %extend { |
---|
29 | // Needs ntohs |
---|
30 | const uint16_t ip_sum; /**< checksum */ |
---|
31 | const uint16_t ip_len; /**< total length */ |
---|
32 | const uint16_t ip_id; /**< identification */ |
---|
33 | const uint16_t ip_off; /**< fragment offset field */ |
---|
34 | // Needs ntoha |
---|
35 | const char *const ip_src; |
---|
36 | const char *const ip_dst; |
---|
37 | } |
---|
38 | }; |
---|
39 | |
---|
40 | %extend libtrace_ip { |
---|
41 | ~libtrace_ip() { free(self); } |
---|
42 | } |
---|
43 | |
---|
44 | %{ |
---|
45 | #define MAKE_NTOHS(class,member) \ |
---|
46 | uint16_t class ## _ ## member ## _get (struct class *self) { \ |
---|
47 | return ntohs(self->member); \ |
---|
48 | } |
---|
49 | |
---|
50 | #define MAKE_NTOHL(class,member) \ |
---|
51 | uint32_t class ## _ ## member ## _get (struct class *self) { \ |
---|
52 | return ntohl(self->member); \ |
---|
53 | } |
---|
54 | |
---|
55 | MAKE_NTOHS(libtrace_ip,ip_sum); |
---|
56 | MAKE_NTOHS(libtrace_ip,ip_len); |
---|
57 | MAKE_NTOHS(libtrace_ip,ip_id); |
---|
58 | MAKE_NTOHS(libtrace_ip,ip_off); |
---|
59 | char *libtrace_ip_ip_src_get(struct libtrace_ip *self) { |
---|
60 | return strdup(inet_ntoa(self->ip_src)); |
---|
61 | } |
---|
62 | char *libtrace_ip_ip_dst_get(struct libtrace_ip *self) { |
---|
63 | return strdup(inet_ntoa(self->ip_dst)); |
---|
64 | } |
---|
65 | %}; |
---|
66 | |
---|
67 | |
---|
68 | struct libtrace_tcp |
---|
69 | { |
---|
70 | uint16_t res1:4; /**< Reserved bits */ |
---|
71 | uint16_t doff:4; |
---|
72 | uint16_t fin:1; /**< FIN */ |
---|
73 | uint16_t syn:1; /**< SYN flag */ |
---|
74 | uint16_t rst:1; /**< RST flag */ |
---|
75 | uint16_t psh:1; /**< PuSH flag */ |
---|
76 | uint16_t ack:1; /**< ACK flag */ |
---|
77 | uint16_t urg:1; /**< URG flag */ |
---|
78 | uint16_t res2:2; /**< Reserved */ |
---|
79 | %extend { |
---|
80 | // needs ntohs |
---|
81 | const uint16_t source; /**< Source Port */ |
---|
82 | const uint16_t dest; /**< Destination port */ |
---|
83 | const uint16_t window; /**< Window Size */ |
---|
84 | const uint16_t check; /**< Checksum */ |
---|
85 | const uint16_t urg_ptr; /**< Urgent Pointer */ |
---|
86 | // needs ntohl |
---|
87 | const uint32_t seq; /**< Sequence number */ |
---|
88 | const uint32_t ack_seq; /**< Acknowledgement Number */ |
---|
89 | } |
---|
90 | }; |
---|
91 | |
---|
92 | %extend libtrace_tcp { |
---|
93 | ~libtrace_tcp() { free(self); } |
---|
94 | } |
---|
95 | |
---|
96 | %{ |
---|
97 | MAKE_NTOHS(libtrace_tcp,source) |
---|
98 | MAKE_NTOHS(libtrace_tcp,dest) |
---|
99 | MAKE_NTOHS(libtrace_tcp,window) |
---|
100 | MAKE_NTOHS(libtrace_tcp,check) |
---|
101 | MAKE_NTOHS(libtrace_tcp,urg_ptr) |
---|
102 | |
---|
103 | MAKE_NTOHL(libtrace_tcp,seq) |
---|
104 | MAKE_NTOHL(libtrace_tcp,ack_seq) |
---|
105 | %} |
---|
106 | |
---|
107 | /** UDP Header for dealing with UDP packets */ |
---|
108 | struct libtrace_udp { |
---|
109 | %extend { |
---|
110 | // Needs ntohs |
---|
111 | const uint16_t source; /**< Source port */ |
---|
112 | const uint16_t dest; /**< Destination port */ |
---|
113 | const uint16_t len; /**< Length */ |
---|
114 | const uint16_t check; /**< Checksum */ |
---|
115 | } |
---|
116 | }; |
---|
117 | |
---|
118 | %extend libtrace_udp { |
---|
119 | ~libtrace_udp() { free(self); } |
---|
120 | } |
---|
121 | |
---|
122 | %{ |
---|
123 | MAKE_NTOHS(libtrace_udp,source) |
---|
124 | MAKE_NTOHS(libtrace_udp,dest) |
---|
125 | MAKE_NTOHS(libtrace_udp,len) |
---|
126 | MAKE_NTOHS(libtrace_udp,check) |
---|
127 | %} |
---|
128 | |
---|
129 | struct libtrace_icmp |
---|
130 | { |
---|
131 | uint8_t type; /* message type */ |
---|
132 | uint8_t code; /* type sub-code */ |
---|
133 | uint16_t checksum; |
---|
134 | union |
---|
135 | { |
---|
136 | struct |
---|
137 | { |
---|
138 | uint16_t id; |
---|
139 | uint16_t sequence; |
---|
140 | } echo; /* echo datagram */ |
---|
141 | uint32_t gateway; /* gateway address */ |
---|
142 | struct |
---|
143 | { |
---|
144 | uint16_t __unused; |
---|
145 | uint16_t mtu; |
---|
146 | } frag; /* path mtu discovery */ |
---|
147 | } un; |
---|
148 | }; |
---|
149 | |
---|
150 | %extend libtrace_icmp { |
---|
151 | ~libtrace_icmp() { free(self); } |
---|
152 | } |
---|
153 | |
---|
154 | %rename (Packet) libtrace_packet_t; |
---|
155 | struct libtrace_packet_t |
---|
156 | { |
---|
157 | struct libtrace_t *trace; |
---|
158 | char *buffer; |
---|
159 | size_t size; |
---|
160 | uint8_t status; |
---|
161 | }; |
---|
162 | |
---|
163 | %extend libtrace_packet_t { |
---|
164 | libtrace_packet_t() { |
---|
165 | struct libtrace_packet_t *packet = 0; |
---|
166 | packet = malloc(sizeof(struct libtrace_packet_t)); |
---|
167 | packet->buffer = malloc(sizeof(char) * 65536); |
---|
168 | return packet; |
---|
169 | } |
---|
170 | ~libtrace_packet_t() { free(self->buffer); free(self);} |
---|
171 | struct libtrace_ip *trace_get_ip() { |
---|
172 | return trace_get_ip(self); |
---|
173 | } |
---|
174 | struct libtrace_tcp *trace_get_tcp() { |
---|
175 | return trace_get_tcp(self); |
---|
176 | } |
---|
177 | struct libtrace_udp *trace_get_udp() { |
---|
178 | return trace_get_udp(self); |
---|
179 | } |
---|
180 | struct libtrace_icmp *trace_get_icmp() { |
---|
181 | return trace_get_icmp(self); |
---|
182 | } |
---|
183 | double trace_get_seconds() { |
---|
184 | return trace_get_seconds(self); |
---|
185 | } |
---|
186 | }; |
---|
187 | |
---|
188 | %rename (Trace) libtrace_t; |
---|
189 | struct libtrace_t {}; |
---|
190 | |
---|
191 | %extend libtrace_t { |
---|
192 | libtrace_t(char *uri) { return trace_create(uri); }; |
---|
193 | ~libtrace_t() { trace_destroy(self); } |
---|
194 | int trace_read_packet(struct libtrace_packet_t *packet) { |
---|
195 | return trace_read_packet(self,packet); |
---|
196 | } |
---|
197 | }; |
---|
198 | |
---|