1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
|
/* Copyright (C) 2003 Renaud Deraison
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <includes.h>
#undef DEBUG_FORWARD
#undef DEBUG
#undef DEBUG_HIGH
#define NUM_CLIENTS 128
#define NUM_BPF_PER_CLIENT 5
#define BPF_SOCKET_PATH NESSUS_STATE_DIR"/nessus/bpf"
#define BPF_SERVER_PID_FILE NESSUS_STATE_DIR"/nessus/bpf_server.pid"
/*
* The traditional pcap code is much more handy, so we'll try to use
* it instead if our hack is not necessary
*/
#ifdef HAVE_DEV_BPFN
#define CLNT_BUF_SIZ 1600
struct bpf_listener {
int soc;
char filter[512];
char iface[128] ;
int flag;
struct bpf_program bpf_filter;
};
struct bpf_pcap {
pcap_t * pcap;
char iface[128];
struct bpf_program bpf_filter;
struct bpf_pcap * next;
};
struct bpf_client {
int soc;
int datalink;
unsigned char *packet;
};
/* Server-side */
static struct bpf_listener clients[NUM_CLIENTS];
static struct bpf_pcap * pcaps;
/* Client-side */
static struct bpf_client clnts[NUM_BPF_PER_CLIENT];
static void setbufsize(int soc)
{
int optval = CLNT_BUF_SIZ * 40;
if(setsockopt(soc, SOL_SOCKET, SO_SNDBUF, &optval, sizeof(optval)) < 0)
perror("inc sndbuf");
if(setsockopt(soc, SOL_SOCKET, SO_RCVBUF, &optval, sizeof(optval)) < 0)
perror("inc sndbuf");
}
static void sigterm()
{
unlink(BPF_SOCKET_PATH);
_EXIT(0);
}
#ifdef DEBUG
void dump_packet(char * p, int len)
{
int i;
printf("\n----\n");
for(i=0;i<len;i+=16)
{
int j;
for(j=0;j<16;j++)
{
printf("%.2x ", (unsigned char)p[j+i]);
if(j %8 == 0)printf(" ");
}
printf("\n");
}
}
#endif
/*------------------------------------------------------------------------*
* Server part *
*------------------------------------------------------------------------*/
int bpf_svr_close(int);
static struct bpf_pcap * add_pcap(char * iface, pcap_t * pcap)
{
struct bpf_pcap * bpc;
bpf_u_int32 netmask, network;
if(pcap == NULL)
return pcaps;
bpc = malloc(sizeof(*bpc));
bpc->pcap = pcap;
strncpy(bpc->iface, iface, sizeof(bpc->iface) - 1);
bpc->iface[sizeof(bpc->iface) - 1] = '\0';
pcap_lookupnet(iface, &network, &netmask, 0);
pcap_compile(pcap, &bpc->bpf_filter, "", 1, netmask);
bpc->next = pcaps;
pcaps = bpc;
return bpc;
}
static void rm_pcap(char * iface )
{
struct bpf_pcap * bpc = pcaps, * prev = NULL;
while( bpc != NULL )
{
if(strcmp(bpc->iface, iface) == 0)
{
struct bpf_pcap * next;
next = bpc->next;
pcap_close(bpc->pcap);
efree(&bpc);
if ( prev ) prev->next = next;
else pcaps = next;
}
prev = bpc;
if ( bpc != NULL )
bpc = bpc->next;
}
}
static pcap_t * get_pcap(char * iface)
{
struct bpf_pcap * bpc = pcaps;
while( bpc != NULL )
{
if(strcmp(bpc->iface, iface) == 0)
return bpc->pcap;
bpc = bpc->next;
}
return NULL;
}
static pcap_t * new_pcap(char * iface)
{
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 netmask, network;
struct bpf_program filter_prog;
char filter[] = "ip";
pcap_t * pcap;
if(strcmp(iface, "lo") == 0 ||
strcmp(iface, "lo0") == 0)return NULL;
pcap = pcap_open_live(iface, 1500, 0, 1, errbuf);
if(pcap == NULL)
{
fprintf(stderr, "new_pcap(%s) failed - %s\n", iface, errbuf);
return NULL;
}
if(pcap_lookupnet(iface, &network, &netmask, 0) < 0)
{
printf("pcap_lookupnet failed\n");
pcap_close(pcap);
return NULL;
}
if(pcap_compile(pcap, &filter_prog, filter, 1, netmask) < 0)
{
pcap_perror(pcap, "pcap_compile");
pcap_close(pcap);
return NULL;
}
if(pcap_setfilter(pcap, &filter_prog) < 0)
{
pcap_perror(pcap, "pcap_setfilter\n");
pcap_close(pcap);
return NULL;
}
return pcap;
}
static pcap_t * bpf_add_pcap(char * iface)
{
pcap_t * ret = get_pcap(iface);
if(ret != NULL)
return ret;
else
add_pcap(iface, new_pcap(iface));
return get_pcap(iface);
}
static int bpf_recv_line(int soc, char * buf, int len)
{
int r = 0;
bzero(buf, len);
for(;r<len;)
{
int e;
again:
e = recv(soc, &(buf[r]), 1, 0);
if(e <= 0) {
if(e < 0 && errno == EINTR)goto again;
return e;
}
r ++;
if(buf[r-1] == '\n'){
return r;
}
}
return r;
}
static int process(char * iface, u_char * p, int len)
{
int i;
#ifdef DEBUG_HIGH
printf("bpf_share: process()\n");
#endif
for(i=0;i<NUM_CLIENTS;i++)
{
#ifdef DEBUG_HIGH
if(clients[i].soc != 0)
printf("%d) %d, %s %s %s\n", i, clients[i].soc, clients[i].iface, iface, clients[i].filter);
#endif
if(clients[i].soc > 0 && (strcmp(clients[i].iface, iface) == 0))
{
if(clients[i].filter[0] == '\0' || bpf_filter(clients[i].bpf_filter.bf_insns, p, len, len) != 0)
{
int e;
int n;
int lim;
#ifdef DEBUG_FORWARD
printf("Found packet that matches %s %d\n", clients[i].filter, i);
#endif
#ifdef DEBUG
printf("bpf_share: forward data to %d\n", i);
#endif
n = 0;
while( n != sizeof(len))
{
fd_set wr;
struct timeval tv;
again:
FD_ZERO(&wr);
FD_SET(clients[i].soc, &wr);
tv.tv_sec = tv.tv_usec = 0;
e = select(clients[i].soc + 1, NULL, &wr, NULL, &tv);
if(e <= 0)
{
if(e < 0 && errno == EINTR)goto again;
else break;
}
e = send(clients[i].soc, (char*)(&len)+n, sizeof(len)-n, 0);
if(e <= 0)
{
if(e < 0 && errno == EINTR)goto again;
if(errno != EPIPE)perror("bpf_share.c:process():send ");
bpf_svr_close(i);
goto endfor;
}
else n += e;
}
n = 0;
if(len > CLNT_BUF_SIZ)
lim = CLNT_BUF_SIZ;
else
lim = len;
while(n != lim)
{
fd_set wr;
struct timeval tv;
again2:
FD_ZERO(&wr);
FD_SET(clients[i].soc, &wr);
tv.tv_sec = tv.tv_usec = 0;
e = select(clients[i].soc + 1, NULL, &wr, NULL, &tv);
if(e <= 0)
{
if(e < 0 && errno == EINTR)goto again2;
else break;
}
e = send(clients[i].soc, (char*)p + n, lim - n, 0);
if(e <= 0)
{
if ( e < 0 && errno == EINTR )goto again2;
if(errno != EPIPE)perror("bpf_share.c:process():send ");
bpf_svr_close(i);
goto endfor;
}
else n+=e;
}
#ifdef DEBUG_FORWARD
printf("====>PACKET FORWARDED TO %d->%d\n", i, clients[i].soc);
#endif
endfor:
;
}
}
}
return 0;
}
static int mklistener()
{
struct sockaddr_un addr;
char name[] = BPF_SOCKET_PATH;
int soc;
int one = 1;
struct stat st;
if(stat(name, &st) == 0)
{
unlink(name);
}
soc = socket(AF_UNIX, SOCK_STREAM, 0);
if(soc < 0)
return -1;
bzero(&addr, sizeof(addr));
addr.sun_family = AF_UNIX;
bcopy(name, addr.sun_path, strlen(name));
setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if(bind(soc, (struct sockaddr*)(&addr), sizeof(addr)) == -1)
{
perror("bpf_share.c:mklistener():bind ");
}
chmod(name, 0700);
if(listen(soc, NUM_CLIENTS - 1) < 0)
perror("bpf_share.c:mklistener():listen ");
return soc;
}
static int add_client(int soc)
{
int i;
for(i=0;i<NUM_CLIENTS && clients[i].soc;i++);
if(clients[i].soc)
{
fprintf(stderr, "bpf_share: Too many clients already.\n");
close(soc);
return -1;
}
clients[i].soc = soc;
clients[i].filter[0] = '\0';
clients[i].iface[0] = '\0';
#ifdef DEBUG
printf("Added client at index %d (%d)\n", i, clients[i].soc);
#endif
return i;
}
static int read_clients()
{
fd_set rd;
int i;
char buf[512];
int max = -1;
struct timeval tv = {0, 0};
FD_ZERO(&rd);
for(i=0;i<NUM_CLIENTS;i++)
{
if(clients[i].soc > 0)
{
FD_SET(clients[i].soc, &rd);
if(clients[i].soc > max) max = clients[i].soc;
}
}
if(max == -1)
{
usleep(50000);
return 0;
}
if(select(max+1, &rd, NULL, NULL, &tv) > 0)
{
for(i=0;i<NUM_CLIENTS;i++)
{
if(clients[i].soc && FD_ISSET(clients[i].soc, &rd))
{
int n;
n = bpf_recv_line(clients[i].soc, buf, sizeof(buf));
#ifdef DEBUG
printf("Received %s\n", buf);
#endif
if(n <= 0)
{
#ifdef DEBUG_FORWARD
printf("Connection closed for %d\n", i);
#endif
/* connection was closed */
bpf_svr_close(i);
}
else
{
if(clients[i].iface[0] == '\0')
{
int dl;
int pcap_compile_failed = 0;
pcap_t * pcap;
bpf_u_int32 netmask, network;
if(buf[0] != '\0')buf[strlen(buf) - 1 ] = '\0';
clients[i].iface[sizeof(clients[i].iface) - 1] = '\0';
strncpy(clients[i].iface, buf, sizeof(clients[i].iface) - 1);
send(clients[i].soc, ".", 1, 0);
again:
pcap = bpf_add_pcap(clients[i].iface);
if(pcap != NULL)
{
dl = htonl(pcap_datalink(pcap));
send(clients[i].soc, &dl, sizeof(dl), 0);
n = bpf_recv_line(clients[i].soc, buf, sizeof(buf));
if(buf[0] != '\0')buf[strlen(buf) - 1 ] = '\0';
clients[i].filter[sizeof(clients[i].filter) - 1] = '\0';
strncpy(clients[i].filter, buf, sizeof(clients[i].filter) - 1);
#ifdef DEBUG
printf("FILTER = %s (%s) %d\n", buf, clients[i].filter, i);
#endif
pcap_lookupnet(clients[i].iface, &network, &netmask, 0);
/* pcap_restart(NULL); */
if ( pcap_compile(pcap, &clients[i].bpf_filter, buf, 1, netmask)
< 0 )
{
if ( pcap_compile_failed == 0 )
{
rm_pcap(clients[i].iface);
pcap_compile_failed++;
goto again;
}
else {
fprintf(stderr, "pcap_compile(%s) failed\n", buf);
send(clients[i].soc, "e", 1, 0);
}
}
else
clients[i].flag = 1;
}
else send(clients[i].soc, "e", 1, 0);
}
}
}
}
}
for(i=0;i<NUM_CLIENTS;i++)
{
if(clients[i].soc && clients[i].flag)
{
send(clients[i].soc, ",", 1, 0);
clients[i].flag = 0;
}
}
return 0;
}
static int add_clients(int soc)
{
fd_set rd;
struct timeval tv = {0,0};
unsigned int clnt;
FD_ZERO(&rd);
FD_SET(soc, &rd);
if(select(soc+1, &rd, &rd, &rd, &tv) > 0)
{
struct sockaddr_un soca;
unsigned int len = sizeof(soca);
clnt = accept(soc, (struct sockaddr*)&soca,&len);
if(clnt > 0)
{
#ifdef DEBUG_FORWARD
printf("New client!\n");
#endif
setbufsize(clnt);
add_client(clnt);
}
}
return 0;
}
static int pcaps_read()
{
struct bpf_pcap * bpc = pcaps;
if(bpc == NULL)usleep(50000);
while(bpc != NULL)
{
u_char * p;
struct pcap_pkthdr head;
int count = 0;
again:
p = (u_char*)pcap_next(bpc->pcap, &head);
if(p != NULL){
process(bpc->iface, p, head.caplen);
count ++;
if ( count < 30 ) goto again;
}
bpc = bpc->next;
}
return 0;
}
int bpf_server()
{
int i;
int lst;
int pid;
int fd = open(BPF_SERVER_PID_FILE, O_RDONLY);
if ( fd >= 0 )
{
char buf[256];
pid_t pid;
read(fd, buf, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = 0;
pid = atoi(buf);
close(fd);
if ( kill(pid, 0) == 0 ) return pid; /* Already running */
else unlink(BPF_SERVER_PID_FILE);
}
for(i=0;i<NUM_CLIENTS;i++)bzero(&clients[i], sizeof(clients[i]));
if((pid = fork()) == 0)
{
fd = open(BPF_SERVER_PID_FILE, O_CREAT|O_TRUNC|O_WRONLY, 0644);
if ( fd >= 0 )
{
char buf[256];
snprintf(buf, sizeof(buf), "%d", getpid());
write(fd, buf, strlen(buf));
close(fd);
}
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, sigterm);
setproctitle("bpf server");
lst = mklistener();
for(;;)
{
add_clients(lst);
read_clients();
pcaps_read();
}
}
return pid;
}
int bpf_svr_close(int n)
{
if(clients[n].soc) {
shutdown(clients[n].soc, 2);
close(clients[n].soc);
}
if(clients[n].bpf_filter.bf_insns != NULL)free(clients[n].bpf_filter.bf_insns);
bzero(&clients[n], sizeof(clients[n]));
return 0;
}
/*--------------------------------------------------------------------------*
* Client part *
*--------------------------------------------------------------------------*/
static int bpf_set_iface(int soc, char * iface)
{
char buf[2] = {0, 0};
char snd[256];
fd_set fds;
struct timeval tv;
int e;
snprintf(snd, sizeof(snd), "%s\n", iface);
again:
FD_ZERO(&fds);
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_SET(soc, &fds);
e = select(soc + 1, NULL, &fds, NULL, &tv);
if ( e <= 0 )
{
if(e < 0 && errno == EINTR)goto again;
else return -1;
}
e = send(soc, snd, strlen(snd), 0);
if ( e <= 0 )
{
if ( e < 0 && errno == EINTR )goto again;
else return -1;
}
recv_again:
FD_ZERO(&fds);
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_SET(soc, &fds);
e = select(soc + 1, &fds, NULL, NULL, &tv);
if ( e <= 0 )
{
if(e < 0 && errno == EINTR)goto recv_again;
else return -1;
}
e = recv(soc, buf, 1, 0);
if(e <= 0 )
{
if ( e < 0 && errno == EINTR ) goto recv_again;
else return -1;
}
if(buf[0] == '.')
{
int dl;
recv(soc, &dl, sizeof(dl), 0);
dl = ntohl(dl);
return dl;
}
else
return -1;
}
static int bpf_set_filter(int soc, char * filter)
{
char buf[2] = {0, 0};
char snd[1024];
struct timeval tv;
fd_set fds;
int e;
snprintf(snd, sizeof(snd), "%s\n", filter);
again:
FD_ZERO(&fds);
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_SET(soc, &fds);
e = select(soc + 1, NULL, &fds, NULL, &tv);
if(e <= 0)
{
if ( e < 0 && errno == EINTR )goto again;
else return -1;
}
e = send(soc, snd, strlen(snd), 0);
if ( e <= 0 )
{
if ( e < 0 && errno == EINTR ) goto again;
else return -1;
}
recv_again:
FD_ZERO(&fds);
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_SET(soc, &fds);
e = select(soc + 1, &fds, NULL, NULL, &tv);
if(e <= 0)
{
if ( e < 0 && errno == EINTR )goto recv_again;
else return -1;
}
e = recv(soc, buf, 1, 0);
if ( e <= 0 )
{
if ( e < 0 && errno == EINTR ) goto recv_again;
else return -1;
}
if(buf[0] == ',')
return 0;
else
return -1;
}
int bpf_open_live(char * iface, char * filter)
{
int soc;
struct sockaddr_un addr;
int len = sizeof(addr);
char name[] = BPF_SOCKET_PATH;
char errbuf[PCAP_ERRBUF_SIZE];
int i;
for(i=0;i<NUM_BPF_PER_CLIENT && clnts[i].soc != 0;i++);
if(clnts[i].soc != 0)
return -1;
if(iface == NULL)
iface = pcap_lookupdev(errbuf);
soc = socket(AF_UNIX, SOCK_STREAM, 0);
if(soc < 0)
{
perror("bpf_open_live():socket ");
return -1;
}
bzero(&addr, sizeof(addr));
addr.sun_family = AF_UNIX;
bcopy(name, addr.sun_path, strlen(name));
setbufsize(soc);
if(connect(soc, (struct sockaddr*)&addr, len) == -1 )
{
perror("bpf_open_live():connect ");
close(soc);
return -1;
}
clnts[i].datalink = bpf_set_iface(soc, iface);
if(clnts[i].datalink < 0 )
{
close(soc);
bzero(&clnts[i], sizeof(clnts[i]));
return -1;
}
if( bpf_set_filter(soc, filter) < 0)
{
close(soc);
bzero(&clnts[i], sizeof(clnts[i]));
return -1;
}
clnts[i].packet = emalloc(CLNT_BUF_SIZ);
clnts[i].soc = soc;
return i;
}
ExtFunc u_char* bpf_next_tv(int clnt, int * caplen, struct timeval * tv)
{
fd_set rd;
int soc;
int lim;
struct timeval tmp;
int e;
if(clnt < 0 )return NULL;
soc = clnts[clnt].soc;
if( soc <= 0 ){
fprintf(stderr, "[%d] bpf_next_tv() : called on a closed bpf !\n", getpid());
return NULL;
}
bzero(clnts[clnt].packet, CLNT_BUF_SIZ);
again:
errno = 0;
tmp = *tv;
FD_ZERO(&rd);
FD_SET(soc, &rd);
#ifdef DEBUG
printf("(%d) bpf_next\n", getpid());
#endif
e = select(soc + 1, &rd, NULL, NULL, &tmp);
if ( e < 0 && errno == EINTR)goto again;
if(e > 0)
{
int n = 0;
#ifdef DEBUG
printf("(%d) Select(%d->%d)\n", getpid(), clnt, soc);
#endif
while(n != sizeof(*caplen))
{
char * x = (char*)caplen;
int e;
recv_again:
e = recv(soc, x+n, sizeof(*caplen)-n, 0);
if( e <= 0 )
{
if(e < 0 && errno == EINTR)goto recv_again;
perror("bpf_next():recv ");
bpf_close(clnt);
return NULL;
}
else
n += e;
}
#ifdef DEBUG
printf("(%d) HEAD received (%d->%d)\n", getpid(), clnt, soc);
#endif
if( *caplen > CLNT_BUF_SIZ)
lim = CLNT_BUF_SIZ;
else
lim = *caplen;
n = 0;
while(n != lim)
{
int e;
recv_again2:
e = recv(soc, &(clnts[clnt].packet[n]), lim-n, 0);
if(e < 0 && errno == EINTR)goto recv_again2;
if(e <= 0 )
{
bpf_close(clnt);
return NULL;
}
else
n+=e;
}
#ifdef DEBUG
dump_packet(clnts[clnt].packet, lim);
#endif
return clnts[clnt].packet;
}
return NULL;
}
ExtFunc u_char* bpf_next(int clnt, int * caplen)
{
struct timeval tv = {0, 100000};
return bpf_next_tv(clnt, caplen, &tv);
}
int bpf_datalink(int bpf)
{
return clnts[bpf].datalink;
}
void bpf_close(int bpf)
{
if(clnts[bpf].soc){
shutdown(clnts[bpf].soc, 2);
close(clnts[bpf].soc);
}
efree(&clnts[bpf].packet);
bzero(&clnts[bpf], sizeof(clnts[bpf]));
}
#else
static pcap_t * pcaps[NUM_CLIENTS];
int bpf_open_live(char * iface, char * filter)
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t * ret;
bpf_u_int32 netmask, network;
struct bpf_program filter_prog;
int i;
for(i=0;i< NUM_CLIENTS && pcaps[i];i++);
if(pcaps[i])
{
printf("no free pcap\n");
return -1;
}
if(iface == NULL)
iface = pcap_lookupdev(errbuf);
ret = pcap_open_live(iface, 1500, 0, 1, errbuf);
if(ret == NULL)
{
printf("%s\n", errbuf);
return -1;
}
if(pcap_lookupnet(iface, &network, &netmask, 0) < 0)
{
printf("pcap_lookupnet failed\n");
pcap_close(ret);
return -1;
}
if(pcap_compile(ret, &filter_prog, filter, 1, netmask) < 0)
{
pcap_perror(ret, "pcap_compile");
pcap_close(ret);
return -1;
}
if(pcap_setfilter(ret, &filter_prog) < 0)
{
pcap_perror(ret, "pcap_setfilter\n");
pcap_close(ret);
return -1;
}
pcaps[i] = ret;
return i;
}
u_char* bpf_next_tv(int bpf, int * caplen, struct timeval * tv)
{
u_char * p = NULL;
struct pcap_pkthdr head;
struct timeval timeout, now;
timeout.tv_sec += tv->tv_sec;
timeout.tv_usec += tv->tv_usec;
while ( timeout.tv_usec >= 1000000 ) {
timeout.tv_sec ++;
timeout.tv_usec -= 1000000;
}
do {
p = (u_char*)pcap_next(pcaps[bpf], &head);
*caplen = head.caplen;
if ( p != NULL ) break;
gettimeofday(&now, NULL);
} while ( !((now.tv_sec > timeout.tv_sec) ||
(now.tv_sec == timeout.tv_sec && now.tv_usec >= timeout.tv_usec ) ));
return p;
}
u_char* bpf_next(int bpf, int * caplen)
{
struct timeval tv = {0, 100000};
return bpf_next_tv(bpf, caplen, &tv);
}
int bpf_datalink(int bpf)
{
return pcap_datalink(pcaps[bpf]);
}
void bpf_close(int bpf)
{
pcap_close(pcaps[bpf]);
pcaps[bpf] = NULL;
}
int bpf_server()
{
return 0;
}
#endif /* HAVE_DEV_BPFN */
#undef __STANDALONE__
#ifdef __STANDALONE__
/*
* This code tests our bpf sharer
*/
int main()
{
printf("Hello human\n");
bpf_server();
}
#endif
|