"Fossies" - the Fresh Open Source Software Archive

Member "scanlogd-2.2.8/in_pcap.c" (10 Mar 2021, 1236 Bytes) of package /linux/misc/scanlogd-2.2.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "in_pcap.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.2.7_vs_2.2.8.

    1 #define _BSD_SOURCE
    2 #define _DEFAULT_SOURCE
    3 #include <stdio.h>
    4 
    5 #include <pcap.h>
    6 
    7 #include "params.h"
    8 #include "in.h"
    9 
   10 static pcap_t *p;
   11 
   12 int in_init(void)
   13 {
   14     char *device;
   15     char error[PCAP_ERRBUF_SIZE];
   16     struct bpf_program filter;
   17 
   18 #ifdef SCANLOGD_DEVICE
   19     device = SCANLOGD_DEVICE;
   20 #else
   21     if (!(device = pcap_lookupdev(error))) {
   22         fprintf(stderr, "pcap_lookupdev: %s\n", error);
   23         return 1;
   24     }
   25 #endif
   26 
   27     if (!(p = pcap_open_live(device, sizeof(struct header),
   28         SCANLOGD_PROMISC, 0, error))) {
   29         fprintf(stderr, "pcap_open_live: %s\n", error);
   30         return 1;
   31     }
   32 
   33     if (pcap_compile(p, &filter, SCANLOGD_PCAP_FILTER, 1, 0)) {
   34         pcap_perror(p, "pcap_compile");
   35         return 1;
   36     }
   37 
   38     if (pcap_setfilter(p, &filter)) {
   39         pcap_perror(p, "pcap_setfilter");
   40         return 1;
   41     }
   42 
   43     return 0;
   44 }
   45 
   46 void in_run(void (*process_packet)(struct header *packet, int size))
   47 {
   48     int hw_size, size;
   49     char *packet;
   50     struct pcap_pkthdr header;
   51 
   52     switch (pcap_datalink(p)) {
   53     case DLT_RAW:
   54     case DLT_SLIP:
   55         hw_size = 0;
   56 
   57     case DLT_PPP:
   58         hw_size = 4;
   59 
   60     case DLT_EN10MB:
   61     default:
   62         hw_size = 14;
   63     }
   64 
   65     while (1)
   66     if ((packet = (char *)pcap_next(p, &header))) {
   67         packet += hw_size;
   68         size = header.caplen - hw_size;
   69         process_packet((struct header *)packet, size);
   70     }
   71 }