forked from mariusae/trickle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trickledu.c
294 lines (228 loc) · 5.67 KB
/
trickledu.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
/*
* trickledu.c
*
* Copyright (c) 2003 Marius Aamodt Eriksen <marius@monkey.org>
* All rights reserved.
*
* $Id: trickledu.c,v 1.16 2004/02/13 06:11:21 marius Exp $
*/
#include <sys/types.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <sys/socket.h>
#include <sys/un.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#include <string.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) */
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif /* HAVE_NETINET_IN_H */
#include "trickle.h"
#include "message.h"
#include "trickledu.h"
#include "util.h"
#include "xdr.h"
static void _trickled_open(struct msg *, int *);
#define DECLARE(name, ret, args) static ret (*libc_##name) args
DECLARE(socket, int, (int, int, int));
DECLARE(read, ssize_t, (int, void *, size_t));
DECLARE(write, ssize_t, (int, const void *, size_t));
DECLARE(close, int, (int));
static char *argv0, *sockname;
static int trickled_sock = -1, *trickled;
static pid_t trickled_pid = -1;
void
trickled_configure(char *xsockname, int (*xlibc_socket)(int, int, int),
ssize_t (*xlibc_read)(int, void *, size_t),
ssize_t (*xlibc_write)(int, const void *, size_t),
int (*xlibc_close)(int),
char *xargv0)
{
sockname = xsockname;
libc_socket = xlibc_socket;
libc_write = xlibc_write;
libc_read = xlibc_read;
libc_close = xlibc_close;
argv0 = xargv0;
}
void
trickled_open(int *xtrickled)
{
struct msg msg;
struct msg_conf *conf;
memset(&msg, 0, sizeof(msg));
msg.type = MSG_TYPE_CONF;
conf = &msg.data.conf;
/* memcpy(conf->lim, lim, sizeof(conf->lim)); */
conf->pid = getpid();
strlcpy(conf->argv0, argv0, sizeof(conf->argv0));
conf->uid = geteuid();
conf->gid = getegid();
_trickled_open(&msg, xtrickled);
}
void
trickled_close(int *xtrickled)
{
(*libc_close)(trickled_sock);
*xtrickled = 0;
trickled_sock = -1;
}
void
trickled_ctl_open(int *xtrickled)
{
struct msg msg;
memset(&msg, 0, sizeof(msg));
msg.type = MSG_TYPE_SPECTATOR;
_trickled_open(&msg, xtrickled);
}
static void
_trickled_open(struct msg *msg, int *xtrickled)
{
int s;
struct sockaddr_un xsun;
trickled = xtrickled;
*trickled = 0;
if ((s = (*libc_socket)(AF_UNIX, SOCK_STREAM, 0)) == -1)
return;
memset(&xsun, 0, sizeof(xsun));
xsun.sun_family = AF_UNIX;
strlcpy(xsun.sun_path, sockname, sizeof(xsun.sun_path));
if (connect(s, (struct sockaddr *)&xsun, sizeof(xsun)) == -1) {
(*libc_close)(s);
return;
}
trickled_pid = getpid();
trickled_sock = *trickled = s;
if (trickled_sendmsg(msg) == -1) {
(*libc_close)(s);
return;
}
}
int
trickled_sendmsg(struct msg *msg)
{
u_char buf[2048];
uint32_t buflen = sizeof(buf), xbuflen;
if (trickled_sock != -1 && trickled_pid != getpid()) {
trickled_close(trickled);
trickled_open(trickled);
}
if (trickled_sock == -1)
goto fail;
if (msg2xdr(msg, buf, &buflen) == -1)
return (-1); /* XXX fail? */
xbuflen = htonl(buflen);
if (atomicio(libc_write, trickled_sock, &xbuflen, sizeof(xbuflen)) !=
sizeof(xbuflen))
return (-1);
if (atomicio(libc_write, trickled_sock, buf, buflen) == buflen)
return (0);
fail:
*trickled = 0;
trickled_sock = -1;
return (-1);
}
int
trickled_recvmsg(struct msg *msg)
{
u_char buf[2048];
uint32_t buflen, xbuflen;
if (trickled_sock == -1)
goto fail;
if (atomicio(libc_read, trickled_sock, &xbuflen, sizeof(xbuflen)) !=
sizeof(xbuflen))
return (-1);
buflen = ntohl(xbuflen);
if (buflen > sizeof(buf))
return (-1);
if (atomicio(libc_read, trickled_sock, buf, buflen) == buflen) {
if (xdr2msg(msg, buf, buflen) == -1)
return (-1);
return (0);
}
fail:
*trickled = 0;
trickled_sock = -1;
return (-1);
}
int
trickled_update(short dir, size_t len)
{
struct msg msg;
struct msg_update *update = &msg.data.update;
msg.type = MSG_TYPE_UPDATE;
update->len = len;
update->dir = dir;
return (trickled_sendmsg(&msg));
}
int
trickled_delay(short dir, size_t *len)
{
struct msg msg;
struct msg_delay *delay = &msg.data.delay;
struct msg_delayinfo *delayinfo = &msg.data.delayinfo;
msg.type = MSG_TYPE_DELAY;
delay->len = *len;
delay->dir = dir;
if (trickled_sendmsg(&msg) == -1)
return (-1);
/* Ignore all other messages in the meantime XXX for now. */
do {
if (trickled_recvmsg(&msg) == -1)
return (-1);
} while (msg.type != MSG_TYPE_CONT);
*len = delayinfo->len;
return (0);
}
struct timeval *
trickled_getdelay(short dir, size_t *len)
{
struct msg msg;
struct msg_delay *delay = &msg.data.delay;
struct msg_delayinfo *delayinfo = &msg.data.delayinfo;
static struct timeval tv;
msg.type = MSG_TYPE_GETDELAY;
delay->len = *len;
delay->dir = dir;
if (trickled_sendmsg(&msg) == -1)
return (NULL);
/* Ignore all other messages in the meantime XXX for now. */
do {
if (trickled_recvmsg(&msg) == -1)
return (NULL);
} while (msg.type != MSG_TYPE_DELAYINFO);
if (ISSET(msg.status, MSG_STATUS_FAIL))
return (NULL);
tv = delayinfo->delaytv;
*len = delayinfo->len;
return (&tv);
}
int
trickled_getinfo(uint32_t *uplim, uint32_t *uprate,
uint32_t *downlim, uint32_t *downrate)
{
struct msg msg;
struct msg_getinfo *getinfo = &msg.data.getinfo;
msg.type = MSG_TYPE_GETINFO;
if (trickled_sendmsg(&msg) == -1)
return (-1);
do {
if (trickled_recvmsg(&msg) == -1)
return (-1);
} while (msg.type != MSG_TYPE_GETINFO);
*uplim = getinfo->dirinfo[TRICKLE_SEND].lim;
*uprate = getinfo->dirinfo[TRICKLE_SEND].rate;
*downlim = getinfo->dirinfo[TRICKLE_RECV].lim;
*downrate = getinfo->dirinfo[TRICKLE_RECV].rate;
return (0);
}