2 * pcap-sita.c: Packet capture interface additions for SITA ACN devices
4 * Copyright (c) 2007 Fulko Hew, SITA INC Canada, Inc <fulko.hew@sita.aero>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. The names of the authors may not be used to endorse or promote
19 * products derived from this software without specific prior
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
43 #include "pcap-sita.h"
45 /* non-configureable manifests follow */
47 #define IOP_SNIFFER_PORT 49152 /* TCP port on the IOP used for 'distributed pcap' usage */
48 #define MAX_LINE_SIZE 255 /* max size of a buffer/line in /etc/hosts we allow */
49 #define MAX_CHASSIS 8 /* number of chassis in an ACN site */
50 #define MAX_GEOSLOT 8 /* max number of access units in an ACN site */
55 typedef struct iface
{
56 struct iface
*next
; /* a pointer to the next interface */
57 char *name
; /* this interface's name */
58 char *IOPname
; /* this interface's name on an IOP */
59 uint32_t iftype
; /* the type of interface (DLT values) */
63 char *ip
; /* this unit's IP address (as extracted from /etc/hosts) */
64 int fd
; /* the connection to this unit (if it exists) */
65 int find_fd
; /* a big kludge to avoid my programming limitations since I could have this unit open for findalldevs purposes */
66 int first_time
; /* 0 = just opened via acn_open_live(), ie. the first time, NZ = nth time */
67 struct sockaddr_in
*serv_addr
; /* the address control block for comms to this unit */
70 iface_t
*iface
; /* a pointer to a linked list of interface structures */
71 char *imsg
; /* a pointer to an inbound message */
72 int len
; /* the current size of the inbound message */
75 static unit_t units
[MAX_CHASSIS
+1][MAX_GEOSLOT
+1]; /* we use indexes of 1 through 8, but we reserve/waste index 0 */
76 static fd_set readfds
; /* a place to store the file descriptors for the connections to the IOPs */
79 pcap_if_t
*acn_if_list
; /* pcap's list of available interfaces */
81 static void dump_interface_list(void) {
84 int longest_name_len
= 0;
90 if (iff
->name
&& (strlen(iff
->name
) > longest_name_len
)) longest_name_len
= strlen(iff
->name
);
94 printf("Interface List:\n");
96 n
= (iff
->name
) ? iff
->name
: "";
97 d
= (iff
->description
) ? iff
->description
: "";
98 f
= (iff
->flags
== PCAP_IF_LOOPBACK
) ? "L" : "";
99 printf("%3d: %*s %s '%s'\n", if_number
++, longest_name_len
, n
, f
, d
);
100 addr
= iff
->addresses
;
102 printf("%*s ", (5 + longest_name_len
), ""); /* add some indentation */
103 printf("%15s ", (addr
->addr
) ? inet_ntoa(((struct sockaddr_in
*)addr
->addr
)->sin_addr
) : "");
104 printf("%15s ", (addr
->netmask
) ? inet_ntoa(((struct sockaddr_in
*)addr
->netmask
)->sin_addr
) : "");
105 printf("%15s ", (addr
->broadaddr
) ? inet_ntoa(((struct sockaddr_in
*)addr
->broadaddr
)->sin_addr
) : "");
106 printf("%15s ", (addr
->dstaddr
) ? inet_ntoa(((struct sockaddr_in
*)addr
->dstaddr
)->sin_addr
) : "");
114 static void dump(unsigned char *ptr
, int i
, int indent
) {
115 fprintf(stderr
, "%*s", indent
, " ");
117 fprintf(stderr
, "%2.2x ", *ptr
++);
119 fprintf(stderr
, "\n");
122 static void dump_interface_list_p(void) {
128 printf("Interface Pointer @ %p is %p:\n", &acn_if_list
, iff
);
130 printf("%3d: %p %p next: %p\n", if_number
++, iff
->name
, iff
->description
, iff
->next
);
131 dump((unsigned char *)iff
, sizeof(pcap_if_t
), 5);
132 addr
= iff
->addresses
;
134 printf(" %p %p %p %p, next: %p\n", addr
->addr
, addr
->netmask
, addr
->broadaddr
, addr
->dstaddr
, addr
->next
);
135 dump((unsigned char *)addr
, sizeof(pcap_addr_t
), 10);
142 static void dump_unit_table(void) {
143 int chassis
, geoslot
;
146 printf("%c:%c %s %s\n", 'C', 'S', "fd", "IP Address");
147 for (chassis
= 0; chassis
<= MAX_CHASSIS
; chassis
++) {
148 for (geoslot
= 0; geoslot
<= MAX_GEOSLOT
; geoslot
++) {
149 if (units
[chassis
][geoslot
].ip
!= NULL
)
150 printf("%d:%d %2d %s\n", chassis
, geoslot
, units
[chassis
][geoslot
].fd
, units
[chassis
][geoslot
].ip
);
151 p
= units
[chassis
][geoslot
].iface
;
153 char *n
= (p
->name
) ? p
->name
: "";
154 char *i
= (p
->IOPname
) ? p
->IOPname
: "";
156 printf(" %12s -> %12s\n", i
, n
);
162 static int find_unit_by_fd(int fd
, int *chassis
, int *geoslot
, unit_t
**unit_ptr
) {
165 for (c
= 0; c
<= MAX_CHASSIS
; c
++) {
166 for (s
= 0; s
<= MAX_GEOSLOT
; s
++) {
167 if (units
[c
][s
].fd
== fd
|| units
[c
][s
].find_fd
== fd
) {
168 if (chassis
) *chassis
= c
;
169 if (geoslot
) *geoslot
= s
;
170 if (unit_ptr
) *unit_ptr
= &units
[c
][s
];
178 static int read_client_nbytes(int fd
, int count
, unsigned char *buf
) {
180 int chassis
, geoslot
;
183 find_unit_by_fd(fd
, &chassis
, &geoslot
, &u
);
185 if ((len
= recv(fd
, buf
, count
, 0)) <= 0) return -1; /* read in whatever data was sent to us */
188 } /* till we have everything we are looking for */
192 static void empty_unit_iface(unit_t
*u
) {
196 while (cur
) { /* loop over all the interface entries */
197 if (cur
->name
) free(cur
->name
); /* throwing away the contents if they exist */
198 if (cur
->IOPname
) free(cur
->IOPname
);
200 free(cur
); /* then throw away the structure itself */
203 u
->iface
= 0; /* and finally remember that there are no remaining structure */
206 static void empty_unit(int chassis
, int geoslot
) {
207 unit_t
*u
= &units
[chassis
][geoslot
];
210 if (u
->imsg
) { /* then if an inbound message buffer exists */
213 bigger_buffer
= (char *)realloc(u
->imsg
, 1); /* and re-allocate the old large buffer into a new small one */
214 if (bigger_buffer
== NULL
) { /* oops, realloc call failed */
215 fprintf(stderr
, "Warning...call to realloc() failed, value of errno is %d\n", errno
);
218 u
->imsg
= bigger_buffer
;
222 static void empty_unit_table(void) {
223 int chassis
, geoslot
;
225 for (chassis
= 0; chassis
<= MAX_CHASSIS
; chassis
++) {
226 for (geoslot
= 0; geoslot
<= MAX_GEOSLOT
; geoslot
++) {
227 if (units
[chassis
][geoslot
].ip
!= NULL
) {
228 free(units
[chassis
][geoslot
].ip
); /* get rid of the malloc'ed space that holds the IP address */
229 units
[chassis
][geoslot
].ip
= 0; /* then set the pointer to NULL */
231 empty_unit(chassis
, geoslot
);
236 static char *find_nth_interface_name(int n
) {
237 int chassis
, geoslot
;
241 if (n
< 0) n
= 0; /* ensure we are working with a valid number */
242 for (chassis
= 0; chassis
<= MAX_CHASSIS
; chassis
++) { /* scan the table... */
243 for (geoslot
= 0; geoslot
<= MAX_GEOSLOT
; geoslot
++) {
244 if (units
[chassis
][geoslot
].ip
!= NULL
) {
245 p
= units
[chassis
][geoslot
].iface
;
246 while (p
) { /* and all interfaces... */
247 if (p
->IOPname
) last_name
= p
->name
; /* remembering the last name found */
248 if (n
-- == 0) return last_name
; /* and if we hit the instance requested */
254 /* if we couldn't fine the selected entry */
255 if (last_name
) return last_name
; /* ... but we did have at least one entry... return the last entry found */
256 return ""; /* ... but if there wasn't any entry... return an empty string instead */
259 int acn_parse_hosts_file(char *errbuf
) { /* returns: -1 = error, 0 = OK */
261 char buf
[MAX_LINE_SIZE
];
264 int chassis
, geoslot
;
268 if ((fp
= fopen("/etc/hosts", "r")) == NULL
) { /* try to open the hosts file and if it fails */
269 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Cannot open '/etc/hosts' for reading."); /* return the nohostsfile error response */
272 while (fgets(buf
, MAX_LINE_SIZE
-1, fp
)) { /* while looping over the file */
274 pos
= strcspn(buf
, "#\n\r"); /* find the first comment character or EOL */
275 *(buf
+ pos
) = '\0'; /* and clobber it and anything that follows it */
277 pos
= strspn(buf
, " \t"); /* then find the first non-white space */
278 if (pos
== strlen(buf
)) /* if there is nothing but white space on the line */
279 continue; /* ignore that empty line */
280 ptr
= buf
+ pos
; /* and skip over any of that leading whitespace */
282 if ((ptr2
= strstr(ptr
, "_I_")) == NULL
) /* skip any lines that don't have names that look like they belong to IOPs */
284 if (*(ptr2
+ 4) != '_') /* and skip other lines that have names that don't look like ACN components */
286 *(ptr
+ strcspn(ptr
, " \t")) = '\0'; /* null terminate the IP address so its a standalone string */
288 chassis
= *(ptr2
+ 3) - '0'; /* extract the chassis number */
289 geoslot
= *(ptr2
+ 5) - '0'; /* and geo-slot number */
290 if (chassis
< 1 || chassis
> MAX_CHASSIS
||
291 geoslot
< 1 || geoslot
> MAX_GEOSLOT
) { /* if the chassis and/or slot numbers appear to be bad... */
292 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Invalid ACN name in '/etc/hosts'."); /* warn the user */
293 continue; /* and ignore the entry */
295 if ((ptr2
= (char *)malloc(strlen(ptr
) + 1)) == NULL
) {
296 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
300 strcpy(ptr2
, ptr
); /* copy the IP address into our malloc'ed memory */
301 u
= &units
[chassis
][geoslot
];
302 u
->ip
= ptr2
; /* and remember the whole shebang */
303 u
->chassis
= chassis
;
304 u
->geoslot
= geoslot
;
307 if (*errbuf
) return -1;
311 static int open_with_IOP(unit_t
*u
, int flag
) {
315 if (u
->serv_addr
== NULL
) {
316 u
->serv_addr
= malloc(sizeof(struct sockaddr_in
));
318 /* since we called malloc(), lets check to see if we actually got the memory */
319 if (u
->serv_addr
== NULL
) { /* oops, we didn't get the memory requested */
320 fprintf(stderr
, "malloc() request for u->serv_addr failed, value of errno is: %d\n", errno
);
326 /* bzero() is deprecated, replaced with memset() */
327 memset((char *)u
->serv_addr
, 0, sizeof(struct sockaddr_in
));
328 u
->serv_addr
->sin_family
= AF_INET
;
329 u
->serv_addr
->sin_addr
.s_addr
= inet_addr(ip
);
330 u
->serv_addr
->sin_port
= htons(IOP_SNIFFER_PORT
);
332 if ((sockfd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0) {
333 fprintf(stderr
, "pcap can't open a socket for connecting to IOP at %s\n", ip
);
336 if (connect(sockfd
, (struct sockaddr
*)u
->serv_addr
, sizeof(struct sockaddr_in
)) < 0) {
337 fprintf(stderr
, "pcap can't connect to IOP at %s\n", ip
);
340 if (flag
== LIVE
) u
->fd
= sockfd
;
341 else u
->find_fd
= sockfd
;
343 return sockfd
; /* return the non-zero file descriptor as a 'success' indicator */
346 static void close_with_IOP(int chassis
, int geoslot
, int flag
) {
349 if (flag
== LIVE
) id
= &units
[chassis
][geoslot
].fd
;
350 else id
= &units
[chassis
][geoslot
].find_fd
;
352 if (*id
) { /* this was the last time, so... if we are connected... */
353 close(*id
); /* disconnect us */
354 *id
= 0; /* and forget that the descriptor exists because we are not open */
358 static void pcap_cleanup_acn(pcap_t
*handle
) {
359 int chassis
, geoslot
;
362 if (find_unit_by_fd(handle
->fd
, &chassis
, &geoslot
, &u
) == 0)
364 close_with_IOP(chassis
, geoslot
, LIVE
);
367 pcap_cleanup_live_common(handle
);
370 static void send_to_fd(int fd
, int len
, unsigned char *str
) {
372 int chassis
, geoslot
;
375 if ((nwritten
= write(fd
, str
, len
)) <= 0) {
376 find_unit_by_fd(fd
, &chassis
, &geoslot
, NULL
);
377 if (units
[chassis
][geoslot
].fd
== fd
) close_with_IOP(chassis
, geoslot
, LIVE
);
378 else if (units
[chassis
][geoslot
].find_fd
== fd
) close_with_IOP(chassis
, geoslot
, FIND
);
379 empty_unit(chassis
, geoslot
);
387 static void acn_freealldevs(void) {
389 pcap_if_t
*iff
, *next_iff
;
390 pcap_addr_t
*addr
, *next_addr
;
392 for (iff
= acn_if_list
; iff
!= NULL
; iff
= next_iff
) {
393 next_iff
= iff
->next
;
394 for (addr
= iff
->addresses
; addr
!= NULL
; addr
= next_addr
) {
395 next_addr
= addr
->next
;
396 if (addr
->addr
) free(addr
->addr
);
397 if (addr
->netmask
) free(addr
->netmask
);
398 if (addr
->broadaddr
) free(addr
->broadaddr
);
399 if (addr
->dstaddr
) free(addr
->dstaddr
);
402 if (iff
->name
) free(iff
->name
);
403 if (iff
->description
) free(iff
->description
);
408 static void nonUnified_IOP_port_name(char *buf
, size_t bufsize
, const char *proto
, unit_t
*u
) {
410 pcap_snprintf(buf
, bufsize
, "%s_%d_%d", proto
, u
->chassis
, u
->geoslot
);
413 static void unified_IOP_port_name(char *buf
, size_t bufsize
, const char *proto
, unit_t
*u
, int IOPportnum
) {
416 portnum
= ((u
->chassis
- 1) * 64) + ((u
->geoslot
- 1) * 8) + IOPportnum
+ 1;
417 pcap_snprintf(buf
, bufsize
, "%s_%d", proto
, portnum
);
420 static char *translate_IOP_to_pcap_name(unit_t
*u
, char *IOPname
, bpf_u_int32 iftype
) {
421 iface_t
*iface_ptr
, *iface
;
428 iface
= malloc(sizeof(iface_t
)); /* get memory for a structure */
429 if (iface
== NULL
) { /* oops, we didn't get the memory requested */
430 fprintf(stderr
, "Error...couldn't allocate memory for interface structure...value of errno is: %d\n", errno
);
433 memset((char *)iface
, 0, sizeof(iface_t
)); /* bzero is deprecated(), replaced with memset() */
435 iface
->iftype
= iftype
; /* remember the interface type of this interface */
437 name
= malloc(strlen(IOPname
) + 1); /* get memory for the IOP's name */
438 if (name
== NULL
) { /* oops, we didn't get the memory requested */
439 fprintf(stderr
, "Error...couldn't allocate memory for IOPname...value of errno is: %d\n", errno
);
443 strcpy(name
, IOPname
); /* and copy it in */
444 iface
->IOPname
= name
; /* and stick it into the structure */
446 if (strncmp(IOPname
, "lo", 2) == 0) {
447 IOPportnum
= atoi(&IOPname
[2]);
450 nonUnified_IOP_port_name(buf
, sizeof buf
, "lo", u
);
453 unified_IOP_port_name(buf
, sizeof buf
, "???", u
, IOPportnum
);
456 } else if (strncmp(IOPname
, "eth", 3) == 0) {
457 IOPportnum
= atoi(&IOPname
[3]);
460 nonUnified_IOP_port_name(buf
, sizeof buf
, "eth", u
);
463 unified_IOP_port_name(buf
, sizeof buf
, "???", u
, IOPportnum
);
466 } else if (strncmp(IOPname
, "wan", 3) == 0) {
467 IOPportnum
= atoi(&IOPname
[3]);
470 unified_IOP_port_name(buf
, sizeof buf
, "wan", u
, IOPportnum
);
473 unified_IOP_port_name(buf
, sizeof buf
, "???", u
, IOPportnum
);
477 fprintf(stderr
, "Error... invalid IOP name %s\n", IOPname
);
481 name
= malloc(strlen(buf
) + 1); /* get memory for that name */
482 if (name
== NULL
) { /* oops, we didn't get the memory requested */
483 fprintf(stderr
, "Error...couldn't allocate memory for IOP port name...value of errno is: %d\n", errno
);
487 strcpy(name
, buf
); /* and copy it in */
488 iface
->name
= name
; /* and stick it into the structure */
490 if (u
->iface
== 0) { /* if this is the first name */
491 u
->iface
= iface
; /* stick this entry at the head of the list */
493 iface_ptr
= u
->iface
;
494 while (iface_ptr
->next
) { /* othewise scan the list */
495 iface_ptr
= iface_ptr
->next
; /* till we're at the last entry */
497 iface_ptr
->next
= iface
; /* then tack this entry on the end of the list */
502 static int if_sort(char *s1
, char *s2
) {
504 char str1
[MAX_LINE_SIZE
], str2
[MAX_LINE_SIZE
];
505 int s1_p1_len
, s2_p1_len
;
508 if ((s1_p2
= strchr(s1
, '_'))) { /* if an underscore is found... */
509 s1_p1_len
= s1_p2
- s1
; /* the prefix length is the difference in pointers */
510 s1_p2
++; /* the suffix actually starts _after_ the underscore */
511 } else { /* otherwise... */
512 s1_p1_len
= strlen(s1
); /* the prefix length is the length of the string itself */
513 s1_p2
= 0; /* and there is no suffix */
515 if ((s2_p2
= strchr(s2
, '_'))) { /* now do the same for the second string */
516 s2_p1_len
= s2_p2
- s2
;
519 s2_p1_len
= strlen(s2
);
522 strncpy(str1
, s1
, (s1_p1_len
> sizeof(str1
)) ? s1_p1_len
: sizeof(str1
)); *(str1
+ s1_p1_len
) = 0;
523 strncpy(str2
, s2
, (s2_p1_len
> sizeof(str2
)) ? s2_p1_len
: sizeof(str2
)); *(str2
+ s2_p1_len
) = 0;
524 retval
= strcmp(str1
, str2
);
525 if (retval
!= 0) return retval
; /* if they are not identical, then we can quit now and return the indication */
526 return strcmp(s1_p2
, s2_p2
); /* otherwise we return the result of comparing the 2nd half of the string */
529 static void sort_if_table(void) {
530 pcap_if_t
*p1
, *p2
, *prev
, *temp
;
533 if (!acn_if_list
) return; /* nothing to do if the list is empty */
536 p1
= acn_if_list
; /* start at the head of the list */
539 while ((p2
= p1
->next
)) {
540 if (if_sort(p1
->name
, p2
->name
) > 0) {
541 if (prev
) { /* we are swapping things that are _not_ at the head of the list */
546 } else { /* special treatment if we are swapping with the head of the list */
559 if (has_swapped
== 0)
565 static int process_client_data (char *errbuf
) { /* returns: -1 = error, 0 = OK */
566 int chassis
, geoslot
;
568 pcap_if_t
*iff
, *prev_iff
;
569 pcap_addr_t
*addr
, *prev_addr
;
572 struct sockaddr_in
*s
;
574 bpf_u_int32 interfaceType
;
579 for (chassis
= 0; chassis
<= MAX_CHASSIS
; chassis
++) {
580 for (geoslot
= 0; geoslot
<= MAX_GEOSLOT
; geoslot
++) { /* now loop over all the devices */
581 u
= &units
[chassis
][geoslot
];
583 ptr
= u
->imsg
; /* point to the start of the msg for this IOP */
584 while (ptr
< (u
->imsg
+ u
->len
)) {
585 if ((iff
= malloc(sizeof(pcap_if_t
))) == NULL
) {
586 pcap_fmt_errmsg_for_errno(errbuf
,
587 PCAP_ERRBUF_SIZE
, errno
, "malloc");
590 memset((char *)iff
, 0, sizeof(pcap_if_t
)); /* bzero() is deprecated, replaced with memset() */
591 if (acn_if_list
== 0) acn_if_list
= iff
; /* remember the head of the list */
592 if (prev_iff
) prev_iff
->next
= iff
; /* insert a forward link */
594 if (*ptr
) { /* if there is a count for the name */
595 if ((iff
->name
= malloc(*ptr
+ 1)) == NULL
) { /* get that amount of space */
596 pcap_fmt_errmsg_for_errno(errbuf
,
597 PCAP_ERRBUF_SIZE
, errno
,
601 memcpy(iff
->name
, (ptr
+ 1), *ptr
); /* copy the name into the malloc'ed space */
602 *(iff
->name
+ *ptr
) = 0; /* and null terminate the string */
603 ptr
+= *ptr
; /* now move the pointer forwards by the length of the count plus the length of the string */
607 if (*ptr
) { /* if there is a count for the description */
608 if ((iff
->description
= malloc(*ptr
+ 1)) == NULL
) { /* get that amount of space */
609 pcap_fmt_errmsg_for_errno(errbuf
,
610 PCAP_ERRBUF_SIZE
, errno
,
614 memcpy(iff
->description
, (ptr
+ 1), *ptr
); /* copy the name into the malloc'ed space */
615 *(iff
->description
+ *ptr
) = 0; /* and null terminate the string */
616 ptr
+= *ptr
; /* now move the pointer forwards by the length of the count plus the length of the string */
620 interfaceType
= ntohl(*(bpf_u_int32
*)ptr
);
621 ptr
+= 4; /* skip over the interface type */
624 if (flags
) iff
->flags
= PCAP_IF_LOOPBACK
; /* if this is a loopback style interface, lets mark it as such */
626 address_count
= *ptr
++;
629 while (address_count
--) {
630 if ((addr
= malloc(sizeof(pcap_addr_t
))) == NULL
) {
631 pcap_fmt_errmsg_for_errno(errbuf
,
632 PCAP_ERRBUF_SIZE
, errno
,
636 memset((char *)addr
, 0, sizeof(pcap_addr_t
)); /* bzero() is deprecated, replaced with memset() */
637 if (iff
->addresses
== 0) iff
->addresses
= addr
;
638 if (prev_addr
) prev_addr
->next
= addr
; /* insert a forward link */
639 if (*ptr
) { /* if there is a count for the address */
640 if ((s
= malloc(sizeof(struct sockaddr_in
))) == NULL
) { /* get that amount of space */
641 pcap_fmt_errmsg_for_errno(errbuf
,
646 memset((char *)s
, 0, sizeof(struct sockaddr_in
)); /* bzero() is deprecated, replaced with memset() */
647 addr
->addr
= (struct sockaddr
*)s
;
648 s
->sin_family
= AF_INET
;
649 s
->sin_addr
.s_addr
= *(bpf_u_int32
*)(ptr
+ 1); /* copy the address in */
650 ptr
+= *ptr
; /* now move the pointer forwards according to the specified length of the address */
652 ptr
++; /* then forwards one more for the 'length of the address' field */
653 if (*ptr
) { /* process any netmask */
654 if ((s
= malloc(sizeof(struct sockaddr_in
))) == NULL
) {
655 pcap_fmt_errmsg_for_errno(errbuf
,
660 /* bzero() is deprecated, replaced with memset() */
661 memset((char *)s
, 0, sizeof(struct sockaddr_in
));
663 addr
->netmask
= (struct sockaddr
*)s
;
664 s
->sin_family
= AF_INET
;
665 s
->sin_addr
.s_addr
= *(bpf_u_int32
*)(ptr
+ 1);
669 if (*ptr
) { /* process any broadcast address */
670 if ((s
= malloc(sizeof(struct sockaddr_in
))) == NULL
) {
671 pcap_fmt_errmsg_for_errno(errbuf
,
676 /* bzero() is deprecated, replaced with memset() */
677 memset((char *)s
, 0, sizeof(struct sockaddr_in
));
679 addr
->broadaddr
= (struct sockaddr
*)s
;
680 s
->sin_family
= AF_INET
;
681 s
->sin_addr
.s_addr
= *(bpf_u_int32
*)(ptr
+ 1);
685 if (*ptr
) { /* process any destination address */
686 if ((s
= malloc(sizeof(struct sockaddr_in
))) == NULL
) {
687 pcap_fmt_errmsg_for_errno(errbuf
,
692 /* bzero() is deprecated, replaced with memset() */
693 memset((char *)s
, 0, sizeof(struct sockaddr_in
));
695 addr
->dstaddr
= (struct sockaddr
*)s
;
696 s
->sin_family
= AF_INET
;
697 s
->sin_addr
.s_addr
= *(bpf_u_int32
*)(ptr
+ 1);
705 newname
= translate_IOP_to_pcap_name(u
, iff
->name
, interfaceType
); /* add a translation entry and get a point to the mangled name */
706 bigger_buffer
= realloc(iff
->name
, strlen(newname
) + 1));
707 if (bigger_buffer
== NULL
) { /* we now re-write the name stored in the interface list */
708 pcap_fmt_errmsg_for_errno(errbuf
,
709 PCAP_ERRBUF_SIZE
, errno
, "realloc");
712 iff
->name
= bigger_buffer
;
713 strcpy(iff
->name
, newname
); /* to this new name */
720 static int read_client_data (int fd
) {
721 unsigned char buf
[256];
722 int chassis
, geoslot
;
726 find_unit_by_fd(fd
, &chassis
, &geoslot
, &u
);
728 if ((len
= recv(fd
, buf
, sizeof(buf
), 0)) <= 0) return 0; /* read in whatever data was sent to us */
730 if ((u
->imsg
= realloc(u
->imsg
, (u
->len
+ len
))) == NULL
) /* extend the buffer for the new data */
732 memcpy((u
->imsg
+ u
->len
), buf
, len
); /* append the new data */
737 static void wait_for_all_answers(void) {
741 int chassis
, geoslot
;
750 for (fd
= 0; fd
<= max_fs
; fd
++) { /* scan the list of descriptors we may be listening to */
751 if (FD_ISSET(fd
, &readfds
)) flag
= 1; /* and see if there are any still set */
753 if (flag
== 0) return; /* we are done, when they are all gone */
755 memcpy(&working_set
, &readfds
, sizeof(readfds
)); /* otherwise, we still have to listen for more stuff, till we timeout */
756 retval
= select(max_fs
+ 1, &working_set
, NULL
, NULL
, &tv
);
757 if (retval
== -1) { /* an error occured !!!!! */
759 } else if (retval
== 0) { /* timeout occured, so process what we've got sofar and return */
763 for (fd
= 0; fd
<= max_fs
; fd
++) { /* scan the list of things to do, and do them */
764 if (FD_ISSET(fd
, &working_set
)) {
765 if (read_client_data(fd
) == 0) { /* if the socket has closed */
766 FD_CLR(fd
, &readfds
); /* and descriptors we listen to for errors */
767 find_unit_by_fd(fd
, &chassis
, &geoslot
, NULL
);
768 close_with_IOP(chassis
, geoslot
, FIND
); /* and close out connection to him */
776 static char *get_error_response(int fd
, char *errbuf
) { /* return a pointer on error, NULL on no error */
781 recv(fd
, &byte
, 1, 0); /* read another byte in */
782 if (errbuf
&& (len
++ < PCAP_ERRBUF_SIZE
)) { /* and if there is still room in the buffer */
783 *errbuf
++ = byte
; /* stick it in */
784 *errbuf
= '\0'; /* ensure the string is null terminated just in case we might exceed the buffer's size */
787 if (len
> 1) { return errbuf
; }
788 else { return NULL
; }
793 int acn_findalldevs(char *errbuf
) { /* returns: -1 = error, 0 = OK */
794 int chassis
, geoslot
;
799 for (chassis
= 0; chassis
<= MAX_CHASSIS
; chassis
++) {
800 for (geoslot
= 0; geoslot
<= MAX_GEOSLOT
; geoslot
++) {
801 u
= &units
[chassis
][geoslot
];
802 if (u
->ip
&& (open_with_IOP(u
, FIND
))) { /* connect to the remote IOP */
803 send_to_fd(u
->find_fd
, 1, (unsigned char *)"\0");
804 if (get_error_response(u
->find_fd
, errbuf
))
805 close_with_IOP(chassis
, geoslot
, FIND
);
807 if (u
->find_fd
> max_fs
)
808 max_fs
= u
->find_fd
; /* remember the highest number currently in use */
809 FD_SET(u
->find_fd
, &readfds
); /* we are going to want to read this guy's response to */
811 send_to_fd(u
->find_fd
, 1, (unsigned char *)"Q"); /* this interface query request */
816 wait_for_all_answers();
817 if (process_client_data(errbuf
))
823 static int pcap_stats_acn(pcap_t
*handle
, struct pcap_stat
*ps
) {
824 unsigned char buf
[12];
826 send_to_fd(handle
->fd
, 1, (unsigned char *)"S"); /* send the get_stats command to the IOP */
828 if (read_client_nbytes(handle
->fd
, sizeof(buf
), buf
) == -1) return -1; /* try reading the required bytes */
830 ps
->ps_recv
= ntohl(*(uint32_t *)&buf
[0]); /* break the buffer into its three 32 bit components */
831 ps
->ps_drop
= ntohl(*(uint32_t *)&buf
[4]);
832 ps
->ps_ifdrop
= ntohl(*(uint32_t *)&buf
[8]);
837 static int acn_open_live(const char *name
, char *errbuf
, int *linktype
) { /* returns 0 on error, else returns the file descriptor */
838 int chassis
, geoslot
;
841 pcap_if_list_t devlist
;
843 pcap_platform_finddevs(&devlist
, errbuf
);
844 for (chassis
= 0; chassis
<= MAX_CHASSIS
; chassis
++) { /* scan the table... */
845 for (geoslot
= 0; geoslot
<= MAX_GEOSLOT
; geoslot
++) {
846 u
= &units
[chassis
][geoslot
];
849 while (p
) { /* and all interfaces... */
850 if (p
->IOPname
&& p
->name
&& (strcmp(p
->name
, name
) == 0)) { /* and if we found the interface we want... */
851 *linktype
= p
->iftype
;
852 open_with_IOP(u
, LIVE
); /* start a connection with that IOP */
853 send_to_fd(u
->fd
, strlen(p
->IOPname
)+1, (unsigned char *)p
->IOPname
); /* send the IOP's interface name, and a terminating null */
854 if (get_error_response(u
->fd
, errbuf
)) {
857 return u
->fd
; /* and return that open descriptor */
864 return -1; /* if the interface wasn't found, return an error */
867 static void acn_start_monitor(int fd
, int snaplen
, int timeout
, int promiscuous
, int direction
) {
868 unsigned char buf
[8];
871 //printf("acn_start_monitor()\n"); // fulko
872 find_unit_by_fd(fd
, NULL
, NULL
, &u
);
873 if (u
->first_time
== 0) {
875 *(uint32_t *)&buf
[1] = htonl(snaplen
);
877 buf
[6] = promiscuous
;
879 //printf("acn_start_monitor() first time\n"); // fulko
880 send_to_fd(fd
, 8, buf
); /* send the start monitor command with its parameters to the IOP */
883 //printf("acn_start_monitor() complete\n"); // fulko
886 static int pcap_inject_acn(pcap_t
*p
, const void *buf _U_
, int size _U_
) {
887 pcap_strlcpy(p
->errbuf
, "Sending packets isn't supported on ACN adapters",
892 static int pcap_setfilter_acn(pcap_t
*handle
, struct bpf_program
*bpf
) {
899 send_to_fd(fd
, 1, (unsigned char *)"F"); /* BPF filter follows command */
901 longInt
= htonl(count
);
902 send_to_fd(fd
, 4, (unsigned char *)&longInt
); /* send the instruction sequence count */
904 while (count
--) { /* followed by the list of instructions */
905 shortInt
= htons(p
->code
);
906 longInt
= htonl(p
->k
);
907 send_to_fd(fd
, 2, (unsigned char *)&shortInt
);
908 send_to_fd(fd
, 1, (unsigned char *)&p
->jt
);
909 send_to_fd(fd
, 1, (unsigned char *)&p
->jf
);
910 send_to_fd(fd
, 4, (unsigned char *)&longInt
);
913 if (get_error_response(fd
, NULL
))
918 static int pcap_setdirection_acn(pcap_t
*handle
, pcap_direction_t d
) {
919 pcap_snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
920 "Setting direction is not supported on ACN adapters");
924 static int acn_read_n_bytes_with_timeout(pcap_t
*handle
, int count
) {
939 memcpy(&w_fds
, &r_fds
, sizeof(r_fds
));
942 retval
= select(fd
+ 1, &w_fds
, NULL
, NULL
, &tv
);
943 if (retval
== -1) { /* an error occured !!!!! */
944 // fprintf(stderr, "error during packet data read\n");
945 return -1; /* but we need to return a good indication to prevent unneccessary popups */
946 } else if (retval
== 0) { /* timeout occured, so process what we've got sofar and return */
947 // fprintf(stderr, "timeout during packet data read\n");
950 if ((len
= recv(fd
, (bp
+ offset
), count
, 0)) <= 0) {
951 // fprintf(stderr, "premature exit during packet data rx\n");
961 static int pcap_read_acn(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
) {
962 #define HEADER_SIZE (4 * 4)
963 unsigned char packet_header
[HEADER_SIZE
];
964 struct pcap_pkthdr pcap_header
;
966 //printf("pcap_read_acn()\n"); // fulko
967 acn_start_monitor(handle
->fd
, handle
->snapshot
, handle
->opt
.timeout
, handle
->opt
.promisc
, handle
->direction
); /* maybe tell him to start monitoring */
968 //printf("pcap_read_acn() after start monitor\n"); // fulko
970 handle
->bp
= packet_header
;
971 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 */
973 pcap_header
.ts
.tv_sec
= ntohl(*(uint32_t *)&packet_header
[0]); /* tv_sec */
974 pcap_header
.ts
.tv_usec
= ntohl(*(uint32_t *)&packet_header
[4]); /* tv_usec */
975 pcap_header
.caplen
= ntohl(*(uint32_t *)&packet_header
[8]); /* caplen */
976 pcap_header
.len
= ntohl(*(uint32_t *)&packet_header
[12]); /* len */
978 handle
->bp
= (u_char
*)handle
->buffer
+ handle
->offset
; /* start off the receive pointer at the right spot */
979 if (acn_read_n_bytes_with_timeout(handle
, pcap_header
.caplen
) == -1) return 0; /* then try to read in the rest of the data */
981 callback(user
, &pcap_header
, handle
->bp
); /* call the user supplied callback function */
985 static int pcap_activate_sita(pcap_t
*handle
) {
988 if (handle
->opt
.rfmon
) {
990 * No monitor mode on SITA devices (they're not Wi-Fi
993 return PCAP_ERROR_RFMON_NOTSUP
;
996 /* Initialize some components of the pcap structure. */
998 handle
->inject_op
= pcap_inject_acn
;
999 handle
->setfilter_op
= pcap_setfilter_acn
;
1000 handle
->setdirection_op
= pcap_setdirection_acn
;
1001 handle
->set_datalink_op
= NULL
; /* can't change data link type */
1002 handle
->getnonblock_op
= pcap_getnonblock_fd
;
1003 handle
->setnonblock_op
= pcap_setnonblock_fd
;
1004 handle
->cleanup_op
= pcap_cleanup_acn
;
1005 handle
->read_op
= pcap_read_acn
;
1006 handle
->stats_op
= pcap_stats_acn
;
1008 fd
= acn_open_live(handle
->opt
.device
, handle
->errbuf
,
1014 * Turn a negative snapshot value (invalid), a snapshot value of
1015 * 0 (unspecified), or a value bigger than the normal maximum
1016 * value, into the maximum allowed value.
1018 * If some application really *needs* a bigger snapshot
1019 * length, we should just increase MAXIMUM_SNAPLEN.
1021 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
1022 handle
->snapshot
= MAXIMUM_SNAPLEN
;
1025 handle
->bufsize
= handle
->snapshot
;
1027 /* Allocate the buffer */
1029 handle
->buffer
= malloc(handle
->bufsize
+ handle
->offset
);
1030 if (!handle
->buffer
) {
1031 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1033 pcap_cleanup_acn(handle
);
1038 * "handle->fd" is a socket, so "select()" and "poll()"
1039 * should work on it.
1041 handle
->selectable_fd
= handle
->fd
;
1046 pcap_t
*pcap_create_interface(const char *device _U_
, char *ebuf
) {
1049 p
= pcap_create_common(ebuf
, 0);
1053 p
->activate_op
= pcap_activate_sita
;
1057 int pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
) {
1059 //printf("pcap_findalldevs()\n"); // fulko
1061 *alldevsp
= 0; /* initialize the returned variables before we do anything */
1063 if (acn_parse_hosts_file(errbuf
)) /* scan the hosts file for potential IOPs */
1065 //printf("pcap_findalldevs() returning BAD after parsehosts\n"); // fulko
1068 //printf("pcap_findalldevs() got hostlist now finding devs\n"); // fulko
1069 if (acn_findalldevs(errbuf
)) /* then ask the IOPs for their monitorable devices */
1071 //printf("pcap_findalldevs() returning BAD after findalldevs\n"); // fulko
1074 devlistp
->beginning
= acn_if_list
;
1075 acn_if_list
= 0; /* then forget our list head, because someone will call pcap_freealldevs() to empty the malloc'ed stuff */
1076 //printf("pcap_findalldevs() returning ZERO OK\n"); // fulko
1081 * Libpcap version string.
1084 pcap_lib_version(void)
1086 return PCAP_VERSION_STRING
" (SITA-only)";