-
Notifications
You must be signed in to change notification settings - Fork 1
/
nethub.c
290 lines (267 loc) · 7.21 KB
/
nethub.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
const char *version = "nethub version 1.1\n";
const char *usage =
"Usage: nethub --help\n"
" nethub [-v] -u SOCKET\n"
" nethub [-v] [-46] [-l ADDRESS] -p PORT\n";
const char *help =
"Options:\n"
" -h, --help print this help\n"
" -v, --verbose no need to explain\n"
" -n, --slots NUM specify the maximum number of clients\n"
" -u, --socket PATH create a Unix domain socket\n"
" -p, --port PORT open a TCP server\n"
" -4, --ipv4 force IPv4\n"
" -6, --ipv6 force IPv6\n"
" -l, --bind ADDRESS specify the address to bind (TCP)\n";
int print_help = 0;
int verbose = 0;
int slots = 32;
char *socket_path = NULL;
char *port = NULL;
char *bind_address = NULL;
int family = AF_UNSPEC;
int server = 0;
int *clients = NULL;
static struct option options[] = {
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"socket", required_argument, 0, 'u'},
{"port", required_argument, 0, 'p'},
{"bind", required_argument, 0, 'l'},
{"ipv4", no_argument, 0, '4'},
{"ipv6", no_argument, 0, '6'},
{"slots", required_argument, 0, 'n'},
{0, 0, 0, 0}
};
int parse_args(int argc, char **argv){
int c;
while((c = getopt_long(argc, argv, "hvu:p:l:n:46", options, NULL)) != -1){
switch(c){
case 'h':
print_help = 1;
return 1;
case 'v':
verbose = 1;
break;
case 'u':
socket_path = optarg;
break;
case 'p':
port = optarg;
break;
case 'l':
bind_address = optarg;
break;
case 'n':
slots = atoi(optarg);
if(slots <= 0){
fprintf(stderr, "invalid number of slots: %s\n", optarg);
return 0;
}
break;
case '4':
family = AF_INET;
break;
case '6':
family = AF_INET6;
break;
default:
return 0;
}
}
if(optind != argc){
fputs("invalid arguments; see --help\n", stderr);
return 0;
}
if(socket_path == NULL && port == NULL){
fputs("no socket or port specified\n", stderr);
return 0;
}
return 1;
}
int server_init_unix(){
server = socket(AF_UNIX, SOCK_STREAM, 0);
if(server == -1){
perror("socket");
return 0;
}
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, socket_path);
int len = strlen(addr.sun_path) + sizeof(addr.sun_family);
if(bind(server, (struct sockaddr*) &addr, len) == -1){
perror("bind");
return 0;
}
if(verbose)
fprintf(stderr, "listening on %s\n", socket_path);
return 1;
}
int server_init_tcp(){
struct addrinfo *addrs, *addr;
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int rc = getaddrinfo(bind_address, port, &hints, &addrs);
if(rc != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rc));
return 0;
}
for(addr = addrs; addr != NULL; addr = addr->ai_next){
server = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if(server == -1)
continue;
if(bind(server, addr->ai_addr, addr->ai_addrlen) != -1)
break;
close(server);
}
freeaddrinfo(addrs);
if(addr == NULL){
fputs("no address to bind\n", stderr);
return 0;
}
if(verbose)
fprintf(stderr, "listening on %s, port %s\n", (bind_address == NULL ? "*" : bind_address), port);
return 1;
}
int server_accept(){
struct sockaddr_in incomer;
socklen_t len = sizeof(incomer);
memset(&incomer, 0, len);
int fd = accept(server, (struct sockaddr*) &incomer, &len);
if(fd == -1){
perror("accept");
return 0;
}
int i;
for(i=0; i<slots; i++){
if(clients[i] == -1){
clients[i] = fd;
if(verbose)
fprintf(stderr, "incoming connection (fd %d)\n", fd);
return 1;
}
}
fputs("can't accept more connections\n", stderr);
close(fd);
return 0;
}
void shut(int *fd){
if(*fd < 0)
return;
shutdown(*fd, SHUT_RDWR);
close(*fd);
if(verbose)
fprintf(stderr, "connection closed (fd %d)\n", *fd);
*fd = -1;
}
void clean_up(){
close(server);
int i;
for(i=0; i<slots; i++)
shut(clients+i);
free(clients);
if(socket_path != NULL)
unlink(socket_path);
exit(EXIT_SUCCESS);
}
int forward(int src){
static char buffer[512];
ssize_t sz = recv(src, &buffer, 512, 0);
if(sz == -1)
perror("recv");
if(sz <= 0)
return 0;
int i;
for(i=0; i<slots; i++){
if(clients[i] != -1 && clients[i] != src){
if(send(clients[i], buffer, sz, 0) == -1)
perror("send");
}
}
return 1;
}
#undef max
#define max(x,y) ((x) > (y) ? (x) : (y))
int main(int argc, char **argv){
if(argc == 1){
fputs(version, stdout);
fputs(usage, stdout);
return EXIT_SUCCESS;
}
if(!parse_args(argc, argv))
return EXIT_FAILURE;
if(print_help){
puts(version);
puts(usage);
puts(help);
puts("See the man page for extensive documentation.");
return EXIT_SUCCESS;
}
if(socket_path != NULL){
if(!server_init_unix())
return EXIT_FAILURE;
}
else if(!server_init_tcp())
return EXIT_FAILURE;
if(listen(server, slots) == -1){
perror("listen");
close(server);
return EXIT_FAILURE;
}
clients = (int*) malloc(slots * sizeof(int));
int i;
for(i=0; i<slots; i++)
clients[i] = -1;
struct sigaction action;
action.sa_handler = clean_up;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
sigaction(SIGINT, &action, NULL);
sigaction(SIGHUP, &action, NULL);
sigaction(SIGTERM, &action, NULL);
int nfds, rc;
fd_set rd;
while(1){
FD_ZERO(&rd);
FD_SET(server, &rd);
nfds = server;
for(i=0; i<slots; i++){
if(clients[i] >= 0){
FD_SET(clients[i], &rd);
nfds = max(nfds, clients[i]);
}
}
rc = select(nfds+1, &rd, NULL, NULL, NULL);
if(rc == -1 && errno == EINTR)
continue;
if(rc == -1){
perror("select");
break;
}
if(FD_ISSET(server, &rd))
server_accept();
for(i=0; i<slots; i++){
if(clients[i] < 0)
continue;
if(FD_ISSET(clients[i], &rd)){
if(!forward(clients[i]))
shut(clients+i);
}
}
}
clean_up();
return EXIT_SUCCESS;
}