2011-03-11 04:19:01 +00:00
|
|
|
/*
|
2014-01-18 20:56:57 +00:00
|
|
|
* This file Copyright (C) 2010-2014 Mnemosyne LLC
|
2011-03-11 04:19:01 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2011-03-11 04:19:01 +00:00
|
|
|
*
|
2011-03-13 14:38:49 +00:00
|
|
|
* $Id$
|
2011-03-11 04:19:01 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-13 14:33:28 +00:00
|
|
|
#include <limits.h> /* USHRT_MAX */
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdio.h> /* fprintf () */
|
|
|
|
#include <string.h> /* strchr (), memcmp (), memcpy () */
|
2011-03-13 14:33:28 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/http.h> /* for HTTP_OK */
|
|
|
|
|
2015-05-04 19:58:34 +00:00
|
|
|
#define __LIBTRANSMISSION_ANNOUNCER_MODULE__
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "announcer-common.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "net.h" /* tr_globalIPv6 () */
|
2011-03-11 04:19:01 +00:00
|
|
|
#include "peer-mgr.h" /* pex */
|
|
|
|
#include "torrent.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread () */
|
2011-03-13 14:33:28 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "web.h" /* tr_http_escape () */
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(name, ...) \
|
2013-01-25 23:34:20 +00:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (tr_logGetDeepEnabled ()) \
|
|
|
|
tr_logAddDeep (__FILE__, __LINE__, name, __VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
while (0)
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
***** ANNOUNCE
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
static const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
get_event_string (const tr_announce_request * req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (req->partial_seed)
|
|
|
|
if (req->event != TR_ANNOUNCE_EVENT_STOPPED)
|
2011-03-11 04:19:01 +00:00
|
|
|
return "paused";
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_announce_event_get_string (req->event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2011-03-11 15:44:24 +00:00
|
|
|
|
2012-05-17 17:40:31 +00:00
|
|
|
static char*
|
2012-12-05 17:29:46 +00:00
|
|
|
announce_url_new (const tr_session * session, const tr_announce_request * req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
const char * str;
|
|
|
|
const unsigned char * ipv6;
|
2012-12-05 17:29:46 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new ();
|
2011-03-11 04:19:01 +00:00
|
|
|
char escaped_info_hash[SHA_DIGEST_LENGTH*3 + 1];
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_http_escape_sha1 (escaped_info_hash, req->info_hash);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
evbuffer_expand (buf, 1024);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
evbuffer_add_printf (buf, "%s"
|
2011-03-11 04:19:01 +00:00
|
|
|
"%c"
|
|
|
|
"info_hash=%s"
|
|
|
|
"&peer_id=%*.*s"
|
|
|
|
"&port=%d"
|
|
|
|
"&uploaded=%" PRIu64
|
|
|
|
"&downloaded=%" PRIu64
|
|
|
|
"&left=%" PRIu64
|
|
|
|
"&numwant=%d"
|
|
|
|
"&key=%x"
|
|
|
|
"&compact=1"
|
|
|
|
"&supportcrypto=1",
|
|
|
|
req->url,
|
2012-12-05 17:29:46 +00:00
|
|
|
strchr (req->url, '?') ? '&' : '?',
|
2011-03-11 04:19:01 +00:00
|
|
|
escaped_info_hash,
|
|
|
|
PEER_ID_LEN, PEER_ID_LEN, req->peer_id,
|
2011-03-13 00:18:11 +00:00
|
|
|
req->port,
|
2011-03-11 04:19:01 +00:00
|
|
|
req->up,
|
|
|
|
req->down,
|
2012-05-20 14:14:59 +00:00
|
|
|
req->leftUntilComplete,
|
2011-03-11 04:19:01 +00:00
|
|
|
req->numwant,
|
2012-12-05 17:29:46 +00:00
|
|
|
req->key);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session->encryptionMode == TR_ENCRYPTION_REQUIRED)
|
|
|
|
evbuffer_add_printf (buf, "&requirecrypto=1");
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (req->corrupt)
|
|
|
|
evbuffer_add_printf (buf, "&corrupt=%" PRIu64, req->corrupt);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
str = get_event_string (req);
|
|
|
|
if (str && *str)
|
|
|
|
evbuffer_add_printf (buf, "&event=%s", str);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
str = req->tracker_id_str;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (str && *str)
|
|
|
|
evbuffer_add_printf (buf, "&trackerid=%s", str);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* There are two incompatible techniques for announcing an IPv6 address.
|
|
|
|
BEP-7 suggests adding an "ipv6=" parameter to the announce URL,
|
|
|
|
while OpenTracker requires that peers announce twice, once over IPv4
|
|
|
|
and once over IPv6.
|
|
|
|
|
|
|
|
To be safe, we should do both: add the "ipv6=" parameter and
|
|
|
|
announce twice. At any rate, we're already computing our IPv6
|
|
|
|
address (for the LTEP handshake), so this comes for free. */
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
ipv6 = tr_globalIPv6 ();
|
|
|
|
if (ipv6) {
|
2011-03-11 04:19:01 +00:00
|
|
|
char ipv6_readable[INET6_ADDRSTRLEN];
|
2012-12-05 17:29:46 +00:00
|
|
|
evutil_inet_ntop (AF_INET6, ipv6, ipv6_readable, INET6_ADDRSTRLEN);
|
|
|
|
evbuffer_add_printf (buf, "&ipv6=");
|
|
|
|
tr_http_escape (buf, ipv6_readable, -1, true);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return evbuffer_free_to_str (buf);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tr_pex*
|
2012-12-14 04:34:42 +00:00
|
|
|
listToPex (tr_variant * peerList, size_t * setme_len)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t n;
|
2012-12-14 04:34:42 +00:00
|
|
|
const size_t len = tr_variantListSize (peerList);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_pex * pex = tr_new0 (tr_pex, len);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=n=0; i<len; ++i)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int64_t port;
|
|
|
|
const char * ip;
|
|
|
|
tr_address addr;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * peer = tr_variantListChild (peerList, i);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (peer == NULL)
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindStr (peer, TR_KEY_ip, &ip, NULL))
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_address_from_string (&addr, ip))
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindInt (peer, TR_KEY_port, &port))
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((port < 0) || (port > USHRT_MAX))
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_address_is_valid_for_peers (&addr, port))
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
pex[n].addr = addr;
|
2012-12-05 17:29:46 +00:00
|
|
|
pex[n].port = htons ((uint16_t)port);
|
2011-03-11 04:19:01 +00:00
|
|
|
++n;
|
|
|
|
}
|
|
|
|
|
|
|
|
*setme_len = n;
|
|
|
|
return pex;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct announce_data
|
|
|
|
{
|
|
|
|
tr_announce_response response;
|
2013-09-08 17:08:18 +00:00
|
|
|
tr_announce_response_func response_func;
|
2011-03-11 04:19:01 +00:00
|
|
|
void * response_func_user_data;
|
|
|
|
char log_name[128];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
on_announce_done_eventthread (void * vdata)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
struct announce_data * data = vdata;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (data->response_func != NULL)
|
|
|
|
data->response_func (&data->response, data->response_func_user_data);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (data->response.pex6);
|
|
|
|
tr_free (data->response.pex);
|
|
|
|
tr_free (data->response.tracker_id_str);
|
|
|
|
tr_free (data->response.warning);
|
|
|
|
tr_free (data->response.errmsg);
|
|
|
|
tr_free (data);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
on_announce_done (tr_session * session,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool did_connect,
|
|
|
|
bool did_timeout,
|
2011-03-11 04:19:01 +00:00
|
|
|
long response_code,
|
|
|
|
const void * msg,
|
|
|
|
size_t msglen,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * vdata)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
tr_announce_response * response;
|
|
|
|
struct announce_data * data = vdata;
|
|
|
|
|
|
|
|
response = &data->response;
|
|
|
|
response->did_connect = did_connect;
|
|
|
|
response->did_timeout = did_timeout;
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (data->log_name, "Got announce response");
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response_code != HTTP_OK)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-07 01:53:31 +00:00
|
|
|
const char * fmt = _("Tracker gave HTTP response code %1$ld (%2$s)");
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * response_str = tr_webGetResponseStr (response_code);
|
|
|
|
response->errmsg = tr_strdup_printf (fmt, response_code, response_str);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant benc;
|
|
|
|
const bool variant_loaded = !tr_variantFromBenc (&benc, msg, msglen);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2014-09-21 18:05:14 +00:00
|
|
|
if (tr_env_key_exists ("TR_CURL_VERBOSE"))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!variant_loaded)
|
2012-12-05 17:29:46 +00:00
|
|
|
fprintf (stderr, "%s", "Announce response was not in benc format\n");
|
2011-07-30 22:40:15 +00:00
|
|
|
else {
|
|
|
|
int i, len;
|
2012-12-14 04:34:42 +00:00
|
|
|
char * str = tr_variantToStr (&benc, TR_VARIANT_FMT_JSON, &len);
|
2012-12-05 17:29:46 +00:00
|
|
|
fprintf (stderr, "%s", "Announce response:\n< ");
|
|
|
|
for (i=0; i<len; ++i)
|
|
|
|
fputc (str[i], stderr);
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
tr_free (str);
|
2011-07-30 22:40:15 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (variant_loaded && tr_variantIsDict (&benc))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int64_t i;
|
2012-12-14 04:34:42 +00:00
|
|
|
size_t len;
|
|
|
|
tr_variant * tmp;
|
2011-03-11 04:19:01 +00:00
|
|
|
const char * str;
|
|
|
|
const uint8_t * raw;
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindStr (&benc, TR_KEY_failure_reason, &str, &len))
|
2012-12-14 04:34:42 +00:00
|
|
|
response->errmsg = tr_strndup (str, len);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindStr (&benc, TR_KEY_warning_message, &str, &len))
|
2012-12-14 04:34:42 +00:00
|
|
|
response->warning = tr_strndup (str, len);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (&benc, TR_KEY_interval, &i))
|
2011-03-11 04:19:01 +00:00
|
|
|
response->interval = i;
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (&benc, TR_KEY_min_interval, &i))
|
2011-03-11 04:19:01 +00:00
|
|
|
response->min_interval = i;
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindStr (&benc, TR_KEY_tracker_id, &str, &len))
|
2012-12-14 04:34:42 +00:00
|
|
|
response->tracker_id_str = tr_strndup (str, len);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (&benc, TR_KEY_complete, &i))
|
2011-03-11 04:19:01 +00:00
|
|
|
response->seeders = i;
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (&benc, TR_KEY_incomplete, &i))
|
2011-03-11 04:19:01 +00:00
|
|
|
response->leechers = i;
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (&benc, TR_KEY_downloaded, &i))
|
2011-03-11 04:19:01 +00:00
|
|
|
response->downloads = i;
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindRaw (&benc, TR_KEY_peers6, &raw, &len)) {
|
2013-09-08 17:32:09 +00:00
|
|
|
dbgmsg (data->log_name, "got a peers6 length of %"TR_PRIuSIZE, len);
|
2012-12-14 04:34:42 +00:00
|
|
|
response->pex6 = tr_peerMgrCompact6ToPex (raw, len,
|
2012-12-05 17:29:46 +00:00
|
|
|
NULL, 0, &response->pex6_count);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindRaw (&benc, TR_KEY_peers, &raw, &len)) {
|
2013-09-08 17:32:09 +00:00
|
|
|
dbgmsg (data->log_name, "got a compact peers length of %"TR_PRIuSIZE, len);
|
2012-12-14 04:34:42 +00:00
|
|
|
response->pex = tr_peerMgrCompactToPex (raw, len,
|
2012-12-05 17:29:46 +00:00
|
|
|
NULL, 0, &response->pex_count);
|
2012-12-22 20:35:19 +00:00
|
|
|
} else if (tr_variantDictFindList (&benc, TR_KEY_peers, &tmp)) {
|
2012-12-05 17:29:46 +00:00
|
|
|
response->pex = listToPex (tmp, &response->pex_count);
|
2013-09-08 17:32:09 +00:00
|
|
|
dbgmsg (data->log_name, "got a peers list with %"TR_PRIuSIZE" entries",
|
2012-12-05 17:29:46 +00:00
|
|
|
response->pex_count);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (variant_loaded)
|
|
|
|
tr_variantFree (&benc);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_runInEventThread (session, on_announce_done_eventthread, data);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_tracker_http_announce (tr_session * session,
|
2011-03-11 04:19:01 +00:00
|
|
|
const tr_announce_request * request,
|
|
|
|
tr_announce_response_func response_func,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * response_func_user_data)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
struct announce_data * d;
|
2012-12-05 17:29:46 +00:00
|
|
|
char * url = announce_url_new (session, request);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
d = tr_new0 (struct announce_data, 1);
|
2011-10-14 00:27:14 +00:00
|
|
|
d->response.seeders = -1;
|
|
|
|
d->response.leechers = -1;
|
|
|
|
d->response.downloads = -1;
|
2011-03-11 04:19:01 +00:00
|
|
|
d->response_func = response_func;
|
|
|
|
d->response_func_user_data = response_func_user_data;
|
2012-12-05 17:29:46 +00:00
|
|
|
memcpy (d->response.info_hash, request->info_hash, SHA_DIGEST_LENGTH);
|
|
|
|
tr_strlcpy (d->log_name, request->log_name, sizeof (d->log_name));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (request->log_name, "Sending announce to libcurl: \"%s\"", url);
|
2013-04-13 20:25:28 +00:00
|
|
|
tr_webRun (session, url, on_announce_done, d);
|
2011-04-27 20:41:47 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (url);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
***** SCRAPE
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
struct scrape_data
|
|
|
|
{
|
|
|
|
tr_scrape_response response;
|
2013-09-08 17:08:18 +00:00
|
|
|
tr_scrape_response_func response_func;
|
2011-03-11 04:19:01 +00:00
|
|
|
void * response_func_user_data;
|
|
|
|
char log_name[128];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
on_scrape_done_eventthread (void * vdata)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
struct scrape_data * data = vdata;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (data->response_func != NULL)
|
|
|
|
data->response_func (&data->response, data->response_func_user_data);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (data->response.errmsg);
|
|
|
|
tr_free (data->response.url);
|
|
|
|
tr_free (data);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
on_scrape_done (tr_session * session,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool did_connect,
|
|
|
|
bool did_timeout,
|
2011-03-11 04:19:01 +00:00
|
|
|
long response_code,
|
|
|
|
const void * msg,
|
|
|
|
size_t msglen,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * vdata)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2011-03-11 15:44:24 +00:00
|
|
|
tr_scrape_response * response;
|
2011-03-11 04:19:01 +00:00
|
|
|
struct scrape_data * data = vdata;
|
|
|
|
|
|
|
|
response = &data->response;
|
|
|
|
response->did_connect = did_connect;
|
|
|
|
response->did_timeout = did_timeout;
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (data->log_name, "Got scrape response for \"%s\"", response->url);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response_code != HTTP_OK)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-07 01:53:31 +00:00
|
|
|
const char * fmt = _("Tracker gave HTTP response code %1$ld (%2$s)");
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * response_str = tr_webGetResponseStr (response_code);
|
|
|
|
response->errmsg = tr_strdup_printf (fmt, response_code, response_str);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2011-03-11 04:19:01 +00:00
|
|
|
int64_t intVal;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * files;
|
|
|
|
tr_variant * flags;
|
|
|
|
size_t len;
|
2011-03-11 04:19:01 +00:00
|
|
|
const char * str;
|
2012-12-14 04:34:42 +00:00
|
|
|
const bool variant_loaded = !tr_variantFromBenc (&top, msg, msglen);
|
2012-02-03 16:44:07 +00:00
|
|
|
|
2014-09-21 18:05:14 +00:00
|
|
|
if (tr_env_key_exists ("TR_CURL_VERBOSE"))
|
2011-09-24 05:54:47 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!variant_loaded)
|
2012-12-05 17:29:46 +00:00
|
|
|
fprintf (stderr, "%s", "Scrape response was not in benc format\n");
|
2011-09-24 05:54:47 +00:00
|
|
|
else {
|
|
|
|
int i, len;
|
2012-12-14 04:34:42 +00:00
|
|
|
char * str = tr_variantToStr (&top, TR_VARIANT_FMT_JSON, &len);
|
2012-12-05 17:29:46 +00:00
|
|
|
fprintf (stderr, "%s", "Scrape response:\n< ");
|
|
|
|
for (i=0; i<len; ++i)
|
|
|
|
fputc (str[i], stderr);
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
tr_free (str);
|
2011-09-24 05:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 16:44:07 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (variant_loaded)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindStr (&top, TR_KEY_failure_reason, &str, &len))
|
2012-12-14 04:34:42 +00:00
|
|
|
response->errmsg = tr_strndup (str, len);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (&top, TR_KEY_flags, &flags))
|
|
|
|
if (tr_variantDictFindInt (flags, TR_KEY_min_request_interval, &intVal))
|
2011-09-25 21:48:34 +00:00
|
|
|
response->min_request_interval = intVal;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (&top, TR_KEY_files, &files))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (;;)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int j;
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_quark key;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * val;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* get the next "file" */
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!tr_variantDictChild (files, i++, &key, &val))
|
2011-03-11 04:19:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* populate the corresponding row in our response array */
|
2012-12-05 17:29:46 +00:00
|
|
|
for (j=0; j<response->row_count; ++j)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
struct tr_scrape_response_row * row = &response->rows[j];
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!memcmp (tr_quark_get_string(key,NULL), row->info_hash, SHA_DIGEST_LENGTH))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (val, TR_KEY_complete, &intVal))
|
2011-03-11 04:19:01 +00:00
|
|
|
row->seeders = intVal;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (val, TR_KEY_incomplete, &intVal))
|
2011-03-11 04:19:01 +00:00
|
|
|
row->leechers = intVal;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (val, TR_KEY_downloaded, &intVal))
|
2011-03-11 04:19:01 +00:00
|
|
|
row->downloads = intVal;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindInt (val, TR_KEY_downloaders, &intVal))
|
2011-03-11 04:19:01 +00:00
|
|
|
row->downloaders = intVal;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_runInEventThread (session, on_scrape_done_eventthread, data);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-05-17 17:40:31 +00:00
|
|
|
static char *
|
2012-12-05 17:29:46 +00:00
|
|
|
scrape_url_new (const tr_scrape_request * req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char delimiter;
|
2012-12-05 17:29:46 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new ();
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
evbuffer_add_printf (buf, "%s", req->url);
|
|
|
|
delimiter = strchr (req->url, '?') ? '&' : '?';
|
|
|
|
for (i=0; i<req->info_hash_count; ++i)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
char str[SHA_DIGEST_LENGTH*3 + 1];
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_http_escape_sha1 (str, req->info_hash[i]);
|
|
|
|
evbuffer_add_printf (buf, "%cinfo_hash=%s", delimiter, str);
|
2011-03-11 04:19:01 +00:00
|
|
|
delimiter = '&';
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return evbuffer_free_to_str (buf);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_tracker_http_scrape (tr_session * session,
|
2011-03-11 04:19:01 +00:00
|
|
|
const tr_scrape_request * request,
|
|
|
|
tr_scrape_response_func response_func,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * response_func_user_data)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct scrape_data * d;
|
2012-12-05 17:29:46 +00:00
|
|
|
char * url = scrape_url_new (request);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
d = tr_new0 (struct scrape_data, 1);
|
|
|
|
d->response.url = tr_strdup (request->url);
|
2011-03-11 04:19:01 +00:00
|
|
|
d->response_func = response_func;
|
|
|
|
d->response_func_user_data = response_func_user_data;
|
|
|
|
d->response.row_count = request->info_hash_count;
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<d->response.row_count; ++i)
|
2011-10-14 00:27:14 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
memcpy (d->response.rows[i].info_hash, request->info_hash[i], SHA_DIGEST_LENGTH);
|
2011-10-14 00:27:14 +00:00
|
|
|
d->response.rows[i].seeders = -1;
|
|
|
|
d->response.rows[i].leechers = -1;
|
|
|
|
d->response.rows[i].downloads = -1;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_strlcpy (d->log_name, request->log_name, sizeof (d->log_name));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg (request->log_name, "Sending scrape to libcurl: \"%s\"", url);
|
2013-04-13 20:25:28 +00:00
|
|
|
tr_webRun (session, url, on_scrape_done, d);
|
2011-04-27 20:41:47 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (url);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|