Changeset 1fc2f6a for lib/trace.c
- Timestamp:
- 08/01/05 15:40:37 (15 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:
- 9a36e6d
- Parents:
- 74c7660
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/trace.c
r74c7660 r1fc2f6a 42 42 #include "common.h" 43 43 #include "config.h" 44 #include "format.h"45 44 #include <assert.h> 46 45 #include <errno.h> … … 103 102 #include "libtrace.h" 104 103 #include "fifo.h" 104 #include "format.h" 105 105 106 106 #if HAVE_PCAP_BPF_H … … 141 141 //typedef enum {ERF, PCAP, PCAPINT, DAG, RTCLIENT, WAG, WAGINT } format_e_t; 142 142 143 typedef enum {RTSERVER, GZERF } output_t;143 //typedef enum {RTSERVER, GZERF } output_t; 144 144 #if HAVE_BPF 145 145 /** A type encapsulating a bpf filter … … 153 153 }; 154 154 #endif 155 156 struct libtrace_out_t {157 output_t outputformat;158 159 union {160 struct {161 char *hostname;162 short port;163 } rt;164 char *path;165 char *interface;166 } conn_info;167 168 union {169 int fd;170 #if HAVE_ZLIB171 gzFile *file;172 #else173 FILE *file;174 #endif175 #if HAVE_PCAP176 pcap_t *pcap;177 #endif178 } output;179 180 struct fifo_t *fifo;181 };182 183 184 155 185 156 struct format_t **format_list = 0; … … 280 251 char *scan = calloc(sizeof(char),URI_PROTO_LINE); 281 252 char *uridata = 0; 253 int i; 282 254 283 255 // parse the URI to determine what sort of event we are dealing with … … 296 268 strncpy(scan,uri, (uridata - uri)); 297 269 298 if (!strncasecmp(scan,"gzerf",5)) { 299 (*libtrace)->outputformat = GZERF; 300 } else if (!strncasecmp(scan, "rt", 2)) { 301 (*libtrace)->outputformat = RTSERVER; 302 } else { 303 return 0; 304 } 305 306 uridata ++; 307 switch((*libtrace)->outputformat) { 308 case GZERF: 309 /* 310 * Acceptable uridata takes the form: 311 * /path/to/file.gz 312 */ 313 (*libtrace)->conn_info.path = strdup(uridata); 314 break; 315 case RTSERVER: 316 /* 317 * Possible uridata formats: 318 * hostname 319 * hostname:port 320 */ 321 if (strlen(uridata) == 0) { 322 (*libtrace)->conn_info.rt.hostname = 323 strdup("localhost"); 324 (*libtrace)->conn_info.rt.port = 325 COLLECTOR_PORT; 270 (*libtrace)->format = 0; 271 for (i = 0; i < nformats; i++) { 272 if (strlen(scan) == strlen(format_list[i]->name) && 273 !strncasecmp(scan, 274 format_list[i]->name, 275 strlen(scan))) { 276 (*libtrace)->format=format_list[i]; 326 277 break; 327 } 328 if ((scan = strchr(uridata,':')) == NULL) { 329 (*libtrace)->conn_info.rt.hostname = 330 strdup(uridata); 331 (*libtrace)->conn_info.rt.port = 332 COLLECTOR_PORT; 333 } else { 334 (*libtrace)->conn_info.rt.hostname = 335 (char *)strndup(uridata,(scan - uridata)); 336 337 (*libtrace)->conn_info.rt.port = 338 atoi(++scan); 339 } 340 break; 341 default: 342 fprintf(stderr, "How did you get here??\n"); 343 } 278 } 279 } 280 if ((*libtrace)->format == 0) { 281 fprintf(stderr, 282 "libtrace has no support for this format (%s)\n",scan); 283 return 0; 284 } 285 286 // push uridata past the delimiter 287 uridata++; 288 (*libtrace)->conn_info.path = strdup(uridata); 289 290 // libtrace->format now contains the type of uri 291 // libtrace->uridata contains the appropriate data for this 292 293 if ((*libtrace)->format->init_output) { 294 (*libtrace)->format->init_output( (*libtrace)); 295 } else { 296 fprintf(stderr, 297 "No init_output function for format %s\n",scan); 298 return 0; 299 } 300 301 344 302 (*libtrace)->fifo = create_fifo(1048576); 345 303 assert( (*libtrace)->fifo); … … 399 357 struct libtrace_out_t *trace_output_create(char *uri) { 400 358 struct libtrace_out_t *libtrace = malloc(sizeof(struct libtrace_out_t)); 401 struct sockaddr_in remote, client;402 int client_fd, clilen;403 struct hostent *he;404 359 405 360 if (init_output(&libtrace, uri) == 0) 406 361 return 0; 407 362 408 switch(libtrace->outputformat) { 363 /* 364 * switch(libtrace->outputformat) { 409 365 case RTSERVER: 410 366 if ((he=gethostbyname(libtrace->conn_info.rt.hostname)) == NULL) { … … 440 396 441 397 case GZERF: 442 /* Catch undefined O_LARGEFILE on *BSD etc */443 398 #ifndef O_LARGEFILE 444 399 # define O_LARGEFILE 0 … … 462 417 exit(0); 463 418 } 419 */ 464 420 return libtrace; 465 421 } … … 484 440 void trace_output_destroy(struct libtrace_out_t *libtrace) { 485 441 assert(libtrace); 486 487 if (libtrace->outputformat == RTSERVER) { 488 close(libtrace->output.fd); 489 } 490 else { 491 #if HAVE_ZLIB 492 gzclose(libtrace->output.file); 493 #else 494 fclose(libtrace->output.file); 495 #endif 496 } 442 libtrace->format->fin_output(libtrace); 497 443 destroy_fifo(libtrace->fifo); 498 444 free(libtrace); … … 609 555 } 610 556 611 static int trace_write(struct libtrace_out_t *libtrace, void *buffer, size_t len) {612 int numbytes = 0;613 614 assert(libtrace);615 assert(len >= 0);616 617 if (buffer == NULL) {618 return 0;619 }620 621 while (1) {622 switch(libtrace->outputformat) {623 case RTSERVER:624 #ifndef MSG_NOSIGNAL625 #define MSG_NOSIGNAL 0626 #endif627 // Write to the network628 if ((numbytes = send(libtrace->output.fd,629 buffer,630 len,631 MSG_NOSIGNAL)) == -1) {632 if (errno == EINTR) {633 continue;634 }635 perror("send");636 return -1;637 }638 break;639 case GZERF:640 #if HAVE_ZLIB641 if ((numbytes = gzwrite(libtrace->output.file,642 buffer,643 len)) == -1) {644 perror("gzwrite");645 return -1;646 }647 break;648 #else649 // Do binary write instead650 if ((numbytes = fwrite(buffer, len, 1, libtrace->output.file)) == 0) {651 perror("fwrite");652 return -1;653 }654 break;655 #endif656 default:657 fprintf(stderr, "Bad output type\n");658 break;659 }660 break;661 }662 return numbytes;663 }664 665 557 /** Writes a packet to the specified output 666 558 * … … 672 564 * */ 673 565 int trace_write_packet(struct libtrace_out_t *libtrace, struct libtrace_packet_t *packet) { 674 // initialise stuff675 int numbytes, size;676 char buf[RP_BUFSIZE];677 int intsize = sizeof(int);678 void *buffer = &buf[intsize];679 int write_required = 0;680 681 566 assert(libtrace); 682 567 assert(packet); 683 568 684 if (libtrace->outputformat == GZERF) { 685 // do gzwrite 686 if ((numbytes = gzwrite(libtrace->output.file, packet->buffer, packet->size)) == 0) { 687 perror("gzwrite"); 688 return -1; 689 } 690 return numbytes; 691 } 692 693 // do fifo stuff for RT output instead 694 if (libtrace->outputformat == RTSERVER) { 695 do { 696 assert(libtrace->fifo); 697 698 if (fifo_out_available(libtrace->fifo) == 0 || write_required) { 699 // Packet added to fifo 700 if ((numbytes = fifo_write(libtrace->fifo, packet->buffer, packet->size)) == 0) { 701 // some error with the fifo 702 perror("fifo_write"); 703 return -1; 704 } 705 write_required = 0; 706 } 707 708 // Read from fifo and add protocol header 709 if ((numbytes = fifo_out_read(libtrace->fifo, buffer, sizeof(dag_record_t))) == 0) { 710 // failure reading in from fifo 711 fifo_out_reset(libtrace->fifo); 712 write_required = 1; 713 continue; 714 } 715 size = ntohs(((dag_record_t *)buffer)->rlen); 716 assert(size < LIBTRACE_PACKET_BUFSIZE); 717 718 if ((numbytes = fifo_out_read(libtrace->fifo, buffer, size)) == 0) { 719 // failure reading in from fifo 720 fifo_out_reset(libtrace->fifo); 721 write_required = 1; 722 continue; 723 } 724 fifo_out_update(libtrace->fifo, size); 725 // Sort out the protocol header 726 memcpy(buf, &packet->status, intsize); 727 728 729 // Send the buffer out on the wire 730 if ((numbytes = trace_write(libtrace, buf, size + sizeof(int))) <=0 ) { 731 return numbytes; 732 } 733 734 // Need an ack to come back 735 // TODO: Obviously this is a little unfinished 736 if ("ACK_ARRIVES") { 737 fifo_ack_update(libtrace->fifo, size); 738 return numbytes; 739 } else { 740 fifo_out_reset(libtrace->fifo); 741 } 742 } while(1); 743 } 744 745 // Unacceptable output format 746 fprintf(stderr, "Unknown Output format \n"); 747 assert(0); 569 if (libtrace->format->write_packet) { 570 return libtrace->format->write_packet(libtrace, packet); 571 } 572 748 573 } 749 574
Note: See TracChangeset
for help on using the changeset viewer.