Changeset 9580a74
- Timestamp:
- 11/12/04 10:07:29 (16 years ago)
- Branches:
- 4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
- Children:
- 1b2fd6f
- Parents:
- 9338143
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/trace.c
r35902e3 r9580a74 28 28 * 29 29 */ 30 30 31 31 32 /** @file … … 81 82 #include "fifo.h" 82 83 83 #if defHAVE_PCAP_BPF_H84 #if HAVE_PCAP_BPF_H 84 85 # include <pcap-bpf.h> 86 # ifndef HAVE_BPF 87 # define HAVE_BPF 1 88 # endif 85 89 #else 86 90 # ifdef HAVE_NET_BPF_H 87 91 # include <net/bpf.h> 92 # ifndef HAVE_BPF 93 # define HAVE_BPF 1 94 # endif 88 95 # endif 89 96 #endif 90 97 91 #include <zlib.h> 92 #include <pcap.h> 93 #include <zlib.h> 98 #if HAVE_PCAP_H 99 # include <pcap.h> 100 # ifndef HAVE_PCAP 101 # define HAVE_PCAP 1 102 # endif 103 #endif 104 105 #ifdef HAVE_ZLIB_H 106 # include <zlib.h> 107 # ifndef HAVE_ZLIB 108 # define HAVE_ZLIB 1 109 # endif 110 #endif 94 111 95 112 … … 99 116 # include "dagnew.h" 100 117 # include "dagapi.h" 101 # define DAGDEVICE 1 118 # ifndef DAGDEVICE 119 # define DAGDEVICE 1 120 # endif 102 121 #else 103 122 # include "dagformat.h" … … 127 146 union { 128 147 int fd; 148 #if HAVE_ZLIB 129 149 gzFile *file; 150 #else 151 FILE *file; 152 #endif 153 #if HAVE_PCAP 130 154 pcap_t *pcap; 155 #endif 131 156 } input; 132 157 struct fifo_t *fifo; … … 172 197 if (!strncasecmp(scan,"erf",3)) { 173 198 (*libtrace)->format=ERF; 199 #if HAVE_PCAP 174 200 } else if (!strncasecmp(scan,"pcapint",7)) { 175 201 (*libtrace)->format=PCAPINT; 176 202 } else if (!strncasecmp(scan,"pcap",4)) { 177 203 (*libtrace)->format=PCAP; 204 #else 205 } else if (!strncasecmp(scan,"pcap",4)) { // also catches pcapint 206 fprintf(stderr,"This version of libtrace has been compiled without PCAP support\n"); 207 return 0; 208 #endif 209 178 210 #ifdef DAGDEVICE 179 211 } else if (!strncasecmp(scan,"dag",3)) { … … 202 234 203 235 switch((*libtrace)->format) { 236 #if HAVE_PCAP 204 237 case PCAPINT: 238 #endif 205 239 case WAGINT: 206 240 /* Can have uridata of the following format … … 212 246 (*libtrace)->conn_info.path = strdup(uridata); 213 247 break; 248 #if HAVE_PCAP 214 249 case PCAP: 250 #endif 215 251 case ERF: 216 252 case WAG: … … 359 395 break; 360 396 case TRACE: 397 #if HAVE_PCAP 361 398 if (libtrace->format == PCAP) { 362 399 if ((libtrace->input.pcap = pcap_open_offline(libtrace->conn_info.path, errbuf)) == NULL) { … … 365 402 } 366 403 } else { 404 #else 405 { 406 #endif 407 #if HAVE_ZLIB 367 408 libtrace->input.file = gzopen(libtrace->conn_info.path, "r"); 368 } 409 #else 410 libtrace->input.file = fopen(libtrace->conn_info.path, "r"); 411 #endif 412 } 369 413 break; 370 414 case STDIN: 415 #if HAVE_PCAP 371 416 if (libtrace->format == PCAP) { 372 417 libtrace->input.pcap = pcap_open_offline("-",errbuf); 373 418 } else { 419 #else 420 { 421 #endif 422 #if HAVE_ZLIB 374 423 libtrace->input.file = gzdopen(STDIN, "r"); 424 #else 425 libtrace->input.file = fdopen(STDIN, "r"); 426 #endif 375 427 } 376 428 break; … … 396 448 case INTERFACE: 397 449 switch (libtrace->format) { 450 #if HAVE_PCAP 398 451 case PCAPINT: 399 452 case PCAP: … … 405 458 errbuf); 406 459 break; 407 default: 408 fprintf(stderr,"Unknown format trace, hoping I can just read\n"); 460 #endif 409 461 case WAGINT: 410 462 case WAG: … … 429 481 break; 430 482 #endif 483 default: 484 fprintf(stderr,"Unknown format trace, hoping I can just read\n"); 485 break; 431 486 432 487 } … … 445 500 void trace_destroy(struct libtrace_t *libtrace) { 446 501 assert(libtrace); 502 #if HAVE_PCAP 447 503 if (libtrace->format == PCAP || libtrace->format == PCAPINT) { 448 504 pcap_close(libtrace->input.pcap); 505 #else 506 if (0) { 507 #endif 449 508 } else if (libtrace->sourcetype == SOCKET || libtrace->sourcetype == RT) { 450 509 close(libtrace->input.fd); … … 454 513 #endif 455 514 } else { 515 #if HAVE_ZLIB 456 516 gzclose(libtrace->input.file); 517 #else 518 fclose(libtrace->input.file); 519 #endif 457 520 } 458 521 // need to free things! … … 518 581 break; 519 582 default: 583 #if HAVE_ZLIB 520 584 if ((numbytes=gzread(libtrace->input.file, 521 585 buffer, … … 524 588 return -1; 525 589 } 590 #else 591 if ((numbytes=fread(buffer,len,1,libtrace->input.file)) == 0 ) { 592 if(feof(libtrace->input.file)) { 593 return 0; 594 } 595 perror("fread"); 596 return -1; 597 } 598 #endif 526 599 } 527 600 break; … … 553 626 assert(libtrace); 554 627 assert(packet); 555 //if(packet->buffer == 0) {556 // packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE);557 //}558 559 //bzero(buffer,len);560 628 561 629 /* Store the trace we are reading from into the packet opaque … … 564 632 565 633 buffer = packet->buffer; 634 #if HAVE_PCAP 566 635 /* PCAP gives us it's own per-packet interface. Let's use it */ 567 636 if (libtrace->format == PCAP || libtrace->format == PCAPINT) { … … 576 645 return numbytes; 577 646 } 647 #endif 578 648 579 649 /* If we're reading from an ERF input, it's an offline trace. We can make some assumptions */ … … 734 804 dag_record_size + 2); 735 805 break; 806 #if HAVE_PCAP 736 807 case PCAPINT: 737 808 case PCAP: 738 809 ethptr = (struct ether_header *)(packet->buffer + sizeof(struct pcap_pkthdr)); 739 810 break; 811 #endif 740 812 case WAGINT: 741 813 case WAG: … … 930 1002 struct timeval trace_get_timeval(struct libtrace_packet_t *packet) { 931 1003 struct timeval tv; 1004 #if HAVE_PCAP 932 1005 struct pcap_pkthdr *pcapptr = 0; 1006 #endif 933 1007 uint64_t ts; 934 1008 //uint32_t seconds; 935 1009 switch (packet->trace->format) { 1010 #if HAVE_PCAP 936 1011 case PCAPINT: 937 1012 case PCAP: … … 939 1014 tv = pcapptr->ts; 940 1015 break; 1016 #endif 941 1017 case WAGINT: 942 1018 case WAG: … … 986 1062 int trace_get_capture_length(struct libtrace_packet_t *packet) { 987 1063 dag_record_t *erfptr = 0; 1064 #if HAVE_PCAP 988 1065 struct pcap_pkthdr *pcapptr = 0; 1066 #endif 989 1067 struct wag_event_t *wag_event; 990 1068 switch (packet->trace->format) { … … 994 1072 erfptr = (dag_record_t *)packet->buffer; 995 1073 return ntohs(erfptr->rlen); 1074 #if HAVE_PCAP 996 1075 case PCAPINT: 997 1076 case PCAP: … … 999 1078 //return ntohs(pcapptr->caplen); 1000 1079 return pcapptr->caplen; 1080 #endif 1001 1081 case WAGINT: 1002 1082 case WAG: … … 1029 1109 int trace_get_wire_length(struct libtrace_packet_t *packet){ 1030 1110 dag_record_t *erfptr = 0; 1111 #if HAVE_PCAP 1031 1112 struct pcap_pkthdr *pcapptr = 0; 1113 #endif 1032 1114 struct wag_event_t *wag_event = 0; 1033 1115 switch (packet->trace->format) { … … 1038 1120 return ntohs(erfptr->wlen); 1039 1121 break; 1122 #if HAVE_PCAP 1040 1123 case PCAPINT: 1041 1124 case PCAP: … … 1043 1126 return ntohs(pcapptr->len); 1044 1127 break; 1128 #endif 1045 1129 case WAGINT: 1046 1130 case WAG: … … 1067 1151 libtrace_linktype_t trace_get_link_type(struct libtrace_packet_t *packet ) { 1068 1152 dag_record_t *erfptr = 0; 1153 #if HAVE_PCAP 1069 1154 struct pcap_pkthdr *pcapptr = 0; 1155 #endif 1070 1156 int linktype = 0; 1071 1157 switch (packet->trace->format) { … … 1082 1168 1083 1169 break; 1170 #if HAVE_PCAP 1084 1171 case PCAPINT: 1085 1172 case PCAP: … … 1095 1182 } 1096 1183 break; 1184 #endif 1097 1185 case WAGINT: 1098 1186 case WAG: … … 1181 1269 /* Is there a packet ready? */ 1182 1270 switch (trace->sourcetype) { 1271 #if HAVE_PCAP 1183 1272 case INTERFACE: 1184 1273 { … … 1196 1285 return event; 1197 1286 } 1287 #endif 1198 1288 case SOCKET: 1199 1289 case DEVICE: … … 1257 1347 */ 1258 1348 struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { 1349 #ifdef HAVE_BPF_H 1259 1350 struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); 1260 1351 filter->filterstring = strdup(filterstring); 1261 1352 filter->filter = 0; 1262 1353 return filter; 1354 #else 1355 fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); 1356 return 0; 1357 #endif 1263 1358 } 1264 1359 … … 1273 1368 int trace_bpf_filter(struct libtrace_filter_t *filter, 1274 1369 struct libtrace_packet_t *packet) { 1275 1370 #ifdef HAVE_BPF_H 1276 1371 void *linkptr = 0; 1277 1372 int clen = 0; … … 1308 1403 assert(filter->filter); 1309 1404 return bpf_filter(filter->filter, linkptr, clen, clen); 1310 1405 #else 1406 fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); 1407 return 0; 1408 #endif 1311 1409 } 1312 1410 … … 1495 1593 size_t trace_truncate_packet(struct libtrace_packet_t *packet, size_t size) { 1496 1594 dag_record_t *erfptr; 1595 #if HAVE_PCAP 1497 1596 struct pcap_pkthdr *pcaphdr; 1597 #endif 1498 1598 1499 1599 assert(packet); … … 1504 1604 } 1505 1605 switch (packet->trace->format) { 1606 #if HAVE_PCAP 1506 1607 case PCAPINT: 1507 1608 case PCAP: … … 1510 1611 packet->size = pcaphdr->caplen; 1511 1612 break; 1613 #endif 1512 1614 case ERF: 1513 1615 case DAG:
Note: See TracChangeset
for help on using the changeset viewer.