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
|
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "benc.h"
#include "btpd_if.h"
#include "iobuf.h"
#include "subr.h"
struct ipc {
int sd;
};
static const char *errmsgs[] = {
#define ERRDEF(name, msg) msg,
#include "ipcdefs.h"
#undef ERRDEF
NULL
};
static const char *tval_names[] = {
#define TVDEF(val, type, name) name,
#include "ipcdefs.h"
#undef TVDEF
NULL
};
const char *
ipc_strerror(enum ipc_err err)
{
if (err < 0 || err >= IPC_ERRCOUNT)
return "unknown error";
return errmsgs[err];
}
const char *
tval_name(enum ipc_tval key)
{
if (key < 0 || key >= IPC_TVALCOUNT)
return "unknown key";
return tval_names[key];
}
int
ipc_open(const char *dir, struct ipc **out)
{
int sd = -1, err = 0;
size_t plen;
struct ipc *res;
struct sockaddr_un addr;
plen = sizeof(addr.sun_path);
if (snprintf(addr.sun_path, plen, "%s/sock", dir) >= plen)
return ENAMETOOLONG;
addr.sun_family = AF_UNIX;
if ((sd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
return errno;
if (connect(sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
err = errno;
close(sd);
return err;
}
if ((res = malloc(sizeof(*res))) == NULL) {
close(sd);
return ENOMEM;
}
res->sd = sd;
*out = res;
return 0;
}
void
ipc_close(struct ipc *ipc)
{
close(ipc->sd);
free(ipc);
}
static enum ipc_err
ipc_response(struct ipc *ipc, char **out, uint32_t *len)
{
uint32_t size;
char *buf;
if (read_fully(ipc->sd, &size, sizeof(size)) != 0)
return IPC_COMMERR;
if (size == 0)
return IPC_COMMERR;
if ((buf = malloc(size)) == NULL)
return IPC_COMMERR;
if (read_fully(ipc->sd, buf, size) != 0) {
free(buf);
return IPC_COMMERR;
}
*out = buf;
*len = size;
return IPC_OK;
}
static enum ipc_err
ipc_req_res(struct ipc *ipc, const char *req, uint32_t qlen, char **res,
uint32_t *rlen)
{
if (write_fully(ipc->sd, &qlen, sizeof(qlen)) != 0)
return IPC_COMMERR;
if (write_fully(ipc->sd, req, qlen) != 0)
return IPC_COMMERR;
if (ipc_response(ipc, res, rlen) != 0)
return IPC_COMMERR;
if (benc_validate(*res, *rlen) != 0)
return IPC_COMMERR;
if (!benc_isdct(*res))
return IPC_COMMERR;
return IPC_OK;
}
static enum ipc_err
ipc_buf_req_res(struct ipc *ipc, struct iobuf *iob, char **res,
uint32_t *rlen)
{
enum ipc_err err;
if (iob->error)
err = IPC_COMMERR;
else
err = ipc_req_res(ipc, iob->buf, iob->off, res, rlen);
iobuf_free(iob);
return err;
}
static enum ipc_err
ipc_buf_req_code(struct ipc *ipc, struct iobuf *iob)
{
enum ipc_err err;
char *res;
uint32_t rlen;
if ((err = ipc_buf_req_res(ipc, iob, &res, &rlen)) == 0) {
err = benc_dget_int(res, "code");
free(res);
}
return err;
}
enum ipc_err
btpd_die(struct ipc *ipc, int seconds)
{
struct iobuf iob = iobuf_init(16);
if (seconds >= 0)
iobuf_print(&iob, "l3:diei%dee", seconds);
else
iobuf_swrite(&iob, "l3:diee");
return ipc_buf_req_code(ipc, &iob);
}
static enum ipc_err
tget_common(char *ans, enum ipc_tval *keys, size_t nkeys, tget_cb_t cb,
void *arg)
{
int err;
const char *res;
struct ipc_get_res cbres[IPC_TVALCOUNT];
if ((err = benc_dget_int(ans, "code")) != 0)
return err;
res = benc_dget_lst(ans, "result");
int obji = 0;
for (res = benc_first(res); res != NULL; res = benc_next(res)) {
if (benc_isint(res)) {
cb(obji, benc_int(res, NULL), NULL, arg);
obji++;
continue;
}
const char *t = benc_first(res);
const char *v = benc_next(t);
for (int j = 0; j < nkeys; j++) {
cbres[keys[j]].type = benc_int(t, NULL);
switch (cbres[keys[j]].type) {
case IPC_TYPE_ERR:
case IPC_TYPE_NUM:
cbres[keys[j]].v.num = benc_int(v, NULL);
break;
case IPC_TYPE_STR:
case IPC_TYPE_BIN:
cbres[keys[j]].v.str.p= benc_mem(v, &cbres[keys[j]].v.str.l,
NULL);
break;
}
t = benc_next(v);
if (t != NULL)
v = benc_next(t);
}
cb(obji, IPC_OK, cbres, arg);
obji++;
}
free(ans);
return IPC_OK;
}
enum ipc_err
btpd_tget(struct ipc *ipc, struct ipc_torrent *tps, size_t ntps,
enum ipc_tval *keys, size_t nkeys, tget_cb_t cb, void *arg)
{
char *res;
uint32_t rlen;
enum ipc_err err;
struct iobuf iob;
if (nkeys == 0 || ntps == 0)
return IPC_COMMERR;
iob = iobuf_init(1 << 14);
iobuf_swrite(&iob, "l4:tgetd4:froml");
for (int i = 0; i < ntps; i++) {
if (tps[i].by_hash) {
iobuf_swrite(&iob, "20:");
iobuf_write(&iob, tps[i].u.hash, 20);
} else
iobuf_print(&iob, "i%ue", tps[i].u.num);
}
iobuf_swrite(&iob, "e4:keysl");
for (int k = 0; k < nkeys; k++)
iobuf_print(&iob, "i%de", keys[k]);
iobuf_swrite(&iob, "eee");
if ((err = ipc_buf_req_res(ipc, &iob, &res, &rlen)) == 0)
err = tget_common(res, keys, nkeys, cb, arg);
return err;
}
enum ipc_err
btpd_tget_wc(struct ipc *ipc, enum ipc_twc twc, enum ipc_tval *keys,
size_t nkeys, tget_cb_t cb, void *arg)
{
char *res;
uint32_t rlen;
struct iobuf iob;
enum ipc_err err;
if (nkeys == 0)
return IPC_COMMERR;
iob = iobuf_init(1 << 14);
iobuf_print(&iob, "l4:tgetd4:fromi%de4:keysl", twc);
for (int i = 0; i < nkeys; i++)
iobuf_print(&iob, "i%de", keys[i]);
iobuf_swrite(&iob, "eee");
if ((err = ipc_buf_req_res(ipc, &iob, &res, &rlen)) == 0)
err = tget_common(res, keys, nkeys, cb, arg);
return err;
}
enum ipc_err
btpd_add(struct ipc *ipc, const char *mi, size_t mi_size, const char *content,
const char *name)
{
struct iobuf iob = iobuf_init(1 << 10);
iobuf_print(&iob, "l3:addd7:content%d:%s", (int)strlen(content),
content);
if (name != NULL)
iobuf_print(&iob, "4:name%d:%s", (int)strlen(name), name);
iobuf_print(&iob, "7:torrent%lu:", (unsigned long)mi_size);
iobuf_write(&iob, mi, mi_size);
iobuf_swrite(&iob, "ee");
return ipc_buf_req_code(ipc, &iob);
}
static enum ipc_err
simple_treq(struct ipc *ipc, char *cmd, struct ipc_torrent *tp)
{
struct iobuf iob = iobuf_init(32);
if (tp->by_hash) {
iobuf_print(&iob, "l%d:%s20:", (int)strlen(cmd), cmd);
iobuf_write(&iob, tp->u.hash, 20);
iobuf_swrite(&iob, "e");
} else
iobuf_print(&iob, "l%d:%si%uee", (int)strlen(cmd), cmd, tp->u.num);
return ipc_buf_req_code(ipc, &iob);
}
enum ipc_err
btpd_del(struct ipc *ipc, struct ipc_torrent *tp)
{
return simple_treq(ipc, "del", tp);
}
enum ipc_err
btpd_start(struct ipc *ipc, struct ipc_torrent *tp)
{
return simple_treq(ipc, "start", tp);
}
enum ipc_err
btpd_stop(struct ipc *ipc, struct ipc_torrent *tp)
{
return simple_treq(ipc, "stop", tp);
}
enum ipc_err
btpd_stop_all(struct ipc *ipc)
{
struct iobuf iob = iobuf_init(16);
iobuf_swrite(&iob, "l8:stop-alle");
return ipc_buf_req_code(ipc, &iob);
}
|