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