-
Notifications
You must be signed in to change notification settings - Fork 3
/
ctl_server.c
370 lines (329 loc) · 9.19 KB
/
ctl_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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* $Id: ctl_server.c,v 1.6 2008-09-08 05:35:52 niallo Exp $ */
/*
* Copyright (c) 2007, 2008 Niall O'Higgins <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/time.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <ctype.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "includes.h"
char *gui_port;
static void ctl_server_handle_connect(struct bufferevent *, short, void *);
static void ctl_server_conn_bootstrap(struct ctl_server_conn *);
static struct ctl_server_conn * ctl_server_conn_create(struct ctl_server *);
static void ctl_server_conn_free(struct ctl_server_conn *);
static void ctl_server_handle_conn_message(struct bufferevent *, void *);
static void ctl_server_handle_conn_error(struct bufferevent *, short, void *);
static void ctl_server_broadcast_message(struct ctl_server *, char *);
static void ctl_server_write_message(struct ctl_server_conn *, char *);
static char * ctl_server_pieces(struct session *);
static char * ctl_server_peers(struct session *);
/*
* ctl_server_start()
*
* Start a new control server for the specified session on the specified port.
*/
void
ctl_server_start(struct session *sc, char *port, off_t started)
{
struct ctl_server *cs;
cs = xmalloc(sizeof(*cs));
memset(cs, 0, sizeof(*cs));
TAILQ_INIT(&cs->conns);
sc->ctl_server = cs;
cs->started = started;
cs->sc = sc;
cs->fd = network_listen("0.0.0.0", port);
cs->bev = bufferevent_new(cs->fd, NULL, NULL,
ctl_server_handle_connect, cs);
if (cs->bev == NULL)
errx(1, "ctl_server_start: bufferevent_new failure");
bufferevent_enable(cs->bev, EV_PERSIST|EV_READ);
}
/*
* ctl_server_notify_bytes()
*
* Notify control connections of number of bytes received.
*/
void
ctl_server_notify_bytes(struct session *sc, off_t bytes)
{
char *msg;
int l;
if (sc->ctl_server == NULL)
return;
msg = xmalloc(CTL_MESSAGE_LEN);
memset(msg, '\0', CTL_MESSAGE_LEN);
l = snprintf(msg, CTL_MESSAGE_LEN, "bytes:%jd\r\n", (intmax_t)bytes);
if (l == -1 || l >= (int)CTL_MESSAGE_LEN)
errx(1, "ctl_server_notify_bytes() string truncation");
ctl_server_broadcast_message(sc->ctl_server, msg);
xfree(msg);
}
/*
* ctl_server_notify_pieces()
*
* Notify control connections of pieces received.
*/
void
ctl_server_notify_pieces(struct session *sc)
{
char *msg;
if (sc->ctl_server == NULL)
return;
msg = ctl_server_pieces(sc);
ctl_server_broadcast_message(sc->ctl_server, msg);
xfree(msg);
}
/*
* ctl_server_notify_peers()
*
* Notify control connections of connected peers.
*/
void
ctl_server_notify_peers(struct session *sc)
{
char *msg;
if (sc->ctl_server == NULL)
return;
msg = ctl_server_peers(sc);
ctl_server_broadcast_message(sc->ctl_server, msg);
xfree(msg);
}
/*
* ctl_server_handle_connect()
*
* Handle incoming connections to the control server.
*/
static void
ctl_server_handle_connect(struct bufferevent *bufev, short error, void *data)
{
struct ctl_server *cs;
struct ctl_server_conn *csc;
socklen_t addrlen;
trace("ctl_server_handle_connect() called");
if (error & EVBUFFER_TIMEOUT)
errx(1, "timeout");
if (error & EVBUFFER_EOF)
errx(1, "eof");
cs = data;
csc = ctl_server_conn_create(cs);
addrlen = sizeof(csc->sa);
trace("ctl_server_handle_connect() accepting connection");
if ((csc->fd = accept(cs->fd, (struct sockaddr *) &csc->sa, &addrlen)) == -1) {
trace("ctl_server_handle_connect() accept error");
ctl_server_conn_free(csc);
return;
}
trace("ctl_server_handle_connectt() accepted connection: %s:%d",
inet_ntoa(csc->sa.sin_addr), ntohs(csc->sa.sin_port));
csc->bev = bufferevent_new(csc->fd, ctl_server_handle_conn_message,
NULL, ctl_server_handle_conn_error, csc);
if (csc->bev == NULL)
errx(1, "ctl_server_handle_connect(): bufferevent_new failure");
bufferevent_enable(csc->bev, EV_READ|EV_WRITE);
cs->bev = bufferevent_new(cs->fd, NULL, NULL,
ctl_server_handle_connect, cs);
if (cs->bev == NULL)
errx(1, "ctl_server_start: bufferevent_new failure");
bufferevent_enable(cs->bev, EV_PERSIST|EV_READ);
TAILQ_INSERT_TAIL(&cs->conns, csc, conn_list);
ctl_server_conn_bootstrap(csc);
}
/*
* ctl_server_conn_create()
*
* Create a connection to the control server.
*/
static struct ctl_server_conn *
ctl_server_conn_create(struct ctl_server *cs)
{
struct ctl_server_conn *csc;
csc = xmalloc(sizeof(*csc));
memset(csc, 0, sizeof(*csc));
csc->cs = cs;
return (csc);
}
/*
* ctl_server_conn_free()
*
* Free a connection to the control server.
*/
static void
ctl_server_conn_free(struct ctl_server_conn *csc)
{
(void) close(csc->fd);
xfree(csc);
}
/*
* ctl_server_conn_bootstrap()
*
* Send initial state data to this new connection.
*/
static void
ctl_server_conn_bootstrap(struct ctl_server_conn *csc)
{
off_t len;
char *msg;
int l;
trace("bootstrapping");
#define BOOTSTRAP_LEN 1024
if (csc->cs->sc->tp->type == SINGLEFILE) {
len = csc->cs->sc->tp->body.singlefile.tfp.file_length;
} else {
len = csc->cs->sc->tp->body.multifile.total_length;
}
msg = xmalloc(BOOTSTRAP_LEN);
memset(msg, '\0', BOOTSTRAP_LEN);
l = snprintf(msg, BOOTSTRAP_LEN,
"num_peers:%u\r\nnum_pieces:%u\r\ntorrent_size:%jd\r\ntorrent_bytes:%jd\r\n",
csc->cs->sc->num_peers, csc->cs->sc->tp->num_pieces, (intmax_t)len, (intmax_t)csc->cs->started);
if (l == -1 || l >= (int)BOOTSTRAP_LEN)
errx(1, "ctl_server_conn_bootstrap() string truncation");
ctl_server_write_message(csc, msg);
xfree(msg);
msg = ctl_server_pieces(csc->cs->sc);
ctl_server_write_message(csc, msg);
xfree(msg);
msg = ctl_server_peers(csc->cs->sc);
ctl_server_write_message(csc, msg);
xfree(msg);
trace("bootstrapped");
}
/*
* ctl_server_handle_conn_message()
*
* Handle a message from a connection to the control server.
*/
static void
ctl_server_handle_conn_message(struct bufferevent *bufev, void *data)
{
/* TODO */
}
/*
* ctl_server_handle_conn_error()
*
* Handle a message from a connection to the control server.
*/
static void
ctl_server_handle_conn_error(struct bufferevent *bufev, short error, void *data)
{
struct ctl_server_conn *csc;
csc = data;
trace("ctl_server_handle_conn_error() freeing connection");
TAILQ_REMOVE(&csc->cs->conns, csc, conn_list);
ctl_server_conn_free(csc);
}
/*
* ctl_server_write_message()
*
* Write a message to a connection.
*/
static void
ctl_server_write_message(struct ctl_server_conn *csc, char *msg)
{
if (bufferevent_write(csc->bev, msg, strlen(msg)) != 0)
errx(1, "ctl_server_write_message() failure");
}
/*
* ctl_server_broadcast_message()
*
* Broadcast a message to all connections.
*/
static void
ctl_server_broadcast_message(struct ctl_server *cs, char *msg)
{
struct ctl_server_conn *csc;
TAILQ_FOREACH(csc, &cs->conns, conn_list)
ctl_server_write_message(csc, msg);
}
/*
* ctl_server_pieces()
*
* Allocate and return string containing pieces message.
*/
static char *
ctl_server_pieces(struct session *sc)
{
struct torrent_piece *tpp;
u_int32_t count, i, msglen;
char *msg, piece[8];
/* almost certainly too much space, but who cares */
msglen = CTL_MESSAGE_LEN + (5 * sc->tp->good_pieces);
msg = xmalloc(msglen);
memset(msg, '\0', msglen);
snprintf(msg, msglen, "pieces:");
count = 0;
for (i = 0; i < sc->tp->num_pieces; i++) {
tpp = torrent_piece_find(sc->tp, i);
if (tpp->flags & TORRENT_PIECE_CKSUMOK) {
count++;
if (count == sc->tp->good_pieces) {
snprintf(piece, sizeof(piece), "%u\r\n", i);
} else {
snprintf(piece, sizeof(piece), "%u,", i);
}
if (strlcat(msg, piece, msglen) >= msglen)
errx(1,
"ctl_server_pieces() string truncation");
}
}
return (msg);
}
/*
* ctl_server_peers()
*
* Allocate and return string containing peers message.
*/
static char *
ctl_server_peers(struct session *sc)
{
struct peer *p;
u_int32_t count, msglen;
char *msg, peer[32];
/* almost certainly too much space, but who cares */
msglen = CTL_MESSAGE_LEN + (32 * sc->num_peers);
msg = xmalloc(msglen);
memset(msg, '\0', msglen);
snprintf(msg, msglen, "peers:");
count = 0;
TAILQ_FOREACH(p, &sc->peers, peer_list) {
count++;
if (count == sc->num_peers) {
snprintf(peer, sizeof(peer), "%s:%d\r\n",
inet_ntoa(p->sa.sin_addr),
ntohs(p->sa.sin_port));
} else {
snprintf(peer, sizeof(peer), "%s:%d,",
inet_ntoa(p->sa.sin_addr),
ntohs(p->sa.sin_port));
}
if (strlcat(msg, peer, msglen) >= msglen)
errx(1, "ctl_server_peers() string truncation");
}
return (msg);
}