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