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 | %{ |
---|
41 | #define MAKE_NTOHS(class,member) \ |
---|
42 | uint16_t class ## _ ## member ## _get (struct class *self) { \ |
---|
43 | return ntohs(self->member); \ |
---|
44 | } |
---|
45 | |
---|
46 | #define MAKE_NTOHL(class,member) \ |
---|
47 | uint32_t class ## _ ## member ## _get (struct class *self) { \ |
---|
48 | return ntohl(self->member); \ |
---|
49 | } |
---|
50 | |
---|
51 | MAKE_NTOHS(libtrace_ip,ip_sum); |
---|
52 | MAKE_NTOHS(libtrace_ip,ip_len); |
---|
53 | MAKE_NTOHS(libtrace_ip,ip_id); |
---|
54 | MAKE_NTOHS(libtrace_ip,ip_off); |
---|
55 | char *libtrace_ip_ip_src_get(struct libtrace_ip *self) { |
---|
56 | return strdup(inet_ntoa(self->ip_src)); |
---|
57 | } |
---|
58 | char *libtrace_ip_ip_dst_get(struct libtrace_ip *self) { |
---|
59 | return strdup(inet_ntoa(self->ip_dst)); |
---|
60 | } |
---|
61 | %}; |
---|
62 | |
---|
63 | |
---|
64 | struct libtrace_tcp |
---|
65 | { |
---|
66 | uint16_t res1:4; /**< Reserved bits */ |
---|
67 | uint16_t doff:4; |
---|
68 | uint16_t fin:1; /**< FIN */ |
---|
69 | uint16_t syn:1; /**< SYN flag */ |
---|
70 | uint16_t rst:1; /**< RST flag */ |
---|
71 | uint16_t psh:1; /**< PuSH flag */ |
---|
72 | uint16_t ack:1; /**< ACK flag */ |
---|
73 | uint16_t urg:1; /**< URG flag */ |
---|
74 | uint16_t res2:2; /**< Reserved */ |
---|
75 | %extend { |
---|
76 | // needs ntohs |
---|
77 | const uint16_t source; /**< Source Port */ |
---|
78 | const uint16_t dest; /**< Destination port */ |
---|
79 | const uint16_t window; /**< Window Size */ |
---|
80 | const uint16_t check; /**< Checksum */ |
---|
81 | const uint16_t urg_ptr; /**< Urgent Pointer */ |
---|
82 | // needs ntohl |
---|
83 | const uint32_t seq; /**< Sequence number */ |
---|
84 | const uint32_t ack_seq; /**< Acknowledgement Number */ |
---|
85 | } |
---|
86 | }; |
---|
87 | |
---|
88 | %{ |
---|
89 | MAKE_NTOHS(libtrace_tcp,source) |
---|
90 | MAKE_NTOHS(libtrace_tcp,dest) |
---|
91 | MAKE_NTOHS(libtrace_tcp,window) |
---|
92 | MAKE_NTOHS(libtrace_tcp,check) |
---|
93 | MAKE_NTOHS(libtrace_tcp,urg_ptr) |
---|
94 | |
---|
95 | MAKE_NTOHL(libtrace_tcp,seq) |
---|
96 | MAKE_NTOHL(libtrace_tcp,ack_seq) |
---|
97 | %} |
---|
98 | |
---|
99 | /** UDP Header for dealing with UDP packets */ |
---|
100 | struct libtrace_udp { |
---|
101 | %extend { |
---|
102 | // Needs ntohs |
---|
103 | const uint16_t source; /**< Source port */ |
---|
104 | const uint16_t dest; /**< Destination port */ |
---|
105 | const uint16_t len; /**< Length */ |
---|
106 | const uint16_t check; /**< Checksum */ |
---|
107 | } |
---|
108 | }; |
---|
109 | |
---|
110 | %{ |
---|
111 | MAKE_NTOHS(libtrace_udp,source) |
---|
112 | MAKE_NTOHS(libtrace_udp,dest) |
---|
113 | MAKE_NTOHS(libtrace_udp,len) |
---|
114 | MAKE_NTOHS(libtrace_udp,check) |
---|
115 | %} |
---|
116 | |
---|
117 | struct libtrace_icmp |
---|
118 | { |
---|
119 | uint8_t type; /* message type */ |
---|
120 | uint8_t code; /* type sub-code */ |
---|
121 | uint16_t checksum; |
---|
122 | union |
---|
123 | { |
---|
124 | struct |
---|
125 | { |
---|
126 | uint16_t id; |
---|
127 | uint16_t sequence; |
---|
128 | } echo; /* echo datagram */ |
---|
129 | uint32_t gateway; /* gateway address */ |
---|
130 | struct |
---|
131 | { |
---|
132 | uint16_t __unused; |
---|
133 | uint16_t mtu; |
---|
134 | } frag; /* path mtu discovery */ |
---|
135 | } un; |
---|
136 | }; |
---|
137 | |
---|
138 | %{ |
---|
139 | typedef struct Packet { |
---|
140 | struct libtrace_t *libtrace; |
---|
141 | void *buffer; |
---|
142 | int status; |
---|
143 | int len; |
---|
144 | } Packet; |
---|
145 | %} |
---|
146 | |
---|
147 | %nodefault; |
---|
148 | typedef struct Packet { |
---|
149 | void *buffer; |
---|
150 | int status; |
---|
151 | int len; |
---|
152 | } Packet; |
---|
153 | |
---|
154 | %extend Packet { |
---|
155 | struct libtrace_ip *get_ip() { |
---|
156 | return get_ip(self->libtrace,self->buffer,self->len); |
---|
157 | } |
---|
158 | struct libtrace_tcp *get_tcp() { |
---|
159 | return get_tcp(self->libtrace,self->buffer,self->len); |
---|
160 | } |
---|
161 | struct libtrace_udp *get_udp() { |
---|
162 | return get_udp(self->libtrace,self->buffer,self->len); |
---|
163 | } |
---|
164 | struct libtrace_icmp *get_icmp() { |
---|
165 | return get_icmp(self->libtrace,self->buffer,self->len); |
---|
166 | } |
---|
167 | double get_seconds() { |
---|
168 | return get_seconds(self->libtrace,self->buffer,self->len); |
---|
169 | } |
---|
170 | }; |
---|
171 | |
---|
172 | %rename (Trace) libtrace_t; |
---|
173 | struct libtrace_t {}; |
---|
174 | |
---|
175 | %extend libtrace_t { |
---|
176 | libtrace_t(char *uri) { return create_trace(uri); }; |
---|
177 | ~libtrace_t() { destroy_trace(self); } |
---|
178 | Packet *read_packet() { |
---|
179 | Packet *buffer = malloc(sizeof(Packet)); |
---|
180 | buffer->buffer = malloc(1600); |
---|
181 | buffer->len=libtrace_read_packet(self,buffer->buffer,1600,&buffer->status); |
---|
182 | buffer->libtrace = self; |
---|
183 | if (buffer->len == 0) { |
---|
184 | free(buffer); |
---|
185 | return NULL; |
---|
186 | } |
---|
187 | return buffer; |
---|
188 | } |
---|
189 | }; |
---|
190 | |
---|
191 | /* |
---|
192 | void *get_link(struct libtrace_t *libtrace, void *buffer, int buflen); |
---|
193 | int get_capture_length(struct libtrace_t *libtrace, void *buffer, int buflen); |
---|
194 | int get_wire_length(struct libtrace_t *libtrace, void *buffer, int buflen); |
---|
195 | libtrace_linktype_t get_link_type( |
---|
196 | struct libtrace_t *libtrace, void *buffer, int buflen); |
---|
197 | uint8_t *get_destination_mac(struct libtrace_t *libtrace, |
---|
198 | void *buffer, int buflen); |
---|
199 | uint8_t *get_source_mac(struct libtrace_t *libtrace, |
---|
200 | void *buffer, int buflen); |
---|
201 | libtrace_event_t libtrace_event(struct libtrace_t *trace, |
---|
202 | int *fd,double *seconds, |
---|
203 | void *buffer, int *size); |
---|
204 | */ |
---|