File: client.c

package info (click to toggle)
trickle 1.07-9
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 1,660 kB
  • ctags: 535
  • sloc: sh: 8,307; ansic: 4,722; makefile: 73
file content (334 lines) | stat: -rw-r--r-- 7,075 bytes parent folder | download | duplicates (8)
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
/*
 * client.c
 *
 * Copyright (c) 2003 Marius Aamodt Eriksen <marius@monkey.org>
 * All rights reserved.
 *
 * $Id: client.c,v 1.14 2003/05/09 02:16:42 marius Exp $
 */

#include <sys/types.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <sys/queue.h>
#include <sys/tree.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */

#include <event.h>
#ifdef HAVE_ERR_H
#include <err.h>
#endif /* HAVE_ERR_H */
#include <stdio.h>
#include <unistd.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */
#if defined(HAVE_TIME_H) && defined(TIME_WITH_SYS_TIME)
#include <time.h>
#endif /* defined(HAVE_TIME_H) && defined(TIME_WITH_SYS_TIME) */
#include <string.h>

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif /* HAVE_NETINET_IN_H */

#include "print.h"
#include "trickle.h"
#include "message.h"
#include "client.h"
#include "bwstat.h"
#include "util.h"
#include "xdr.h"

static int
clicmp(struct client *a, struct client *b)
{
	if (a->pid == b->pid)
		return (0);

	if (a->pid > b->pid)
		return (1);

	return (-1);
}

SPLAY_HEAD(clitree, client) clients;
SPLAY_PROTOTYPE(clitree, client, next, clicmp);
SPLAY_GENERATE(clitree, client, next, clicmp);

static void client_delaycb(int, short, void *);

void
client_init(uint winsz)
{	
	bwstat_init(winsz);

	SPLAY_INIT(&clients);
}

int
client_register(struct client *cli)
{
	if ((cli->stat = bwstat_new()) == NULL)
		return (-1);

	SPLAY_INSERT(clitree, &clients, cli);

	return (0);
}

int
client_configure(struct client *cli)
{
	struct bwstat *bs = cli->stat;

	if (cli->pri > 20)
		return (-1);
	bs->pts = 21 - cli->pri;	

	if (cli->tsmooth < 0.0)
		return (-1);
	bs->tsmooth = cli->tsmooth;

	if (cli->lsmooth == 0)
		return (-1);
	bs->lsmooth = cli->lsmooth;

	return (0);
}

void
client_unregister(struct client *cli)
{
	if (evtimer_initialized(&cli->delayev))
		evtimer_del(&cli->delayev);

	bwstat_free(cli->stat);

	SPLAY_REMOVE(clitree, &clients, cli);
}

void
client_getinfo(struct client *cli, uint sendlim, uint recvlim)
{
	struct bwstat *bs = bwstat_gettot();
	struct bwstat_data *bsdrecv = &bs->data[TRICKLE_RECV],
	    *bsdsend = &bs->data[TRICKLE_SEND];
	struct msg msg;
	struct msg_getinfo *getinfo = &msg.data.getinfo;

	memset(&msg, 0, sizeof(msg));
	msg.type = MSG_TYPE_GETINFO;

	getinfo->dirinfo[TRICKLE_SEND].rate = bsdsend->winrate;
	getinfo->dirinfo[TRICKLE_SEND].lim = sendlim;

	getinfo->dirinfo[TRICKLE_RECV].rate = bsdrecv->winrate;
	getinfo->dirinfo[TRICKLE_RECV].lim = recvlim;

	client_sendmsg(cli, &msg);
}

void
client_delay(struct client *cli, short which, size_t len, uint lim)
{
	struct timeval *tv;

	if ((tv = bwstat_getdelay(cli->stat, &len, lim, which)) != NULL) {
		warnxv(4, "Delay %d (%s/%s) by: %d bytes in %dsec%dusec START",
		    cli->pid, cli->argv0, cli->uname, len, tv->tv_sec,
		    tv->tv_usec);
		cli->delaytv = *tv;
		cli->delaylen = len;
		cli->delaywhich = which;
		evtimer_set(&cli->delayev, client_delaycb, cli);
		evtimer_add(&cli->delayev, &cli->delaytv);
	} else {
		struct msg msg;
		struct msg_delayinfo *delayinfo = &msg.data.delayinfo;

		msg.type = MSG_TYPE_CONT;
		delayinfo->len = len;
		if (client_sendmsg(cli, &msg) == -1)
			;	/* XXX delete client */
	}
}

void
client_getdelay(struct client *cli, short which, size_t len, uint lim)
{
	struct timeval *tv;
	struct msg msg;
	struct msg_delayinfo *delayinfo = &msg.data.delayinfo;

	memset(&msg, 0, sizeof(msg));

	if ((tv = bwstat_getdelay(cli->stat, &len, lim, which)) != NULL)
		delayinfo->delaytv = *tv;
	else
		SET(msg.status, MSG_STATUS_FAIL);

	delayinfo->len = len;
	tv = &delayinfo->delaytv;

	warnxv(3, "Returning delay %d (%s/%s) info: %d bytes in %dsec%dusec",
	    cli->pid, cli->argv0, cli->uname, len, tv->tv_sec, tv->tv_usec);

	msg.type = MSG_TYPE_DELAYINFO;

	client_sendmsg(cli, &msg);
}

static void
client_delaycb(int fd, short which, void *arg)
{
	struct client *cli = arg;
	struct msg msg;
	struct msg_delayinfo *delayinfo = &msg.data.delayinfo;

	warnxv(4, "Delay %d (%s/%s) by %dsec%dusec END", cli->pid,
	    cli->argv0, cli->uname, cli->delaytv.tv_sec, cli->delaytv.tv_usec);

	delayinfo->len = cli->delaylen;

	msg.type = MSG_TYPE_CONT;

	/* XXX on error */
	client_sendmsg(cli, &msg);
}

#if 0
static double
difftv(struct timeval *tv0, struct timeval *tv1)
{
	struct timeval diff_tv;

	timersub(tv0, tv1, &diff_tv);
        return (diff_tv.tv_sec + (diff_tv.tv_usec / 1000000.0));
}
#endif /* 0 */

void
client_update(struct client *cli, short which, size_t len)
{
	struct bwstat_data *bsd = &cli->stat->data[which];

	warnxv(4, "Statistics (%s) for %d (%s/%s):",
	    which == TRICKLE_SEND ? "SEND" : "RECV",
	    cli->pid, cli->argv0, cli->uname);

#if 0
	/* XXX for benchmarking. */
	if (which == TRICKLE_SEND) {
		struct timeval tv, xtv;
		static struct timeval begtv;

		gettimeofday(&tv, NULL);

		if (!timerisset(&begtv)) {
			begtv = tv;
			return;
		}

		warnxv(4, "DATA %f %d.%d %d.%d",
		    difftv(&tv, &begtv),
		    bsd->rate / 1024, (bsd->rate % 1024) * 100 / 1024,
		    bsd->winrate / 1024, (bsd->winrate % 1024) * 100 / 1024);
	}
#endif /* 0 */

	bwstat_update(cli->stat, len, which);

	warnxv(4, "\tavg: %d.%d KB/s; win: %d.%d KB/s", 
	    bsd->rate / 1024, (bsd->rate % 1024) * 100 / 1024,
	    bsd->winrate / 1024, (bsd->winrate % 1024) * 100 / 1024);
}

void
client_force(void)
{
	struct client *cli;

	SPLAY_FOREACH(cli, clitree, &clients) {
		bwstat_update(cli->stat, 0, BWSTAT_SEND);
		bwstat_update(cli->stat, 0, BWSTAT_RECV);
	}

	bwstat_update(NULL, 0, BWSTAT_SEND);
	bwstat_update(NULL, 0, BWSTAT_RECV);
}

void
client_printrates(void)
{
	struct bwstat *bs = bwstat_gettot();
	struct bwstat_data *bsdrecv = &bs->data[TRICKLE_RECV],
	    *bsdsend = &bs->data[TRICKLE_SEND], *bsd;

	bsd = bsdsend;

	warnxv(0, "UPLOAD total:\n"
	    "\tavg: %d.%d KB/s; win: %d.%d KB/s", 
	    bsd->rate / 1024, (bsd->rate % 1024) * 100 / 1024,
	    bsd->winrate / 1024, (bsd->winrate % 1024) * 100 / 1024);

	bsd = bsdrecv;

	warnxv(0, "DOWNLOAD total:\n"
	    "\tavg: %d.%d KB/s; win: %d.%d KB/s", 
	    bsd->rate / 1024, (bsd->rate % 1024) * 100 / 1024,
	    bsd->winrate / 1024, (bsd->winrate % 1024) * 100 / 1024);
}

int
client_sendmsg(struct client *cli, struct msg *msg)
{
	u_char buf[2048];
	uint32_t buflen = sizeof(buf), xbuflen;

	if (cli->s == -1)
		return (-1);

	if (msg2xdr(msg, buf, &buflen) == -1)
		return (-1);

	xbuflen = htonl(buflen);
	if (atomicio(write, cli->s, &xbuflen, sizeof(xbuflen)) !=
	    sizeof(xbuflen))
	    return (-1);

	if (atomicio(write, cli->s, buf, buflen) == buflen)
		return (0);

	return (-1);
}

int
client_recvmsg(struct client *cli, struct msg *msg)
{
	u_char buf[2048];
	uint32_t buflen, xbuflen;

	if (cli->s == -1)
		return (-1);

	if (atomicio(read, cli->s, &xbuflen, sizeof(xbuflen)) !=
	    sizeof(xbuflen))
		return (-1);
	buflen = ntohl(xbuflen);
	if (buflen > sizeof(buf))
		return (-1);

	if (atomicio(read, cli->s, buf, buflen) == buflen) {
		if (xdr2msg(msg, buf, buflen) == -1)
			return (-1);
		return (0);
	}

	return (-1);
}