-
Notifications
You must be signed in to change notification settings - Fork 0
/
myfilter.c
305 lines (258 loc) · 8.12 KB
/
myfilter.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <argp.h>
#include <arpa/inet.h> /* inet_ntoa */
#include <asm-generic/socket.h>
#include <ifaddrs.h> /* getifaddrs() */
#include <linux/if.h> /* IFNAMSIZ */
#include <netinet/in.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* memset */
#include <sys/socket.h>
#include <unistd.h>
#include <pthread.h>
#include <libubus.h>
#include <libubox/blobmsg_json.h>
#define BUFFER_SIZE 65536
const char *argp_program_version =
"myfilter 0.1";
const char *argp_program_bug_address =
"<alexandergusarov@gmail.com>";
/* Program documentation. */
static char doc[] =
"Simple UDP sniffer";
/* The options we understand. */
static struct argp_option options[] =
{
{"interface", 'i', "INTERFACE", 0, "Network interface name", 0},
{"source-addr", 'a', "ADDRESS", 0, "Source IP address", 0},
{"source-port", 'p', "PORT", 0, "Source UDP port", 0},
{"update-interval", 'u', "UPDATE_INTERVAL", 0, "Statistics update time", 1},
{"ubus-socket", 's', "UBUS_SOCKET", 0, "ubus UNIX socket", 1},
{0}
};
/* Program configuration */
struct arguments
{
char *interface_name;
bool source_ip_set;
struct in_addr source_ip;
uint16_t source_port;
uint64_t update_interval;
const char *ubus_socket;
};
typedef struct {
uint64_t packets;
uint64_t bytes;
} statistics_t;
statistics_t statistics = {0};
pthread_mutex_t statistic_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_t ubus_event_thread;
void sigint_handler(int);
int check_interface_name(const char *);
void *ubus_event_thread_fn(void *);
/* SIGINT handler */
void sigint_handler(int signo __attribute__ ((unused))) {
/* Print statistics */
printf("\rTotal: packets = %lu, bytes = %lu\n", statistics.packets, statistics.bytes);
/* Wait ubus thread to join */
pthread_cancel(ubus_event_thread);
pthread_join(ubus_event_thread, NULL);
exit(EXIT_SUCCESS);
}
/* Print error message and exit */
static void perror_exit(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
/* Check input string for valid network interface name */
int check_interface_name(const char *interface_name)
{
if (interface_name == NULL)
{
printf("Interface name is NULL\n");
return(0);
}
if (strlen(interface_name) > IFNAMSIZ)
{
printf("Interface name is too long\n");
return(0);
}
/* Get network interfaces list */
struct ifaddrs *ifaddr;
if (getifaddrs(&ifaddr) == -1)
perror_exit("getifaddrs()");
/* Cycle through to find name match*/
for (struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_name != NULL &&
strcmp(interface_name, ifa->ifa_name) == 0)
{
/* matched */
freeifaddrs(ifaddr);
return(1);
}
}
return(0);
}
/* Parse a single option. */
static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
switch (key)
{
case 'i':
{
if(!check_interface_name(arg))
argp_error(state, "No such interface: %s", arg);
arguments->interface_name = arg;
}
break;
case 'a':
{
if(!inet_aton(arg, &arguments->source_ip))
argp_error(state, "Invalid source IP address: %s", arg);
arguments->source_ip_set = true;
}
break;
case 'p':
{
int port = atoi(arg);
if (port < 1 || port > 65535)
argp_error(state, "Invalid UDP port: %d", port);
arguments->source_port = port;
}
break;
case 'u':
{
uint64_t interval = atoll(arg);
if (interval < 1)
argp_error(state, "Invalid update interval: %lu", interval);
arguments->update_interval = interval;
}
break;
case 's':
{
if (strlen(arg) < 1)
argp_error(state, "Invalid UNIX socket: %s", arg);
arguments->ubus_socket = arg;
}
break;
default:
return(ARGP_ERR_UNKNOWN);
}
return(0);
}
int main(int argc, char *argv[])
{
/* Initializing arguments structure */
struct arguments arguments =
{
.interface_name = NULL,
.source_ip_set = false,
.source_port = 0,
.update_interval = 1000,
.ubus_socket = NULL
};
/* Parsing command line arguments */
struct argp argp = {options, parse_opt, NULL, doc, 0, 0, 0};
if (argp_parse(&argp, argc, argv, 0, 0, &arguments))
perror_exit("argp_parse()");
/* Block the SIGINT signal. The threads will inherit the signal mask.
This will avoid them catching SIGINT instead of this thread. */
sigset_t sigset, oldset;
if (sigemptyset(&sigset))
perror_exit("sigemptyset()");
if (sigaddset(&sigset, SIGINT))
perror_exit("sigaddset()");
if (pthread_sigmask(SIG_BLOCK, &sigset, &oldset))
perror_exit("pthread_sigmask()");
/* Spawn ubus event thread. */
if (pthread_create(&ubus_event_thread, NULL, ubus_event_thread_fn, &arguments))
perror_exit("pthread_create()");
/* Install the signal handler for SIGINT. */
struct sigaction s;
s.sa_handler = sigint_handler;
if (sigemptyset(&s.sa_mask))
perror_exit("sigemptyset()");
s.sa_flags = 0;
if (sigaction(SIGINT, &s, NULL))
perror_exit("sigaction()");
/* Restore the old signal mask only for this thread. */
if (pthread_sigmask(SIG_SETMASK, &oldset, NULL))
perror_exit("pthread_sigmask()");
printf("Starting...\n");
/* Allocating buffer for received packet */
uint8_t *buffer = (uint8_t *)malloc(BUFFER_SIZE);
if (buffer == NULL)
perror_exit("malloc()");
/* Open raw socket for UDP */
int socket_raw = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if (socket_raw < 0)
perror_exit("socket()");
else
printf("socket(): Using SOCK_RAW socket and UDP protocol is OK.\n");
/* Binding to network interface */
if (arguments.interface_name != NULL)
{
if (setsockopt(socket_raw, SOL_SOCKET, SO_BINDTODEVICE,
arguments.interface_name, strlen(arguments.interface_name)))
perror_exit("setsockopt()");
printf("setsockopt(): Binded to interface %s\n", arguments.interface_name);
}
/* Capturing packets */
struct sockaddr_in saddr;
socklen_t saddr_size = sizeof(saddr);
ssize_t data_size = -1;
while (1)
{
/* Receive all UDP packets */
data_size = recvfrom(socket_raw, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&saddr, &saddr_size);
if (data_size < 0)
perror_exit("recvfrom()");
/* Filter only matching packets */
if ((arguments.source_ip_set && saddr.sin_addr.s_addr != arguments.source_ip.s_addr) ||
(arguments.source_port && saddr.sin_port != arguments.source_port))
continue;
/* Update statistics */
pthread_mutex_lock(&statistic_lock);
++statistics.packets;
statistics.bytes += data_size;
pthread_mutex_unlock(&statistic_lock);
}
return(EXIT_SUCCESS);
}
/* Thread to send ubus event with statistics */
void *ubus_event_thread_fn(void *p)
{
struct ubus_context ctx;
if (p == NULL)
perror_exit("ubus_event_thread_fn(): arguments structure pointer is NULL");
struct arguments *arg = (struct arguments *)p;
/* Prepare timespec for nanosleep */
struct timespec delay;
delay.tv_sec = arg->update_interval / 1000;
delay.tv_nsec = (arg->update_interval % 1000) * 1000;
if (ubus_connect_ctx(&ctx, arg->ubus_socket))
perror_exit("ubus_connect_ctx()");
while (1)
{
static struct blob_buf b;
blobmsg_buf_init(&b);
/* Get statistics info */
pthread_mutex_lock(&statistic_lock);
blobmsg_add_u64(&b, "packets", statistics.packets);
blobmsg_add_u64(&b, "bytes", statistics.bytes);
pthread_mutex_unlock(&statistic_lock);
/* Send ubus event*/
ubus_send_event(&ctx, "statistics", b.head);
/* Sleep */
nanosleep(&delay, NULL);
}
return(NULL);
}