-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
282 lines (247 loc) · 6.23 KB
/
server.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
/* ------------------------------------------------------------
* Server/Connection management
* XXX TODO: handle timeouts better. Set time?
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdbool.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>
#include "error.h"
#include "server.h"
struct Connection
{
Server *server;
char *id;
int fd;
Connection *next;
Connection *prev;
void (*can_write) (Connection * n, void *h);
void (*can_read) (Connection * n, void *h);
void (*except) (Connection * n, void *h);
void (*timeout) (Connection * n, void *h);
void *h;
};
struct Server
{
Connection *first;
Connection *last;
struct timeval timeout;
void *h;
};
Server *
new_server (void *h)
{
Server *v = malloc (sizeof *v);
v->first = NULL;
v->last = NULL;
v->h = h;
return v;
}
Connection *
new_connection (Server * v, int fd, char *id, void *h)
{
Connection *n = malloc (sizeof *n);
n->server = v;
n->next = NULL;
n->prev = v->last;
if (v->last)
v->last->next = n;
n->fd = fd;
n->id = strdup(id ? id : "(unnamed)");
n->can_write = NULL;
n->can_read = NULL;
n->except = NULL;
n->timeout = NULL;
if (!v->first)
v->first = n;
v->last = n;
n->h = h;
return n;
}
void
connection_remove (Connection * n)
{
Server *v = n->server;
if (n == v->first)
v->first = n->next;
if (n == v->last)
v->last = n->prev;
if (n->next)
n->next->prev = n->prev;
if (n->prev)
n->prev->next = n->next;
free (n->id);
free (n);
}
void
connection_write (Connection * n, const char *data, int count)
{
/* Temporarily set the connection blocking. Blugh. */
fcntl (n->fd, F_SETFL, fcntl (n->fd, F_GETFL) & ~O_NONBLOCK);
for (;;)
{
int written;
if (count <= 0)
break;
written = write (n->fd, data, count);
if (written < 0)
fatal (0, "Couldn't write to connection '%s'", n->id);
count -= written;
data += written;
}
/* Set non-blocking again */
fcntl (n->fd, F_SETFL, fcntl (n->fd, F_GETFL) | O_NONBLOCK);
}
Connection *
server_listenport (Server * v, int port, int backlog, void *h)
{
int fd;
int reuseaddr = 1;
int opts;
struct sockaddr_in sa;
char buffer[BUFSIZ];
/* Set it up */
fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0)
fatal (0, "Couldn't open socket");
if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
&reuseaddr, sizeof reuseaddr) < 0)
fatal (0, "Couldn't set socket options");
opts = fcntl (fd, F_GETFL);
if (opts < 0)
fatal (0, "Couldn't get opts for socket");
if (fcntl (fd, F_SETFL, opts | O_NONBLOCK) < 0)
fatal (0, "Couldn't set socket non-blocking");
opts = fcntl (fd, F_GETFL);
/* Bind to the port */
memset (&sa, '\0', sizeof sa);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons (port);
if (bind (fd, (struct sockaddr *) &sa, sizeof sa) < -1)
fatal (0, "Couldn't bind port %d", port);
if (listen (fd, backlog) < -1)
fatal (0, "Couldn't listen on port %d", port);
sprintf (buffer, "listen :%d", port);
return new_connection (v, fd, buffer, h);
}
Connection *
connection_port (Server * v, char *hostname, int port, void *h)
{
int socket_fd;
struct hostent *hostinfo;
struct sockaddr_in __hostaddr, *hostaddr = &__hostaddr;
char buffer[BUFSIZ];
memset (hostaddr, 0, sizeof (struct sockaddr));
/* Look up address */
hostinfo = gethostbyname (hostname);
if (!hostinfo)
/* fatal (0, "Can't resolve hostname '%s'", hostname); */
return NULL;
hostaddr->sin_family = hostinfo->h_addrtype;
memcpy (&(hostaddr->sin_addr), hostinfo->h_addr, hostinfo->h_length);
hostaddr->sin_port = htons (port);
socket_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socket_fd == -1)
fatal (0, "Can't create socket");
if (connect (socket_fd, (struct sockaddr *) hostaddr, sizeof (*hostaddr)))
return NULL;
sprintf (buffer, "%s:%d", hostname, port);
return new_connection (v, socket_fd, buffer, h);
}
void server_set_timeout (Server *v, int timeout)
{
v->timeout.tv_sec = timeout / 1000000;
v->timeout.tv_usec = timeout % 1000000;
}
void server_select (Server * v)
{
Connection *n, *next_n;
fd_set readfds, writefds, exceptfds;
int high_fd;
struct timeval timeout;
int rv;
int count_active = 0;
FD_ZERO (&readfds);
FD_ZERO (&writefds);
FD_ZERO (&exceptfds);
timeout = v->timeout;
if (v->first == NULL)
fatal (0, "Attempt to select on a Server with no connections");
high_fd = v->first->fd;
for (n = v->first; n; n = n->next)
{
if (!(n->can_write || n->can_read || n->except))
continue;
if (n->fd > high_fd)
high_fd = n->fd;
count_active++;
if (n->can_write)
FD_SET (n->fd, &writefds);
if (n->can_read)
FD_SET (n->fd, &readfds);
if (n->except)
FD_SET (n->fd, &exceptfds);
}
rv = select (high_fd + 1, &readfds, &writefds, &exceptfds, &timeout);
if (rv == -1)
fatal (0, "Error in select");
if (rv == 0)
{
for (n = v->first; n; n = next_n)
{
next_n = n->next;
if (n->timeout)
n->timeout (n, n->h);
}
}
else
for (n = v->first; n; n = next_n)
{
next_n = n->next;
if (FD_ISSET (n->fd, &exceptfds))
n->except (n, n->h);
else if (FD_ISSET (n->fd, &readfds))
n->can_read (n, n->h);
else if (FD_ISSET (n->fd, &writefds))
n->can_write (n, n->h);
}
}
int connection_fd(Connection *n)
{
return n->fd;
}
void connection_set_can_read (Connection *n,
void (*can_read) (Connection * n, void *h))
{
n->can_read = can_read;
}
void connection_set_can_write (Connection *n,
void (*can_write) (Connection * n, void *h))
{
n->can_write = can_write;
}
void connection_set_except (Connection *n,
void (*except) (Connection * n, void *h))
{
n->except = except;
}
void connection_set_timeout (Connection *n,
void (*timeout) (Connection * n, void *h))
{
n->timeout = timeout;
}