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