Blame pcap-sita.c

Packit 209cc3
/*
Packit 209cc3
 *  pcap-sita.c: Packet capture interface additions for SITA ACN devices
Packit 209cc3
 *
Packit 209cc3
 *  Copyright (c) 2007 Fulko Hew, SITA INC Canada, Inc <fulko.hew@sita.aero>
Packit 209cc3
 *
Packit 209cc3
 *  License: BSD
Packit 209cc3
 *
Packit 209cc3
 *  Redistribution and use in source and binary forms, with or without
Packit 209cc3
 *  modification, are permitted provided that the following conditions
Packit 209cc3
 *  are met:
Packit 209cc3
 *
Packit 209cc3
 *  1. Redistributions of source code must retain the above copyright
Packit 209cc3
 *     notice, this list of conditions and the following disclaimer.
Packit 209cc3
 *  2. Redistributions in binary form must reproduce the above copyright
Packit 209cc3
 *     notice, this list of conditions and the following disclaimer in
Packit 209cc3
 *     the documentation and/or other materials provided with the
Packit 209cc3
 *     distribution.
Packit 209cc3
 *  3. The names of the authors may not be used to endorse or promote
Packit 209cc3
 *     products derived from this software without specific prior
Packit 209cc3
 *     written permission.
Packit 209cc3
 *
Packit 209cc3
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
Packit 209cc3
 *  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
Packit 209cc3
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit 209cc3
 */
Packit 209cc3
Packit 209cc3
#ifdef HAVE_CONFIG_H
Packit 209cc3
#include <config.h>
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
#include <stdio.h>
Packit 209cc3
#include <string.h>
Packit 209cc3
#include <stdlib.h>
Packit 209cc3
#include <unistd.h>
Packit 209cc3
#include <fcntl.h>
Packit 209cc3
#include <errno.h>
Packit 209cc3
#include <sys/time.h>
Packit 209cc3
#include <sys/socket.h>
Packit 209cc3
#include <netinet/in.h>
Packit 209cc3
#include <arpa/inet.h>
Packit 209cc3
#include "pcap-int.h"
Packit 209cc3
Packit 209cc3
#include "pcap-sita.h"
Packit 209cc3
Packit 209cc3
	/* non-configureable manifests follow */
Packit 209cc3
Packit 209cc3
#define IOP_SNIFFER_PORT	49152			/* TCP port on the IOP used for 'distributed pcap' usage */
Packit 209cc3
#define MAX_LINE_SIZE		255				/* max size of a buffer/line in /etc/hosts we allow */
Packit 209cc3
#define MAX_CHASSIS			8				/* number of chassis in an ACN site */
Packit 209cc3
#define MAX_GEOSLOT			8				/* max number of access units in an ACN site */
Packit 209cc3
Packit 209cc3
#define FIND			0
Packit 209cc3
#define LIVE			1
Packit 209cc3
Packit 209cc3
typedef struct iface {
Packit 209cc3
	struct iface	*next;		/* a pointer to the next interface */
Packit 209cc3
	char		*name;		/* this interface's name */
Packit 209cc3
	char		*IOPname;	/* this interface's name on an IOP */
Packit 209cc3
	uint32_t	iftype;		/* the type of interface (DLT values) */
Packit 209cc3
} iface_t;
Packit 209cc3
Packit 209cc3
typedef struct unit {
Packit 209cc3
	char			*ip;		/* this unit's IP address (as extracted from /etc/hosts) */
Packit 209cc3
	int			fd;		/* the connection to this unit (if it exists) */
Packit 209cc3
	int			find_fd;	/* a big kludge to avoid my programming limitations since I could have this unit open for findalldevs purposes */
Packit 209cc3
	int			first_time;	/* 0 = just opened via acn_open_live(),  ie. the first time, NZ = nth time */
Packit 209cc3
	struct sockaddr_in	*serv_addr;	/* the address control block for comms to this unit */
Packit 209cc3
	int			chassis;
Packit 209cc3
	int			geoslot;
Packit 209cc3
	iface_t			*iface;		/* a pointer to a linked list of interface structures */
Packit 209cc3
	char			*imsg;		/* a pointer to an inbound message */
Packit 209cc3
	int			len;		/* the current size of the inbound message */
Packit 209cc3
} unit_t;
Packit 209cc3
Packit 209cc3
static unit_t		units[MAX_CHASSIS+1][MAX_GEOSLOT+1];	/* we use indexes of 1 through 8, but we reserve/waste index 0 */
Packit 209cc3
static fd_set		readfds;				/* a place to store the file descriptors for the connections to the IOPs */
Packit 209cc3
static int		max_fs;
Packit 209cc3
Packit 209cc3
pcap_if_t		*acn_if_list;		/* pcap's list of available interfaces */
Packit 209cc3
Packit 209cc3
static void dump_interface_list(void) {
Packit 209cc3
	pcap_if_t		*iff;
Packit 209cc3
	pcap_addr_t		*addr;
Packit 209cc3
	int			longest_name_len = 0;
Packit 209cc3
	char			*n, *d, *f;
Packit 209cc3
	int			if_number = 0;
Packit 209cc3
Packit 209cc3
	iff = acn_if_list;
Packit 209cc3
	while (iff) {
Packit 209cc3
		if (iff->name && (strlen(iff->name) > longest_name_len)) longest_name_len = strlen(iff->name);
Packit 209cc3
		iff = iff->next;
Packit 209cc3
	}
Packit 209cc3
	iff = acn_if_list;
Packit 209cc3
	printf("Interface List:\n");
Packit 209cc3
	while (iff) {
Packit 209cc3
		n = (iff->name)							? iff->name			: "";
Packit 209cc3
		d = (iff->description)					? iff->description	: "";
Packit 209cc3
		f = (iff->flags == PCAP_IF_LOOPBACK)	? "L"				: "";
Packit 209cc3
		printf("%3d: %*s %s '%s'\n", if_number++, longest_name_len, n, f, d);
Packit 209cc3
		addr = iff->addresses;
Packit 209cc3
		while (addr) {
Packit 209cc3
			printf("%*s ", (5 + longest_name_len), "");		/* add some indentation */
Packit 209cc3
			printf("%15s  ", (addr->addr)		? inet_ntoa(((struct sockaddr_in *)addr->addr)->sin_addr)		: "");
Packit 209cc3
			printf("%15s  ", (addr->netmask)	? inet_ntoa(((struct sockaddr_in *)addr->netmask)->sin_addr)	: "");
Packit 209cc3
			printf("%15s  ", (addr->broadaddr)	? inet_ntoa(((struct sockaddr_in *)addr->broadaddr)->sin_addr)	: "");
Packit 209cc3
			printf("%15s  ", (addr->dstaddr)	? inet_ntoa(((struct sockaddr_in *)addr->dstaddr)->sin_addr)	: "");
Packit 209cc3
			printf("\n");
Packit 209cc3
			addr = addr->next;
Packit 209cc3
		}
Packit 209cc3
		iff = iff->next;
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void dump(unsigned char *ptr, int i, int indent) {
Packit 209cc3
	fprintf(stderr, "%*s", indent, " ");
Packit 209cc3
	for (; i > 0; i--) {
Packit 209cc3
		fprintf(stderr, "%2.2x ", *ptr++);
Packit 209cc3
	}
Packit 209cc3
	fprintf(stderr, "\n");
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void dump_interface_list_p(void) {
Packit 209cc3
	pcap_if_t		*iff;
Packit 209cc3
	pcap_addr_t		*addr;
Packit 209cc3
	int				if_number = 0;
Packit 209cc3
Packit 209cc3
	iff = acn_if_list;
Packit 209cc3
	printf("Interface Pointer @ %p is %p:\n", &acn_if_list, iff);
Packit 209cc3
	while (iff) {
Packit 209cc3
		printf("%3d: %p %p next: %p\n", if_number++, iff->name, iff->description, iff->next);
Packit 209cc3
		dump((unsigned char *)iff, sizeof(pcap_if_t), 5);
Packit 209cc3
		addr = iff->addresses;
Packit 209cc3
		while (addr) {
Packit 209cc3
			printf("          %p %p %p %p, next: %p\n", addr->addr, addr->netmask, addr->broadaddr, addr->dstaddr, addr->next);
Packit 209cc3
			dump((unsigned char *)addr, sizeof(pcap_addr_t), 10);
Packit 209cc3
			addr = addr->next;
Packit 209cc3
		}
Packit 209cc3
		iff = iff->next;
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void dump_unit_table(void) {
Packit 209cc3
	int		chassis, geoslot;
Packit 209cc3
	iface_t	*p;
Packit 209cc3
Packit 209cc3
	printf("%c:%c %s %s\n", 'C', 'S', "fd", "IP Address");
Packit 209cc3
	for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {
Packit 209cc3
		for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
Packit 209cc3
			if (units[chassis][geoslot].ip != NULL)
Packit 209cc3
				printf("%d:%d %2d %s\n", chassis, geoslot, units[chassis][geoslot].fd, units[chassis][geoslot].ip);
Packit 209cc3
			p = units[chassis][geoslot].iface;
Packit 209cc3
			while (p) {
Packit 209cc3
				char *n = (p->name)			? p->name			: "";
Packit 209cc3
				char *i = (p->IOPname)		? p->IOPname		: "";
Packit 209cc3
				p = p->next;
Packit 209cc3
				printf("   %12s    -> %12s\n", i, n);
Packit 209cc3
			}
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int find_unit_by_fd(int fd, int *chassis, int *geoslot, unit_t **unit_ptr) {
Packit 209cc3
	int		c, s;
Packit 209cc3
Packit 209cc3
	for (c = 0; c <= MAX_CHASSIS; c++) {
Packit 209cc3
		for (s = 0; s <= MAX_GEOSLOT; s++) {
Packit 209cc3
			if (units[c][s].fd == fd || units[c][s].find_fd == fd) {
Packit 209cc3
				if (chassis)	*chassis = c;
Packit 209cc3
				if (geoslot)	*geoslot = s;
Packit 209cc3
				if (unit_ptr)	*unit_ptr = &units[c][s];
Packit 209cc3
				return 1;
Packit 209cc3
			}
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int read_client_nbytes(int fd, int count, unsigned char *buf) {
Packit 209cc3
	unit_t			*u;
Packit 209cc3
	int				chassis, geoslot;
Packit 209cc3
	int				len;
Packit 209cc3
Packit 209cc3
	find_unit_by_fd(fd, &chassis, &geoslot, &u);
Packit 209cc3
	while (count) {
Packit 209cc3
		if ((len = recv(fd, buf, count, 0)) <= 0)	return -1;	/* read in whatever data was sent to us */
Packit 209cc3
		count -= len;
Packit 209cc3
		buf += len;
Packit 209cc3
	}															/* till we have everything we are looking for */
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void empty_unit_iface(unit_t *u) {
Packit 209cc3
	iface_t	*p, *cur;
Packit 209cc3
Packit 209cc3
	cur = u->iface;
Packit 209cc3
	while (cur) {											/* loop over all the interface entries */
Packit 209cc3
		if (cur->name)			free(cur->name);			/* throwing away the contents if they exist */
Packit 209cc3
		if (cur->IOPname)		free(cur->IOPname);
Packit 209cc3
		p = cur->next;
Packit 209cc3
		free(cur);											/* then throw away the structure itself */
Packit 209cc3
		cur = p;
Packit 209cc3
	}
Packit 209cc3
	u->iface = 0;											/* and finally remember that there are no remaining structure */
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void empty_unit(int chassis, int geoslot) {
Packit 209cc3
	unit_t	*u = &units[chassis][geoslot];
Packit 209cc3
Packit 209cc3
	empty_unit_iface(u);
Packit 209cc3
	if (u->imsg) {											/* then if an inbound message buffer exists */
Packit 209cc3
		void *bigger_buffer;
Packit 209cc3
Packit 209cc3
		bigger_buffer = (char *)realloc(u->imsg, 1);				/* and re-allocate the old large buffer into a new small one */
Packit 209cc3
		if (bigger_buffer == NULL) {	/* oops, realloc call failed */
Packit 209cc3
			fprintf(stderr, "Warning...call to realloc() failed, value of errno is %d\n", errno);
Packit 209cc3
			return;
Packit 209cc3
		}
Packit 209cc3
		u->imsg = bigger_buffer;
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void empty_unit_table(void) {
Packit 209cc3
	int		chassis, geoslot;
Packit 209cc3
Packit 209cc3
	for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {
Packit 209cc3
		for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
Packit 209cc3
			if (units[chassis][geoslot].ip != NULL) {
Packit 209cc3
				free(units[chassis][geoslot].ip);			/* get rid of the malloc'ed space that holds the IP address */
Packit 209cc3
				units[chassis][geoslot].ip = 0;				/* then set the pointer to NULL */
Packit 209cc3
			}
Packit 209cc3
			empty_unit(chassis, geoslot);
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static char *find_nth_interface_name(int n) {
Packit 209cc3
	int		chassis, geoslot;
Packit 209cc3
	iface_t	*p;
Packit 209cc3
	char	*last_name = 0;
Packit 209cc3
Packit 209cc3
	if (n < 0) n = 0;												/* ensure we are working with a valid number */
Packit 209cc3
	for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {			/* scan the table... */
Packit 209cc3
		for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
Packit 209cc3
			if (units[chassis][geoslot].ip != NULL) {
Packit 209cc3
				p = units[chassis][geoslot].iface;
Packit 209cc3
				while (p) {											/* and all interfaces... */
Packit 209cc3
					if (p->IOPname) last_name = p->name;			/* remembering the last name found */
Packit 209cc3
					if (n-- == 0) return last_name;					/* and if we hit the instance requested */
Packit 209cc3
					p = p->next;
Packit 209cc3
				}
Packit 209cc3
			}
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
											/* if we couldn't fine the selected entry */
Packit 209cc3
	if (last_name)	return last_name;		/* ... but we did have at least one entry... return the last entry found */
Packit 209cc3
	return "";								/* ... but if there wasn't any entry... return an empty string instead */
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
int acn_parse_hosts_file(char *errbuf) {				/* returns: -1 = error, 0 = OK */
Packit 209cc3
	FILE	*fp;
Packit 209cc3
	char	buf[MAX_LINE_SIZE];
Packit 209cc3
	char	*ptr, *ptr2;
Packit 209cc3
	int		pos;
Packit 209cc3
	int		chassis, geoslot;
Packit 209cc3
	unit_t	*u;
Packit 209cc3
Packit 209cc3
	empty_unit_table();
Packit 209cc3
	if ((fp = fopen("/etc/hosts", "r")) == NULL) {										/* try to open the hosts file and if it fails */
Packit 209cc3
		pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot open '/etc/hosts' for reading.");	/* return the nohostsfile error response */
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
	while (fgets(buf, MAX_LINE_SIZE-1, fp)) {			/* while looping over the file */
Packit 209cc3
Packit 209cc3
		pos = strcspn(buf, "#\n\r");					/* find the first comment character or EOL */
Packit 209cc3
		*(buf + pos) = '\0';							/* and clobber it and anything that follows it */
Packit 209cc3
Packit 209cc3
		pos = strspn(buf, " \t");						/* then find the first non-white space */
Packit 209cc3
		if (pos == strlen(buf))							/* if there is nothing but white space on the line */
Packit 209cc3
			continue;									/* ignore that empty line */
Packit 209cc3
		ptr = buf + pos;								/* and skip over any of that leading whitespace */
Packit 209cc3
Packit 209cc3
		if ((ptr2 = strstr(ptr, "_I_")) == NULL)		/* skip any lines that don't have names that look like they belong to IOPs */
Packit 209cc3
			continue;
Packit 209cc3
		if (*(ptr2 + 4) != '_')							/* and skip other lines that have names that don't look like ACN components */
Packit 209cc3
			continue;
Packit 209cc3
		*(ptr + strcspn(ptr, " \t")) = '\0';			/* null terminate the IP address so its a standalone string */
Packit 209cc3
Packit 209cc3
		chassis = *(ptr2 + 3) - '0';					/* extract the chassis number */
Packit 209cc3
		geoslot = *(ptr2 + 5) - '0';					/* and geo-slot number */
Packit 209cc3
		if (chassis < 1 || chassis > MAX_CHASSIS ||
Packit 209cc3
			geoslot < 1 || geoslot > MAX_GEOSLOT) {		/* if the chassis and/or slot numbers appear to be bad... */
Packit 209cc3
			pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Invalid ACN name in '/etc/hosts'.");	/* warn the user */
Packit 209cc3
			continue;																	/* and ignore the entry */
Packit 209cc3
		}
Packit 209cc3
		if ((ptr2 = (char *)malloc(strlen(ptr) + 1)) == NULL) {
Packit 209cc3
			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
Packit 209cc3
			    errno, "malloc");
Packit 209cc3
			continue;
Packit 209cc3
		}
Packit 209cc3
		strcpy(ptr2, ptr);								/* copy the IP address into our malloc'ed memory */
Packit 209cc3
		u = &units[chassis][geoslot];
Packit 209cc3
		u->ip = ptr2;									/* and remember the whole shebang */
Packit 209cc3
		u->chassis = chassis;
Packit 209cc3
		u->geoslot = geoslot;
Packit 209cc3
	}
Packit 209cc3
	fclose(fp);
Packit 209cc3
	if (*errbuf)	return -1;
Packit 209cc3
	else			return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int open_with_IOP(unit_t  *u, int flag) {
Packit 209cc3
	int					sockfd;
Packit 209cc3
	char				*ip;
Packit 209cc3
Packit 209cc3
	if (u->serv_addr == NULL) {
Packit 209cc3
		u->serv_addr = malloc(sizeof(struct sockaddr_in));
Packit 209cc3
Packit 209cc3
		/* since we called malloc(), lets check to see if we actually got the memory	*/
Packit 209cc3
		if (u->serv_addr == NULL) {	/* oops, we didn't get the memory requested	*/
Packit 209cc3
			fprintf(stderr, "malloc() request for u->serv_addr failed, value of errno is: %d\n", errno);
Packit 209cc3
			return 0;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
	}
Packit 209cc3
	ip = u->ip;
Packit 209cc3
	/* bzero() is deprecated, replaced with memset()	*/
Packit 209cc3
	memset((char *)u->serv_addr, 0, sizeof(struct sockaddr_in));
Packit 209cc3
	u->serv_addr->sin_family		= AF_INET;
Packit 209cc3
	u->serv_addr->sin_addr.s_addr	= inet_addr(ip);
Packit 209cc3
	u->serv_addr->sin_port			= htons(IOP_SNIFFER_PORT);
Packit 209cc3
Packit 209cc3
	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
Packit 209cc3
		fprintf(stderr, "pcap can't open a socket for connecting to IOP at %s\n", ip);
Packit 209cc3
		return 0;
Packit 209cc3
	}
Packit 209cc3
	if (connect(sockfd, (struct sockaddr *)u->serv_addr, sizeof(struct sockaddr_in)) < 0) {
Packit 209cc3
		fprintf(stderr, "pcap can't connect to IOP at %s\n", ip);
Packit 209cc3
		return 0;
Packit 209cc3
	}
Packit 209cc3
	if (flag == LIVE)	u->fd = sockfd;
Packit 209cc3
	else				u->find_fd = sockfd;
Packit 209cc3
	u->first_time = 0;
Packit 209cc3
	return sockfd;			/* return the non-zero file descriptor as a 'success' indicator */
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void close_with_IOP(int chassis, int geoslot, int flag) {
Packit 209cc3
	int		*id;
Packit 209cc3
Packit 209cc3
	if (flag == LIVE)	id = &units[chassis][geoslot].fd;
Packit 209cc3
	else				id = &units[chassis][geoslot].find_fd;
Packit 209cc3
Packit 209cc3
	if (*id) {										/* this was the last time, so... if we are connected... */
Packit 209cc3
		close(*id);									/* disconnect us */
Packit 209cc3
		*id = 0;									/* and forget that the descriptor exists because we are not open */
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void pcap_cleanup_acn(pcap_t *handle) {
Packit 209cc3
	int		chassis, geoslot;
Packit 209cc3
	unit_t	*u;
Packit 209cc3
Packit 209cc3
	if (find_unit_by_fd(handle->fd, &chassis, &geoslot, &u) == 0)
Packit 209cc3
		return;
Packit 209cc3
	close_with_IOP(chassis, geoslot, LIVE);
Packit 209cc3
	if (u)
Packit 209cc3
		u->first_time = 0;
Packit 209cc3
	pcap_cleanup_live_common(handle);
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void send_to_fd(int fd, int len, unsigned char *str) {
Packit 209cc3
	int		nwritten;
Packit 209cc3
	int		chassis, geoslot;
Packit 209cc3
Packit 209cc3
	while (len > 0) {
Packit 209cc3
		if ((nwritten = write(fd, str, len)) <= 0) {
Packit 209cc3
			find_unit_by_fd(fd, &chassis, &geoslot, NULL);
Packit 209cc3
			if (units[chassis][geoslot].fd == fd)			close_with_IOP(chassis, geoslot, LIVE);
Packit 209cc3
			else if (units[chassis][geoslot].find_fd == fd)	close_with_IOP(chassis, geoslot, FIND);
Packit 209cc3
			empty_unit(chassis, geoslot);
Packit 209cc3
			return;
Packit 209cc3
		}
Packit 209cc3
		len -= nwritten;
Packit 209cc3
		str += nwritten;
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void acn_freealldevs(void) {
Packit 209cc3
Packit 209cc3
	pcap_if_t	*iff, *next_iff;
Packit 209cc3
	pcap_addr_t	*addr, *next_addr;
Packit 209cc3
Packit 209cc3
	for (iff = acn_if_list; iff != NULL; iff = next_iff) {
Packit 209cc3
		next_iff = iff->next;
Packit 209cc3
		for (addr = iff->addresses; addr != NULL; addr = next_addr) {
Packit 209cc3
			next_addr = addr->next;
Packit 209cc3
			if (addr->addr)			free(addr->addr);
Packit 209cc3
			if (addr->netmask)		free(addr->netmask);
Packit 209cc3
			if (addr->broadaddr)	free(addr->broadaddr);
Packit 209cc3
			if (addr->dstaddr)		free(addr->dstaddr);
Packit 209cc3
			free(addr);
Packit 209cc3
		}
Packit 209cc3
		if (iff->name)			free(iff->name);
Packit 209cc3
		if (iff->description)	free(iff->description);
Packit 209cc3
		free(iff);
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void nonUnified_IOP_port_name(char *buf, size_t bufsize, const char *proto, unit_t *u) {
Packit 209cc3
Packit 209cc3
	pcap_snprintf(buf, bufsize, "%s_%d_%d", proto, u->chassis, u->geoslot);
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void unified_IOP_port_name(char *buf, size_t bufsize, const char *proto, unit_t *u, int IOPportnum) {
Packit 209cc3
	int			portnum;
Packit 209cc3
Packit 209cc3
	portnum = ((u->chassis - 1) * 64) + ((u->geoslot - 1) * 8) + IOPportnum + 1;
Packit 209cc3
	pcap_snprintf(buf, bufsize, "%s_%d", proto, portnum);
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 iftype) {
Packit 209cc3
	iface_t		*iface_ptr, *iface;
Packit 209cc3
	char		*name;
Packit 209cc3
	char		buf[32];
Packit 209cc3
	char		*proto;
Packit 209cc3
	char		*port;
Packit 209cc3
	int			IOPportnum = 0;
Packit 209cc3
Packit 209cc3
	iface = malloc(sizeof(iface_t));		/* get memory for a structure */
Packit 209cc3
	if (iface == NULL) {	/* oops, we didn't get the memory requested	*/
Packit 209cc3
		fprintf(stderr, "Error...couldn't allocate memory for interface structure...value of errno is: %d\n", errno);
Packit 209cc3
		return NULL;
Packit 209cc3
	}
Packit 209cc3
	memset((char *)iface, 0, sizeof(iface_t));	/* bzero is deprecated(), replaced with memset() */
Packit 209cc3
Packit 209cc3
	iface->iftype = iftype;					/* remember the interface type of this interface */
Packit 209cc3
Packit 209cc3
	name = malloc(strlen(IOPname) + 1);		/* get memory for the IOP's name */
Packit 209cc3
        if (name == NULL) {    /* oops, we didn't get the memory requested     */
Packit 209cc3
                fprintf(stderr, "Error...couldn't allocate memory for IOPname...value of errno is: %d\n", errno);
Packit 209cc3
                return NULL;
Packit 209cc3
        }
Packit 209cc3
Packit 209cc3
	strcpy(name, IOPname);					/* and copy it in */
Packit 209cc3
	iface->IOPname = name;					/* and stick it into the structure */
Packit 209cc3
Packit 209cc3
	if (strncmp(IOPname, "lo", 2) == 0) {
Packit 209cc3
		IOPportnum = atoi(&IOPname[2]);
Packit 209cc3
		switch (iftype) {
Packit 209cc3
			case DLT_EN10MB:
Packit 209cc3
				nonUnified_IOP_port_name(buf, sizeof buf, "lo", u);
Packit 209cc3
				break;
Packit 209cc3
			default:
Packit 209cc3
				unified_IOP_port_name(buf, sizeof buf, "???", u, IOPportnum);
Packit 209cc3
				break;
Packit 209cc3
		}
Packit 209cc3
	} else if (strncmp(IOPname, "eth", 3) == 0) {
Packit 209cc3
		IOPportnum = atoi(&IOPname[3]);
Packit 209cc3
		switch (iftype) {
Packit 209cc3
			case DLT_EN10MB:
Packit 209cc3
				nonUnified_IOP_port_name(buf, sizeof buf, "eth", u);
Packit 209cc3
				break;
Packit 209cc3
			default:
Packit 209cc3
				unified_IOP_port_name(buf, sizeof buf, "???", u, IOPportnum);
Packit 209cc3
				break;
Packit 209cc3
		}
Packit 209cc3
	} else if (strncmp(IOPname, "wan", 3) == 0) {
Packit 209cc3
		IOPportnum = atoi(&IOPname[3]);
Packit 209cc3
		switch (iftype) {
Packit 209cc3
			case DLT_SITA:
Packit 209cc3
				unified_IOP_port_name(buf, sizeof buf, "wan", u, IOPportnum);
Packit 209cc3
				break;
Packit 209cc3
			default:
Packit 209cc3
				unified_IOP_port_name(buf, sizeof buf, "???", u, IOPportnum);
Packit 209cc3
				break;
Packit 209cc3
		}
Packit 209cc3
	} else {
Packit 209cc3
		fprintf(stderr, "Error... invalid IOP name %s\n", IOPname);
Packit 209cc3
		return NULL;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	name = malloc(strlen(buf) + 1);			/* get memory for that name */
Packit 209cc3
        if (name == NULL) {    /* oops, we didn't get the memory requested     */
Packit 209cc3
                fprintf(stderr, "Error...couldn't allocate memory for IOP port name...value of errno is: %d\n", errno);
Packit 209cc3
                return NULL;
Packit 209cc3
        }
Packit 209cc3
Packit 209cc3
	strcpy(name, buf);						/* and copy it in */
Packit 209cc3
	iface->name = name;						/* and stick it into the structure */
Packit 209cc3
Packit 209cc3
	if (u->iface == 0) {					/* if this is the first name */
Packit 209cc3
		u->iface = iface;					/* stick this entry at the head of the list */
Packit 209cc3
	} else {
Packit 209cc3
		iface_ptr = u->iface;
Packit 209cc3
		while (iface_ptr->next) {			/* othewise scan the list */
Packit 209cc3
			iface_ptr = iface_ptr->next;	/* till we're at the last entry */
Packit 209cc3
		}
Packit 209cc3
		iface_ptr->next = iface;			/* then tack this entry on the end of the list */
Packit 209cc3
	}
Packit 209cc3
	return iface->name;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int if_sort(char *s1, char *s2) {
Packit 209cc3
	char	*s1_p2, *s2_p2;
Packit 209cc3
	char	str1[MAX_LINE_SIZE], str2[MAX_LINE_SIZE];
Packit 209cc3
	int		s1_p1_len, s2_p1_len;
Packit 209cc3
	int		retval;
Packit 209cc3
Packit 209cc3
	if ((s1_p2 = strchr(s1, '_'))) {	/* if an underscore is found... */
Packit 209cc3
		s1_p1_len = s1_p2 - s1;			/* the prefix length is the difference in pointers */
Packit 209cc3
		s1_p2++;						/* the suffix actually starts _after_ the underscore */
Packit 209cc3
	} else {							/* otherwise... */
Packit 209cc3
		s1_p1_len = strlen(s1);			/* the prefix length is the length of the string itself */
Packit 209cc3
		s1_p2 = 0;						/* and there is no suffix */
Packit 209cc3
	}
Packit 209cc3
	if ((s2_p2 = strchr(s2, '_'))) {	/* now do the same for the second string */
Packit 209cc3
		s2_p1_len = s2_p2 - s2;
Packit 209cc3
		s2_p2++;
Packit 209cc3
	} else {
Packit 209cc3
		s2_p1_len = strlen(s2);
Packit 209cc3
		s2_p2 = 0;
Packit 209cc3
	}
Packit 209cc3
	strncpy(str1, s1, (s1_p1_len > sizeof(str1)) ? s1_p1_len : sizeof(str1));   *(str1 + s1_p1_len) = 0;
Packit 209cc3
	strncpy(str2, s2, (s2_p1_len > sizeof(str2)) ? s2_p1_len : sizeof(str2));   *(str2 + s2_p1_len) = 0;
Packit 209cc3
	retval = strcmp(str1, str2);
Packit 209cc3
	if (retval != 0) return retval;		/* if they are not identical, then we can quit now and return the indication */
Packit 209cc3
	return strcmp(s1_p2, s2_p2);		/* otherwise we return the result of comparing the 2nd half of the string */
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void sort_if_table(void) {
Packit 209cc3
	pcap_if_t	*p1, *p2, *prev, *temp;
Packit 209cc3
	int			has_swapped;
Packit 209cc3
Packit 209cc3
	if (!acn_if_list) return;				/* nothing to do if the list is empty */
Packit 209cc3
Packit 209cc3
	while (1) {
Packit 209cc3
		p1 = acn_if_list;					/* start at the head of the list */
Packit 209cc3
		prev = 0;
Packit 209cc3
		has_swapped = 0;
Packit 209cc3
		while ((p2 = p1->next)) {
Packit 209cc3
			if (if_sort(p1->name, p2->name) > 0) {
Packit 209cc3
				if (prev) {					/* we are swapping things that are _not_ at the head of the list */
Packit 209cc3
					temp = p2->next;
Packit 209cc3
					prev->next = p2;
Packit 209cc3
					p2->next = p1;
Packit 209cc3
					p1->next = temp;
Packit 209cc3
				} else {					/* special treatment if we are swapping with the head of the list */
Packit 209cc3
					temp = p2->next;
Packit 209cc3
					acn_if_list= p2;
Packit 209cc3
					p2->next = p1;
Packit 209cc3
					p1->next = temp;
Packit 209cc3
				}
Packit 209cc3
				p1 = p2;
Packit 209cc3
				prev = p1;
Packit 209cc3
				has_swapped = 1;
Packit 209cc3
			}
Packit 209cc3
			prev = p1;
Packit 209cc3
			p1 = p1->next;
Packit 209cc3
		}
Packit 209cc3
		if (has_swapped == 0)
Packit 209cc3
			return;
Packit 209cc3
	}
Packit 209cc3
	return;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int process_client_data (char *errbuf) {								/* returns: -1 = error, 0 = OK */
Packit 209cc3
	int					chassis, geoslot;
Packit 209cc3
	unit_t				*u;
Packit 209cc3
	pcap_if_t			*iff, *prev_iff;
Packit 209cc3
	pcap_addr_t			*addr, *prev_addr;
Packit 209cc3
	char				*ptr;
Packit 209cc3
	int					address_count;
Packit 209cc3
	struct sockaddr_in	*s;
Packit 209cc3
	char				*newname;
Packit 209cc3
	bpf_u_int32				interfaceType;
Packit 209cc3
	unsigned char		flags;
Packit 209cc3
	void *bigger_buffer;
Packit 209cc3
Packit 209cc3
	prev_iff = 0;
Packit 209cc3
	for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {
Packit 209cc3
		for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {				/* now loop over all the devices */
Packit 209cc3
			u = &units[chassis][geoslot];
Packit 209cc3
			empty_unit_iface(u);
Packit 209cc3
			ptr = u->imsg;													/* point to the start of the msg for this IOP */
Packit 209cc3
			while (ptr < (u->imsg + u->len)) {
Packit 209cc3
				if ((iff = malloc(sizeof(pcap_if_t))) == NULL) {
Packit 209cc3
					pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
					    PCAP_ERRBUF_SIZE, errno, "malloc");
Packit 209cc3
					return -1;
Packit 209cc3
				}
Packit 209cc3
				memset((char *)iff, 0, sizeof(pcap_if_t)); /* bzero() is deprecated, replaced with memset() */
Packit 209cc3
				if (acn_if_list == 0)	acn_if_list = iff;					/* remember the head of the list */
Packit 209cc3
				if (prev_iff)			prev_iff->next = iff;				/* insert a forward link */
Packit 209cc3
Packit 209cc3
				if (*ptr) {													/* if there is a count for the name */
Packit 209cc3
					if ((iff->name = malloc(*ptr + 1)) == NULL) {			/* get that amount of space */
Packit 209cc3
						pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
						    PCAP_ERRBUF_SIZE, errno,
Packit 209cc3
						    "malloc");
Packit 209cc3
						return -1;
Packit 209cc3
					}
Packit 209cc3
					memcpy(iff->name, (ptr + 1), *ptr);						/* copy the name into the malloc'ed space */
Packit 209cc3
					*(iff->name + *ptr) = 0;								/* and null terminate the string */
Packit 209cc3
					ptr += *ptr;											/* now move the pointer forwards by the length of the count plus the length of the string */
Packit 209cc3
				}
Packit 209cc3
				ptr++;
Packit 209cc3
Packit 209cc3
				if (*ptr) {													/* if there is a count for the description */
Packit 209cc3
					if ((iff->description = malloc(*ptr + 1)) == NULL) {	/* get that amount of space */
Packit 209cc3
						pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
						    PCAP_ERRBUF_SIZE, errno,
Packit 209cc3
						    "malloc");
Packit 209cc3
						return -1;
Packit 209cc3
					}
Packit 209cc3
					memcpy(iff->description, (ptr + 1), *ptr);				/* copy the name into the malloc'ed space */
Packit 209cc3
					*(iff->description + *ptr) = 0;							/* and null terminate the string */
Packit 209cc3
					ptr += *ptr;											/* now move the pointer forwards by the length of the count plus the length of the string */
Packit 209cc3
				}
Packit 209cc3
				ptr++;
Packit 209cc3
Packit 209cc3
				interfaceType = ntohl(*(bpf_u_int32 *)ptr);
Packit 209cc3
				ptr += 4;													/* skip over the interface type */
Packit 209cc3
Packit 209cc3
				flags = *ptr++;
Packit 209cc3
				if (flags) iff->flags = PCAP_IF_LOOPBACK;					/* if this is a loopback style interface, lets mark it as such */
Packit 209cc3
Packit 209cc3
				address_count = *ptr++;
Packit 209cc3
Packit 209cc3
				prev_addr = 0;
Packit 209cc3
				while (address_count--) {
Packit 209cc3
					if ((addr = malloc(sizeof(pcap_addr_t))) == NULL) {
Packit 209cc3
						pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
						    PCAP_ERRBUF_SIZE, errno,
Packit 209cc3
						    "malloc");
Packit 209cc3
						return -1;
Packit 209cc3
					}
Packit 209cc3
 					memset((char *)addr, 0, sizeof(pcap_addr_t)); /* bzero() is deprecated, replaced with memset() */
Packit 209cc3
					if (iff->addresses == 0) iff->addresses = addr;
Packit 209cc3
					if (prev_addr) prev_addr->next = addr;							/* insert a forward link */
Packit 209cc3
					if (*ptr) {														/* if there is a count for the address */
Packit 209cc3
						if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) {		/* get that amount of space */
Packit 209cc3
							pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
							    PCAP_ERRBUF_SIZE,
Packit 209cc3
							    errno, "malloc");
Packit 209cc3
							return -1;
Packit 209cc3
						}
Packit 209cc3
						memset((char *)s, 0, sizeof(struct sockaddr_in)); /* bzero() is deprecated, replaced with memset() */
Packit 209cc3
						addr->addr = (struct sockaddr *)s;
Packit 209cc3
						s->sin_family		= AF_INET;
Packit 209cc3
						s->sin_addr.s_addr	= *(bpf_u_int32 *)(ptr + 1);			/* copy the address in */
Packit 209cc3
						ptr += *ptr;										/* now move the pointer forwards according to the specified length of the address */
Packit 209cc3
					}
Packit 209cc3
					ptr++;													/* then forwards one more for the 'length of the address' field */
Packit 209cc3
					if (*ptr) {												/* process any netmask */
Packit 209cc3
						if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) {
Packit 209cc3
							pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
							    PCAP_ERRBUF_SIZE,
Packit 209cc3
							    errno, "malloc");
Packit 209cc3
							return -1;
Packit 209cc3
						}
Packit 209cc3
						/* bzero() is deprecated, replaced with memset() */
Packit 209cc3
						memset((char *)s, 0, sizeof(struct sockaddr_in));
Packit 209cc3
Packit 209cc3
						addr->netmask = (struct sockaddr *)s;
Packit 209cc3
						s->sin_family		= AF_INET;
Packit 209cc3
						s->sin_addr.s_addr	= *(bpf_u_int32*)(ptr + 1);
Packit 209cc3
						ptr += *ptr;
Packit 209cc3
					}
Packit 209cc3
					ptr++;
Packit 209cc3
					if (*ptr) {												/* process any broadcast address */
Packit 209cc3
						if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) {
Packit 209cc3
							pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
							    PCAP_ERRBUF_SIZE,
Packit 209cc3
							    errno, "malloc");
Packit 209cc3
							return -1;
Packit 209cc3
						}
Packit 209cc3
						/* bzero() is deprecated, replaced with memset() */
Packit 209cc3
						memset((char *)s, 0, sizeof(struct sockaddr_in));
Packit 209cc3
Packit 209cc3
						addr->broadaddr = (struct sockaddr *)s;
Packit 209cc3
						s->sin_family		= AF_INET;
Packit 209cc3
						s->sin_addr.s_addr	= *(bpf_u_int32*)(ptr + 1);
Packit 209cc3
						ptr += *ptr;
Packit 209cc3
					}
Packit 209cc3
					ptr++;
Packit 209cc3
					if (*ptr) {												/* process any destination address */
Packit 209cc3
						if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) {
Packit 209cc3
							pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
							    PCAP_ERRBUF_SIZE,
Packit 209cc3
							    errno, "malloc");
Packit 209cc3
							return -1;
Packit 209cc3
						}
Packit 209cc3
						/* bzero() is deprecated, replaced with memset() */
Packit 209cc3
						memset((char *)s, 0, sizeof(struct sockaddr_in));
Packit 209cc3
Packit 209cc3
						addr->dstaddr = (struct sockaddr *)s;
Packit 209cc3
						s->sin_family		= AF_INET;
Packit 209cc3
						s->sin_addr.s_addr	= *(bpf_u_int32*)(ptr + 1);
Packit 209cc3
						ptr += *ptr;
Packit 209cc3
					}
Packit 209cc3
					ptr++;
Packit 209cc3
					prev_addr = addr;
Packit 209cc3
				}
Packit 209cc3
				prev_iff = iff;
Packit 209cc3
Packit 209cc3
				newname = translate_IOP_to_pcap_name(u, iff->name, interfaceType);		/* add a translation entry and get a point to the mangled name */
Packit 209cc3
				bigger_buffer = realloc(iff->name, strlen(newname) + 1));
Packit 209cc3
				if (bigger_buffer == NULL) {	/* we now re-write the name stored in the interface list */
Packit 209cc3
					pcap_fmt_errmsg_for_errno(errbuf,
Packit 209cc3
					    PCAP_ERRBUF_SIZE, errno, "realloc");
Packit 209cc3
					return -1;
Packit 209cc3
				}
Packit 209cc3
				iff->name = bigger_buffer;
Packit 209cc3
				strcpy(iff->name, newname);												/* to this new name */
Packit 209cc3
			}
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int read_client_data (int fd) {
Packit 209cc3
	unsigned char	buf[256];
Packit 209cc3
	int				chassis, geoslot;
Packit 209cc3
	unit_t			*u;
Packit 209cc3
	int				len;
Packit 209cc3
Packit 209cc3
	find_unit_by_fd(fd, &chassis, &geoslot, &u);
Packit 209cc3
Packit 209cc3
	if ((len = recv(fd, buf, sizeof(buf), 0)) <= 0)	return 0;	/* read in whatever data was sent to us */
Packit 209cc3
Packit 209cc3
	if ((u->imsg = realloc(u->imsg, (u->len + len))) == NULL)	/* extend the buffer for the new data */
Packit 209cc3
		return 0;
Packit 209cc3
	memcpy((u->imsg + u->len), buf, len);						/* append the new data */
Packit 209cc3
	u->len += len;
Packit 209cc3
	return 1;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void wait_for_all_answers(void) {
Packit 209cc3
	int		retval;
Packit 209cc3
	struct	timeval tv;
Packit 209cc3
	int		fd;
Packit 209cc3
	int		chassis, geoslot;
Packit 209cc3
Packit 209cc3
	tv.tv_sec = 2;
Packit 209cc3
	tv.tv_usec = 0;
Packit 209cc3
Packit 209cc3
	while (1) {
Packit 209cc3
		int flag = 0;
Packit 209cc3
		fd_set working_set;
Packit 209cc3
Packit 209cc3
		for (fd = 0; fd <= max_fs; fd++) {								/* scan the list of descriptors we may be listening to */
Packit 209cc3
			if (FD_ISSET(fd, &readfds)) flag = 1;						/* and see if there are any still set */
Packit 209cc3
		}
Packit 209cc3
		if (flag == 0) return;											/* we are done, when they are all gone */
Packit 209cc3
Packit 209cc3
		memcpy(&working_set, &readfds, sizeof(readfds));				/* otherwise, we still have to listen for more stuff, till we timeout */
Packit 209cc3
		retval = select(max_fs + 1, &working_set, NULL, NULL, &tv;;
Packit 209cc3
		if (retval == -1) {												/* an error occured !!!!! */
Packit 209cc3
			return;
Packit 209cc3
		} else if (retval == 0) {										/* timeout occured, so process what we've got sofar and return */
Packit 209cc3
			printf("timeout\n");
Packit 209cc3
			return;
Packit 209cc3
		} else {
Packit 209cc3
			for (fd = 0; fd <= max_fs; fd++) {							/* scan the list of things to do, and do them */
Packit 209cc3
				if (FD_ISSET(fd, &working_set)) {
Packit 209cc3
					if (read_client_data(fd) == 0) {					/* if the socket has closed */
Packit 209cc3
						FD_CLR(fd, &readfds);							/* and descriptors we listen to for errors */
Packit 209cc3
						find_unit_by_fd(fd, &chassis, &geoslot, NULL);
Packit 209cc3
						close_with_IOP(chassis, geoslot, FIND);			/* and close out connection to him */
Packit 209cc3
					}
Packit 209cc3
				}
Packit 209cc3
			}
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static char *get_error_response(int fd, char *errbuf) {		/* return a pointer on error, NULL on no error */
Packit 209cc3
	char	byte;
Packit 209cc3
	int		len = 0;
Packit 209cc3
Packit 209cc3
	while (1) {
Packit 209cc3
		recv(fd, &byte, 1, 0);							/* read another byte in */
Packit 209cc3
		if (errbuf && (len++ < PCAP_ERRBUF_SIZE)) {		/* and if there is still room in the buffer */
Packit 209cc3
			*errbuf++ = byte;							/* stick it in */
Packit 209cc3
			*errbuf = '\0';								/* ensure the string is null terminated just in case we might exceed the buffer's size */
Packit 209cc3
		}
Packit 209cc3
		if (byte == '\0') {
Packit 209cc3
			if (len > 1)	{ return errbuf;	}
Packit 209cc3
			else			{ return NULL;		}
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
int acn_findalldevs(char *errbuf) {								/* returns: -1 = error, 0 = OK */
Packit 209cc3
	int		chassis, geoslot;
Packit 209cc3
	unit_t	*u;
Packit 209cc3
Packit 209cc3
	FD_ZERO(&readfds);
Packit 209cc3
	max_fs = 0;
Packit 209cc3
	for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {
Packit 209cc3
		for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
Packit 209cc3
			u = &units[chassis][geoslot];
Packit 209cc3
			if (u->ip && (open_with_IOP(u, FIND))) {			/* connect to the remote IOP */
Packit 209cc3
				send_to_fd(u->find_fd, 1, (unsigned char *)"\0");
Packit 209cc3
				if (get_error_response(u->find_fd, errbuf))
Packit 209cc3
					close_with_IOP(chassis, geoslot, FIND);
Packit 209cc3
				else {
Packit 209cc3
					if (u->find_fd > max_fs)
Packit 209cc3
						max_fs = u->find_fd;								/* remember the highest number currently in use */
Packit 209cc3
					FD_SET(u->find_fd, &readfds);						/* we are going to want to read this guy's response to */
Packit 209cc3
					u->len = 0;
Packit 209cc3
					send_to_fd(u->find_fd, 1, (unsigned char *)"Q");		/* this interface query request */
Packit 209cc3
				}
Packit 209cc3
			}
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
	wait_for_all_answers();
Packit 209cc3
	if (process_client_data(errbuf))
Packit 209cc3
		return -1;
Packit 209cc3
	sort_if_table();
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int pcap_stats_acn(pcap_t *handle, struct pcap_stat *ps) {
Packit 209cc3
	unsigned char	buf[12];
Packit 209cc3
Packit 209cc3
	send_to_fd(handle->fd, 1, (unsigned char *)"S");						/* send the get_stats command to the IOP */
Packit 209cc3
Packit 209cc3
	if (read_client_nbytes(handle->fd, sizeof(buf), buf) == -1) return -1;	/* try reading the required bytes */
Packit 209cc3
Packit 209cc3
	ps->ps_recv		= ntohl(*(uint32_t *)&buf[0]);							/* break the buffer into its three 32 bit components */
Packit 209cc3
	ps->ps_drop		= ntohl(*(uint32_t *)&buf[4]);
Packit 209cc3
	ps->ps_ifdrop	= ntohl(*(uint32_t *)&buf[8]);
Packit 209cc3
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int acn_open_live(const char *name, char *errbuf, int *linktype) {		/* returns 0 on error, else returns the file descriptor */
Packit 209cc3
	int			chassis, geoslot;
Packit 209cc3
	unit_t		*u;
Packit 209cc3
	iface_t		*p;
Packit 209cc3
	pcap_if_list_t	devlist;
Packit 209cc3
Packit 209cc3
	pcap_platform_finddevs(&devlist, errbuf);
Packit 209cc3
	for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) {										/* scan the table... */
Packit 209cc3
		for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) {
Packit 209cc3
			u = &units[chassis][geoslot];
Packit 209cc3
			if (u->ip != NULL) {
Packit 209cc3
				p = u->iface;
Packit 209cc3
				while (p) {																		/* and all interfaces... */
Packit 209cc3
					if (p->IOPname && p->name && (strcmp(p->name, name) == 0)) {				/* and if we found the interface we want... */
Packit 209cc3
						*linktype = p->iftype;
Packit 209cc3
						open_with_IOP(u, LIVE);													/* start a connection with that IOP */
Packit 209cc3
						send_to_fd(u->fd, strlen(p->IOPname)+1, (unsigned char *)p->IOPname);	/* send the IOP's interface name, and a terminating null */
Packit 209cc3
						if (get_error_response(u->fd, errbuf)) {
Packit 209cc3
							return -1;
Packit 209cc3
						}
Packit 209cc3
						return u->fd;															/* and return that open descriptor */
Packit 209cc3
					}
Packit 209cc3
					p = p->next;
Packit 209cc3
				}
Packit 209cc3
			}
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
	return -1;																				/* if the interface wasn't found, return an error */
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static void acn_start_monitor(int fd, int snaplen, int timeout, int promiscuous, int direction) {
Packit 209cc3
	unsigned char	buf[8];
Packit 209cc3
	unit_t			*u;
Packit 209cc3
Packit 209cc3
	//printf("acn_start_monitor()\n");				// fulko
Packit 209cc3
	find_unit_by_fd(fd, NULL, NULL, &u);
Packit 209cc3
	if (u->first_time == 0) {
Packit 209cc3
		buf[0]					= 'M';
Packit 209cc3
		*(uint32_t *)&buf[1]	= htonl(snaplen);
Packit 209cc3
		buf[5]					= timeout;
Packit 209cc3
		buf[6]					= promiscuous;
Packit 209cc3
		buf[7]					= direction;
Packit 209cc3
	//printf("acn_start_monitor() first time\n");				// fulko
Packit 209cc3
		send_to_fd(fd, 8, buf);								/* send the start monitor command with its parameters to the IOP */
Packit 209cc3
		u->first_time = 1;
Packit 209cc3
	}
Packit 209cc3
	//printf("acn_start_monitor() complete\n");				// fulko
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int pcap_inject_acn(pcap_t *p, const void *buf _U_, size_t size _U_) {
Packit 209cc3
	pcap_strlcpy(p->errbuf, "Sending packets isn't supported on ACN adapters",
Packit 209cc3
	    PCAP_ERRBUF_SIZE);
Packit 209cc3
	return (-1);
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int pcap_setfilter_acn(pcap_t *handle, struct bpf_program *bpf) {
Packit 209cc3
	int				fd = handle->fd;
Packit 209cc3
	int				count;
Packit 209cc3
	struct bpf_insn	*p;
Packit 209cc3
	uint16_t		shortInt;
Packit 209cc3
	uint32_t		longInt;
Packit 209cc3
Packit 209cc3
	send_to_fd(fd, 1, (unsigned char *)"F");			/* BPF filter follows command */
Packit 209cc3
	count = bpf->bf_len;
Packit 209cc3
	longInt = htonl(count);
Packit 209cc3
	send_to_fd(fd, 4, (unsigned char *)&longInt);		/* send the instruction sequence count */
Packit 209cc3
	p = bpf->bf_insns;
Packit 209cc3
	while (count--) {									/* followed by the list of instructions */
Packit 209cc3
		shortInt = htons(p->code);
Packit 209cc3
		longInt = htonl(p->k);
Packit 209cc3
		send_to_fd(fd, 2, (unsigned char *)&shortInt);
Packit 209cc3
		send_to_fd(fd, 1, (unsigned char *)&p->jt);
Packit 209cc3
		send_to_fd(fd, 1, (unsigned char *)&p->jf);
Packit 209cc3
		send_to_fd(fd, 4, (unsigned char *)&longInt);
Packit 209cc3
		p++;
Packit 209cc3
	}
Packit 209cc3
	if (get_error_response(fd, NULL))
Packit 209cc3
		return -1;
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int pcap_setdirection_acn(pcap_t *handle, pcap_direction_t d) {
Packit 209cc3
	pcap_snprintf(handle->errbuf, sizeof(handle->errbuf),
Packit 209cc3
	    "Setting direction is not supported on ACN adapters");
Packit 209cc3
	return -1;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int acn_read_n_bytes_with_timeout(pcap_t *handle, int count) {
Packit 209cc3
	struct		timeval tv;
Packit 209cc3
	int			retval, fd;
Packit 209cc3
	fd_set		r_fds;
Packit 209cc3
	fd_set		w_fds;
Packit 209cc3
	u_char		*bp;
Packit 209cc3
	int			len = 0;
Packit 209cc3
	int			offset = 0;
Packit 209cc3
Packit 209cc3
	tv.tv_sec = 5;
Packit 209cc3
	tv.tv_usec = 0;
Packit 209cc3
Packit 209cc3
	fd = handle->fd;
Packit 209cc3
	FD_ZERO(&r_fds);
Packit 209cc3
	FD_SET(fd, &r_fds);
Packit 209cc3
	memcpy(&w_fds, &r_fds, sizeof(r_fds));
Packit 209cc3
	bp = handle->bp;
Packit 209cc3
	while (count) {
Packit 209cc3
		retval = select(fd + 1, &w_fds, NULL, NULL, &tv;;
Packit 209cc3
		if (retval == -1) {											/* an error occured !!!!! */
Packit 209cc3
//			fprintf(stderr, "error during packet data read\n");
Packit 209cc3
			return -1;												/* but we need to return a good indication to prevent unneccessary popups */
Packit 209cc3
		} else if (retval == 0) {									/* timeout occured, so process what we've got sofar and return */
Packit 209cc3
//			fprintf(stderr, "timeout during packet data read\n");
Packit 209cc3
			return -1;
Packit 209cc3
		} else {
Packit 209cc3
			if ((len = recv(fd, (bp + offset), count, 0)) <= 0) {
Packit 209cc3
//				fprintf(stderr, "premature exit during packet data rx\n");
Packit 209cc3
				return -1;
Packit 209cc3
			}
Packit 209cc3
			count -= len;
Packit 209cc3
			offset += len;
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int pcap_read_acn(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) {
Packit 209cc3
	#define HEADER_SIZE (4 * 4)
Packit 209cc3
	unsigned char		packet_header[HEADER_SIZE];
Packit 209cc3
	struct pcap_pkthdr	pcap_header;
Packit 209cc3
Packit 209cc3
	//printf("pcap_read_acn()\n");			// fulko
Packit 209cc3
	acn_start_monitor(handle->fd, handle->snapshot, handle->opt.timeout, handle->opt.promisc, handle->direction);	/* maybe tell him to start monitoring */
Packit 209cc3
	//printf("pcap_read_acn() after start monitor\n");			// fulko
Packit 209cc3
Packit 209cc3
	handle->bp = packet_header;
Packit 209cc3
	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 */
Packit 209cc3
Packit 209cc3
	pcap_header.ts.tv_sec	= ntohl(*(uint32_t *)&packet_header[0]);				/* tv_sec */
Packit 209cc3
	pcap_header.ts.tv_usec	= ntohl(*(uint32_t *)&packet_header[4]);				/* tv_usec */
Packit 209cc3
	pcap_header.caplen		= ntohl(*(uint32_t *)&packet_header[8]);				/* caplen */
Packit 209cc3
	pcap_header.len			= ntohl(*(uint32_t *)&packet_header[12]);				/* len */
Packit 209cc3
Packit 209cc3
	handle->bp = (u_char *)handle->buffer + handle->offset;									/* start off the receive pointer at the right spot */
Packit 209cc3
	if (acn_read_n_bytes_with_timeout(handle, pcap_header.caplen) == -1) return 0;	/* then try to read in the rest of the data */
Packit 209cc3
Packit 209cc3
	callback(user, &pcap_header, handle->bp);										/* call the user supplied callback function */
Packit 209cc3
	return 1;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
static int pcap_activate_sita(pcap_t *handle) {
Packit 209cc3
	int		fd;
Packit 209cc3
Packit 209cc3
	if (handle->opt.rfmon) {
Packit 209cc3
		/*
Packit 209cc3
		 * No monitor mode on SITA devices (they're not Wi-Fi
Packit 209cc3
		 * devices).
Packit 209cc3
		 */
Packit 209cc3
		return PCAP_ERROR_RFMON_NOTSUP;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	/* Initialize some components of the pcap structure. */
Packit 209cc3
Packit 209cc3
	handle->inject_op = pcap_inject_acn;
Packit 209cc3
	handle->setfilter_op = pcap_setfilter_acn;
Packit 209cc3
	handle->setdirection_op = pcap_setdirection_acn;
Packit 209cc3
	handle->set_datalink_op = NULL;	/* can't change data link type */
Packit 209cc3
	handle->getnonblock_op = pcap_getnonblock_fd;
Packit 209cc3
	handle->setnonblock_op = pcap_setnonblock_fd;
Packit 209cc3
	handle->cleanup_op = pcap_cleanup_acn;
Packit 209cc3
	handle->read_op = pcap_read_acn;
Packit 209cc3
	handle->stats_op = pcap_stats_acn;
Packit 209cc3
Packit 209cc3
	fd = acn_open_live(handle->opt.device, handle->errbuf,
Packit 209cc3
	    &handle->linktype);
Packit 209cc3
	if (fd == -1)
Packit 209cc3
		return PCAP_ERROR;
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * Turn a negative snapshot value (invalid), a snapshot value of
Packit 209cc3
	 * 0 (unspecified), or a value bigger than the normal maximum
Packit 209cc3
	 * value, into the maximum allowed value.
Packit 209cc3
	 *
Packit 209cc3
	 * If some application really *needs* a bigger snapshot
Packit 209cc3
	 * length, we should just increase MAXIMUM_SNAPLEN.
Packit 209cc3
	 */
Packit 209cc3
	if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
Packit 209cc3
		handle->snapshot = MAXIMUM_SNAPLEN;
Packit 209cc3
Packit 209cc3
	handle->fd = fd;
Packit 209cc3
	handle->bufsize = handle->snapshot;
Packit 209cc3
Packit 209cc3
	/* Allocate the buffer */
Packit 209cc3
Packit 209cc3
	handle->buffer	 = malloc(handle->bufsize + handle->offset);
Packit 209cc3
	if (!handle->buffer) {
Packit 209cc3
		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
Packit 209cc3
		    errno, "malloc");
Packit 209cc3
		pcap_cleanup_acn(handle);
Packit 209cc3
		return PCAP_ERROR;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * "handle->fd" is a socket, so "select()" and "poll()"
Packit 209cc3
	 * should work on it.
Packit 209cc3
	 */
Packit 209cc3
	handle->selectable_fd = handle->fd;
Packit 209cc3
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
pcap_t *pcap_create_interface(const char *device _U_, char *ebuf) {
Packit 209cc3
	pcap_t *p;
Packit 209cc3
Packit 209cc3
	p = pcap_create_common(ebuf, 0);
Packit 209cc3
	if (p == NULL)
Packit 209cc3
		return (NULL);
Packit 209cc3
Packit 209cc3
	p->activate_op = pcap_activate_sita;
Packit 209cc3
	return (p);
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
int pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) {
Packit 209cc3
Packit 209cc3
	//printf("pcap_findalldevs()\n");				// fulko
Packit 209cc3
Packit 209cc3
	*alldevsp = 0;												/* initialize the returned variables before we do anything */
Packit 209cc3
	strcpy(errbuf, "");
Packit 209cc3
	if (acn_parse_hosts_file(errbuf))							/* scan the hosts file for potential IOPs */
Packit 209cc3
		{
Packit 209cc3
		//printf("pcap_findalldevs() returning BAD after parsehosts\n");				// fulko
Packit 209cc3
		return -1;
Packit 209cc3
		}
Packit 209cc3
	//printf("pcap_findalldevs() got hostlist now finding devs\n");				// fulko
Packit 209cc3
	if (acn_findalldevs(errbuf))								/* then ask the IOPs for their monitorable devices */
Packit 209cc3
		{
Packit 209cc3
		//printf("pcap_findalldevs() returning BAD after findalldevs\n");				// fulko
Packit 209cc3
		return -1;
Packit 209cc3
		}
Packit 209cc3
	devlistp->beginning = acn_if_list;
Packit 209cc3
	acn_if_list = 0;											/* then forget our list head, because someone will call pcap_freealldevs() to empty the malloc'ed stuff */
Packit 209cc3
	//printf("pcap_findalldevs() returning ZERO OK\n");				// fulko
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * Libpcap version string.
Packit 209cc3
 */
Packit 209cc3
const char *
Packit 209cc3
pcap_lib_version(void)
Packit 209cc3
{
Packit 209cc3
	return PCAP_VERSION_STRING " (SITA-only)";
Packit 209cc3
}