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