-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcphub.c
214 lines (188 loc) · 6.2 KB
/
tcphub.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
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h> // O_NONBLOCK
#include <signal.h>
#include <arpa/inet.h>
#include "server.h"
#include "client.h"
#include "decoder.h"
#define MAX_CONNECTION 64
#define ENV_PORT "TCPHUB_PORT"
#define ENV_DECODER "TCPHUB_DECODER"
#define ENV_DECODER_DIR "TCPHUB_DECODER_DIR"
#define DEFAULT_DECODER "text"
#define DEFAULT_DECODER_DIR "tcphub"
#define TOOMANYCONNECTION "Sorry, too many connection, try again later."
void usage(const char *cmd);
void hub(char *port);
int main(int argc, char *argv[])
{
char *port = NULL;
static char *decoder = DEFAULT_DECODER;
char *opt_decoder = NULL;
static char *decoder_dir = DEFAULT_DECODER_DIR;
char *opt_decoder_dir = NULL;
char *env;
env = getenv(ENV_PORT);
if ((env != NULL) && (strlen(env) != 0)) {
if ((port = malloc(strlen(env) + 1)) == NULL) {
perror("malloc");
return EXIT_FAILURE;
}
strcpy(port, env);
}
env = getenv(ENV_DECODER);
if ((env != NULL) && (strlen(env) != 0)) {
opt_decoder = malloc(strlen(env) + 1);
if (opt_decoder == NULL) {
perror("malloc");
return EXIT_FAILURE;
}
strcpy(opt_decoder, env);
decoder = opt_decoder;
}
env = getenv(ENV_DECODER_DIR);
if ((env != NULL) && (strlen(env) != 0)) {
opt_decoder_dir = malloc(strlen(env) + 1);
if (opt_decoder_dir == NULL) {
perror("malloc");
return EXIT_FAILURE;
}
strcpy(opt_decoder_dir, env);
decoder_dir = opt_decoder_dir;
}
if (port == NULL) {
if (argc < 2) {
usage(argv[0]);
return EXIT_FAILURE;
}
port = argv[1];
}
if (argc > 2) {
decoder = argv[2];
}
if (argc > 3) {
decoder_dir = argv[3];
}
if (argc > 4) {
usage(argv[0]);
return EXIT_FAILURE;
}
if (load_decoder(decoder, decoder_dir) < 0) {
fprintf(stderr, "Error: can't load decoder '%s'\n", decoder);
return EXIT_FAILURE;
}
printf("Start tcphub on port %s\n", port);
fflush(stdout);
hub(port);
return EXIT_SUCCESS;
}
void hub(char *port)
{
int sockserver;
struct sockaddr_in address;
socklen_t length = sizeof(struct sockaddr_in);
Client clientv[MAX_CONNECTION];
int clientc = 0;
int optvalue = 1;
int i, j, ret;
signal(SIGPIPE, SIG_IGN);
if ((sockserver = create_tcp_server(NULL, port)) < 0) {
exit(EXIT_FAILURE);
}
if (setsockopt(sockserver, SOL_SOCKET, SO_OOBINLINE, &optvalue,
sizeof(i)) < 0) {
perror("setsocketopt");
fprintf(stderr, "WARNING: out-of-band data will be ignored.\n");
}
fd_set readfds, writefds;
while (1) {
FD_ZERO(&readfds);
FD_SET(sockserver, &readfds);
FD_ZERO(&writefds);
for (i = 0 ; i < clientc ; i++) {
FD_SET(client_getsocket(clientv+i), &readfds);
if (client_waitingtosend(clientv+i)) {
FD_SET(client_getsocket(clientv+i), &writefds);
}
}
if (select(FD_SETSIZE, &readfds, &writefds, NULL, NULL) < 0) {
if (errno == EINTR) {
continue;
} else {
perror("select");
exit(EXIT_FAILURE);
}
}
if (FD_ISSET(sockserver, &readfds)) {
int sock = accept4(sockserver, (struct sockaddr*)&address, &length,
O_NONBLOCK);
if (sock < 0) {
if (errno == EBADF || errno == EFAULT || errno == EMFILE
|| errno == ENFILE || errno == ENOMEM) {
perror("accept");
exit(EXIT_FAILURE);
} else {
perror("WARNING (accept)"); // FIXME
}
}
if (clientc == MAX_CONNECTION) {
write(sock, TOOMANYCONNECTION, strlen(TOOMANYCONNECTION));
close(sock);
} else {
if (setsockopt(sock, SOL_SOCKET, SO_OOBINLINE, &optvalue,
sizeof(i)) < 0) {
perror("setsocketopt");
fprintf(stderr, "WARNING: "
"out-of-band data will be ignored.\n");
}
if (client_new(clientv+clientc, sock) < 0) {
exit(EXIT_FAILURE);
}
clientc++;
printf("Client connected, total: %d\n", clientc);
}
}
for (i = 0 ; i < clientc ; i++) {
if (FD_ISSET(client_getsocket(clientv+i), &readfds)) {
if ((ret = client_recv(clientv+i)) < 0) {
client_delete(clientv+i);
if (clientc-i-1) {
memmove(clientv+i, clientv+i+1,
(clientc-i-1) * sizeof(*clientv));
}
clientc--;
i--;
printf("Client disconnected, total: %d\n", clientc);
} else {
while (client_decode(clientv+i)) {
for (j = 0 ; j < clientc ; j++) {
if (j != i) {
client_sendfrom(clientv+j, clientv+i);
}
}
client_cleartrame(clientv+i);
}
}
}
if (FD_ISSET(client_getsocket(clientv+i), &writefds)) {
if ((ret = client_send(clientv+i)) < 0) {
client_delete(clientv+i);
if (clientc-i-1) {
memmove(clientv+i, clientv+i+1,
(clientc-i-1) * sizeof(*clientv));
}
clientc--;
i--;
printf("Client disconnected, total: %d\n", clientc);
}
}
}
}
}
void usage(const char *cmd)
{
printf("Usage: %s port [decoder [decoders directory]]\n", cmd);
}