-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrocksockserver.c
297 lines (267 loc) · 7.21 KB
/
rocksockserver.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
/*
*
* author: rofl0r
*
* License: LGPL 2.1+ with static linking exception
*
*
*/
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include "rocksockserver.h"
#include "endianness.h"
#define LOGP(X) do { if(srv->perr) srv->perr(X); } while(0)
#ifdef USE_LIBULZ
#include "../lib/include/strlib.h"
#include "../lib/include/timelib.h"
#else
#include <stdio.h>
#include <arpa/inet.h>
#define microsleep(X) usleep(X)
static inline char* my_intToString(int i, char *b, size_t s) {
int x = snprintf(b, s, "%d", i);
if(x > 0 && x < s) return b;
return 0;
}
#define intToString(i, b) my_intToString(i, b, sizeof(b))
#define ipv4fromstring(s, b) inet_aton(s, (struct in_addr *)(void*)(b))
#endif
typedef struct {
const char* host;
unsigned short port;
#ifndef IPV4_ONLY
struct addrinfo* hostaddr;
#else
struct sockaddr_in hostaddr;
#endif
} rs_hostInfo;
int rocksockserver_resolve_host(rs_hostInfo* hostinfo) {
if (!hostinfo || !hostinfo->host || !hostinfo->port) return -1;
#ifndef IPV4_ONLY
char pbuf[8];
char* ports;
int ret;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if(!(ports = intToString(hostinfo->port, pbuf))) return -1;
return getaddrinfo(hostinfo->host, ports, &hints, &hostinfo->hostaddr);
#else
memset(&hostinfo->hostaddr, 0, sizeof(struct sockaddr_in));
ipv4fromstring(hostinfo->host, (unsigned char*) &hostinfo->hostaddr.sin_addr);
hostinfo->hostaddr.sin_family = AF_INET;
hostinfo->hostaddr.sin_port = htons(hostinfo->port);
return 0;
#endif
}
/* returns 0 on success.
possible error return codes:
-1: erroneus parameter
-2: bind() failed
-3: socket() failed
-4: listen() failed
positive number: dns error, pass to gai_strerror()
*/
int rocksockserver_init(rocksockserver* srv, const char* listenip, unsigned short port, void* userdata) {
int ret = 0;
int yes = 1;
rs_hostInfo conn;
if(!srv || !listenip || !port) return -1;
conn.host = listenip;
conn.port = port;
FD_ZERO(&srv->master);
srv->userdata = userdata;
srv->sleeptime_us = 20000; // set a reasonable default value. it's a compromise between throughput and cpu usage basically.
ret = rocksockserver_resolve_host(&conn);
if(ret) return ret;
#ifndef IPV4_ONLY
struct addrinfo* p;
for(p = conn.hostaddr; p != NULL; p = p->ai_next) {
srv->listensocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (srv->listensocket < 0) {
continue;
}
// lose the pesky "address already in use" error message
setsockopt(srv->listensocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(srv->listensocket, p->ai_addr, p->ai_addrlen) < 0) {
close(srv->listensocket);
continue;
}
break;
}
if (!p) {
LOGP("bind");
ret = -2;
}
freeaddrinfo(conn.hostaddr);
if(ret == -2) return -2;
#else
srv->listensocket = socket(AF_INET, SOCK_STREAM, 0);
if(srv->listensocket < 0) {
LOGP("socket");
return -3;
}
setsockopt(srv->listensocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if(bind(srv->listensocket, (struct sockaddr*) &conn.hostaddr, sizeof(struct sockaddr_in)) < 0) {
close(srv->listensocket);
LOGP("bind");
return -2;
}
#endif
// listen
if (listen(srv->listensocket, 10) == -1) {
LOGP("listen");
ret = -4;
} else {
FD_SET(srv->listensocket, &srv->master);
srv->maxfd = srv->listensocket;
}
return ret;
}
int rocksockserver_disconnect_client(rocksockserver* srv, int client) {
if(client < 0 || client > USER_MAX_FD) return -1;
if(FD_ISSET(client, &srv->master)) {
close(client);
FD_CLR(client, &srv->master);
if(client == srv->maxfd)
srv->maxfd--;
srv->numfds--;
return 0;
}
return 1;
}
void rocksockserver_watch_fd(rocksockserver* srv, int newfd) {
FD_SET(newfd, &srv->master);
if (newfd > srv->maxfd)
srv->maxfd = newfd;
}
int rocksockserver_loop(rocksockserver* srv,
char* buf, size_t bufsize,
int (*on_clientconnect) (void* userdata, struct sockaddr_storage* clientaddr, int fd),
int (*on_clientread) (void* userdata, int fd, size_t nread),
int (*on_clientwantsdata) (void* userdata, int fd),
int (*on_clientdisconnect) (void* userdata, int fd)
) {
fd_set read_fds, write_fds;
int newfd, k;
int lastfd = 3;
#ifdef IS_LITTLE_ENDIAN
int i;
size_t j;
#endif
ptrdiff_t nbytes;
struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen;
char* fdptr;
fd_set* setptr;
for(;;) {
read_fds = srv->master;
write_fds = srv->master;
if ((srv->numfds = select(srv->maxfd+1, &read_fds, &write_fds, NULL, NULL)) && srv->numfds == -1)
LOGP("select");
if(!srv->numfds) continue;
// optimization for the case searched_fd = lastfd, when we only have to handle one connection.
// i guess that should be the majority of cases.
k = lastfd;
setptr = &write_fds;
if(FD_ISSET(k, setptr)) goto gotcha;
setptr = &read_fds;
if(FD_ISSET(k, setptr)) goto gotcha;
nextfd:
setptr = &write_fds;
loopstart:
fdptr = (char*) setptr;
#ifdef IS_LITTLE_ENDIAN
for(i = 0; i * CHAR_BIT <= srv->maxfd; i+= sizeof(size_t)) { // we assume that sizeof(fd_set) is a multiple of sizeof(size_t)
if( *(size_t*)(fdptr + i)) {
for(j = 0; j <= sizeof(size_t); j++) {
if(fdptr[i + j]) {
for(k = (i + j) * CHAR_BIT; k <= srv->maxfd; k++) {
#else
for(k = 0; k <= srv->maxfd; k++) {
#endif
if(FD_ISSET(k, setptr)) {
gotcha:
srv->numfds--;
FD_CLR(k, setptr);
if(setptr == &write_fds)
goto handlewrite;
else
goto handleread;
}
}
#ifdef IS_LITTLE_ENDIAN
}
}
}
}
#endif
if(setptr == &write_fds) {
setptr = &read_fds;
goto loopstart;
} else {
LOGP("FATAL");
/*
printf("maxfd %d, k %d, numfds %d, set %d\n", srv->maxfd, k, srv->numfds, *(int*)(fdptr));
for(k = 0; k < USER_MAX_FD; k++)
if(FD_ISSET(k, setptr))
printf("bit set: %d\n", k);
*/
return 1;
}
handleread:
//printf("read_fd %d\n", k);
if (k == srv->listensocket) {
// new connection available
addrlen = sizeof(remoteaddr);
newfd = accept(srv->listensocket, (struct sockaddr *)&remoteaddr, &addrlen);
if (newfd == -1) {
LOGP("accept");
} else {
if(newfd >= USER_MAX_FD)
close(newfd); // only USER_MAX_FD connections can be handled.
else {
FD_SET(newfd, &srv->master);
if (newfd > srv->maxfd)
srv->maxfd = newfd;
if(on_clientconnect) on_clientconnect(srv->userdata, &remoteaddr, newfd);
}
}
} else {
if(buf && k != srv->signalfd) {
if ((nbytes = recv(k, buf, bufsize, 0)) <= 0) {
if (nbytes == 0) {
if(on_clientdisconnect) on_clientdisconnect(srv->userdata, k);
} else {
LOGP("recv");
}
rocksockserver_disconnect_client(srv, k);
} else {
if(on_clientread) on_clientread(srv->userdata, k, nbytes);
}
} else {
if(on_clientread) on_clientread(srv->userdata, k, 0);
}
}
goto zzz;
handlewrite:
//printf("write_fd %d\n", k);
if(on_clientwantsdata) on_clientwantsdata(srv->userdata, k);
zzz:
if(srv->numfds > 0) goto nextfd;
lastfd = k;
microsleep(srv->sleeptime_us);
}
return 0;
}