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