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