-
Notifications
You must be signed in to change notification settings - Fork 4
/
mux.c
142 lines (122 loc) · 3.92 KB
/
mux.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
// Source <--> Muxer <--> Multiple Target
int main(int argc, char* argv[])
{
int ret, flags;
int listener, sender, ep;
int PORT_ENDP=29000, PORT_UR_BEGIN=29100, PORT_UR_SIZE=10;
while((ret = getopt(argc, argv, "hl:t:s:")) != -1)
{
switch(ret)
{
case 'l':
PORT_ENDP=atoi(optarg);
break;
case 't':
PORT_UR_BEGIN=atoi(optarg);
break;
case 's':
PORT_UR_SIZE=atoi(optarg);
break;
case 'h':
default:
fprintf(stderr, "Usage: %s -l ListenPort -t TargetPort -s TargetRange\n", argv[0]);
exit(1);
}
}
fprintf(stderr, "listening on %d, sending to [%d,%d)\n", PORT_ENDP, PORT_UR_BEGIN, PORT_UR_BEGIN+PORT_UR_SIZE);
listener = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons(PORT_ENDP);
ret = bind(listener, (const struct sockaddr*)&saddr, sizeof(saddr));
if (ret < 0) {
perror("bind");
exit(1);
}
sender = socket(AF_INET, SOCK_DGRAM, 0);
// Set to non-blocking
flags = fcntl(listener, F_GETFL, 0);
if (flags < 0)
{
perror("fcntl");
exit(1);
}
flags |= O_NONBLOCK;
fcntl(listener, F_SETFL, flags);
flags = fcntl(sender, F_GETFL, 0);
if (flags < 0)
{
perror("fcntl");
exit(1);
}
flags |= O_NONBLOCK;
fcntl(sender, F_SETFL, flags);
ep = epoll_create(1024);
struct epoll_event ev1;
ev1.events = EPOLLIN | EPOLLET;
ev1.data.fd = listener;
struct epoll_event ev2;
ev2.events = EPOLLIN | EPOLLET;
ev2.data.fd = sender;
epoll_ctl(ep, EPOLL_CTL_ADD, listener, &ev1);
epoll_ctl(ep, EPOLL_CTL_ADD, sender, &ev2);
struct epoll_event events[1024];
struct sockaddr_in* addrTargets = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in) * PORT_UR_SIZE);
memset(addrTargets, 0, sizeof(struct sockaddr_in) * PORT_UR_SIZE);
for(int i=0; i<PORT_UR_SIZE; i++) {
addrTargets[i].sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &addrTargets[i].sin_addr);
addrTargets[i].sin_port = htons(PORT_UR_BEGIN + i);
}
struct sockaddr_in addrFrom;
memset(&addrFrom, 0, sizeof(addrFrom));
int rrOffset = 0;
char buffer[4096];
while(1)
{
int nfds = epoll_wait(ep, events, sizeof(events), -1);
if (nfds < 0) {
perror("epoll_wait");
break;
}
for (int i=0;i<nfds; i++)
{
if (events[i].data.fd == listener)
{
int nsize;
socklen_t socklen = sizeof(addrFrom);
while((nsize = recvfrom(listener, buffer, sizeof(buffer), 0, (struct sockaddr*)&addrFrom, &socklen)) > 0) {
sendto(sender, buffer, nsize, 0, (const struct sockaddr*)(addrTargets + rrOffset), sizeof(struct sockaddr_in));
if (++rrOffset >= PORT_UR_SIZE) {
rrOffset = 0;
}
}
if (nsize < 0 && errno != EAGAIN) {
perror("recvfrom: ");
}
}
else if (events[i].data.fd == sender)
{
int nsize;
while((nsize = recvfrom(sender, buffer, sizeof(buffer), 0, NULL, NULL)) > 0) {
sendto(listener, buffer, nsize, 0, (const struct sockaddr*)&addrFrom, sizeof(struct sockaddr_in));
}
if (nsize < 0 && errno != EAGAIN) {
perror("recvfrom: ");
}
}
}
}
}