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