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