Changeset 880aa58
- Timestamp:
- 02/16/06 15:44:06 (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:
- f03fc17
- Parents:
- 1fbd938
- Location:
- lib
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/fifo.c
r7068467 r880aa58 33 33 #include <errno.h> 34 34 #include <stdlib.h> /* free */ 35 #include <stdio.h> /* perror,sprintf, printf */35 #include <stdio.h> /* sprintf, printf */ 36 36 #include <assert.h> /* assert */ 37 37 #include <string.h> /* bzero */ … … 85 85 86 86 if ((fifo->base = malloc(fifo->length)) == 0) { 87 perror("malloc");87 libtrace_set_err(errno,"malloc failed"); 88 88 return 0; 89 89 } -
lib/format_erf.c
r1fbd938 r880aa58 163 163 CONNINFO.path = libtrace->uridata; 164 164 if (stat(CONNINFO.path,&buf) == -1) { 165 perror("stat");165 trace_set_err(errno,"stat(%s)",CONNINFO.path); 166 166 return 0; 167 167 } … … 170 170 libtrace->sourcetype = TRACE_SOURCE_DEVICE; 171 171 if((INPUT.fd = dag_open(CONNINFO.path)) < 0) { 172 fprintf(stderr,"Cannot open DAG %s: %m\n",173 CONNINFO.path ,errno);174 exit(0);172 trace_set_err(errno,"Cannot open DAG %s", 173 CONNINFO.path); 174 return 0; 175 175 } 176 176 if((DAG.buf = (void *)dag_mmap(INPUT.fd)) == MAP_FAILED) { 177 fprintf(stderr,"Cannot mmap DAG %s: %m\n",178 CONNINFO.path ,errno);179 exit(0);177 trace_set_err(errno,"Cannot mmap DAG %s", 178 CONNINFO.path); 179 return 0; 180 180 } 181 181 if(dag_start(INPUT.fd) < 0) { 182 fprintf(stderr,"Cannot start DAG %s: %m\n",183 CONNINFO.path ,errno);184 exit(0);182 trace_set_err(errno,"Cannot start DAG %s", 183 CONNINFO.path); 184 return 0; 185 185 } 186 186 } else { 187 fprintf(stderr,"%s isn't a valid char device, exiting\n",187 trace_set_err(errno,"Not a valid dag device: %s", 188 188 CONNINFO.path); 189 189 return 0; … … 221 221 libtrace->sourcetype = TRACE_SOURCE_STDIN; 222 222 INPUT.file = LIBTRACE_FDOPEN(fileno(stdin), "r"); 223 224 225 223 } else { 226 224 if (stat(CONNINFO.path,&buf) == -1 ) { 227 perror("stat"); 225 trace_set_err(errno,"stat(%s)", 226 CONNINFO.path); 228 227 return 0; 229 228 } … … 232 231 if ((INPUT.fd = socket( 233 232 AF_UNIX, SOCK_STREAM, 0)) == -1) { 234 perror("socket"); 233 trace_set_err(errno, 234 "socket(AF_UNIX,SOCK_STREAM)"); 235 235 return 0; 236 236 } … … 244 244 (struct sockaddr *)&unix_sock, 245 245 sizeof(struct sockaddr)) == -1) { 246 perror("connect (unix)"); 246 trace_set_err(errno, 247 "connect(%s)", CONNINFO.path); 247 248 return 0; 248 249 } 249 250 } else { 251 int fd; 250 252 251 253 libtrace->sourcetype = TRACE_SOURCE_TRACE; … … 256 258 * letting you do this... 257 259 */ 260 fd=open(CONNINFO.path,O_LARGEFILE); 261 if (fd==-1) { 262 trace_set_err(errno,"Unable to open %s", 263 CONNINFO.path); 264 } 258 265 INPUT.file = LIBTRACE_FDOPEN(open( 259 266 CONNINFO.path, … … 295 302 296 303 if ((he=gethostbyname(CONNINFO.rt.hostname)) == NULL) { 297 perror("gethostbyname"); 304 trace_set_err(errno,"failed to resolve %s", 305 CONNINFO.rt.hostname); 298 306 return 0; 299 307 } 300 308 if ((INPUT.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 301 perror("socket");309 trace_set_err(errno,"socket(AF_INET,SOCK_STREAM)"); 302 310 return 0; 303 311 } … … 310 318 if (connect(INPUT.fd, (struct sockaddr *)&remote, 311 319 sizeof(struct sockaddr)) == -1) { 312 perror("connect (inet)"); 320 trace_set_err(errno,"connect(%s)", 321 CONNINFO.rt.hostname); 313 322 return 0; 314 323 } … … 347 356 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 348 357 if (fd <= 0) { 358 trace_set_err(errno,"OPEN(%s,\"w\")", 359 libtrace->uridata); 349 360 return 0; 350 361 } … … 367 378 default: 368 379 /* Unknown option */ 380 trace_set_err(TRACE_ERR_UNKNOWN_OPTION, 381 "Unknown option"); 369 382 return -1; 370 383 } … … 485 498 packet->buffer, 486 499 dag_record_size)) == -1) { 487 perror("libtrace_read"); 500 trace_set_err(errno,"read(%s)", 501 CONNINFO.path); 488 502 return -1; 489 503 } … … 497 511 assert(size < LIBTRACE_PACKET_BUFSIZE); 498 512 499 /* If your trace is legacy, or corrupt, then this assert may fire. */500 /* turns out some older traces have fixed snaplens, which are padded501 * with 00's if the packet is smaller, so this doesn't work. Sigh.502 assert(ntohs(((dag_record_t *)buffer)->rlen) <=503 ntohs(((dag_record_t*)buffer)->wlen)+erf_get_framing_length(packet));504 */505 513 /* Unknown/corrupt */ 506 514 assert(((dag_record_t *)packet->buffer)->type < 10); … … 510 518 buffer2, 511 519 size)) != size) { 512 perror("libtrace_read");520 trace_set_err(errno, "read(%s)", CONNINFO.path); 513 521 return -1; 514 522 } … … 541 549 continue; 542 550 } 543 perror("recv"); 551 trace_set_err(errno,"recv(%s)", 552 libtrace->uridata); 544 553 return -1; 545 554 } … … 594 603 tracefifo_out_update(libtrace->fifo, sizeof(uint32_t)); 595 604 596 /*597 // read in the ERF header598 if ((numbytes = tracefifo_out_read(libtrace->fifo, buffer,599 dag_record_size)) == 0) {600 tracefifo_out_reset(libtrace->fifo);601 read_required = 1;602 continue;603 }604 */605 605 if (status.type == RT_MSG) { 606 606 /* Need to skip this packet as it is a message packet */ … … 636 636 } 637 637 638 static int erf_dump_packet(struct libtrace_out_t *libtrace, dag_record_t *erfptr, int pad, void *buffer, size_t size) { 638 static int erf_dump_packet(struct libtrace_out_t *libtrace, 639 dag_record_t *erfptr, int pad, void *buffer, size_t size) { 639 640 int numbytes = 0; 640 641 if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, erfptr, dag_record_size + pad)) == 0) { 641 perror("libtrace_write");642 trace_set_err(errno,"write(%s)",CONNINFO.path); 642 643 return -1; 643 644 } 644 645 645 646 if (buffer) { 646 if ((numbytes =LIBTRACE_WRITE(OUTPUT.file, buffer, size)) == 0) {647 perror("libtrace_write");647 if ((numbytes=LIBTRACE_WRITE(OUTPUT.file, buffer, size)) == 0) { 648 trace_set_err(errno,"write(%s)",CONNINFO.path); 648 649 return -1; 649 650 } … … 665 666 if (!OUTPUT.file) { 666 667 if (erf_open_output(libtrace) <= 0) { 667 perror("erf_open_output");668 trace_set_err(errno,"open(%s)",libtrace->uridata); 668 669 return -1; 669 670 } -
lib/format_helper.c
r6342c18 r880aa58 55 55 } 56 56 if (ioctl(event.fd,FIONREAD,&data)==-1) { 57 perror("ioctl(FIONREAD)"); 57 event.type = TRACE_EVENT_TERMINATE; 58 return event; 58 59 } 59 60 if (data>0) { … … 82 83 trace->event.packet.size); 83 84 } else { 84 // return here, the test for 85 // event.size will sort out the error 85 /* return here, the test for 86 * event.size will sort out the error 87 */ 86 88 event.type = TRACE_EVENT_TERMINATE; 87 89 return event; … … 91 93 ts=trace_get_seconds(packet); 92 94 if (trace->event.tdelta!=0) { 93 / / Get the adjusted current time95 /* Get the adjusted current time */ 94 96 gettimeofday(&stv, NULL); 95 97 now = stv.tv_sec + 96 98 ((double)stv.tv_usec / 1000000.0); 97 / / adjust for trace delta99 /* adjust for trace delta */ 98 100 now -= trace->event.tdelta; 99 101 100 / /if the trace timestamp is still in the102 /*if the trace timestamp is still in the 101 103 //future, return a SLEEP event, 102 104 //otherwise fire the packet 105 */ 103 106 if (ts > now) { 104 107 event.seconds = ts - … … 109 112 } else { 110 113 gettimeofday(&stv, NULL); 111 / /work out the difference between the114 /* work out the difference between the 112 115 // start of trace replay, and the first 113 116 // packet in the trace 117 */ 114 118 trace->event.tdelta = stv.tv_sec + 115 119 ((double)stv.tv_usec / 1000000.0); … … 117 121 } 118 122 119 / / This is the first packet, so just fire away.123 /* This is the first packet, so just fire away. */ 120 124 packet->size = trace->event.packet.size; 121 125 memcpy(packet->buffer, -
lib/format_legacy.c
r1fbd938 r880aa58 95 95 gzFile *file; 96 96 #else 97 / /FILE *file;97 /*FILE *file; */ 98 98 int file; 99 99 #endif … … 135 135 CONNINFO.path = libtrace->uridata; 136 136 if (!strncmp(CONNINFO.path,"-",1)) { 137 / / STDIN137 /* STDIN */ 138 138 libtrace->sourcetype = TRACE_SOURCE_STDIN; 139 139 INPUT.file = LIBTRACE_FDOPEN(fileno(stdin), "r"); 140 140 } else { 141 141 if (stat(CONNINFO.path,&buf) == -1 ) { 142 perror("stat");142 trace_set_err(errno,"stat(%s)",CONNINFO.path); 143 143 return 0; 144 144 } … … 147 147 if ((INPUT.fd = socket( 148 148 AF_UNIX, SOCK_STREAM, 0)) == -1) { 149 perror("socket");149 trace_set_err("socket(%s)",CONNINFO.path); 150 150 return 0; 151 151 } … … 159 159 (struct sockaddr *)&unix_sock, 160 160 sizeof(struct sockaddr)) == -1) { 161 perror("connect (unix)");161 trace_set_err("socket(%s)",CONNINFO.path); 162 162 return 0; 163 163 } 164 164 } else { 165 int fd; 165 166 libtrace->sourcetype = TRACE_SOURCE_TRACE; 166 167 167 / /we use an FDOPEN call to reopen an FD168 /* we use an FDOPEN call to reopen an FD 168 169 // returned from open(), so that we can set 169 170 // O_LARGEFILE. This gets around gzopen not 170 171 // letting you do this... 171 INPUT.file = LIBTRACE_FDOPEN(open( 172 CONNINFO.path, 173 O_LARGEFILE),"r"); 172 */ 173 fd=open( CONNINFO.path, O_LARGEFILE); 174 if (fd==-1) { 175 trace_set_err(errno,"open(%s)",CONNINFO.path); 176 return 0; 177 } 178 INPUT.file = LIBTRACE_FDOPEN(fd,"r"); 174 179 } 175 180 } … … 190 195 buffer, 191 196 64)) == -1) { 192 perror("libtrace_read");197 trace_set_err(errno,"read(%s)",libtrace->uridata); 193 198 return -1; 194 199 } … … 196 201 197 202 packet->header = packet->buffer; 198 packet->payload = packet->buffer +199 packet->trace->format->get_framing_length(packet) ;203 packet->payload = (void*)((char*)packet->buffer + 204 packet->trace->format->get_framing_length(packet)); 200 205 201 206 return 64; -
lib/format_pcap.c
r1fbd938 r880aa58 48 48 #include <stdlib.h> 49 49 #include <string.h> 50 #include <errno.h> 50 51 51 52 #if HAVE_PCAP_BPF_H … … 98 99 }; 99 100 100 static int linktype_to_dlt(libtrace_linktype_t t) {101 static int table[] = {102 -1, /* LEGACY */103 -1, /* HDLC over POS */104 DLT_EN10MB, /* Ethernet */105 -1, /* ATM */106 DLT_IEEE802_11, /* 802.11 */107 -1 /* END OF TABLE */108 };109 if (t>sizeof(table)/sizeof(*table)) {110 return -1;111 }112 return table[t];113 }114 115 101 static int pcap_init_input(struct libtrace_t *libtrace) { 116 102 char errbuf[PCAP_ERRBUF_SIZE]; … … 125 111 pcap_open_offline(CONNINFO.path, 126 112 errbuf)) == NULL) { 127 fprintf(stderr,"%s\n",errbuf);113 trace_set_err(TRACE_ERR_INIT_FAILED,"%s",errbuf); 128 114 return 0; 129 115 } 130 116 } else { 131 117 if (stat(CONNINFO.path,&buf) == -1) { 132 perror("stat");118 trace_set_err(errno,"stat(%s)",CONNINFO.path); 133 119 return 0; 134 120 } … … 140 126 1, 141 127 errbuf)) == NULL) { 142 fprintf(stderr,"%s\n",errbuf); 128 trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 129 errbuf); 143 130 return 0; 144 131 } … … 147 134 pcap_open_offline(CONNINFO.path, 148 135 errbuf)) == NULL) { 149 fprintf(stderr,"%s\n",errbuf); 136 trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 137 errbuf); 150 138 return 0; 151 139 } … … 177 165 1, 178 166 errbuf)) == NULL) { 179 fprintf(stderr,"%s\n",errbuf);167 trace_set_err(TRACE_ERR_INIT_FAILED,"%s",errbuf); 180 168 return 0; 181 169 } … … 184 172 185 173 static int pcapint_init_output(struct libtrace_out_t *libtrace __attribute__((unused))) { 174 trace_set_err(TRACE_ERR_NO_INIT_OUT,"Writing to a pcap interface not implemented yet"); 186 175 return -1; 187 176 } … … 265 254 static int pcapint_write_packet(struct libtrace_out_t *libtrace __attribute__((unused)), const struct libtrace_packet_t *packet __attribute__((unused))) { 266 255 256 assert(0); 267 257 return 0; 268 258 } … … 273 263 pcapptr = (struct pcap_pkthdr *)packet->header; 274 264 linktype = pcap_datalink(packet->trace->format_data->input.pcap); 275 switch(linktype) { 276 case DLT_NULL: 277 return TRACE_TYPE_NONE; 278 case DLT_EN10MB: 279 return TRACE_TYPE_ETH; 280 case DLT_ATM_RFC1483: 281 return TRACE_TYPE_ATM; 282 case DLT_IEEE802_11: 283 return TRACE_TYPE_80211; 284 #ifdef DLT_LINUX_SLL 285 case DLT_LINUX_SLL: 286 return TRACE_TYPE_LINUX_SLL; 287 #endif 288 #ifdef DLT_PFLOG 289 case DLT_PFLOG: 290 return TRACE_TYPE_PFLOG; 291 #endif 292 } 293 return -1; 265 return pcap_dlt_to_libtrace(linktype); 294 266 } 295 267 … … 302 274 sll = trace_get_link(packet); 303 275 if (!sll) { 276 trace_set_err(TRACE_ERR_BAD_PACKET, 277 "Bad or missing packet"); 304 278 return -1; 305 279 } … … 328 302 pflog = trace_get_link(packet); 329 303 if (!pflog) { 304 trace_set_err(TRACE_ERR_BAD_PACKET, 305 "Bad or missing packet"); 330 306 return -1; 331 307 } -
lib/format_wag.c
r1fbd938 r880aa58 111 111 struct { 112 112 int level; 113 int filemode; 113 114 } zlib; 114 115 } options; … … 130 131 131 132 if (stat(CONNINFO.path,&buf) == -1 ) { 132 perror("stat");133 trace_set_err(errno,"stat(%s)",CONNINFO.path); 133 134 return 0; 134 135 } … … 139 140 140 141 } else { 141 fprintf(stderr, "%s is not a valid char device, exiting\n", 142 trace_set_err(TRACE_ERR_INIT_FAILED, 143 "%s is not a valid char device", 142 144 CONNINFO.path); 143 145 return 0; … … 157 159 libtrace->sourcetype = TRACE_SOURCE_STDIN; 158 160 INPUT.file = LIBTRACE_FDOPEN(fileno(stdin),"r"); 159 160 161 } else { 161 /* Do we need this socket stuff at all?? 162 * If we do, put it into wag_init_input as it uses 163 * INPUT.fd 162 int fd; 163 /* TRACE */ 164 libtrace->sourcetype = TRACE_SOURCE_TRACE; 165 166 /* we use an FDOPEN call to reopen an FD 167 * returned from open(), so that we can set 168 * O_LARGEFILE. This gets around gzopen not 169 * letting you do this... 164 170 */ 165 166 /* 167 if (stat(CONNINFO.path,&buf) == -1 ) { 168 perror("stat"); 171 fd = open(CONNINFO.path, O_LARGEFILE); 172 if (fd==-1) { 173 trace_set_err(errno,"open(%s)",CONNINFO.path); 169 174 return 0; 170 175 } 171 if (S_ISSOCK(buf.st_mode)) { 172 libtrace->sourcetype = SOCKET; 173 // SOCKET 174 if ((INPUT.fd = socket( 175 AF_UNIX, SOCK_STREAM, 0)) == -1) { 176 perror("socket"); 177 return 0; 178 } 179 unix_sock.sun_family = AF_UNIX; 180 bzero(unix_sock.sun_path,108); 181 snprintf(unix_sock.sun_path, 182 108,"%s" 183 ,CONNINFO.path); 184 185 if (connect(INPUT.fd, 186 (struct sockaddr *)&unix_sock, 187 sizeof(struct sockaddr)) == -1) { 188 perror("connect (unix)"); 189 return 0; 190 } 191 } else { 192 */ 193 /* TRACE */ 194 libtrace->sourcetype = TRACE_SOURCE_TRACE; 195 196 /* we use an FDOPEN call to reopen an FD 197 * returned from open(), so that we can set 198 * O_LARGEFILE. This gets around gzopen not 199 * letting you do this... 200 */ 201 INPUT.file = LIBTRACE_FDOPEN(open( 202 CONNINFO.path, 203 O_LARGEFILE), "r"); 176 INPUT.file = LIBTRACE_FDOPEN(fd, "r"); 204 177 205 178 } … … 219 192 OUTPUT.file = LIBTRACE_FDOPEN(dup(1), filemode); 220 193 } else { 194 int fd; 221 195 /* TRACE */ 222 OUTPUT.file = LIBTRACE_FDOPEN(open( 223 libtrace->uridata, 224 O_CREAT | O_LARGEFILE | O_WRONLY, 225 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), filemode); 196 fd=open(libtrace->uridata,OPTIONS.zlib.filemode, 197 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 198 if (fd==-1) { 199 trace_set_err(errno,"open(%s)",libtrace->uridata); 200 return 0; 201 } 202 OUTPUT.file = LIBTRACE_FDOPEN(fd, filemode); 226 203 } 227 204 … … 242 219 case TRACE_OPTION_OUTPUT_COMPRESS: 243 220 /* E feature unavailable */ 221 trace_set_err(TRACE_ERR_OPTION_UNAVAIL, 222 "zlib not supported"); 244 223 return -1; 245 224 #endif 246 225 default: 247 226 /* E unknown feature */ 227 trace_set_err(TRACE_ERR_UNKNOWN_OPTION, 228 "Unknown option"); 248 229 return -1; 249 230 } … … 283 264 if (errno == EINTR || errno==EAGAIN) 284 265 continue; 285 perror("read(frame)"); 266 267 trace_set_err(errno,"read(%s)",libtrace->uridata); 286 268 return -1; 287 269 } … … 297 279 298 280 if (magic != 0xdaa1) { 299 printf("Magic number is BAD!\n");300 281 trace_set_err(TRACE_ERR_BAD_PACKET,"magic number bad or missing"); 282 return -1; 301 283 } 302 284 303 if (framesize > len) { 304 printf("Framesize > len\n"); 305 return -1; 306 } 285 /* We should deal. this is called "snapping", but we don't yet */ 286 assert(framesize>len); 307 287 308 288 buf_ptr = (void*)((char*)buffer + sizeof (struct frame_t)); … … 315 295 if (errno == EINTR || errno==EAGAIN) 316 296 continue; 317 perror("read(frame)");297 trace_set_err(errno,"read(%s)",libtrace->uridata); 318 298 return -1; 319 299 } … … 363 343 364 344 if ((numbytes = LIBTRACE_READ(INPUT.file, buffer, sizeof(struct frame_t))) == -1) { 365 perror("libtrace_read");345 libtrace_set_err(errno,"read(%s)",packet->trace->uridata); 366 346 return -1; 367 347 } … … 378 358 379 359 if ((numbytes=LIBTRACE_READ(INPUT.file, buffer2, size)) != size) { 380 perror("libtrace read");360 libtrace_set_err(errno,"read(%s)",packet->trace->uridata); 381 361 return -1; 382 362 } … … 392 372 int numbytes =0 ; 393 373 if (packet->trace->format != &wag_trace) { 394 fprintf(stderr,"Cannot convert from wag trace format to %s format yet\n", 374 trace_set_err(TRACE_ERR_NO_CONVERSION, 375 "Cannot convert from wag trace format to %s format yet", 395 376 packet->trace->format->name); 396 377 return -1; … … 400 381 * to read from the header and payload pointers 401 382 */ 402 if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->header, trace_get_framing_length(packet))) == 0) {403 perror("libtrace_write");383 if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->header, trace_get_framing_length(packet))) == -1) { 384 libtrace_set_err(errno,"write(%s)",packet->trace->uridata); 404 385 return -1; 405 386 } 406 387 if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->payload, 407 388 packet->size - trace_get_framing_length(packet))) == 0) { 408 perror("libtrace_write");389 libtrace_set_err(errno,"write(%s)",packet->trace->uridata); 409 390 return -1; 410 391 } -
lib/libtrace.h
r1fbd938 r880aa58 101 101 102 102 /** Enumeration of error codes */ 103 enum {E_NOERROR, E_BAD_FORMAT, E_NO_INIT, E_NO_INIT_OUT, E_URI_LONG, E_URI_NOCOLON, E_INIT_FAILED }; 103 enum { 104 TRACE_ERR_NOERROR = 0, 105 TRACE_ERR_BAD_FORMAT = -1, 106 TRACE_ERR_NO_INIT = -2, 107 TRACE_ERR_NO_INIT_OUT = -3, 108 TRACE_ERR_URI_LONG = -4, 109 TRACE_ERR_URI_NOCOLON = -5, 110 TRACE_ERR_INIT_FAILED = -6, 111 TRACE_ERR_UNKNOWN_OPTION= -7, 112 TRACE_ERR_NO_CONVERSION = -8, 113 TRACE_ERR_BAD_PACKET = -9, 114 TRACE_ERR_OPTION_UNAVAIL= -10 115 }; 104 116 105 117 /** @name Packet structures … … 275 287 char *trace_get_output_format(const libtrace_out_t *libtrace); 276 288 277 /** Prints error information278 *279 * Prints out a descriptive error message for the currently set trace_err value280 */281 void trace_perror(const char *caller);282 283 289 /** @name Creation and destruction of traces 284 290 * These members deal with creating, configuring and cleaning up a trace object -
lib/libtrace_int.h
r1fbd938 r880aa58 89 89 } trace_err; 90 90 91 void trace_set_error(int errcode,const char *msg,...); 91 92 92 93 #define RP_BUFSIZE 65536 … … 115 116 char *uridata; 116 117 struct tracefifo_t *fifo; 117 118 struct libtrace_filter_t *filter; /**< used by libtrace if the module 119 * doesn't support filters natively 120 */ 121 int snaplen; /**< used by libtrace if the module 122 * doesn't support snapping natively 123 */ 118 124 }; 119 125 -
lib/trace.c
r1fbd938 r880aa58 183 183 } 184 184 } 185 }186 187 /* Prints error information188 *189 * Prints out a descriptive error message for the currently set trace_err value190 */191 void trace_perror(const char *caller) {192 switch (trace_err.err_num) {193 case E_BAD_FORMAT:194 fprintf(stderr, "%s: No support for format (%s)\n", caller, trace_err.problem);195 break;196 case E_NO_INIT:197 fprintf(stderr, "%s: Format (%s) does not have an init_trace function defined\n", caller, trace_err.problem);198 break;199 case E_NO_INIT_OUT:200 fprintf(stderr, "%s: Format (%s) does not have an init_output function defined\n", caller, trace_err.problem);201 break;202 case E_URI_LONG:203 fprintf(stderr, "%s: uri is too long\n", caller);204 break;205 case E_URI_NOCOLON:206 fprintf(stderr, "%s: A uri must contain at least one colon e.g. format:destination\n", caller);207 break;208 case E_INIT_FAILED:209 fprintf(stderr, "%s: libtrace failed to initialise (%s)\n",caller,trace_err.problem);210 211 default:212 fprintf(stderr, "Unknown errcode %d\n",trace_err.err_num);213 break;214 }215 trace_err.err_num = E_NOERROR;216 185 } 217 186 … … 262 231 int i = 0; 263 232 264 trace_err.err_num = E_NOERROR;233 trace_err.err_num = TRACE_ERR_NOERROR; 265 234 266 235 /* parse the URI to determine what sort of event we are dealing with */ … … 270 239 271 240 libtrace->event.tdelta = 0.0; 241 libtrace->filter = NULL; 242 libtrace->snaplen = 0; 272 243 273 244 libtrace->format = 0; … … 282 253 } 283 254 if (libtrace->format == 0) { 284 trace_ err.err_num = E_BAD_FORMAT;285 strcpy(trace_err.problem,scan);255 trace_set_err(TRACE_ERR_BAD_FORMAT, 256 "Unknown format (%s)",scan); 286 257 return 0; 287 258 } … … 294 265 if (libtrace->format->init_input) { 295 266 if (!libtrace->format->init_input( libtrace)) { 296 trace_err.err_num = E_INIT_FAILED; 297 strcpy(trace_err.problem, scan); 267 /* init_input should call trace_set_err to set 268 * the error message 269 */ 298 270 return 0; 299 271 } 300 272 } else { 301 trace_ err.err_num = E_NO_INIT;302 strcpy(trace_err.problem,scan);273 trace_set_err(TRACE_ERR_NO_INIT, 274 "Format does not support input (%s)",scan); 303 275 return 0; 304 276 } … … 316 288 * @returns opaque pointer to a (sparsely initialised) libtrace_t 317 289 * 318 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions with319 * the dummy trace. Its intended purpose is to act as a packet->trace for libtrace_packet_t's320 * that are not associated with a libtrace_t structure.290 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions 291 * with the dummy trace. Its intended purpose is to act as a packet->trace for 292 * libtrace_packet_t's that are not associated with a libtrace_t structure. 321 293 */ 322 294 struct libtrace_t * trace_create_dead (const char *uri) { … … 326 298 int i; 327 299 328 trace_err.err_num = E_NOERROR;300 trace_err.err_num = TRACE_ERR_NOERROR; 329 301 330 302 if((uridata = strchr(uri,':')) == NULL) { … … 346 318 } 347 319 if (libtrace->format == 0) { 348 trace_err.err_num = E_BAD_FORMAT; 349 strcpy(trace_err.problem,scan);320 trace_set_err(TRACE_ERR_BAD_FORMAT, 321 "Unknown format (%s)",scan); 350 322 return 0; 351 323 } … … 359 331 * 360 332 * @param uri the uri string describing the output format and destination 361 * @returns opaque pointer to a libtrace_output_t 333 * @returns opaque pointer to a libtrace_output_t 362 334 * @author Shane Alcock 363 335 * 364 * Valid URI's are: 365 * - gzerf:/path/to/erf/file.gz 366 * - gzerf:/path/to/erf/file 367 * - rtserver:hostname 368 * - rtserver:hostname:port 369 * 370 * If an error occured when attempting to open the output trace, NULL is returned 371 * and trace_errno is set. Use trace_perror() to get more information 336 * Valid URI's are: - gzerf:/path/to/erf/file.gz - gzerf:/path/to/erf/file - 337 * rtserver:hostname - rtserver:hostname:port 338 * 339 * If an error occured when attempting to open the output trace, NULL is 340 * returned and trace_errno is set. 372 341 */ 373 342 … … 379 348 int i; 380 349 381 trace_err.err_num = E_NOERROR;350 trace_err.err_num = TRACE_ERR_NOERROR; 382 351 /* parse the URI to determine what sort of event we are dealing with */ 383 352 … … 398 367 } 399 368 if (libtrace->format == 0) { 400 trace_ err.err_num = E_BAD_FORMAT;401 strcpy(trace_err.problem,scan);369 trace_set_err(TRACE_ERR_BAD_FORMAT, 370 "Unknown output format (%s)",scan); 402 371 return 0; 403 372 } … … 414 383 } 415 384 } else { 416 trace_ err.err_num = E_NO_INIT_OUT;417 strcpy(trace_err.problem,scan);385 trace_set_err(TRACE_ERR_NO_INIT_OUT, 386 "Format does not support writing (%s)",scan); 418 387 return 0; 419 388 } … … 542 511 543 512 if (libtrace->format->read_packet) { 544 return (packet->size=libtrace->format->read_packet(libtrace,packet)); 513 do { 514 packet->size=libtrace->format->read_packet(libtrace,packet); 515 if (packet->size==-1) 516 return packet->size; 517 if (libtrace->filter) { 518 /* If the filter doesn't match, read another 519 * packet 520 */ 521 if (!trace_bpf_filter(libtrace->filter,packet)){ 522 continue; 523 } 524 } 525 if (libtrace->snaplen>0) { 526 /* Snap the packet */ 527 trace_set_capture_length(packet, 528 libtrace->snaplen); 529 } 530 531 return packet->size; 532 } while(1); 545 533 } 546 534 packet->size=-1; … … 1499 1487 if((uridata = strchr(uri,':')) == NULL) { 1500 1488 /* badly formed URI - needs a : */ 1501 trace_err.err_num = E_URI_NOCOLON;1489 trace_set_err(TRACE_ERR_URI_NOCOLON,"Format missing"); 1502 1490 return 0; 1503 1491 } … … 1505 1493 if ((uridata - uri) > URI_PROTO_LINE) { 1506 1494 /* badly formed URI - uri type is too long */ 1507 trace_err.err_num = E_URI_LONG;1495 trace_set_err(TRACE_ERR_URI_LONG,"Format too long"); 1508 1496 return 0; 1509 1497 } … … 1517 1505 } 1518 1506 1507 /** Update the libtrace error 1508 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR 1509 * @param msg a plaintext error message 1510 */ 1511 void trace_set_error(int errcode,const char *msg,...) 1512 { 1513 va_list va; 1514 va_start(msg,va); 1515 trace_err.err_num=errcode; 1516 vsnprintf(trace_err.problem,sizeof(trace_err.problem),msg,va); 1517 va_end(); 1518 }
Note: See TracChangeset
for help on using the changeset viewer.