pcap-sita.c revision 1.1 1 1.1 christos /*
2 1.1 christos * pcap-sita.c: Packet capture interface additions for SITA ACN devices
3 1.1 christos *
4 1.1 christos * Copyright (c) 2007 Fulko Hew, SITA INC Canada, Inc <fulko.hew (at) sita.aero>
5 1.1 christos *
6 1.1 christos * License: BSD
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in
16 1.1 christos * the documentation and/or other materials provided with the
17 1.1 christos * distribution.
18 1.1 christos * 3. The names of the authors may not be used to endorse or promote
19 1.1 christos * products derived from this software without specific prior
20 1.1 christos * written permission.
21 1.1 christos *
22 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
23 1.1 christos * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
24 1.1 christos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 1.1 christos */
26 1.1 christos
27 1.1 christos /* $Id: pcap-sita.c */
28 1.1 christos
29 1.1 christos #ifdef HAVE_CONFIG_H
30 1.1 christos #include "config.h"
31 1.1 christos #endif
32 1.1 christos
33 1.1 christos #include <stdio.h>
34 1.1 christos #include <string.h>
35 1.1 christos #include <stdlib.h>
36 1.1 christos #include <unistd.h>
37 1.1 christos #include <fcntl.h>
38 1.1 christos #include <errno.h>
39 1.1 christos #include <sys/time.h>
40 1.1 christos #include <sys/socket.h>
41 1.1 christos #include <netinet/in.h>
42 1.1 christos #include <arpa/inet.h>
43 1.1 christos #include "pcap-int.h"
44 1.1 christos
45 1.1 christos #include "pcap-sita.h"
46 1.1 christos
47 1.1 christos /* non-configureable manifests follow */
48 1.1 christos
49 1.1 christos #define IOP_SNIFFER_PORT 49152 /* TCP port on the IOP used for 'distributed pcap' usage */
50 1.1 christos #define MAX_LINE_SIZE 255 /* max size of a buffer/line in /etc/hosts we allow */
51 1.1 christos #define MAX_CHASSIS 8 /* number of chassis in an ACN site */
52 1.1 christos #define MAX_GEOSLOT 8 /* max number of access units in an ACN site */
53 1.1 christos
54 1.1 christos #define FIND 0
55 1.1 christos #define LIVE 1
56 1.1 christos
57 1.1 christos typedef struct iface {
58 1.1 christos struct iface *next; /* a pointer to the next interface */
59 1.1 christos char *name; /* this interface's name on Wireshark */
60 1.1 christos char *IOPname; /* this interface's name on an IOP */
61 1.1 christos uint32_t iftype; /* the type of interface (DLT values) */
62 1.1 christos } iface_t;
63 1.1 christos
64 1.1 christos typedef struct unit {
65 1.1 christos char *ip; /* this unit's IP address (as extracted from /etc/hosts) */
66 1.1 christos int fd; /* the connection to this unit (if it exists) */
67 1.1 christos int find_fd; /* a big kludge to avoid my programming limitations since I could have this unit open for findalldevs purposes */
68 1.1 christos int first_time; /* 0 = just opened via acn_open_live(), ie. the first time, NZ = nth time */
69 1.1 christos struct sockaddr_in *serv_addr; /* the address control block for comms to this unit */
70 1.1 christos int chassis;
71 1.1 christos int geoslot;
72 1.1 christos iface_t *iface; /* a pointer to a linked list of interface structures */
73 1.1 christos char *imsg; /* a pointer to an inbound message */
74 1.1 christos int len; /* the current size of the inbound message */
75 1.1 christos } unit_t;
76 1.1 christos
77 1.1 christos static char *errorString;
78 1.1 christos static unit_t units[MAX_CHASSIS+1][MAX_GEOSLOT+1]; /* we use indexes of 1 through 8, but we reserve/waste index 0 */
79 1.1 christos static fd_set readfds; /* a place to store the file descriptors for the connections to the IOPs */
80 1.1 christos static fd_set working_set;
81 1.1 christos static int max_fs;
82 1.1 christos static char static_buf[32];
83 1.1 christos
84 1.1 christos pcap_if_t *acn_if_list; /* pcap's list of available interfaces */
85 1.1 christos
86 1.1 christos static void dump_interface_list(void) {
87 1.1 christos pcap_if_t *iff;
88 1.1 christos pcap_addr_t *addr;
89 1.1 christos int longest_name_len = 0;
90 1.1 christos char *n, *d, *f;
91 1.1 christos int if_number = 0;
92 1.1 christos
93 1.1 christos iff = acn_if_list;
94 1.1 christos while (iff) {
95 1.1 christos if (iff->name && (strlen(iff->name) > longest_name_len)) longest_name_len = strlen(iff->name);
96 1.1 christos iff = iff->next;
97 1.1 christos }
98 1.1 christos iff = acn_if_list;
99 1.1 christos printf("Interface List:\n");
100 1.1 christos while (iff) {
101 1.1 christos n = (iff->name) ? iff->name : "";
102 1.1 christos d = (iff->description) ? iff->description : "";
103 1.1 christos f = (iff->flags == PCAP_IF_LOOPBACK) ? "L" : "";
104 1.1 christos printf("%3d: %*s %s '%s'\n", if_number++, longest_name_len, n, f, d);
105 1.1 christos addr = iff->addresses;
106 1.1 christos while (addr) {
107 1.1 christos printf("%*s ", (5 + longest_name_len), ""); /* add some indentation */
108 1.1 christos printf("%15s ", (addr->addr) ? inet_ntoa(((struct sockaddr_in *)addr->addr)->sin_addr) : "");
109 1.1 christos printf("%15s ", (addr->netmask) ? inet_ntoa(((struct sockaddr_in *)addr->netmask)->sin_addr) : "");
110 1.1 christos printf("%15s ", (addr->broadaddr) ? inet_ntoa(((struct sockaddr_in *)addr->broadaddr)->sin_addr) : "");
111 1.1 christos printf("%15s ", (addr->dstaddr) ? inet_ntoa(((struct sockaddr_in *)addr->dstaddr)->sin_addr) : "");
112 1.1 christos printf("\n");
113 1.1 christos addr = addr->next;
114 1.1 christos }
115 1.1 christos iff = iff->next;
116 1.1 christos }
117 1.1 christos }
118 1.1 christos
119 1.1 christos static void dump(unsigned char *ptr, int i, int indent) {
120 1.1 christos fprintf(stderr, "%*s", indent, " ");
121 1.1 christos for (; i > 0; i--) {
122 1.1 christos fprintf(stderr, "%2.2x ", *ptr++);
123 1.1 christos }
124 1.1 christos fprintf(stderr, "\n");
125 1.1 christos }
126 1.1 christos
127 1.1 christos static void dump_interface_list_p(void) {
128 1.1 christos pcap_if_t *iff;
129 1.1 christos pcap_addr_t *addr;
130 1.1 christos int if_number = 0;
131 1.1 christos
132 1.1 christos iff = acn_if_list;
133 1.1 christos printf("Interface Pointer @ %p is %p:\n", &acn_if_list, iff);
134 1.1 christos while (iff) {
135 1.1 christos printf("%3d: %p %p next: %p\n", if_number++, iff->name, iff->description, iff->next);
136 1.1 christos dump((unsigned char *)iff, sizeof(pcap_if_t), 5);
137 1.1 christos addr = iff->addresses;
138 1.1 christos while (addr) {
139 1.1 christos printf(" %p %p %p %p, next: %p\n", addr->addr, addr->netmask, addr->broadaddr, addr->dstaddr, addr->next);
140 1.1 christos dump((unsigned char *)addr, sizeof(pcap_addr_t), 10);
141 1.1 christos addr = addr->next;
142 1.1 christos }
143 1.1 christos iff = iff->next;
144 1.1 christos }
145 1.1 christos }
146 1.1 christos
147 1.1 christos static void dump_unit_table(void) {
148 1.1 christos int chassis, geoslot;
149 1.1 christos iface_t *p;
150 1.1 christos
151 1.1 christos printf("%c:%c %s %s\n", 'C', 'S', "fd", "IP Address");
152 1.1 christos for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {
153 1.1 christos for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
154 1.1 christos if (units[chassis][geoslot].ip != NULL)
155 1.1 christos printf("%d:%d %2d %s\n", chassis, geoslot, units[chassis][geoslot].fd, units[chassis][geoslot].ip);
156 1.1 christos p = units[chassis][geoslot].iface;
157 1.1 christos while (p) {
158 1.1 christos char *n = (p->name) ? p->name : "";
159 1.1 christos char *i = (p->IOPname) ? p->IOPname : "";
160 1.1 christos p = p->next;
161 1.1 christos printf(" %12s -> %12s\n", i, n);
162 1.1 christos }
163 1.1 christos }
164 1.1 christos }
165 1.1 christos }
166 1.1 christos
167 1.1 christos static int find_unit_by_fd(int fd, int *chassis, int *geoslot, unit_t **unit_ptr) {
168 1.1 christos int c, s;
169 1.1 christos
170 1.1 christos for (c = 0; c <= MAX_CHASSIS; c++) {
171 1.1 christos for (s = 0; s <= MAX_GEOSLOT; s++) {
172 1.1 christos if (units[c][s].fd == fd || units[c][s].find_fd == fd) {
173 1.1 christos if (chassis) *chassis = c;
174 1.1 christos if (geoslot) *geoslot = s;
175 1.1 christos if (unit_ptr) *unit_ptr = &units[c][s];
176 1.1 christos return 1;
177 1.1 christos }
178 1.1 christos }
179 1.1 christos }
180 1.1 christos return 0;
181 1.1 christos }
182 1.1 christos
183 1.1 christos static int read_client_nbytes(int fd, int count, unsigned char *buf) {
184 1.1 christos unit_t *u;
185 1.1 christos int chassis, geoslot;
186 1.1 christos int len;
187 1.1 christos
188 1.1 christos find_unit_by_fd(fd, &chassis, &geoslot, &u);
189 1.1 christos while (count) {
190 1.1 christos if ((len = recv(fd, buf, count, 0)) <= 0) return -1; /* read in whatever data was sent to us */
191 1.1 christos count -= len;
192 1.1 christos buf += len;
193 1.1 christos } /* till we have everything we are looking for */
194 1.1 christos return 0;
195 1.1 christos }
196 1.1 christos
197 1.1 christos static void empty_unit_iface(unit_t *u) {
198 1.1 christos iface_t *p, *cur;
199 1.1 christos
200 1.1 christos cur = u->iface;
201 1.1 christos while (cur) { /* loop over all the interface entries */
202 1.1 christos if (cur->name) free(cur->name); /* throwing away the contents if they exist */
203 1.1 christos if (cur->IOPname) free(cur->IOPname);
204 1.1 christos p = cur->next;
205 1.1 christos free(cur); /* then throw away the structure itself */
206 1.1 christos cur = p;
207 1.1 christos }
208 1.1 christos u->iface = 0; /* and finally remember that there are no remaining structure */
209 1.1 christos }
210 1.1 christos
211 1.1 christos static void empty_unit(int chassis, int geoslot) {
212 1.1 christos unit_t *u = &units[chassis][geoslot];
213 1.1 christos
214 1.1 christos empty_unit_iface(u);
215 1.1 christos if (u->imsg) { /* then if an inbound message buffer exists */
216 1.1 christos u->imsg = (char *)realloc(u->imsg, 1); /* and re-allocate the old large buffer into a new small one */
217 1.1 christos }
218 1.1 christos }
219 1.1 christos
220 1.1 christos static void empty_unit_table(void) {
221 1.1 christos int chassis, geoslot;
222 1.1 christos
223 1.1 christos for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {
224 1.1 christos for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
225 1.1 christos if (units[chassis][geoslot].ip != NULL) {
226 1.1 christos free(units[chassis][geoslot].ip); /* get rid of the malloc'ed space that holds the IP address */
227 1.1 christos units[chassis][geoslot].ip = 0; /* then set the pointer to NULL */
228 1.1 christos }
229 1.1 christos empty_unit(chassis, geoslot);
230 1.1 christos }
231 1.1 christos }
232 1.1 christos }
233 1.1 christos
234 1.1 christos static char *find_nth_interface_name(int n) {
235 1.1 christos int chassis, geoslot;
236 1.1 christos iface_t *p;
237 1.1 christos char *last_name = 0;
238 1.1 christos
239 1.1 christos if (n < 0) n = 0; /* ensure we are working with a valid number */
240 1.1 christos for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { /* scan the table... */
241 1.1 christos for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
242 1.1 christos if (units[chassis][geoslot].ip != NULL) {
243 1.1 christos p = units[chassis][geoslot].iface;
244 1.1 christos while (p) { /* and all interfaces... */
245 1.1 christos if (p->IOPname) last_name = p->name; /* remembering the last name found */
246 1.1 christos if (n-- == 0) return last_name; /* and if we hit the instance requested */
247 1.1 christos p = p->next;
248 1.1 christos }
249 1.1 christos }
250 1.1 christos }
251 1.1 christos }
252 1.1 christos /* if we couldn't fine the selected entry */
253 1.1 christos if (last_name) return last_name; /* ... but we did have at least one entry... return the last entry found */
254 1.1 christos return ""; /* ... but if there wasn't any entry... return an empty string instead */
255 1.1 christos }
256 1.1 christos
257 1.1 christos int acn_parse_hosts_file(char *errbuf) { /* returns: -1 = error, 0 = OK */
258 1.1 christos FILE *fp;
259 1.1 christos char buf[MAX_LINE_SIZE];
260 1.1 christos char *ptr, *ptr2;
261 1.1 christos int pos;
262 1.1 christos int chassis, geoslot;
263 1.1 christos unit_t *u;
264 1.1 christos
265 1.1 christos empty_unit_table();
266 1.1 christos if ((fp = fopen("/etc/hosts", "r")) == NULL) { /* try to open the hosts file and if it fails */
267 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot open '/etc/hosts' for reading."); /* return the nohostsfile error response */
268 1.1 christos return -1;
269 1.1 christos }
270 1.1 christos while (fgets(buf, MAX_LINE_SIZE-1, fp)) { /* while looping over the file */
271 1.1 christos
272 1.1 christos pos = strcspn(buf, "#\n\r"); /* find the first comment character or EOL */
273 1.1 christos *(buf + pos) = '\0'; /* and clobber it and anything that follows it */
274 1.1 christos
275 1.1 christos pos = strspn(buf, " \t"); /* then find the first non-white space */
276 1.1 christos if (pos == strlen(buf)) /* if there is nothing but white space on the line */
277 1.1 christos continue; /* ignore that empty line */
278 1.1 christos ptr = buf + pos; /* and skip over any of that leading whitespace */
279 1.1 christos
280 1.1 christos if ((ptr2 = strstr(ptr, "_I_")) == NULL) /* skip any lines that don't have names that look like they belong to IOPs */
281 1.1 christos continue;
282 1.1 christos if (*(ptr2 + 4) != '_') /* and skip other lines that have names that don't look like ACN components */
283 1.1 christos continue;
284 1.1 christos *(ptr + strcspn(ptr, " \t")) = '\0'; /* null terminate the IP address so its a standalone string */
285 1.1 christos
286 1.1 christos chassis = *(ptr2 + 3) - '0'; /* extract the chassis number */
287 1.1 christos geoslot = *(ptr2 + 5) - '0'; /* and geo-slot number */
288 1.1 christos if (chassis < 1 || chassis > MAX_CHASSIS ||
289 1.1 christos geoslot < 1 || geoslot > MAX_GEOSLOT) { /* if the chassis and/or slot numbers appear to be bad... */
290 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "Invalid ACN name in '/etc/hosts'."); /* warn the user */
291 1.1 christos continue; /* and ignore the entry */
292 1.1 christos }
293 1.1 christos if ((ptr2 = (char *)malloc(strlen(ptr) + 1)) == NULL) {
294 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
295 1.1 christos continue;
296 1.1 christos }
297 1.1 christos strcpy(ptr2, ptr); /* copy the IP address into our malloc'ed memory */
298 1.1 christos u = &units[chassis][geoslot];
299 1.1 christos u->ip = ptr2; /* and remember the whole shebang */
300 1.1 christos u->chassis = chassis;
301 1.1 christos u->geoslot = geoslot;
302 1.1 christos }
303 1.1 christos fclose(fp);
304 1.1 christos if (*errbuf) return -1;
305 1.1 christos else return 0;
306 1.1 christos }
307 1.1 christos
308 1.1 christos static int open_with_IOP(unit_t *u, int flag) {
309 1.1 christos int sockfd;
310 1.1 christos char *ip;
311 1.1 christos
312 1.1 christos if (u->serv_addr == NULL) {
313 1.1 christos u->serv_addr = malloc(sizeof(struct sockaddr_in));
314 1.1 christos }
315 1.1 christos ip = u->ip;
316 1.1 christos bzero((char *)u->serv_addr, sizeof(struct sockaddr_in));
317 1.1 christos u->serv_addr->sin_family = AF_INET;
318 1.1 christos u->serv_addr->sin_addr.s_addr = inet_addr(ip);
319 1.1 christos u->serv_addr->sin_port = htons(IOP_SNIFFER_PORT);
320 1.1 christos
321 1.1 christos if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
322 1.1 christos fprintf(stderr, "pcap can't open a socket for connecting to IOP at %s\n", ip);
323 1.1 christos return 0;
324 1.1 christos }
325 1.1 christos if (connect(sockfd, (struct sockaddr *)u->serv_addr, sizeof(struct sockaddr_in)) < 0) {
326 1.1 christos fprintf(stderr, "pcap can't connect to IOP at %s\n", ip);
327 1.1 christos return 0;
328 1.1 christos }
329 1.1 christos if (flag == LIVE) u->fd = sockfd;
330 1.1 christos else u->find_fd = sockfd;
331 1.1 christos u->first_time = 0;
332 1.1 christos return sockfd; /* return the non-zero file descriptor as a 'success' indicator */
333 1.1 christos }
334 1.1 christos
335 1.1 christos static void close_with_IOP(int chassis, int geoslot, int flag) {
336 1.1 christos int *id;
337 1.1 christos
338 1.1 christos if (flag == LIVE) id = &units[chassis][geoslot].fd;
339 1.1 christos else id = &units[chassis][geoslot].find_fd;
340 1.1 christos
341 1.1 christos if (*id) { /* this was the last time, so... if we are connected... */
342 1.1 christos close(*id); /* disconnect us */
343 1.1 christos *id = 0; /* and forget that the descriptor exists because we are not open */
344 1.1 christos }
345 1.1 christos }
346 1.1 christos
347 1.1 christos static void pcap_cleanup_acn(pcap_t *handle) {
348 1.1 christos int chassis, geoslot;
349 1.1 christos unit_t *u;
350 1.1 christos
351 1.1 christos if (find_unit_by_fd(handle->fd, &chassis, &geoslot, &u) == 0)
352 1.1 christos return;
353 1.1 christos close_with_IOP(chassis, geoslot, LIVE);
354 1.1 christos if (u)
355 1.1 christos u->first_time = 0;
356 1.1 christos pcap_cleanup_live_common(handle);
357 1.1 christos }
358 1.1 christos
359 1.1 christos static void send_to_fd(int fd, int len, unsigned char *str) {
360 1.1 christos int nwritten;
361 1.1 christos int chassis, geoslot;
362 1.1 christos
363 1.1 christos while (len > 0) {
364 1.1 christos if ((nwritten = write(fd, str, len)) <= 0) {
365 1.1 christos find_unit_by_fd(fd, &chassis, &geoslot, NULL);
366 1.1 christos if (units[chassis][geoslot].fd == fd) close_with_IOP(chassis, geoslot, LIVE);
367 1.1 christos else if (units[chassis][geoslot].find_fd == fd) close_with_IOP(chassis, geoslot, FIND);
368 1.1 christos empty_unit(chassis, geoslot);
369 1.1 christos return;
370 1.1 christos }
371 1.1 christos len -= nwritten;
372 1.1 christos str += nwritten;
373 1.1 christos }
374 1.1 christos }
375 1.1 christos
376 1.1 christos static void acn_freealldevs(void) {
377 1.1 christos
378 1.1 christos pcap_if_t *iff, *next_iff;
379 1.1 christos pcap_addr_t *addr, *next_addr;
380 1.1 christos
381 1.1 christos for (iff = acn_if_list; iff != NULL; iff = next_iff) {
382 1.1 christos next_iff = iff->next;
383 1.1 christos for (addr = iff->addresses; addr != NULL; addr = next_addr) {
384 1.1 christos next_addr = addr->next;
385 1.1 christos if (addr->addr) free(addr->addr);
386 1.1 christos if (addr->netmask) free(addr->netmask);
387 1.1 christos if (addr->broadaddr) free(addr->broadaddr);
388 1.1 christos if (addr->dstaddr) free(addr->dstaddr);
389 1.1 christos free(addr);
390 1.1 christos }
391 1.1 christos if (iff->name) free(iff->name);
392 1.1 christos if (iff->description) free(iff->description);
393 1.1 christos free(iff);
394 1.1 christos }
395 1.1 christos }
396 1.1 christos
397 1.1 christos static char *nonUnified_port_num(unit_t *u, int IOPportnum) {
398 1.1 christos
399 1.1 christos sprintf(static_buf, "%d_%d", u->chassis, u->geoslot);
400 1.1 christos return static_buf;
401 1.1 christos }
402 1.1 christos
403 1.1 christos static char *unified_port_num(unit_t *u, int IOPportnum) {
404 1.1 christos int portnum;
405 1.1 christos
406 1.1 christos portnum = ((u->chassis - 1) * 64) + ((u->geoslot - 1) * 8) + IOPportnum + 1;
407 1.1 christos sprintf(static_buf, "%d", portnum);
408 1.1 christos return static_buf;
409 1.1 christos }
410 1.1 christos
411 1.1 christos static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 iftype) {
412 1.1 christos iface_t *iface_ptr, *iface;
413 1.1 christos char *name;
414 1.1 christos char buf[32];
415 1.1 christos char *proto;
416 1.1 christos char *port;
417 1.1 christos int IOPportnum = 0;
418 1.1 christos
419 1.1 christos iface = malloc(sizeof(iface_t)); /* get memory for a structure */
420 1.1 christos bzero((char *)iface, sizeof(iface_t));
421 1.1 christos
422 1.1 christos iface->iftype = iftype; /* remember the interface type of this interface */
423 1.1 christos
424 1.1 christos name = malloc(strlen(IOPname) + 1); /* get memory for the IOP's name */
425 1.1 christos strcpy(name, IOPname); /* and copy it in */
426 1.1 christos iface->IOPname = name; /* and stick it into the structure */
427 1.1 christos
428 1.1 christos if (strncmp(IOPname, "lo", 2) == 0) {
429 1.1 christos IOPportnum = atoi(&IOPname[2]);
430 1.1 christos switch (iftype) {
431 1.1 christos case DLT_EN10MB: proto = "lo"; port = nonUnified_port_num(u, IOPportnum); break;
432 1.1 christos default: proto = "???"; port = unified_port_num(u, IOPportnum); break;
433 1.1 christos }
434 1.1 christos } else if (strncmp(IOPname, "eth", 3) == 0) {
435 1.1 christos IOPportnum = atoi(&IOPname[3]);
436 1.1 christos switch (iftype) {
437 1.1 christos case DLT_EN10MB: proto = "eth"; port = nonUnified_port_num(u, IOPportnum); break;
438 1.1 christos default: proto = "???"; port = unified_port_num(u, IOPportnum); break;
439 1.1 christos }
440 1.1 christos } else if (strncmp(IOPname, "wan", 3) == 0) {
441 1.1 christos IOPportnum = atoi(&IOPname[3]);
442 1.1 christos switch (iftype) {
443 1.1 christos case DLT_SITA: proto = "wan"; port = unified_port_num(u, IOPportnum); break;
444 1.1 christos default: proto = "???"; port = unified_port_num(u, IOPportnum); break;
445 1.1 christos }
446 1.1 christos }
447 1.1 christos
448 1.1 christos sprintf(buf, "%s_%s", proto, port); /* compose the user's name for that IOP port name */
449 1.1 christos name = malloc(strlen(buf) + 1); /* get memory for that name */
450 1.1 christos strcpy(name, buf); /* and copy it in */
451 1.1 christos iface->name = name; /* and stick it into the structure */
452 1.1 christos
453 1.1 christos if (u->iface == 0) { /* if this is the first name */
454 1.1 christos u->iface = iface; /* stick this entry at the head of the list */
455 1.1 christos } else {
456 1.1 christos iface_ptr = u->iface;
457 1.1 christos while (iface_ptr->next) { /* othewise scan the list */
458 1.1 christos iface_ptr = iface_ptr->next; /* till we're at the last entry */
459 1.1 christos }
460 1.1 christos iface_ptr->next = iface; /* then tack this entry on the end of the list */
461 1.1 christos }
462 1.1 christos return iface->name;
463 1.1 christos }
464 1.1 christos
465 1.1 christos static int if_sort(char *s1, char *s2) {
466 1.1 christos char *s1_p2, *s2_p2;
467 1.1 christos char str1[MAX_LINE_SIZE], str2[MAX_LINE_SIZE];
468 1.1 christos int s1_p1_len, s2_p1_len;
469 1.1 christos int retval;
470 1.1 christos
471 1.1 christos if ((s1_p2 = strchr(s1, '_'))) { /* if an underscore is found... */
472 1.1 christos s1_p1_len = s1_p2 - s1; /* the prefix length is the difference in pointers */
473 1.1 christos s1_p2++; /* the suffix actually starts _after_ the underscore */
474 1.1 christos } else { /* otherwise... */
475 1.1 christos s1_p1_len = strlen(s1); /* the prefix length is the length of the string itself */
476 1.1 christos s1_p2 = 0; /* and there is no suffix */
477 1.1 christos }
478 1.1 christos if ((s2_p2 = strchr(s2, '_'))) { /* now do the same for the second string */
479 1.1 christos s2_p1_len = s2_p2 - s2;
480 1.1 christos s2_p2++;
481 1.1 christos } else {
482 1.1 christos s2_p1_len = strlen(s2);
483 1.1 christos s2_p2 = 0;
484 1.1 christos }
485 1.1 christos strncpy(str1, s1, (s1_p1_len > sizeof(str1)) ? s1_p1_len : sizeof(str1)); *(str1 + s1_p1_len) = 0;
486 1.1 christos strncpy(str2, s2, (s2_p1_len > sizeof(str2)) ? s2_p1_len : sizeof(str2)); *(str2 + s2_p1_len) = 0;
487 1.1 christos retval = strcmp(str1, str2);
488 1.1 christos if (retval != 0) return retval; /* if they are not identical, then we can quit now and return the indication */
489 1.1 christos return strcmp(s1_p2, s2_p2); /* otherwise we return the result of comparing the 2nd half of the string */
490 1.1 christos }
491 1.1 christos
492 1.1 christos static void sort_if_table(void) {
493 1.1 christos pcap_if_t *p1, *p2, *prev, *temp;
494 1.1 christos int has_swapped;
495 1.1 christos
496 1.1 christos if (!acn_if_list) return; /* nothing to do if the list is empty */
497 1.1 christos
498 1.1 christos while (1) {
499 1.1 christos p1 = acn_if_list; /* start at the head of the list */
500 1.1 christos prev = 0;
501 1.1 christos has_swapped = 0;
502 1.1 christos while ((p2 = p1->next)) {
503 1.1 christos if (if_sort(p1->name, p2->name) > 0) {
504 1.1 christos if (prev) { /* we are swapping things that are _not_ at the head of the list */
505 1.1 christos temp = p2->next;
506 1.1 christos prev->next = p2;
507 1.1 christos p2->next = p1;
508 1.1 christos p1->next = temp;
509 1.1 christos } else { /* special treatment if we are swapping with the head of the list */
510 1.1 christos temp = p2->next;
511 1.1 christos acn_if_list= p2;
512 1.1 christos p2->next = p1;
513 1.1 christos p1->next = temp;
514 1.1 christos }
515 1.1 christos p1 = p2;
516 1.1 christos prev = p1;
517 1.1 christos has_swapped = 1;
518 1.1 christos }
519 1.1 christos prev = p1;
520 1.1 christos p1 = p1->next;
521 1.1 christos }
522 1.1 christos if (has_swapped == 0)
523 1.1 christos return;
524 1.1 christos }
525 1.1 christos return;
526 1.1 christos }
527 1.1 christos
528 1.1 christos static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 = OK */
529 1.1 christos int chassis, geoslot;
530 1.1 christos unit_t *u;
531 1.1 christos pcap_if_t *iff, *prev_iff;
532 1.1 christos pcap_addr_t *addr, *prev_addr;
533 1.1 christos char *ptr;
534 1.1 christos int address_count;
535 1.1 christos struct sockaddr_in *s;
536 1.1 christos char *newname;
537 1.1 christos bpf_u_int32 interfaceType;
538 1.1 christos unsigned char flags;
539 1.1 christos
540 1.1 christos prev_iff = 0;
541 1.1 christos for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {
542 1.1 christos for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { /* now loop over all the devices */
543 1.1 christos u = &units[chassis][geoslot];
544 1.1 christos empty_unit_iface(u);
545 1.1 christos ptr = u->imsg; /* point to the start of the msg for this IOP */
546 1.1 christos while (ptr < (u->imsg + u->len)) {
547 1.1 christos if ((iff = malloc(sizeof(pcap_if_t))) == NULL) {
548 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
549 1.1 christos return -1;
550 1.1 christos }
551 1.1 christos bzero((char *)iff, sizeof(pcap_if_t));
552 1.1 christos if (acn_if_list == 0) acn_if_list = iff; /* remember the head of the list */
553 1.1 christos if (prev_iff) prev_iff->next = iff; /* insert a forward link */
554 1.1 christos
555 1.1 christos if (*ptr) { /* if there is a count for the name */
556 1.1 christos if ((iff->name = malloc(*ptr + 1)) == NULL) { /* get that amount of space */
557 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
558 1.1 christos return -1;
559 1.1 christos }
560 1.1 christos memcpy(iff->name, (ptr + 1), *ptr); /* copy the name into the malloc'ed space */
561 1.1 christos *(iff->name + *ptr) = 0; /* and null terminate the string */
562 1.1 christos ptr += *ptr; /* now move the pointer forwards by the length of the count plus the length of the string */
563 1.1 christos }
564 1.1 christos ptr++;
565 1.1 christos
566 1.1 christos if (*ptr) { /* if there is a count for the description */
567 1.1 christos if ((iff->description = malloc(*ptr + 1)) == NULL) { /* get that amount of space */
568 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
569 1.1 christos return -1;
570 1.1 christos }
571 1.1 christos memcpy(iff->description, (ptr + 1), *ptr); /* copy the name into the malloc'ed space */
572 1.1 christos *(iff->description + *ptr) = 0; /* and null terminate the string */
573 1.1 christos ptr += *ptr; /* now move the pointer forwards by the length of the count plus the length of the string */
574 1.1 christos }
575 1.1 christos ptr++;
576 1.1 christos
577 1.1 christos interfaceType = ntohl(*(bpf_u_int32 *)ptr);
578 1.1 christos ptr += 4; /* skip over the interface type */
579 1.1 christos
580 1.1 christos flags = *ptr++;
581 1.1 christos if (flags) iff->flags = PCAP_IF_LOOPBACK; /* if this is a loopback style interface, lets mark it as such */
582 1.1 christos
583 1.1 christos address_count = *ptr++;
584 1.1 christos
585 1.1 christos prev_addr = 0;
586 1.1 christos while (address_count--) {
587 1.1 christos if ((addr = malloc(sizeof(pcap_addr_t))) == NULL) {
588 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
589 1.1 christos return -1;
590 1.1 christos }
591 1.1 christos bzero((char *)addr, sizeof(pcap_addr_t));
592 1.1 christos if (iff->addresses == 0) iff->addresses = addr;
593 1.1 christos if (prev_addr) prev_addr->next = addr; /* insert a forward link */
594 1.1 christos if (*ptr) { /* if there is a count for the address */
595 1.1 christos if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { /* get that amount of space */
596 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
597 1.1 christos return -1;
598 1.1 christos }
599 1.1 christos bzero((char *)s, sizeof(struct sockaddr_in));
600 1.1 christos addr->addr = (struct sockaddr *)s;
601 1.1 christos s->sin_family = AF_INET;
602 1.1 christos s->sin_addr.s_addr = *(bpf_u_int32 *)(ptr + 1); /* copy the address in */
603 1.1 christos ptr += *ptr; /* now move the pointer forwards according to the specified length of the address */
604 1.1 christos }
605 1.1 christos ptr++; /* then forwards one more for the 'length of the address' field */
606 1.1 christos if (*ptr) { /* process any netmask */
607 1.1 christos if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) {
608 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
609 1.1 christos return -1;
610 1.1 christos }
611 1.1 christos bzero((char *)s, sizeof(struct sockaddr_in));
612 1.1 christos addr->netmask = (struct sockaddr *)s;
613 1.1 christos s->sin_family = AF_INET;
614 1.1 christos s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1);
615 1.1 christos ptr += *ptr;
616 1.1 christos }
617 1.1 christos ptr++;
618 1.1 christos if (*ptr) { /* process any broadcast address */
619 1.1 christos if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) {
620 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
621 1.1 christos return -1;
622 1.1 christos }
623 1.1 christos bzero((char *)s, sizeof(struct sockaddr_in));
624 1.1 christos addr->broadaddr = (struct sockaddr *)s;
625 1.1 christos s->sin_family = AF_INET;
626 1.1 christos s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1);
627 1.1 christos ptr += *ptr;
628 1.1 christos }
629 1.1 christos ptr++;
630 1.1 christos if (*ptr) { /* process any destination address */
631 1.1 christos if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) {
632 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
633 1.1 christos return -1;
634 1.1 christos }
635 1.1 christos bzero((char *)s, sizeof(struct sockaddr_in));
636 1.1 christos addr->dstaddr = (struct sockaddr *)s;
637 1.1 christos s->sin_family = AF_INET;
638 1.1 christos s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1);
639 1.1 christos ptr += *ptr;
640 1.1 christos }
641 1.1 christos ptr++;
642 1.1 christos prev_addr = addr;
643 1.1 christos }
644 1.1 christos prev_iff = iff;
645 1.1 christos
646 1.1 christos newname = translate_IOP_to_pcap_name(u, iff->name, interfaceType); /* add a translation entry and get a point to the mangled name */
647 1.1 christos if ((iff->name = realloc(iff->name, strlen(newname) + 1)) == NULL) { /* we now re-write the name stored in the interface list */
648 1.1 christos snprintf(errbuf, PCAP_ERRBUF_SIZE, "realloc: %s", pcap_strerror(errno));
649 1.1 christos return -1;
650 1.1 christos }
651 1.1 christos strcpy(iff->name, newname); /* to this new name */
652 1.1 christos }
653 1.1 christos }
654 1.1 christos }
655 1.1 christos return 0;
656 1.1 christos }
657 1.1 christos
658 1.1 christos static int read_client_data (int fd) {
659 1.1 christos unsigned char buf[256];
660 1.1 christos int chassis, geoslot;
661 1.1 christos unit_t *u;
662 1.1 christos int len;
663 1.1 christos
664 1.1 christos find_unit_by_fd(fd, &chassis, &geoslot, &u);
665 1.1 christos
666 1.1 christos if ((len = recv(fd, buf, sizeof(buf), 0)) <= 0) return 0; /* read in whatever data was sent to us */
667 1.1 christos
668 1.1 christos if ((u->imsg = realloc(u->imsg, (u->len + len))) == NULL) /* extend the buffer for the new data */
669 1.1 christos return 0;
670 1.1 christos memcpy((u->imsg + u->len), buf, len); /* append the new data */
671 1.1 christos u->len += len;
672 1.1 christos return 1;
673 1.1 christos }
674 1.1 christos
675 1.1 christos static void wait_for_all_answers(void) {
676 1.1 christos int retval;
677 1.1 christos struct timeval tv;
678 1.1 christos int fd;
679 1.1 christos int chassis, geoslot;
680 1.1 christos
681 1.1 christos tv.tv_sec = 2;
682 1.1 christos tv.tv_usec = 0;
683 1.1 christos
684 1.1 christos while (1) {
685 1.1 christos int flag = 0;
686 1.1 christos for (fd = 0; fd <= max_fs; fd++) { /* scan the list of descriptors we may be listening to */
687 1.1 christos if (FD_ISSET(fd, &readfds)) flag = 1; /* and see if there are any still set */
688 1.1 christos }
689 1.1 christos if (flag == 0) return; /* we are done, when they are all gone */
690 1.1 christos
691 1.1 christos memcpy(&working_set, &readfds, sizeof(readfds)); /* otherwise, we still have to listen for more stuff, till we timeout */
692 1.1 christos retval = select(max_fs + 1, &working_set, NULL, NULL, &tv);
693 1.1 christos if (retval == -1) { /* an error occured !!!!! */
694 1.1 christos return;
695 1.1 christos } else if (retval == 0) { /* timeout occured, so process what we've got sofar and return */
696 1.1 christos printf("timeout\n");
697 1.1 christos return;
698 1.1 christos } else {
699 1.1 christos for (fd = 0; fd <= max_fs; fd++) { /* scan the list of things to do, and do them */
700 1.1 christos if (FD_ISSET(fd, &working_set)) {
701 1.1 christos if (read_client_data(fd) == 0) { /* if the socket has closed */
702 1.1 christos FD_CLR(fd, &readfds); /* and descriptors we listen to for errors */
703 1.1 christos find_unit_by_fd(fd, &chassis, &geoslot, NULL);
704 1.1 christos close_with_IOP(chassis, geoslot, FIND); /* and close out connection to him */
705 1.1 christos }
706 1.1 christos }
707 1.1 christos }
708 1.1 christos }
709 1.1 christos }
710 1.1 christos }
711 1.1 christos
712 1.1 christos static char *get_error_response(int fd, char *errbuf) { /* return a pointer on error, NULL on no error */
713 1.1 christos char byte;
714 1.1 christos int len = 0;
715 1.1 christos
716 1.1 christos while (1) {
717 1.1 christos recv(fd, &byte, 1, 0); /* read another byte in */
718 1.1 christos if (errbuf && (len++ < PCAP_ERRBUF_SIZE)) { /* and if there is still room in the buffer */
719 1.1 christos *errbuf++ = byte; /* stick it in */
720 1.1 christos *errbuf = '\0'; /* ensure the string is null terminated just in case we might exceed the buffer's size */
721 1.1 christos }
722 1.1 christos if (byte == '\0') {
723 1.1 christos if (len > 1) { return errbuf; }
724 1.1 christos else { return NULL; }
725 1.1 christos }
726 1.1 christos }
727 1.1 christos }
728 1.1 christos
729 1.1 christos int acn_findalldevs(char *errbuf) { /* returns: -1 = error, 0 = OK */
730 1.1 christos int chassis, geoslot;
731 1.1 christos unit_t *u;
732 1.1 christos
733 1.1 christos FD_ZERO(&readfds);
734 1.1 christos max_fs = 0;
735 1.1 christos for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {
736 1.1 christos for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
737 1.1 christos u = &units[chassis][geoslot];
738 1.1 christos if (u->ip && (open_with_IOP(u, FIND))) { /* connect to the remote IOP */
739 1.1 christos send_to_fd(u->find_fd, 1, (unsigned char *)"\0");
740 1.1 christos if (get_error_response(u->find_fd, errbuf))
741 1.1 christos close_with_IOP(chassis, geoslot, FIND);
742 1.1 christos else {
743 1.1 christos if (u->find_fd > max_fs)
744 1.1 christos max_fs = u->find_fd; /* remember the highest number currently in use */
745 1.1 christos FD_SET(u->find_fd, &readfds); /* we are going to want to read this guy's response to */
746 1.1 christos u->len = 0;
747 1.1 christos send_to_fd(u->find_fd, 1, (unsigned char *)"Q"); /* this interface query request */
748 1.1 christos }
749 1.1 christos }
750 1.1 christos }
751 1.1 christos }
752 1.1 christos wait_for_all_answers();
753 1.1 christos if (process_client_data(errbuf))
754 1.1 christos return -1;
755 1.1 christos sort_if_table();
756 1.1 christos return 0;
757 1.1 christos }
758 1.1 christos
759 1.1 christos static int pcap_stats_acn(pcap_t *handle, struct pcap_stat *ps) {
760 1.1 christos unsigned char buf[12];
761 1.1 christos
762 1.1 christos send_to_fd(handle->fd, 1, (unsigned char *)"S"); /* send the get_stats command to the IOP */
763 1.1 christos
764 1.1 christos if (read_client_nbytes(handle->fd, sizeof(buf), buf) == -1) return -1; /* try reading the required bytes */
765 1.1 christos
766 1.1 christos ps->ps_recv = ntohl(*(uint32_t *)&buf[0]); /* break the buffer into its three 32 bit components */
767 1.1 christos ps->ps_drop = ntohl(*(uint32_t *)&buf[4]);
768 1.1 christos ps->ps_ifdrop = ntohl(*(uint32_t *)&buf[8]);
769 1.1 christos
770 1.1 christos return 0;
771 1.1 christos }
772 1.1 christos
773 1.1 christos static int acn_open_live(const char *name, char *errbuf, int *linktype) { /* returns 0 on error, else returns the file descriptor */
774 1.1 christos int chassis, geoslot;
775 1.1 christos unit_t *u;
776 1.1 christos iface_t *p;
777 1.1 christos pcap_if_t *alldevsp;
778 1.1 christos
779 1.1 christos pcap_findalldevs(&alldevsp, errbuf);
780 1.1 christos for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { /* scan the table... */
781 1.1 christos for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
782 1.1 christos u = &units[chassis][geoslot];
783 1.1 christos if (u->ip != NULL) {
784 1.1 christos p = u->iface;
785 1.1 christos while (p) { /* and all interfaces... */
786 1.1 christos if (p->IOPname && p->name && (strcmp(p->name, name) == 0)) { /* and if we found the interface we want... */
787 1.1 christos *linktype = p->iftype;
788 1.1 christos open_with_IOP(u, LIVE); /* start a connection with that IOP */
789 1.1 christos send_to_fd(u->fd, strlen(p->IOPname)+1, (unsigned char *)p->IOPname); /* send the IOP's interface name, and a terminating null */
790 1.1 christos if (get_error_response(u->fd, errbuf)) {
791 1.1 christos return -1;
792 1.1 christos }
793 1.1 christos return u->fd; /* and return that open descriptor */
794 1.1 christos }
795 1.1 christos p = p->next;
796 1.1 christos }
797 1.1 christos }
798 1.1 christos }
799 1.1 christos }
800 1.1 christos return -1; /* if the interface wasn't found, return an error */
801 1.1 christos }
802 1.1 christos
803 1.1 christos static void acn_start_monitor(int fd, int snaplen, int timeout, int promiscuous, int direction) {
804 1.1 christos unsigned char buf[8];
805 1.1 christos unit_t *u;
806 1.1 christos
807 1.1 christos //printf("acn_start_monitor()\n"); // fulko
808 1.1 christos find_unit_by_fd(fd, NULL, NULL, &u);
809 1.1 christos if (u->first_time == 0) {
810 1.1 christos buf[0] = 'M';
811 1.1 christos *(uint32_t *)&buf[1] = htonl(snaplen);
812 1.1 christos buf[5] = timeout;
813 1.1 christos buf[6] = promiscuous;
814 1.1 christos buf[7] = direction;
815 1.1 christos //printf("acn_start_monitor() first time\n"); // fulko
816 1.1 christos send_to_fd(fd, 8, buf); /* send the start monitor command with its parameters to the IOP */
817 1.1 christos u->first_time = 1;
818 1.1 christos }
819 1.1 christos //printf("acn_start_monitor() complete\n"); // fulko
820 1.1 christos }
821 1.1 christos
822 1.1 christos static int pcap_inject_acn(pcap_t *p, const void *buf _U_, size_t size _U_) {
823 1.1 christos strlcpy(p->errbuf, "Sending packets isn't supported on ACN adapters",
824 1.1 christos PCAP_ERRBUF_SIZE);
825 1.1 christos return (-1);
826 1.1 christos }
827 1.1 christos
828 1.1 christos static int pcap_setfilter_acn(pcap_t *handle, struct bpf_program *bpf) {
829 1.1 christos int fd = handle->fd;
830 1.1 christos int count;
831 1.1 christos struct bpf_insn *p;
832 1.1 christos uint16_t shortInt;
833 1.1 christos uint32_t longInt;
834 1.1 christos
835 1.1 christos send_to_fd(fd, 1, (unsigned char *)"F"); /* BPF filter follows command */
836 1.1 christos count = bpf->bf_len;
837 1.1 christos longInt = htonl(count);
838 1.1 christos send_to_fd(fd, 4, (unsigned char *)&longInt); /* send the instruction sequence count */
839 1.1 christos p = bpf->bf_insns;
840 1.1 christos while (count--) { /* followed by the list of instructions */
841 1.1 christos shortInt = htons(p->code);
842 1.1 christos longInt = htonl(p->k);
843 1.1 christos send_to_fd(fd, 2, (unsigned char *)&shortInt);
844 1.1 christos send_to_fd(fd, 1, (unsigned char *)&p->jt);
845 1.1 christos send_to_fd(fd, 1, (unsigned char *)&p->jf);
846 1.1 christos send_to_fd(fd, 4, (unsigned char *)&longInt);
847 1.1 christos p++;
848 1.1 christos }
849 1.1 christos if (get_error_response(fd, NULL))
850 1.1 christos return -1;
851 1.1 christos return 0;
852 1.1 christos }
853 1.1 christos
854 1.1 christos static int pcap_setdirection_acn(pcap_t *handle, pcap_direction_t d) {
855 1.1 christos snprintf(handle->errbuf, sizeof(handle->errbuf),
856 1.1 christos "Setting direction is not supported on ACN adapters");
857 1.1 christos return -1;
858 1.1 christos }
859 1.1 christos
860 1.1 christos static int acn_read_n_bytes_with_timeout(pcap_t *handle, int count) {
861 1.1 christos struct timeval tv;
862 1.1 christos int retval, fd;
863 1.1 christos fd_set r_fds;
864 1.1 christos fd_set w_fds;
865 1.1 christos u_char *bp;
866 1.1 christos int len = 0;
867 1.1 christos int offset = 0;
868 1.1 christos
869 1.1 christos tv.tv_sec = 5;
870 1.1 christos tv.tv_usec = 0;
871 1.1 christos
872 1.1 christos fd = handle->fd;
873 1.1 christos FD_ZERO(&r_fds);
874 1.1 christos FD_SET(fd, &r_fds);
875 1.1 christos memcpy(&w_fds, &r_fds, sizeof(r_fds));
876 1.1 christos bp = handle->bp;
877 1.1 christos while (count) {
878 1.1 christos retval = select(fd + 1, &w_fds, NULL, NULL, &tv);
879 1.1 christos if (retval == -1) { /* an error occured !!!!! */
880 1.1 christos // fprintf(stderr, "error during packet data read\n");
881 1.1 christos return -1; /* but we need to return a good indication to prevent unneccessary popups */
882 1.1 christos } else if (retval == 0) { /* timeout occured, so process what we've got sofar and return */
883 1.1 christos // fprintf(stderr, "timeout during packet data read\n");
884 1.1 christos return -1;
885 1.1 christos } else {
886 1.1 christos if ((len = recv(fd, (bp + offset), count, 0)) <= 0) {
887 1.1 christos // fprintf(stderr, "premature exit during packet data rx\n");
888 1.1 christos return -1;
889 1.1 christos }
890 1.1 christos count -= len;
891 1.1 christos offset += len;
892 1.1 christos }
893 1.1 christos }
894 1.1 christos return 0;
895 1.1 christos }
896 1.1 christos
897 1.1 christos static int pcap_read_acn(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) {
898 1.1 christos #define HEADER_SIZE (4 * 4)
899 1.1 christos unsigned char packet_header[HEADER_SIZE];
900 1.1 christos struct pcap_pkthdr pcap_header;
901 1.1 christos
902 1.1 christos //printf("pcap_read_acn()\n"); // fulko
903 1.1 christos acn_start_monitor(handle->fd, handle->snapshot, handle->md.timeout, handle->md.clear_promisc, handle->direction); /* maybe tell him to start monitoring */
904 1.1 christos //printf("pcap_read_acn() after start monitor\n"); // fulko
905 1.1 christos
906 1.1 christos handle->bp = packet_header;
907 1.1 christos if (acn_read_n_bytes_with_timeout(handle, HEADER_SIZE) == -1) return 0; /* try to read a packet header in so we can get the sizeof the packet data */
908 1.1 christos
909 1.1 christos pcap_header.ts.tv_sec = ntohl(*(uint32_t *)&packet_header[0]); /* tv_sec */
910 1.1 christos pcap_header.ts.tv_usec = ntohl(*(uint32_t *)&packet_header[4]); /* tv_usec */
911 1.1 christos pcap_header.caplen = ntohl(*(uint32_t *)&packet_header[8]); /* caplen */
912 1.1 christos pcap_header.len = ntohl(*(uint32_t *)&packet_header[12]); /* len */
913 1.1 christos
914 1.1 christos handle->bp = handle->buffer + handle->offset; /* start off the receive pointer at the right spot */
915 1.1 christos if (acn_read_n_bytes_with_timeout(handle, pcap_header.caplen) == -1) return 0; /* then try to read in the rest of the data */
916 1.1 christos
917 1.1 christos callback(user, &pcap_header, handle->bp); /* call the user supplied callback function */
918 1.1 christos return 1;
919 1.1 christos }
920 1.1 christos
921 1.1 christos static int pcap_activate_sita(pcap_t *handle) {
922 1.1 christos int fd;
923 1.1 christos
924 1.1 christos if (handle->opt.rfmon) {
925 1.1 christos /*
926 1.1 christos * No monitor mode on SITA devices (they're not Wi-Fi
927 1.1 christos * devices).
928 1.1 christos */
929 1.1 christos return PCAP_ERROR_RFMON_NOTSUP;
930 1.1 christos }
931 1.1 christos
932 1.1 christos /* Initialize some components of the pcap structure. */
933 1.1 christos
934 1.1 christos handle->inject_op = pcap_inject_acn;
935 1.1 christos handle->setfilter_op = pcap_setfilter_acn;
936 1.1 christos handle->setdirection_op = pcap_setdirection_acn;
937 1.1 christos handle->set_datalink_op = NULL; /* can't change data link type */
938 1.1 christos handle->getnonblock_op = pcap_getnonblock_fd;
939 1.1 christos handle->setnonblock_op = pcap_setnonblock_fd;
940 1.1 christos handle->cleanup_op = pcap_cleanup_acn;
941 1.1 christos handle->read_op = pcap_read_acn;
942 1.1 christos handle->stats_op = pcap_stats_acn;
943 1.1 christos
944 1.1 christos fd = acn_open_live(handle->opt.source, handle->errbuf,
945 1.1 christos &handle->linktype);
946 1.1 christos if (fd == -1)
947 1.1 christos return PCAP_ERROR;
948 1.1 christos handle->md.clear_promisc = handle->md.promisc;
949 1.1 christos handle->fd = fd;
950 1.1 christos handle->bufsize = handle->snapshot;
951 1.1 christos
952 1.1 christos /* Allocate the buffer */
953 1.1 christos
954 1.1 christos handle->buffer = malloc(handle->bufsize + handle->offset);
955 1.1 christos if (!handle->buffer) {
956 1.1 christos snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
957 1.1 christos "malloc: %s", pcap_strerror(errno));
958 1.1 christos pcap_cleanup_acn(handle);
959 1.1 christos return PCAP_ERROR;
960 1.1 christos }
961 1.1 christos
962 1.1 christos /*
963 1.1 christos * "handle->fd" is a socket, so "select()" and "poll()"
964 1.1 christos * should work on it.
965 1.1 christos */
966 1.1 christos handle->selectable_fd = handle->fd;
967 1.1 christos
968 1.1 christos return 0;
969 1.1 christos }
970 1.1 christos
971 1.1 christos pcap_t *pcap_create(const char *device, char *ebuf) {
972 1.1 christos pcap_t *p;
973 1.1 christos
974 1.1 christos p = pcap_create_common(device, ebuf);
975 1.1 christos if (p == NULL)
976 1.1 christos return (NULL);
977 1.1 christos
978 1.1 christos p->activate_op = pcap_activate_sita;
979 1.1 christos return (p);
980 1.1 christos }
981