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