Changeset 9e2a109 for lib/format_erf.c
- Timestamp:
- 08/04/05 15:37:08 (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:
- ef55d05
- Parents:
- fe43699
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/format_erf.c
rfe43699 r9e2a109 30 30 31 31 #include "libtrace.h" 32 #include " format.h"32 #include "libtrace_int.h" 33 33 #include "rtserver.h" 34 34 #include "parse_cmd.h" … … 62 62 #endif 63 63 64 #define CONNINFO libtrace->format_data->conn_info 65 #define INPUT libtrace->format_data->input 66 #define DAG libtrace->format_data->dag 67 struct libtrace_format_data_t { 68 union { 69 /** Information about rtclients */ 70 struct { 71 char *hostname; 72 short port; 73 } rt; 74 char *path; /**< information for local sockets */ 75 } conn_info; 76 /** Information about the current state of the input device */ 77 union { 78 int fd; 79 #if HAVE_ZLIB 80 gzFile *file; 81 #else 82 FILE *file; 83 #endif 84 } input; 85 86 struct { 87 void *buf; 88 unsigned bottom; 89 unsigned top; 90 unsigned diff; 91 unsigned curr; 92 unsigned offset; 93 } dag; 94 }; 95 96 struct libtrace_format_data_out_t { 97 union { 98 struct { 99 char *hostname; 100 short port; 101 } rt; 102 char *path; 103 } conn_info; 104 105 union { 106 struct { 107 int level; 108 } erf; 109 110 } options; 111 112 union { 113 int fd; 114 struct rtserver_t * rtserver; 115 #if HAVE_ZLIB 116 gzFile *file; 117 #else 118 FILE *file; 119 #endif 120 } output; 121 }; 122 64 123 #ifdef HAVE_DAG 65 124 static int dag_init_input(struct libtrace_t *libtrace) { 66 125 struct stat buf; 67 if (stat(libtrace->conn_info.path,&buf) == -1) { 126 libtrace->format_data = (struct libtrace_format_data_t *) 127 malloc(sizeof(struct libtrace_format_data_t)); 128 129 CONNINFO.path = libtrace->uridata; 130 if (stat(CONNINFO.path,&buf) == -1) { 68 131 perror("stat"); 69 132 return 0; … … 71 134 if (S_ISCHR(buf.st_mode)) { 72 135 // DEVICE 73 if(( libtrace->input.fd =74 dag_open( libtrace->conn_info.path)) < 0) {136 if((INPUT.fd = 137 dag_open(CONNINFO.path)) < 0) { 75 138 fprintf(stderr,"Cannot open DAG %s: %m\n", 76 libtrace->conn_info.path,errno);139 CONNINFO.path,errno); 77 140 exit(0); 78 141 } 79 if(( libtrace->dag.buf = (void *)80 dag_mmap( libtrace->input.fd)) == MAP_FAILED) {142 if((DAG.buf = (void *) 143 dag_mmap(INPUT.fd)) == MAP_FAILED) { 81 144 fprintf(stderr,"Cannot mmap DAG %s: %m\n", 82 libtrace->conn_info.path,errno);145 CONNINFO.path,errno); 83 146 exit(0); 84 147 } 85 if(dag_start( libtrace->input.fd) < 0) {148 if(dag_start(INPUT.fd) < 0) { 86 149 fprintf(stderr,"Cannot start DAG %s: %m\n", 87 libtrace->conn_info.path,errno);150 CONNINFO.path,errno); 88 151 exit(0); 89 152 } 90 153 } else { 91 154 fprintf(stderr,"%s isn't a valid char device, exiting\n", 92 libtrace->conn_info.path);155 CONNINFO.path); 93 156 return 0; 94 157 } … … 101 164 struct sockaddr_in remote; 102 165 struct sockaddr_un unix_sock; 103 if (!strncmp(libtrace->conn_info.path,"-",1)) { 166 libtrace->format_data = (struct libtrace_format_data_t *) 167 malloc(sizeof(struct libtrace_format_data_t)); 168 169 CONNINFO.path = libtrace->uridata; 170 if (!strncmp(CONNINFO.path,"-",1)) { 104 171 // STDIN 105 172 #if HAVE_ZLIB 106 libtrace->input.file = gzdopen(STDIN, "r");173 INPUT.file = gzdopen(STDIN, "r"); 107 174 #else 108 libtrace->input.file = stdin;175 INPUT.file = stdin; 109 176 #endif 110 177 111 178 } else { 112 if (stat( libtrace->conn_info.path,&buf) == -1 ) {179 if (stat(CONNINFO.path,&buf) == -1 ) { 113 180 perror("stat"); 114 181 return 0; … … 116 183 if (S_ISSOCK(buf.st_mode)) { 117 184 // SOCKET 118 if (( libtrace->input.fd = socket(185 if ((INPUT.fd = socket( 119 186 AF_UNIX, SOCK_STREAM, 0)) == -1) { 120 187 perror("socket"); … … 125 192 snprintf(unix_sock.sun_path, 126 193 108,"%s" 127 , libtrace->conn_info.path);128 129 if (connect( libtrace->input.fd,194 ,CONNINFO.path); 195 196 if (connect(INPUT.fd, 130 197 (struct sockaddr *)&unix_sock, 131 198 sizeof(struct sockaddr)) == -1) { … … 139 206 // ourselves. However, this way is messy and 140 207 // we lose any error checking on "open" 141 libtrace->input.file =208 INPUT.file = 142 209 gzdopen(open( 143 libtrace->conn_info.path,210 CONNINFO.path, 144 211 O_LARGEFILE), "r"); 145 212 #else 146 libtrace->input.file =213 INPUT.file = 147 214 fdopen(open( 148 libtrace->conn_info.path,215 CONNINFO.path, 149 216 O_LARGEFILE), "r"); 150 217 #endif … … 156 223 static int rtclient_init_input(struct libtrace_t *libtrace) { 157 224 char *scan; 158 char *uridata = libtrace-> conn_info.path;225 char *uridata = libtrace->uridata; 159 226 struct hostent *he; 160 227 struct sockaddr_in remote; 228 libtrace->format_data = (struct libtrace_format_data_t *) 229 malloc(sizeof(struct libtrace_format_data_t)); 161 230 162 231 if (strlen(uridata) == 0) { 163 libtrace->conn_info.rt.hostname =232 CONNINFO.rt.hostname = 164 233 strdup("localhost"); 165 libtrace->conn_info.rt.port =234 CONNINFO.rt.port = 166 235 COLLECTOR_PORT; 167 236 } else { 168 237 if ((scan = strchr(uridata,':')) == NULL) { 169 libtrace->conn_info.rt.hostname =238 CONNINFO.rt.hostname = 170 239 strdup(uridata); 171 libtrace->conn_info.rt.port =240 CONNINFO.rt.port = 172 241 COLLECTOR_PORT; 173 242 } else { 174 libtrace->conn_info.rt.hostname =243 CONNINFO.rt.hostname = 175 244 (char *)strndup(uridata, 176 245 (scan - uridata)); 177 libtrace->conn_info.rt.port =246 CONNINFO.rt.port = 178 247 atoi(++scan); 179 248 } 180 249 } 181 250 182 if ((he=gethostbyname( libtrace->conn_info.rt.hostname)) == NULL) {251 if ((he=gethostbyname(CONNINFO.rt.hostname)) == NULL) { 183 252 perror("gethostbyname"); 184 253 return 0; 185 254 } 186 if (( libtrace->input.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {255 if ((INPUT.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 187 256 perror("socket"); 188 257 return 0; … … 190 259 191 260 remote.sin_family = AF_INET; 192 remote.sin_port = htons( libtrace->conn_info.rt.port);261 remote.sin_port = htons(CONNINFO.rt.port); 193 262 remote.sin_addr = *((struct in_addr *)he->h_addr); 194 263 bzero(&(remote.sin_zero), 8); 195 264 196 if (connect( libtrace->input.fd, (struct sockaddr *)&remote,265 if (connect(INPUT.fd, (struct sockaddr *)&remote, 197 266 sizeof(struct sockaddr)) == -1) { 198 267 perror("connect (inet)"); … … 203 272 static int erf_init_output(struct libtrace_out_t *libtrace) { 204 273 char *filemode = 0; 205 206 libtrace->options.erf.level = 1; 207 asprintf(&filemode,"wb%d",libtrace->options.erf.level); 274 libtrace->format_data = (struct libtrace_format_data_out_t *) 275 calloc(1,sizeof(struct libtrace_format_data_out_t)); 276 277 libtrace->format_data->options.erf.level = 1; 278 asprintf(&filemode,"wb%d",libtrace->format_data->options.erf.level); 208 279 209 280 if (!strncmp(libtrace->uridata,"-",1)) { 210 281 // STDOUT 211 282 #if HAVE_ZLIB 212 libtrace-> output.file = gzdopen(dup(1), filemode);283 libtrace->format_data->output.file = gzdopen(dup(1), filemode); 213 284 #else 214 libtrace-> output.file = stdout;285 libtrace->format_data->output.file = stdout; 215 286 #endif 216 287 } … … 221 292 // ourselves. However, this way is messy and 222 293 // we lose any error checking on "open" 223 libtrace-> output.file = gzdopen(open(294 libtrace->format_data->output.file = gzdopen(open( 224 295 libtrace->uridata, 225 296 O_CREAT | O_LARGEFILE | O_WRONLY, 226 297 S_IRUSR | S_IWUSR), filemode); 227 298 #else 228 libtrace-> output.file = fdopen(open(299 libtrace->format_data->output.file = fdopen(open( 229 300 libtrace->uridata, 230 301 O_CREAT | O_LARGEFILE | O_WRONLY, … … 239 310 char * uridata = libtrace->uridata; 240 311 char * scan; 312 libtrace->format_data = (struct libtrace_format_data_out_t *) 313 calloc(1,sizeof(struct libtrace_format_data_out_t)); 241 314 // extract conn_info from uridata 242 315 if (strlen(uridata) == 0) { 243 libtrace->conn_info.rt.hostname = strdup("localhost"); 244 libtrace->conn_info.rt.port = COLLECTOR_PORT; 316 libtrace->format_data->conn_info.rt.hostname = 317 strdup("localhost"); 318 libtrace->format_data->conn_info.rt.port = COLLECTOR_PORT; 245 319 } 246 320 else { 247 321 if ((scan = strchr(uridata,':')) == NULL) { 248 libtrace-> conn_info.rt.hostname =322 libtrace->format_data->conn_info.rt.hostname = 249 323 strdup(uridata); 250 libtrace-> conn_info.rt.port =324 libtrace->format_data->conn_info.rt.port = 251 325 COLLECTOR_PORT; 252 326 } else { 253 libtrace-> conn_info.rt.hostname =327 libtrace->format_data->conn_info.rt.hostname = 254 328 (char *)strndup(uridata, 255 329 (scan - uridata)); 256 libtrace-> conn_info.rt.port =330 libtrace->format_data->conn_info.rt.port = 257 331 atoi(++scan); 258 332 } … … 260 334 261 335 262 libtrace->output.rtserver = rtserver_create(libtrace->conn_info.rt.hostname, 263 libtrace->conn_info.rt.port); 264 if (!libtrace->output.rtserver) 336 libtrace->format_data->output.rtserver = 337 rtserver_create(libtrace->format_data->conn_info.rt.hostname, 338 libtrace->format_data->conn_info.rt.port); 339 if (!libtrace->format_data->output.rtserver) 265 340 return 0; 266 341 … … 269 344 static int erf_config_output(struct libtrace_out_t *libtrace, int argc, char *argv[]) { 270 345 int opt; 271 int level = libtrace-> options.erf.level;346 int level = libtrace->format_data->options.erf.level; 272 347 optind = 1; 273 348 … … 283 358 } 284 359 } 285 if (level != libtrace-> options.erf.level) {360 if (level != libtrace->format_data->options.erf.level) { 286 361 if (level > 9 || level < 0) { 287 362 // retarded level choice … … 289 364 290 365 } else { 291 libtrace-> options.erf.level = level;292 return gzsetparams(libtrace-> output.file, level, Z_DEFAULT_STRATEGY);366 libtrace->format_data->options.erf.level = level; 367 return gzsetparams(libtrace->format_data->output.file, level, Z_DEFAULT_STRATEGY); 293 368 } 294 369 } … … 303 378 #ifdef HAVE_DAG 304 379 static int dag_fin_input(struct libtrace_t *libtrace) { 305 dag_stop( libtrace->input.fd);380 dag_stop(INPUT.fd); 306 381 } 307 382 #endif … … 309 384 static int erf_fin_input(struct libtrace_t *libtrace) { 310 385 #if HAVE_ZLIB 311 gzclose( libtrace->input.file);386 gzclose(INPUT.file); 312 387 #else 313 fclose( libtrace->input.file);388 fclose(INPUT.file); 314 389 #endif 315 390 } 316 391 317 392 static int rtclient_fin_input(struct libtrace_t *libtrace) { 318 close( libtrace->input.fd);393 close(INPUT.fd); 319 394 } 320 395 321 396 static int erf_fin_output(struct libtrace_out_t *libtrace) { 322 397 #if HAVE_ZLIB 323 gzclose(libtrace-> output.file);398 gzclose(libtrace->format_data->output.file); 324 399 #else 325 fclose(libtrace-> output.file);400 fclose(libtrace->format_data->output.file); 326 401 #endif 327 402 } … … 329 404 330 405 static int rtclient_fin_output(struct libtrace_out_t *libtrace) { 331 rtserver_destroy(libtrace-> output.rtserver);406 rtserver_destroy(libtrace->format_data->output.rtserver); 332 407 } 333 408 … … 342 417 buffer = malloc(len); 343 418 344 libtrace->dag.bottom = libtrace->dag.top;345 libtrace->dag.top = dag_offset(346 libtrace->input.fd,347 &( libtrace->dag.bottom),419 DAG.bottom = DAG.top; 420 DAG.top = dag_offset( 421 INPUT.fd, 422 &(DAG.bottom), 348 423 0); 349 libtrace->dag.diff = libtrace->dag.top -350 libtrace->dag.bottom;351 352 numbytes= libtrace->dag.diff;353 libtrace->dag.offset = 0;424 DAG.diff = DAG.top - 425 DAG.bottom; 426 427 numbytes=DAG.diff; 428 DAG.offset = 0; 354 429 return numbytes; 355 430 } … … 403 478 int rlen; 404 479 405 if ((numbytes=gzread( libtrace->input.file,480 if ((numbytes=gzread(INPUT.file, 406 481 buffer, 407 482 dag_record_size)) == -1) { … … 418 493 419 494 // read in the rest of the packet 420 if ((numbytes=gzread( libtrace->input.file,495 if ((numbytes=gzread(INPUT.file, 421 496 buffer2, 422 497 size)) == -1) { … … 440 515 # define MSG_NOSIGNAL 0 441 516 #endif 442 if ((numbytes = recv( libtrace->input.fd,517 if ((numbytes = recv(INPUT.fd, 443 518 buffer, 444 519 len, … … 471 546 if (fifo_out_available(libtrace->fifo) == 0 || read_required) { 472 547 if ((numbytes = rtclient_read( 473 548 libtrace,buf,RP_BUFSIZE))<=0) { 474 549 return numbytes; 475 550 } … … 516 591 int numbytes = 0; 517 592 518 if ((numbytes = gzwrite(libtrace-> output.file, packet->buffer, packet->size)) == 0) {593 if ((numbytes = gzwrite(libtrace->format_data->output.file, packet->buffer, packet->size)) == 0) { 519 594 perror("gzwrite"); 520 595 return -1; … … 532 607 533 608 do { 534 if (rtserver_checklisten(libtrace-> output.rtserver) < 0)609 if (rtserver_checklisten(libtrace->format_data->output.rtserver) < 0) 535 610 return -1; 536 611 … … 564 639 memcpy(buf, &packet->status, intsize); 565 640 566 if ((numbytes = rtserver_sendclients(libtrace-> output.rtserver, buf, size + sizeof(int))) < 0) {641 if ((numbytes = rtserver_sendclients(libtrace->format_data->output.rtserver, buf, size + sizeof(int))) < 0) { 567 642 write_required = 0; 568 643 continue; … … 591 666 dag_record_t *erfptr = 0; 592 667 erfptr = (dag_record_t *)packet->buffer; 668 printf("%d\n",erfptr->type); 593 669 switch (erfptr->type) { 594 670 case TYPE_ETH: return TRACE_TYPE_ETH; … … 690 766 691 767 692 static struct format_t erf = {768 static struct libtrace_format_t erf = { 693 769 "erf", 694 770 "$Id$", … … 715 791 716 792 #ifdef HAVE_DAG 717 static struct format_t dag = {793 static struct libtrace_format_t dag = { 718 794 "dag", 719 795 "$Id$", … … 740 816 #endif 741 817 742 static struct format_t rtclient = {818 static struct libtrace_format_t rtclient = { 743 819 "rtclient", 744 820 "$Id$",
Note: See TracChangeset
for help on using the changeset viewer.