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