Project

General

Profile

Feature #277 ยป feature-277.patch

patch for load bpf from file - delta yeh, 05/25/2011 03:18 AM

View differences:

314 314
            fprintf(stderr, "ERROR: Failed to set bpf filter.\n");
315 315
            exit(EXIT_FAILURE);
316 316
        }
317 317
    }
318 318
}
319
static void SetBpfStringFromFile(char *filename) {
320
    char *bpf_filter = NULL;
321
    uint32_t bpf_len = 0;
322
    struct stat st;
323
    FILE *fp = NULL;
324

  
325
    if (stat(filename, &st) != 0) {
326
        SCLogError(SC_ERR_LOGDIR_CMDLINE, "Fail to load bpf from %s", filename);
327
        exit(EXIT_FAILURE);
328
    }
329
    bpf_len=st.st_size + 1;
330

  
331
    bpf_filter = SCMalloc(bpf_len);
332
    if (bpf_filter == NULL)
333
        return;
334

  
335
    memset(bpf_filter, 0x00, bpf_len);
336

  
337
    fp = fopen(filename,"r");
338
    if(fp == NULL) {
339
      SCLogError(SC_ERR_LOGDIR_CMDLINE, "Failt to open  bpf file %s", filename);
340
      exit(EXIT_FAILURE);
341
   }
342
   else {
343
      fread(bpf_filter, bpf_filter, 1, fp);
344
      fclose(fp);
345
   }
346

  
347

  
348
    if(strlen(bpf_filter) > 0) {
349
        if (ConfSet("bpf-filter", bpf_filter, 0) != 1) {
350
            fprintf(stderr, "ERROR: Failed to set bpf filter.\n");
351
            exit(EXIT_FAILURE);
352
        }
353
    }
354
}
319 355

  
320 356
void usage(const char *progname)
321 357
{
322 358
#ifdef REVISION
323 359
    printf("%s %s (rev %s)\n", PROG_NAME, PROG_VER, xstr(REVISION));
......
325 361
    printf("%s %s\n", PROG_NAME, PROG_VER);
326 362
#endif
327 363
    printf("USAGE: %s\n\n", progname);
328 364
    printf("\t-c <path>                    : path to configuration file\n");
329 365
    printf("\t-i <dev or ip>               : run in pcap live mode\n");
366
    printf("\t-b <bpf filter file>         : bpf filter file\n");	
330 367
    printf("\t-r <path>                    : run in pcap file/offline mode\n");
331 368
#ifdef NFQ
332 369
    printf("\t-q <qid>                     : run in inline nfqueue mode\n");
333 370
#endif /* NFQ */
334 371
#ifdef IPFW
......
592 629
    };
593 630

  
594 631
    /* getopt_long stores the option index here. */
595 632
    int option_index = 0;
596 633

  
597
    char short_opts[] = "c:Dhi:l:q:d:r:us:U:V";
634
    char short_opts[] = "c:Dhi:l:q:d:r:us:U:Vb:";
598 635

  
599 636
    while ((opt = getopt_long(argc, argv, short_opts, long_opts, &option_index)) != -1) {
600 637
        switch (opt) {
601 638
        case 0:
602 639
            if (strcmp((long_opts[option_index]).name , "pfring") == 0 ||
......
893 930
            printf("\nThis is %s version %s (rev %s)\n\n", PROG_NAME, PROG_VER, xstr(REVISION));
894 931
#else
895 932
            printf("\nThis is %s version %s\n\n", PROG_NAME, PROG_VER);
896 933
#endif
897 934
            exit(EXIT_SUCCESS);
935
        case 'b':
936
	     SetBpfStringFromFile(optarg);
937
	     break;
898 938
        default:
899 939
            usage(argv[0]);
900 940
            exit(EXIT_FAILURE);
901 941
        }
902 942
    }
    (1-1/1)