-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSniff.c
executable file
·61 lines (48 loc) · 1.37 KB
/
Sniff.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
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include "Sniff.h"
#include "Packet.h"
#include "Defs.h"
extern bpf_u_int32 netp;
extern bpf_u_int32 maskp;
extern int datalink;
extern int promisc;
extern int readtimeout;
extern int npackets;
void startSniffer(char *filter)
{
struct in_addr in;
if (dev == NULL)
if ((dev = pcap_lookupdev(errbuf)) == NULL) {
fprintf(stderr, "startSniffer: couldn't find a device to sniff: %s\n", errbuf);
exit(1);
}
printf("\n\n%s starting: listening on device: %s\n", VERSION, dev);
if ((pd = pcap_open_live(dev, SNAPLEN, promisc, readtimeout, errbuf)) == NULL) {
fprintf(stderr, "startSniffer: pcap_open_live failed: %s\n", errbuf);
exit(1);
}
pcap_lookupnet(dev, &netp, &maskp, errbuf);
in.s_addr = netp;
printf("%s (%s/", dev, inet_ntoa(in));
in.s_addr = maskp;
printf("%s) opened successfully in %spromiscuous mode\n", inet_ntoa(in), (promisc ? "" : "non-"));
if (filter != NULL) {
pcap_compile(pd, &fprog, filter, 0, netp);
if ((pcap_setfilter(pd, &fprog)) == -1) {
fprintf(stderr, "startSniffer: pcap_setfilter: cannot set filter\n");
exit(1);
}
pcap_freecode(&fprog);
}
}
void startSniffLoop()
{
int datalink;
datalink = pcap_datalink(pd);
if (pcap_loop(pd, npackets, main_packet_handler, (u_char *)datalink) < 0) {
fprintf(stderr, "startSniffer: pcap_loop: %s\n", pcap_geterr(pd));
exit(1);
}
}