-
Notifications
You must be signed in to change notification settings - Fork 2
/
netsniff.c
159 lines (131 loc) · 3.4 KB
/
netsniff.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pcap.h>
#include <arpa/inet.h>
#include "netsniff.h"
#define CAPTURE_INF -1
/* MAC + IP + TCP hdr */
#define MIN_SNAPLEN 54
#define err_exit(msg) do { \
fprintf(stderr, "%s", msg); \
exit(EXIT_FAILURE); \
} while (0)
static void
pkt_handler_en10mb(u_char *usr, const struct pcap_pkthdr *pkt,
const u_char *d);
static void
pkt_handler_linux_sll(u_char *usr, const struct pcap_pkthdr *pkt,
const u_char *d);
const char *
mac_str(char *__restrict buf, uint8_t *__restrict addr);
int main(int argc, char *argv[])
{
static struct program_options opts;
struct bpf_program fp;
char errbuff[PCAP_ERRBUF_SIZE];
pcap_t *pcap;
int err, linktype;
if (get_program_options(argc, argv, &opts))
err_exit("could not get program options\n");
if (!opts.interface)
opts.interface = "any";
if (opts.snaplen < MIN_SNAPLEN)
opts.snaplen = MIN_SNAPLEN;
pcap = pcap_open_live(opts.interface, opts.snaplen,
opts.promisc, 2000, errbuff);
if (!pcap)
err_exit("cannot open network interface\n");
err = pcap_compile(pcap, &fp, opts.bpf_expr, 0, -1);
if (err)
err_exit(pcap_geterr(pcap));
err = pcap_setfilter(pcap, &fp);
if (err)
err_exit(pcap_geterr(pcap));
linktype = pcap_datalink(pcap);
printf("datalink: %s\n", pcap_datalink_val_to_name(linktype));
switch (linktype) {
case DLT_EN10MB:
err = pcap_loop(pcap, CAPTURE_INF,
pkt_handler_en10mb, NULL);
break;
case DLT_LINUX_SLL:
err = pcap_loop(pcap, CAPTURE_INF,
pkt_handler_linux_sll, NULL);
break;
default:
err_exit("Datalink type is not yet supported!\n");
}
switch (err) {
case 0:
printf("netsniff terminated successfully\n");
break;
case -1:
case -2:
printf("netsniff terminated with error: %d\n", err);
break;
}
exit(EXIT_SUCCESS);
}
static char fmt_buf[1024];
static struct strbuf sb = {
.size = sizeof(fmt_buf),
.buf = fmt_buf,
};
static void etherframe_print(u_char *usr, const struct pcap_pkthdr *pkt,
const u_char *d, uint16_t ethtype)
{
struct iphdr *ip;
struct tcphdr *th;
struct udphdr *uh;
uint16_t ethtype_le;
ethtype_le = ntohs(ethtype);
switch (ethtype_le) {
case ETH_P_IP:
ip = ip_hdr(d);
sb_append_str(&sb, "IP: ");
iphdr_print(ip, &sb);
switch (ip->protocol) {
case IPPROTO_TCP:
th = tcp_hdr(d + ip_hdrlen(ip));
sb_append_str(&sb, "; TCP: ");
tcp_print(th, &sb);
break;
case IPPROTO_UDP:
uh = udp_hdr(d + ip_hdrlen(ip));
sb_append_str(&sb, "; UDP: ");
udp_print(uh, &sb);
break;
default:
sb_append_char(&sb, ' ');
sb_append_str(&sb, ipproto_str(ip->protocol));
}
break;
default:
/* FIXME: This code is open to buffer overrun errors */
sb_append_str(&sb, "ether type: ");
sb.len += sprintf(sb_curr(&sb), "0x%04x ", ethtype_le);
sb_append_str(&sb, ethertype_to_str(ethtype_le));
}
}
static void
pkt_handler_en10mb(u_char *usr, const struct pcap_pkthdr *pkt,
const u_char *d)
{
struct machdr *mac = mac_hdr(d);
sb_reset(&sb);
eth_print(mac, &sb);
sb_append_str(&sb, "; ");
etherframe_print(usr, pkt, d + ETH_HLEN, mac->type);
fprintf(stdout, "pkt: %s\n", sb.buf);
}
static void
pkt_handler_linux_sll(u_char *usr, const struct pcap_pkthdr *pkt,
const u_char *d)
{
struct dlt_linux_sll *ll = dlt_linux_sll_hdr(d);
BUILD_BUG_ON(sizeof(*ll) != 16);
sb_reset(&sb);
etherframe_print(usr, pkt, ll->payload, ll->proto_type);
fprintf(stdout, "pkt: %s\n", sb.buf);
}