-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
366 lines (281 loc) · 9.33 KB
/
main.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "local.h"
#include "mtou.yucc"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <pthread.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <semaphore.h>
#define MAX_QUEUE 2048
typedef struct {
size_t size;
buf_t *data;
} queue_entry_t;
typedef struct {
queue_entry_t entry[MAX_QUEUE];
u32 qi_prod;
u32 qi_cons;
sem_t sem_consume;
pthread_t thr_prod;
pthread_t thr_cons;
} queue_t;
typedef struct {
int fd_rx;
int fd_tx;
struct sockaddr_in addr_tx;
bool verbose;
} param_t;
static int udp_addr(struct addrinfo **addr, const char *host, const char *service, bool passive);
static void start(param_t *param);
static void stop(void);
static void *rx_thread(void *param);
static void *tx_thread(void *param);
static queue_t queue = {
.qi_prod = 0,
.qi_cons = 0
};
int main(int argc, char *argv[])
{
yuck_t argp[1];
struct addrinfo *addr_info = NULL;
struct sockaddr_in addr_rx, addr_rx_if;
struct sockaddr_in addr_tx, addr_tx_if;
struct ip_mreq mreq;
param_t param;
int fd_rx, fd_tx;
yuck_parse(argp, argc, argv);
if (argp->iface_arg && !strlen(argp->iface_arg)) {
fputs("input interface cannot be empty\n", stderr);
exit(1);
}
if ((!argp->in_arg || !strlen(argp->in_arg)) && !argp->iface_arg) {
fputs("input is required when input interface not given\n", stderr);
exit(1);
}
if (!argp->port_arg || !strlen(argp->port_arg)) {
fputs("port is required\n", stderr);
exit(1);
}
if (argp->oface_arg && !strlen(argp->oface_arg)) {
fputs("output interface cannot be empty\n", stderr);
}
if (!argp->out_arg || !strlen(argp->out_arg)) {
fputs("output is required\n", stderr);
exit(1);
}
if (!argp->out_port_arg || !strlen(argp->out_port_arg)) {
argp->out_port_arg = argp->port_arg;
}
if (udp_addr(&addr_info, "0.0.0.0", argp->port_arg, false)) {
exit(1);
}
addr_tx_if = addr_rx_if = *(struct sockaddr_in *)addr_info->ai_addr;
addr_tx_if.sin_port = 0;
if (argp->iface_arg || argp->oface_arg) {
struct ifaddrs *addr;
if (getifaddrs(&addr)) {
perror("getifaddrs()");
exit(1);
}
while (addr) {
if (!addr->ifa_addr || addr->ifa_addr->sa_family != AF_INET) goto next;
const in_addr_t in_addr = ntohl(((struct sockaddr_in *)addr->ifa_addr)->sin_addr.s_addr);
if (in_addr == INADDR_NONE || in_addr == INADDR_ANY) goto next;
if (argp->iface_arg && !strcmp(argp->iface_arg, addr->ifa_name)) {
addr_rx_if.sin_addr = ((struct sockaddr_in *) addr->ifa_addr)->sin_addr;
}
if (argp->oface_arg && !strcmp(argp->oface_arg, addr->ifa_name)) {
addr_tx_if.sin_addr = ((struct sockaddr_in *) addr->ifa_addr)->sin_addr;
}
next:
addr = addr->ifa_next;
}
if (argp->iface_arg && !addr_rx_if.sin_addr.s_addr) {
fputs("no matching suitable input interface found\n", stderr);
exit(1);
}
if (argp->oface_arg && !addr_tx_if.sin_addr.s_addr) {
fputs("no matching suitable output interface found\n", stderr);
exit(1);
}
}
if (argp->in_arg) {
if (udp_addr(&addr_info, argp->in_arg, argp->port_arg, false)) {
exit(1);
}
addr_rx = *(struct sockaddr_in *) addr_info->ai_addr;
} else {
addr_rx = addr_rx_if;
}
if (udp_addr(&addr_info, argp->out_arg, argp->out_port_arg, false)) {
exit(1);
}
addr_tx = *(struct sockaddr_in *)addr_info->ai_addr;
if ((fd_rx = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket(rx)");
exit(1);
}
if ((fd_tx = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket(tx)");
exit(1);
}
int opt = 1;
setsockopt(fd_rx, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
setsockopt(fd_rx, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
setsockopt(fd_tx, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
// [Multicast] sender binds to outgoing interface
if (bind(fd_tx, (struct sockaddr *)&addr_tx_if, sizeof(addr_tx_if))) {
perror("bind(tx)");
exit(1);
}
socklen_t socklen = sizeof(addr_tx_if);
getsockname(fd_tx, (struct sockaddr *) &addr_tx_if, &socklen);
// [Multicast] receiver binds to source address
if (bind(fd_rx, (struct sockaddr *)&addr_rx, sizeof(addr_rx))) {
perror("bind(rx)");
exit(1);
}
if (IN_MULTICAST(ntohl(addr_rx.sin_addr.s_addr))) {
mreq.imr_interface = addr_rx_if.sin_addr;
mreq.imr_multiaddr = addr_rx.sin_addr;
if (setsockopt(fd_rx, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) {
perror("setsockopt(rx/IP_ADD_MEMBERSHIP)");
exit(1);
}
} else if (argp->iface_arg && argp->in_arg) {
fputs("ignoring input interface\n", stderr);
}
if (IN_MULTICAST(ntohl(addr_tx.sin_addr.s_addr))) {
mreq.imr_interface = addr_tx_if.sin_addr;
mreq.imr_multiaddr = addr_tx.sin_addr;
if (setsockopt(fd_tx, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) {
perror("setsockopt(tx/IP_ADD_MEMBERSHIP)");
exit(1);
}
}
param.fd_rx = fd_rx;
param.fd_tx = fd_tx;
param.addr_tx = addr_tx;
param.verbose = argp->verbose_flag > 0;
char addr_rx_if_str[64], addr_tx_if_str[64], addr_rx_str[64], addr_tx_str[64];
strcpy(addr_rx_if_str, inet_ntoa(addr_rx_if.sin_addr));
strcpy(addr_tx_if_str, inet_ntoa(addr_tx_if.sin_addr));
strcpy(addr_rx_str, inet_ntoa(addr_rx.sin_addr));
strcpy(addr_tx_str, inet_ntoa(addr_tx.sin_addr));
printf("<%s> %s:%u -> <%s:%u> %s:%u\n",
addr_rx_if_str, addr_rx_str, ntohs(addr_rx.sin_port),
addr_tx_if_str, ntohs(addr_tx_if.sin_port), addr_tx_str, ntohs(addr_tx.sin_port)
);
yuck_free(argp);
start(¶m);
while (getchar() != '\n');
stop();
return 0;
}
static void start(param_t *param)
{
sem_init(&queue.sem_consume, 0, 0);
pthread_create(&queue.thr_prod, NULL, rx_thread, param);
pthread_create(&queue.thr_cons, NULL, tx_thread, param);
}
static void stop(void)
{
if (queue.thr_prod) {
pthread_cancel(queue.thr_prod);
pthread_join(queue.thr_prod, NULL);
}
if (queue.thr_cons) {
pthread_cancel(queue.thr_cons);
pthread_join(queue.thr_cons, NULL);
}
}
static void *rx_thread(void *param)
{
const param_t *const p = (param_t *)param;
const int fd = p->fd_rx;
const bool verbose = p->verbose;
queue_entry_t *entry;
buf_t buf[MAX_DGRAM_LEN];
struct sockaddr_in src;
socklen_t src_len;
ssize_t len;
set_thread_prio(true);
while (true) {
src_len = sizeof(src);
len = recvfrom(fd, buf, MAX_DGRAM_LEN, 0, (struct sockaddr *)&src, &src_len);
if (len <= 0) {
if (errno == ENOTCONN) {
sleep(1);
continue;
}
if (errno) perror("recvfrom");
else fprintf(stderr, "rx: len=%li\n", len);
sleep(1);
continue;
}
if (verbose) {
if (src_len >= sizeof(src)) printf("[%s:%u] ", inet_ntoa(src.sin_addr), ntohs(src.sin_port));
print_hex(stdout, buf, (u32)len, false);
fflush(stdout);
}
while ((queue.qi_prod + 1) % MAX_QUEUE == queue.qi_cons) {
usleep(0);
}
entry = &queue.entry[queue.qi_prod];
entry->size = (size_t) len;
entry->data = calloc(1, entry->size);
memcpy(entry->data, buf, entry->size);
queue.qi_prod = (queue.qi_prod + 1) % MAX_QUEUE;
sem_post(&queue.sem_consume);
}
}
static void *tx_thread(void *param)
{
const param_t *const p = (param_t *)param;
const int fd = p->fd_tx;
const struct sockaddr_in *const addr = &p->addr_tx;
queue_entry_t *entry;
ssize_t len;
set_thread_prio(false);
while (true) {
sem_wait(&queue.sem_consume);
entry = &queue.entry[queue.qi_cons];
send:
len = sendto(fd, entry->data, entry->size, MSG_WAITALL, (struct sockaddr *)addr, sizeof(*addr));
if (len != entry->size) {
if (len < 0 && errno == EINTR) goto send;
if (errno) perror("sendto");
else fprintf(stderr, "tx: len=%li exp=%li\n", len, entry->size);
}
free(entry->data);
queue.qi_cons = (queue.qi_cons + 1) % MAX_QUEUE;
}
}
static int udp_addr(struct addrinfo **addr, const char *host, const char *service, bool passive)
{
struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = IPPROTO_UDP,
.ai_flags = AI_ADDRCONFIG
};
if (passive) hints.ai_flags |= AI_PASSIVE;
const int err = getaddrinfo(host, service, &hints, addr);
if (err) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
}
return err;
}