static void parse_reply(struct torrent *tp, struct tr_response *res, const char *content, size_t size) { const char *buf; size_t len; const char *peers; const char *v6key[] = {"peers6", "peers_ipv6"}; if (benc_validate(content, size) != 0) goto bad_data; if ((buf = benc_dget_any(content, "failure reason")) != NULL) { if (!benc_isstr(buf)) goto bad_data; res->type = TR_RES_FAIL; res->mi_failure = buf; return; } buf = benc_dget_any(content, "interval"); if (buf != NULL && benc_isint(buf)) res->interval = benc_int(buf, NULL); if ((peers = benc_dget_any(content, "peers")) == NULL) goto after_peers; if (benc_islst(peers)) { for (peers = benc_first(peers); peers != NULL && net_npeers < net_max_peers; peers = benc_next(peers)) maybe_connect_to(tp, peers); } else if (benc_isstr(peers)) { if (net_ipv4) { peers = benc_dget_mem(content, "peers", &len); for (size_t i = 0; i < len && net_npeers < net_max_peers; i += 6) peer_create_out_compact(tp->net, AF_INET, peers + i); } } else goto bad_data; after_peers: if (!net_ipv6) goto after_peers6; for (int k = 0; k < 2; k++) { peers = benc_dget_any(content, v6key[k]); if (peers != NULL && benc_isstr(peers)) { peers = benc_dget_mem(content, v6key[k], &len); for (size_t i = 0; i < len && net_npeers < net_max_peers; i += 18) peer_create_out_compact(tp->net, AF_INET6, peers + i); } } after_peers6: res->type = TR_RES_OK; return; bad_data: res->type = TR_RES_BAD; }
static void cli_read_cb(int sd, short type, void *arg) { struct cli *cli = arg; uint32_t cmdlen; uint8_t *msg = NULL; if (read_fully(sd, &cmdlen, sizeof(cmdlen)) != 0) goto error; msg = btpd_malloc(cmdlen); if (read_fully(sd, msg, cmdlen) != 0) goto error; if (!(benc_validate(msg, cmdlen) == 0 && benc_islst(msg) && benc_first(msg) != NULL && benc_isstr(benc_first(msg)))) goto error; if (cmd_dispatch(cli, msg) != 0) goto error; free(msg); return; error: btpd_ev_del(&cli->read); close(cli->sd); free(cli); if (msg != NULL) free(msg); }
static int cmd_stop(struct cli *cli, int argc, const char *args) { if (argc != 1) return IPC_COMMERR; struct tlib *tl; if (benc_isstr(args) && benc_strlen(args) == 20) tl = tlib_by_hash(benc_mem(args, NULL, NULL)); else if (benc_isint(args)) tl = tlib_by_num(benc_int(args, NULL)); else return IPC_COMMERR; if (tl == NULL || torrent_haunting(tl)) return write_code_buffer(cli, IPC_ENOTENT); else if (!torrent_active(tl)) return write_code_buffer(cli, IPC_ETINACTIVE); else { /* Stopping a torrent may trigger exit so we need to reply before. */ int ret = write_code_buffer(cli, IPC_OK); active_del(tl->hash); torrent_stop(tl->tp, 0); return ret; } }
static int cmd_start(struct cli *cli, int argc, const char *args) { if (argc != 1) return IPC_COMMERR; if (btpd_is_stopping()) return write_code_buffer(cli, IPC_ESHUTDOWN); struct tlib *tl; enum ipc_err code = IPC_OK; if (benc_isstr(args) && benc_strlen(args) == 20) tl = tlib_by_hash(benc_mem(args, NULL, NULL)); else if (benc_isint(args)) tl = tlib_by_num(benc_int(args, NULL)); else return IPC_COMMERR; if (tl == NULL || torrent_haunting(tl)) code = IPC_ENOTENT; else if (!torrent_startable(tl)) code = IPC_ETACTIVE; else if ((code = torrent_start(tl)) == IPC_OK) active_add(tl->hash); return write_code_buffer(cli, code); }
static int cmd_del(struct cli *cli, int argc, const char *args) { if (argc != 1) return IPC_COMMERR; int ret; struct tlib *tl; if (benc_isstr(args) && benc_strlen(args) == 20) tl = tlib_by_hash(benc_mem(args, NULL, NULL)); else if (benc_isint(args)) tl = tlib_by_num(benc_int(args, NULL)); else return IPC_COMMERR; if (tl == NULL || torrent_haunting(tl)) ret = write_code_buffer(cli, IPC_ENOTENT); else { ret = write_code_buffer(cli, IPC_OK); if (tl->tp != NULL) torrent_stop(tl->tp, 1); else tlib_del(tl); } return ret; }
static int mi_test_announce_list(const char *alst) { int lstcount = 0; const char *t = benc_first(alst); while (t != NULL && benc_islst(t)) { int strcount = 0; const char *s = benc_first(t); while (s != NULL && benc_isstr(s)) { strcount++; s = benc_next(s); } if (strcount == 0) return 0; lstcount++; t = benc_next(t); } return lstcount > 0 ? 1 : 0; }
static int cmd_tget(struct cli *cli, int argc, const char *args) { if (argc != 1 || !benc_isdct(args)) return IPC_COMMERR; size_t nkeys; const char *keys, *p; enum ipc_tval *opts; struct iobuf iob; if ((keys = benc_dget_lst(args, "keys")) == NULL) return IPC_COMMERR; nkeys = benc_nelems(keys); opts = btpd_calloc(nkeys, sizeof(*opts)); p = benc_first(keys); for (int i = 0; i < nkeys; i++) opts[i] = benc_int(p, &p); iob = iobuf_init(1 << 15); iobuf_swrite(&iob, "d4:codei0e6:resultl"); p = benc_dget_any(args, "from"); if (benc_isint(p)) { enum ipc_twc from = benc_int(p, NULL); struct htbl_iter it; struct tlib *tl; for (tl = tlib_iter_first(&it); tl != NULL; tl = tlib_iter_next(&it)) { if (!torrent_haunting(tl) && ( from == IPC_TWC_ALL || (!torrent_active(tl) && from == IPC_TWC_INACTIVE) || (torrent_active(tl) && from == IPC_TWC_ACTIVE))) { iobuf_swrite(&iob, "l"); for (int k = 0; k < nkeys; k++) write_ans(&iob, tl, opts[k]); iobuf_swrite(&iob, "e"); } } } else if (benc_islst(p)) { for (p = benc_first(p); p != NULL; p = benc_next(p)) { struct tlib *tl = NULL; if (benc_isint(p)) tl = tlib_by_num(benc_int(p, NULL)); else if (benc_isstr(p) && benc_strlen(p) == 20) tl = tlib_by_hash(benc_mem(p, NULL, NULL)); else { iobuf_free(&iob); free(opts); return IPC_COMMERR; } if (tl != NULL && !torrent_haunting(tl)) { iobuf_swrite(&iob, "l"); for (int i = 0; i < nkeys; i++) write_ans(&iob, tl, opts[i]); iobuf_swrite(&iob, "e"); } else iobuf_print(&iob, "i%de", IPC_ENOTENT); } } iobuf_swrite(&iob, "ee"); free(opts); return write_buffer(cli, &iob); }