File: announcer.c

package info (click to toggle)
transmission 3.00-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 33,160 kB
  • sloc: ansic: 81,055; objc: 18,449; cpp: 16,303; sh: 4,470; javascript: 4,281; makefile: 1,081; xml: 139
file content (1978 lines) | stat: -rw-r--r-- 56,684 bytes parent folder | download | duplicates (2)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
/*
 * This file Copyright (C) 2010-2014 Mnemosyne LLC
 *
 * It may be used under the GNU GPL versions 2 or 3
 * or any future license endorsed by Mnemosyne LLC.
 *
 */

#include <limits.h> /* INT_MAX */
#include <stdio.h>
#include <stdlib.h> /* qsort() */
#include <string.h> /* strcmp(), memcpy(), strncmp() */

#include <event2/buffer.h>
#include <event2/event.h> /* evtimer */

#define __LIBTRANSMISSION_ANNOUNCER_MODULE__

#include "transmission.h"
#include "announcer.h"
#include "announcer-common.h"
#include "crypto-utils.h" /* tr_rand_int(), tr_rand_int_weak() */
#include "log.h"
#include "peer-mgr.h" /* tr_peerMgrCompactToPex() */
#include "ptrarray.h"
#include "session.h"
#include "torrent.h"
#include "tr-assert.h"
#include "utils.h"

struct tr_tier;

static void tier_build_log_name(struct tr_tier const* tier, char* buf, size_t buflen);

#define dbgmsg(tier, ...) \
    do \
    { \
        if (tr_logGetDeepEnabled()) \
        { \
            char name[128]; \
            tier_build_log_name(tier, name, TR_N_ELEMENTS(name)); \
            tr_logAddDeep(__FILE__, __LINE__, name, __VA_ARGS__); \
        } \
    } \
    while (0)

enum
{
    /* unless the tracker says otherwise, rescrape this frequently */
    DEFAULT_SCRAPE_INTERVAL_SEC = (60 * 30),
    /* unless the tracker says otherwise, this is the announce interval */
    DEFAULT_ANNOUNCE_INTERVAL_SEC = (60 * 10),
    /* unless the tracker says otherwise, this is the announce min_interval */
    DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = (60 * 2),
    /* how many web tasks we allow at one time */
    MAX_CONCURRENT_TASKS = 48,
    /* the value of the 'numwant' argument passed in tracker requests. */
    NUMWANT = 80,
    /* */
    UPKEEP_INTERVAL_SECS = 1,
    /* this is how often to call the UDP tracker upkeep */
    TAU_UPKEEP_INTERVAL_SECS = 5,

    /* how many infohashes to remove when we get a scrape-too-long error */
    TR_MULTISCRAPE_STEP = 5
};

/***
****
***/

char const* tr_announce_event_get_string(tr_announce_event e)
{
    switch (e)
    {
    case TR_ANNOUNCE_EVENT_COMPLETED:
        return "completed";

    case TR_ANNOUNCE_EVENT_STARTED:
        return "started";

    case TR_ANNOUNCE_EVENT_STOPPED:
        return "stopped";

    default:
        return "";
    }
}

/***
****
***/

static int compareTransfer(uint64_t a_uploaded, uint64_t a_downloaded, uint64_t b_uploaded, uint64_t b_downloaded)
{
    /* higher upload count goes first */
    if (a_uploaded != b_uploaded)
    {
        return a_uploaded > b_uploaded ? -1 : 1;
    }

    /* then higher download count goes first */
    if (a_downloaded != b_downloaded)
    {
        return a_downloaded > b_downloaded ? -1 : 1;
    }

    return 0;
}

/**
 * Comparison function for tr_announce_requests.
 *
 * The primary key (amount of data transferred) is used to prioritize
 * tracker announcements of active torrents. The remaining keys are
 * used to satisfy the uniqueness requirement of a sorted tr_ptrArray.
 */
static int compareStops(void const* va, void const* vb)
{
    int i;
    tr_announce_request const* a = va;
    tr_announce_request const* b = vb;

    /* primary key: volume of data transferred. */
    if ((i = compareTransfer(a->up, a->down, b->up, b->down)) != 0)
    {
        return i;
    }

    /* secondary key: the torrent's info_hash */
    if ((i = memcmp(a->info_hash, b->info_hash, SHA_DIGEST_LENGTH)) != 0)
    {
        return i;
    }

    /* tertiary key: the tracker's announce url */
    return tr_strcmp0(a->url, b->url);
}

/***
****
***/

struct tr_scrape_info
{
    char* url;

    int multiscrape_max;
};

static void scrapeInfoFree(void* va)
{
    struct tr_scrape_info* a = va;

    tr_free(a->url);
    tr_free(a);
}

static int compareScrapeInfo(void const* va, void const* vb)
{
    struct tr_scrape_info const* a = va;
    struct tr_scrape_info const* b = vb;
    return tr_strcmp0(a->url, b->url);
}

/**
 * "global" (per-tr_session) fields
 */
typedef struct tr_announcer
{
    tr_ptrArray stops; /* tr_announce_request */
    tr_ptrArray scrape_info; /* struct tr_scrape_info */

    tr_session* session;
    struct event* upkeepTimer;
    int slotsAvailable;
    int key;
    time_t tauUpkeepAt;
}
tr_announcer;

static struct tr_scrape_info* tr_announcerGetScrapeInfo(struct tr_announcer* announcer, char const* url)
{
    struct tr_scrape_info* info = NULL;

    if (!tr_str_is_empty(url))
    {
        bool found;
        struct tr_scrape_info const key = { .url = (char*)url };
        int const pos = tr_ptrArrayLowerBound(&announcer->scrape_info, &key, compareScrapeInfo, &found);
        if (found)
        {
            info = tr_ptrArrayNth(&announcer->scrape_info, pos);
        }
        else
        {
            info = tr_new0(struct tr_scrape_info, 1);
            info->url = tr_strdup(url);
            info->multiscrape_max = TR_MULTISCRAPE_MAX;
            tr_ptrArrayInsert(&announcer->scrape_info, info, pos);
        }
    }

    return info;
}

static void onUpkeepTimer(evutil_socket_t foo UNUSED, short bar UNUSED, void* vannouncer);

void tr_announcerInit(tr_session* session)
{
    TR_ASSERT(tr_isSession(session));

    tr_announcer* a = tr_new0(tr_announcer, 1);
    a->stops = TR_PTR_ARRAY_INIT;
    a->key = tr_rand_int(INT_MAX);
    a->session = session;
    a->slotsAvailable = MAX_CONCURRENT_TASKS;
    a->upkeepTimer = evtimer_new(session->event_base, onUpkeepTimer, a);
    tr_timerAdd(a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0);

    session->announcer = a;
}

static void flushCloseMessages(tr_announcer* announcer);

void tr_announcerClose(tr_session* session)
{
    tr_announcer* announcer = session->announcer;

    flushCloseMessages(announcer);

    tr_tracker_udp_start_shutdown(session);

    event_free(announcer->upkeepTimer);
    announcer->upkeepTimer = NULL;

    tr_ptrArrayDestruct(&announcer->stops, NULL);
    tr_ptrArrayDestruct(&announcer->scrape_info, scrapeInfoFree);

    session->announcer = NULL;
    tr_free(announcer);
}

/***
****
***/

/* a row in tr_tier's list of trackers */
typedef struct
{
    char* key;
    char* announce;
    struct tr_scrape_info* scrape_info;

    char* tracker_id_str;

    int seederCount;
    int leecherCount;
    int downloadCount;
    int downloaderCount;

    int consecutiveFailures;

    uint32_t id;
}
tr_tracker;

/* format: host+':'+ port */
static char* getKey(char const* url)
{
    char* ret;
    char* scheme = NULL;
    char* host = NULL;
    int port = 0;

    tr_urlParse(url, TR_BAD_SIZE, &scheme, &host, &port, NULL);
    ret = tr_strdup_printf("%s://%s:%d", scheme != NULL ? scheme : "invalid", host != NULL ? host : "invalid", port);

    tr_free(host);
    tr_free(scheme);
    return ret;
}

static void trackerConstruct(tr_announcer* announcer, tr_tracker* tracker, tr_tracker_info const* inf)
{
    memset(tracker, 0, sizeof(tr_tracker));
    tracker->key = getKey(inf->announce);
    tracker->announce = tr_strdup(inf->announce);
    tracker->scrape_info = tr_announcerGetScrapeInfo(announcer, inf->scrape);
    tracker->id = inf->id;
    tracker->seederCount = -1;
    tracker->leecherCount = -1;
    tracker->downloadCount = -1;
}

static void trackerDestruct(tr_tracker* tracker)
{
    tr_free(tracker->tracker_id_str);
    tr_free(tracker->announce);
    tr_free(tracker->key);
}

/***
****
***/

struct tr_torrent_tiers;

/** @brief A group of trackers in a single tier, as per the multitracker spec */
typedef struct tr_tier
{
    /* number of up/down/corrupt bytes since the last time we sent an
     * "event=stopped" message that was acknowledged by the tracker */
    uint64_t byteCounts[3];

    tr_tracker* trackers;
    int tracker_count;
    tr_tracker* currentTracker;
    int currentTrackerIndex;

    tr_torrent* tor;

    time_t scrapeAt;
    time_t lastScrapeStartTime;
    time_t lastScrapeTime;
    bool lastScrapeSucceeded;
    bool lastScrapeTimedOut;

    time_t announceAt;
    time_t manualAnnounceAllowedAt;
    time_t lastAnnounceStartTime;
    time_t lastAnnounceTime;
    bool lastAnnounceSucceeded;
    bool lastAnnounceTimedOut;

    tr_announce_event* announce_events;
    int announce_event_count;
    int announce_event_alloc;

    /* unique lookup key */
    int key;

    int scrapeIntervalSec;
    int announceIntervalSec;
    int announceMinIntervalSec;

    int lastAnnouncePeerCount;

    bool isRunning;
    bool isAnnouncing;
    bool isScraping;
    bool wasCopied;

    char lastAnnounceStr[128];
    char lastScrapeStr[128];
}
tr_tier;

static time_t get_next_scrape_time(tr_session const* session, tr_tier const* tier, int interval)
{
    time_t ret;
    time_t const now = tr_time();

    /* Maybe don't scrape paused torrents */
    if (!tier->isRunning && !session->scrapePausedTorrents)
    {
        ret = 0;
    }
    /* Add the interval, and then increment to the nearest 10th second.
     * The latter step is to increase the odds of several torrents coming
     * due at the same time to improve multiscrape. */
    else
    {
        ret = now + interval;

        while (ret % 10 != 0)
        {
            ++ret;
        }
    }

    return ret;
}

static void tierConstruct(tr_tier* tier, tr_torrent* tor)
{
    static int nextKey = 1;

    memset(tier, 0, sizeof(tr_tier));

    tier->key = nextKey++;
    tier->currentTrackerIndex = -1;
    tier->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
    tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
    tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
    tier->scrapeAt = get_next_scrape_time(tor->session, tier, tr_rand_int_weak(180));
    tier->tor = tor;
}

static void tierDestruct(tr_tier* tier)
{
    tr_free(tier->announce_events);
}

static void tier_build_log_name(tr_tier const* tier, char* buf, size_t buflen)
{
    tr_snprintf(buf, buflen, "[%s---%s]", (tier != NULL && tier->tor != NULL) ? tr_torrentName(tier->tor) : "?",
        (tier != NULL && tier->currentTracker != NULL) ? tier->currentTracker->key : "?");
}

static void tierIncrementTracker(tr_tier* tier)
{
    /* move our index to the next tracker in the tier */
    int const i = tier->currentTracker == NULL ? 0 : (tier->currentTrackerIndex + 1) % tier->tracker_count;
    tier->currentTrackerIndex = i;
    tier->currentTracker = &tier->trackers[i];

    /* reset some of the tier's fields */
    tier->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
    tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
    tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
    tier->isAnnouncing = false;
    tier->isScraping = false;
    tier->lastAnnounceStartTime = 0;
    tier->lastScrapeStartTime = 0;
}

/***
****
***/

/**
 * @brief Opaque, per-torrent data structure for tracker announce information
 *
 * this opaque data structure can be found in tr_torrent.tiers
 */
typedef struct tr_torrent_tiers
{
    tr_tier* tiers;
    int tier_count;

    tr_tracker* trackers;
    int tracker_count;

    tr_tracker_callback callback;
    void* callbackData;
}
tr_torrent_tiers;

static tr_torrent_tiers* tiersNew(void)
{
    return tr_new0(tr_torrent_tiers, 1);
}

static void tiersDestruct(tr_torrent_tiers* tt)
{
    for (int i = 0; i < tt->tracker_count; ++i)
    {
        trackerDestruct(&tt->trackers[i]);
    }

    tr_free(tt->trackers);

    for (int i = 0; i < tt->tier_count; ++i)
    {
        tierDestruct(&tt->tiers[i]);
    }

    tr_free(tt->tiers);
}

static void tiersFree(tr_torrent_tiers* tt)
{
    tiersDestruct(tt);
    tr_free(tt);
}

static tr_tier* getTier(tr_announcer* announcer, uint8_t const* info_hash, int tierId)
{
    tr_tier* tier = NULL;

    if (announcer != NULL)
    {
        tr_session* session = announcer->session;
        tr_torrent* tor = tr_torrentFindFromHash(session, info_hash);

        if (tor != NULL && tor->tiers != NULL)
        {
            tr_torrent_tiers* tt = tor->tiers;

            for (int i = 0; tier == NULL && i < tt->tier_count; ++i)
            {
                if (tt->tiers[i].key == tierId)
                {
                    tier = &tt->tiers[i];
                }
            }
        }
    }

    return tier;
}

/***
****  PUBLISH
***/

static tr_tracker_event const TRACKER_EVENT_INIT =
{
    .messageType = TR_TRACKER_WARNING,
    .text = NULL,
    .tracker = NULL,
    .pex = NULL,
    .pexCount = 0,
    .seedProbability = 0
};

static void publishMessage(tr_tier* tier, char const* msg, int type)
{
    if (tier != NULL && tier->tor != NULL && tier->tor->tiers != NULL && tier->tor->tiers->callback != NULL)
    {
        tr_torrent_tiers* tiers = tier->tor->tiers;
        tr_tracker_event event = TRACKER_EVENT_INIT;
        event.messageType = type;
        event.text = msg;

        if (tier->currentTracker != NULL)
        {
            event.tracker = tier->currentTracker->announce;
        }

        (*tiers->callback)(tier->tor, &event, tiers->callbackData);
    }
}

static void publishErrorClear(tr_tier* tier)
{
    publishMessage(tier, NULL, TR_TRACKER_ERROR_CLEAR);
}

static void publishWarning(tr_tier* tier, char const* msg)
{
    publishMessage(tier, msg, TR_TRACKER_WARNING);
}

static void publishError(tr_tier* tier, char const* msg)
{
    publishMessage(tier, msg, TR_TRACKER_ERROR);
}

static int8_t getSeedProbability(tr_tier* tier, int seeds, int leechers, int pex_count)
{
    /* special case optimization:
       ocelot omits seeds from peer lists sent to seeds on private trackers.
       so check for that case... */
    if (leechers == pex_count && tr_torrentIsPrivate(tier->tor) && tr_torrentIsSeed(tier->tor) && seeds + leechers < NUMWANT)
    {
        return 0;
    }

    if (seeds >= 0 && leechers >= 0 && seeds + leechers > 0)
    {
        return (int8_t)(100.0 * seeds / (seeds + leechers));
    }

    return -1; /* unknown */
}

static void publishPeersPex(tr_tier* tier, int seeds, int leechers, tr_pex const* pex, int n)
{
    if (tier->tor->tiers->callback != NULL)
    {
        tr_tracker_event e = TRACKER_EVENT_INIT;
        e.messageType = TR_TRACKER_PEERS;
        e.seedProbability = getSeedProbability(tier, seeds, leechers, n);
        e.pex = pex;
        e.pexCount = n;
        dbgmsg(tier, "got %d peers; seed prob %d", n, (int)e.seedProbability);

        (*tier->tor->tiers->callback)(tier->tor, &e, NULL);
    }
}

/***
****
***/

struct ann_tracker_info
{
    tr_tracker_info info;

    char* scheme;
    char* host;
    char* path;
    int port;
};

/* primary key: tier
 * secondary key: udp comes before http */
static int filter_trackers_compare_func(void const* va, void const* vb)
{
    struct ann_tracker_info const* a = va;
    struct ann_tracker_info const* b = vb;

    if (a->info.tier != b->info.tier)
    {
        return a->info.tier - b->info.tier;
    }

    return -strcmp(a->scheme, b->scheme);
}

/**
 * Massages the incoming list of trackers into something we can use.
 */
static tr_tracker_info* filter_trackers(tr_tracker_info* input, int input_count, int* setme_count)
{
    int n = 0;
    struct tr_tracker_info* ret;
    struct ann_tracker_info* tmp = tr_new0(struct ann_tracker_info, input_count);

    /*
    for (int i = 0; i < input_count; ++i)
    {
        fprintf(stderr, "IN: [%d][%s]\n", input[i].tier, input[i].announce);
    }
    */

    /* build a list of valid trackers */
    for (int i = 0; i < input_count; ++i)
    {
        if (tr_urlIsValidTracker(input[i].announce))
        {
            int port;
            char* scheme;
            char* host;
            char* path;
            bool is_duplicate = false;
            tr_urlParse(input[i].announce, TR_BAD_SIZE, &scheme, &host, &port, &path);

            /* weed out one common source of duplicates:
             * "http://tracker/announce" +
             * "http://tracker:80/announce"
             */
            for (int j = 0; !is_duplicate && j < n; ++j)
            {
                is_duplicate = tmp[j].port == port && strcmp(tmp[j].scheme, scheme) == 0 && strcmp(tmp[j].host, host) == 0 &&
                    strcmp(tmp[j].path, path) == 0;
            }

            if (is_duplicate)
            {
                tr_free(path);
                tr_free(host);
                tr_free(scheme);
                continue;
            }

            tmp[n].info = input[i];
            tmp[n].scheme = scheme;
            tmp[n].host = host;
            tmp[n].port = port;
            tmp[n].path = path;
            n++;
        }
    }

    /* if two announce URLs differ only by scheme, put them in the same tier.
     * (note: this can leave gaps in the `tier' values, but since the calling
     * function doesn't care, there's no point in removing the gaps...) */
    for (int i = 0; i < n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            if (tmp[i].info.tier != tmp[j].info.tier && tmp[i].port == tmp[j].port &&
                tr_strcmp0(tmp[i].host, tmp[j].host) == 0 && tr_strcmp0(tmp[i].path, tmp[j].path) == 0)
            {
                tmp[j].info.tier = tmp[i].info.tier;
            }
        }
    }

    /* sort them, for two reasons:
     * (1) unjumble the tiers from the previous step
     * (2) move the UDP trackers to the front of each tier */
    qsort(tmp, n, sizeof(struct ann_tracker_info), filter_trackers_compare_func);

    /* build the output */
    *setme_count = n;
    ret = tr_new0(tr_tracker_info, n);

    for (int i = 0; i < n; ++i)
    {
        ret[i] = tmp[i].info;
    }

    /* cleanup */
    for (int i = 0; i < n; ++i)
    {
        tr_free(tmp[i].path);
        tr_free(tmp[i].host);
        tr_free(tmp[i].scheme);
    }

    tr_free(tmp);

    /*
    for (int i = 0; i < n; ++i)
    {
        fprintf (stderr, "OUT: [%d][%s]\n", ret[i].tier, ret[i].announce);
    }
    */

    return ret;
}

static void addTorrentToTier(tr_torrent_tiers* tt, tr_torrent* tor)
{
    int n;
    int tier_count;
    tr_tier* tier;
    tr_tracker_info* infos = filter_trackers(tor->info.trackers, tor->info.trackerCount, &n);

    /* build the array of trackers */
    tt->trackers = tr_new0(tr_tracker, n);
    tt->tracker_count = n;

    for (int i = 0; i < n; ++i)
    {
        trackerConstruct(tor->session->announcer, &tt->trackers[i], &infos[i]);
    }

    /* count how many tiers there are */
    tier_count = 0;

    for (int i = 0; i < n; ++i)
    {
        if (i == 0 || infos[i].tier != infos[i - 1].tier)
        {
            ++tier_count;
        }
    }

    /* build the array of tiers */
    tier = NULL;
    tt->tiers = tr_new0(tr_tier, tier_count);
    tt->tier_count = 0;

    for (int i = 0; i < n; ++i)
    {
        if (i != 0 && infos[i].tier == infos[i - 1].tier)
        {
            ++tier->tracker_count;
        }
        else
        {
            tier = &tt->tiers[tt->tier_count++];
            tierConstruct(tier, tor);
            tier->trackers = &tt->trackers[i];
            tier->tracker_count = 1;
            tierIncrementTracker(tier);
        }
    }

    /* cleanup */
    tr_free(infos);
}

tr_torrent_tiers* tr_announcerAddTorrent(tr_torrent* tor, tr_tracker_callback callback, void* callbackData)
{
    TR_ASSERT(tr_isTorrent(tor));

    tr_torrent_tiers* tiers = tiersNew();
    tiers->callback = callback;
    tiers->callbackData = callbackData;

    addTorrentToTier(tiers, tor);

    return tiers;
}

/***
****
***/

static bool tierCanManualAnnounce(tr_tier const* tier)
{
    return tier->manualAnnounceAllowedAt <= tr_time();
}

bool tr_announcerCanManualAnnounce(tr_torrent const* tor)
{
    TR_ASSERT(tr_isTorrent(tor));
    TR_ASSERT(tor->tiers != NULL);

    struct tr_torrent_tiers* tt = NULL;

    if (tor->isRunning)
    {
        tt = tor->tiers;
    }

    /* return true if any tier can manual announce */
    for (int i = 0; tt != NULL && i < tt->tier_count; ++i)
    {
        if (tierCanManualAnnounce(&tt->tiers[i]))
        {
            return true;
        }
    }

    return false;
}

time_t tr_announcerNextManualAnnounce(tr_torrent const* tor)
{
    time_t ret = ~(time_t)0;
    struct tr_torrent_tiers* tt = tor->tiers;

    /* find the earliest manual announce time from all peers */
    for (int i = 0; tt != NULL && i < tt->tier_count; ++i)
    {
        if (tt->tiers[i].isRunning)
        {
            ret = MIN(ret, tt->tiers[i].manualAnnounceAllowedAt);
        }
    }

    return ret;
}

static void dbgmsg_tier_announce_queue(tr_tier const* tier)
{
    if (tr_logGetDeepEnabled())
    {
        char name[128];
        char* message;
        struct evbuffer* buf = evbuffer_new();

        tier_build_log_name(tier, name, sizeof(name));

        for (int i = 0; i < tier->announce_event_count; ++i)
        {
            tr_announce_event const e = tier->announce_events[i];
            char const* str = tr_announce_event_get_string(e);
            evbuffer_add_printf(buf, "[%d:%s]", i, str);
        }

        message = evbuffer_free_to_str(buf, NULL);
        tr_logAddDeep(__FILE__, __LINE__, name, "announce queue is %s", message);
        tr_free(message);
    }
}

static void tier_announce_remove_trailing(tr_tier* tier, tr_announce_event e)
{
    while (tier->announce_event_count > 0 && tier->announce_events[tier->announce_event_count - 1] == e)
    {
        --tier->announce_event_count;
    }
}

static void tier_announce_event_push(tr_tier* tier, tr_announce_event e, time_t announceAt)
{
    TR_ASSERT(tier != NULL);

    dbgmsg_tier_announce_queue(tier);
    dbgmsg(tier, "queued \"%s\"", tr_announce_event_get_string(e));

    if (tier->announce_event_count > 0)
    {
        /* special case #1: if we're adding a "stopped" event,
         * dump everything leading up to it except "completed" */
        if (e == TR_ANNOUNCE_EVENT_STOPPED)
        {
            bool has_completed = false;
            tr_announce_event const c = TR_ANNOUNCE_EVENT_COMPLETED;

            for (int i = 0; !has_completed && i < tier->announce_event_count; ++i)
            {
                has_completed = c == tier->announce_events[i];
            }

            tier->announce_event_count = 0;

            if (has_completed)
            {
                tier->announce_events[tier->announce_event_count++] = c;
            }
        }

        /* special case #2: dump all empty strings leading up to this event */
        tier_announce_remove_trailing(tier, TR_ANNOUNCE_EVENT_NONE);

        /* special case #3: no consecutive duplicates */
        tier_announce_remove_trailing(tier, e);
    }

    /* make room in the array for another event */
    if (tier->announce_event_alloc <= tier->announce_event_count)
    {
        tier->announce_event_alloc += 4;
        tier->announce_events = tr_renew(tr_announce_event, tier->announce_events, tier->announce_event_alloc);
    }

    /* add it */
    tier->announce_events[tier->announce_event_count++] = e;
    tier->announceAt = announceAt;

    dbgmsg_tier_announce_queue(tier);
    dbgmsg(tier, "announcing in %d seconds", (int)difftime(announceAt, tr_time()));
}

static tr_announce_event tier_announce_event_pull(tr_tier* tier)
{
    tr_announce_event const e = tier->announce_events[0];

    tr_removeElementFromArray(tier->announce_events, 0, sizeof(tr_announce_event), tier->announce_event_count);
    --tier->announce_event_count;

    return e;
}

static void torrentAddAnnounce(tr_torrent* tor, tr_announce_event e, time_t announceAt)
{
    struct tr_torrent_tiers* tt = tor->tiers;

    /* walk through each tier and tell them to announce */
    for (int i = 0; i < tt->tier_count; ++i)
    {
        tier_announce_event_push(&tt->tiers[i], e, announceAt);
    }
}

void tr_announcerTorrentStarted(tr_torrent* tor)
{
    torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_STARTED, tr_time());
}

void tr_announcerManualAnnounce(tr_torrent* tor)
{
    torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_NONE, tr_time());
}

void tr_announcerTorrentStopped(tr_torrent* tor)
{
    torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_STOPPED, tr_time());
}

void tr_announcerTorrentCompleted(tr_torrent* tor)
{
    torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_COMPLETED, tr_time());
}

void tr_announcerChangeMyPort(tr_torrent* tor)
{
    tr_announcerTorrentStarted(tor);
}

/***
****
***/

void tr_announcerAddBytes(tr_torrent* tor, int type, uint32_t byteCount)
{
    TR_ASSERT(tr_isTorrent(tor));
    TR_ASSERT(type == TR_ANN_UP || type == TR_ANN_DOWN || type == TR_ANN_CORRUPT);

    struct tr_torrent_tiers* tt = tor->tiers;

    for (int i = 0; i < tt->tier_count; ++i)
    {
        tt->tiers[i].byteCounts[type] += byteCount;
    }
}

/***
****
***/

static tr_announce_request* announce_request_new(tr_announcer const* announcer, tr_torrent* tor, tr_tier const* tier,
    tr_announce_event event)
{
    tr_announce_request* req = tr_new0(tr_announce_request, 1);
    req->port = tr_sessionGetPublicPeerPort(announcer->session);
    req->url = tr_strdup(tier->currentTracker->announce);
    req->tracker_id_str = tr_strdup(tier->currentTracker->tracker_id_str);
    memcpy(req->info_hash, tor->info.hash, SHA_DIGEST_LENGTH);
    memcpy(req->peer_id, tr_torrentGetPeerId(tor), PEER_ID_LEN);
    req->up = tier->byteCounts[TR_ANN_UP];
    req->down = tier->byteCounts[TR_ANN_DOWN];
    req->corrupt = tier->byteCounts[TR_ANN_CORRUPT];
    req->leftUntilComplete = tr_torrentHasMetadata(tor) ? tor->info.totalSize - tr_torrentHaveTotal(tor) : INT64_MAX;
    req->event = event;
    req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : NUMWANT;
    req->key = announcer->key;
    req->partial_seed = tr_torrentGetCompleteness(tor) == TR_PARTIAL_SEED;
    tier_build_log_name(tier, req->log_name, sizeof(req->log_name));
    return req;
}

static void announce_request_free(tr_announce_request* req);

void tr_announcerRemoveTorrent(tr_announcer* announcer, tr_torrent* tor)
{
    struct tr_torrent_tiers* tt = tor->tiers;

    if (tt != NULL)
    {
        for (int i = 0; i < tt->tier_count; ++i)
        {
            tr_tier* tier = &tt->tiers[i];

            if (tier->isRunning)
            {
                tr_announce_event const e = TR_ANNOUNCE_EVENT_STOPPED;
                tr_announce_request* req = announce_request_new(announcer, tor, tier, e);

                if (tr_ptrArrayFindSorted(&announcer->stops, req, compareStops) != NULL)
                {
                    announce_request_free(req);
                }
                else
                {
                    tr_ptrArrayInsertSorted(&announcer->stops, req, compareStops);
                }
            }
        }

        tiersFree(tor->tiers);
        tor->tiers = NULL;
    }
}

static int getRetryInterval(tr_tracker const* t)
{
    switch (t->consecutiveFailures)
    {
    case 0:
        return 0;

    case 1:
        return 20;

    case 2:
        return tr_rand_int_weak(60) + 60 * 5;

    case 3:
        return tr_rand_int_weak(60) + 60 * 15;

    case 4:
        return tr_rand_int_weak(60) + 60 * 30;

    case 5:
        return tr_rand_int_weak(60) + 60 * 60;

    default:
        return tr_rand_int_weak(60) + 60 * 120;
    }
}

struct announce_data
{
    int tierId;
    time_t timeSent;
    tr_announce_event event;
    tr_session* session;

    /** If the request succeeds, the value for tier's "isRunning" flag */
    bool isRunningOnSuccess;
};

static void on_announce_error(tr_tier* tier, char const* err, tr_announce_event e)
{
    int interval;

    /* increment the error count */
    if (tier->currentTracker != NULL)
    {
        ++tier->currentTracker->consecutiveFailures;
    }

    /* set the error message */
    dbgmsg(tier, "%s", err);
    tr_logAddTorInfo(tier->tor, "%s", err);
    tr_strlcpy(tier->lastAnnounceStr, err, sizeof(tier->lastAnnounceStr));

    /* switch to the next tracker */
    tierIncrementTracker(tier);

    /* schedule a reannounce */
    interval = getRetryInterval(tier->currentTracker);
    dbgmsg(tier, "Retrying announce in %d seconds.", interval);
    tr_logAddTorInfo(tier->tor, "Retrying announce in %d seconds.", interval);
    tier_announce_event_push(tier, e, tr_time() + interval);
}

static void on_announce_done(tr_announce_response const* response, void* vdata)
{
    struct announce_data* data = vdata;
    tr_announcer* announcer = data->session->announcer;
    tr_tier* tier = getTier(announcer, response->info_hash, data->tierId);
    time_t const now = tr_time();
    tr_announce_event const event = data->event;

    if (announcer != NULL)
    {
        ++announcer->slotsAvailable;
    }

    if (tier != NULL)
    {
        tr_tracker* tracker;

        dbgmsg(tier,
            "Got announce response: "
            "connected:%d "
            "timeout:%d "
            "seeders:%d "
            "leechers:%d "
            "downloads:%d "
            "interval:%d "
            "min_interval:%d "
            "tracker_id_str:%s "
            "pex:%zu "
            "pex6:%zu "
            "err:%s "
            "warn:%s",
            (int)response->did_connect,
            (int)response->did_timeout,
            response->seeders,
            response->leechers,
            response->downloads,
            response->interval,
            response->min_interval,
            response->tracker_id_str != NULL ? response->tracker_id_str : "none",
            response->pex_count,
            response->pex6_count,
            response->errmsg != NULL ? response->errmsg : "none",
            response->warning != NULL ? response->warning : "none");

        tier->lastAnnounceTime = now;
        tier->lastAnnounceTimedOut = response->did_timeout;
        tier->lastAnnounceSucceeded = false;
        tier->isAnnouncing = false;
        tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;

        if (!response->did_connect)
        {
            on_announce_error(tier, _("Could not connect to tracker"), event);
        }
        else if (response->did_timeout)
        {
            on_announce_error(tier, _("Tracker did not respond"), event);
        }
        else if (response->errmsg != NULL)
        {
            /* If the torrent's only tracker returned an error, publish it.
               Don't bother publishing if there are other trackers -- it's
               all too common for people to load up dozens of dead trackers
               in a torrent's metainfo... */
            if (tier->tor->info.trackerCount < 2)
            {
                publishError(tier, response->errmsg);
            }

            on_announce_error(tier, response->errmsg, event);
        }
        else
        {
            int i;
            char const* str;
            int scrape_fields = 0;
            int seeders = 0;
            int leechers = 0;
            int downloads = 0;
            bool const isStopped = event == TR_ANNOUNCE_EVENT_STOPPED;

            publishErrorClear(tier);

            if ((tracker = tier->currentTracker) != NULL)
            {
                tracker->consecutiveFailures = 0;

                if (response->seeders >= 0)
                {
                    tracker->seederCount = seeders = response->seeders;
                    ++scrape_fields;
                }

                if (response->leechers >= 0)
                {
                    tracker->leecherCount = leechers = response->leechers;
                    ++scrape_fields;
                }

                if (response->downloads >= 0)
                {
                    tracker->downloadCount = downloads = response->downloads;
                    ++scrape_fields;
                }

                if ((str = response->tracker_id_str) != NULL)
                {
                    tr_free(tracker->tracker_id_str);
                    tracker->tracker_id_str = tr_strdup(str);
                }
            }

            if ((str = response->warning) != NULL)
            {
                tr_strlcpy(tier->lastAnnounceStr, str, sizeof(tier->lastAnnounceStr));
                dbgmsg(tier, "tracker gave \"%s\"", str);
                publishWarning(tier, str);
            }
            else
            {
                tr_strlcpy(tier->lastAnnounceStr, _("Success"), sizeof(tier->lastAnnounceStr));
            }

            if ((i = response->min_interval) != 0)
            {
                tier->announceMinIntervalSec = i;
            }

            if ((i = response->interval) != 0)
            {
                tier->announceIntervalSec = i;
            }

            if (response->pex_count > 0)
            {
                publishPeersPex(tier, seeders, leechers, response->pex, response->pex_count);
            }

            if (response->pex6_count > 0)
            {
                publishPeersPex(tier, seeders, leechers, response->pex6, response->pex6_count);
            }

            tier->isRunning = data->isRunningOnSuccess;

            /* if the tracker included scrape fields in its announce response,
               then a separate scrape isn't needed */
            if (scrape_fields >= 3 || (scrape_fields >= 1 && tracker->scrape_info == NULL))
            {
                tr_logAddTorDbg(tier->tor, "Announce response contained scrape info; "
                    "rescheduling next scrape to %d seconds from now.", tier->scrapeIntervalSec);
                tier->scrapeAt = get_next_scrape_time(announcer->session, tier, tier->scrapeIntervalSec);
                tier->lastScrapeTime = now;
                tier->lastScrapeSucceeded = true;
            }
            else if (tier->lastScrapeTime + tier->scrapeIntervalSec <= now)
            {
                tier->scrapeAt = get_next_scrape_time(announcer->session, tier, 0);
            }

            tier->lastAnnounceSucceeded = true;
            tier->lastAnnouncePeerCount = response->pex_count + response->pex6_count;

            if (isStopped)
            {
                /* now that we've successfully stopped the torrent,
                 * we can reset the up/down/corrupt count we've kept
                 * for this tracker */
                tier->byteCounts[TR_ANN_UP] = 0;
                tier->byteCounts[TR_ANN_DOWN] = 0;
                tier->byteCounts[TR_ANN_CORRUPT] = 0;
            }

            if (!isStopped && tier->announce_event_count == 0)
            {
                /* the queue is empty, so enqueue a perodic update */
                i = tier->announceIntervalSec;
                dbgmsg(tier, "Sending periodic reannounce in %d seconds", i);
                tier_announce_event_push(tier, TR_ANNOUNCE_EVENT_NONE, now + i);
            }
        }
    }

    tr_free(data);
}

static void announce_request_free(tr_announce_request* req)
{
    tr_free(req->tracker_id_str);
    tr_free(req->url);
    tr_free(req);
}

static void announce_request_delegate(tr_announcer* announcer, tr_announce_request* request, tr_announce_response_func callback,
    void* callback_data)
{
    tr_session* session = announcer->session;

#if 0

    fprintf(stderr, "ANNOUNCE: event %s isPartialSeed %d port %d key %d numwant %d up %" PRIu64 " down %" PRIu64
        " corrupt %" PRIu64 " left %" PRIu64 " url [%s] tracker_id_str [%s] peer_id [%20.20s]\n",
        tr_announce_event_get_string(request->event), (int)request->partial_seed, (int)request->port, request->key,
        request->numwant, request->up, request->down, request->corrupt, request->leftUntilComplete, request->url,
        request->tracker_id_str, request->peer_id);

#endif

    if (strncmp(request->url, "http", 4) == 0)
    {
        tr_tracker_http_announce(session, request, callback, callback_data);
    }
    else if (strncmp(request->url, "udp://", 6) == 0)
    {
        tr_tracker_udp_announce(session, request, callback, callback_data);
    }
    else
    {
        tr_logAddError("Unsupported url: %s", request->url);
    }

    announce_request_free(request);
}

static void tierAnnounce(tr_announcer* announcer, tr_tier* tier)
{
    TR_ASSERT(!tier->isAnnouncing);
    TR_ASSERT(tier->announce_event_count > 0);

    time_t const now = tr_time();

    tr_torrent* tor = tier->tor;
    tr_announce_event announce_event = tier_announce_event_pull(tier);
    tr_announce_request* req = announce_request_new(announcer, tor, tier, announce_event);

    struct announce_data* data = tr_new0(struct announce_data, 1);
    data->session = announcer->session;
    data->tierId = tier->key;
    data->isRunningOnSuccess = tor->isRunning;
    data->timeSent = now;
    data->event = announce_event;

    tier->isAnnouncing = true;
    tier->lastAnnounceStartTime = now;
    --announcer->slotsAvailable;

    announce_request_delegate(announcer, req, on_announce_done, data);
}

/***
****
****  SCRAPE
****
***/

static bool multiscrape_too_big(char const* errmsg)
{
    /* Found a tracker that returns some bespoke string for this case?
       Add your patch here and open a PR */
    static char const* const too_long_errors[] =
    {
        "Bad Request",
        "GET string too long",
        "Request-URI Too Long"
    };

    if (errmsg == NULL)
    {
        return false;
    }

    for (size_t i = 0; i < TR_N_ELEMENTS(too_long_errors); ++i)
    {
        if (strstr(errmsg, too_long_errors[i]) != NULL)
        {
            return true;
        }
    }

    return false;
}

static void on_scrape_error(tr_session* session, tr_tier* tier, char const* errmsg)
{
    int interval;

    /* increment the error count */
    if (tier->currentTracker != NULL)
    {
        ++tier->currentTracker->consecutiveFailures;
    }

    /* set the error message */
    dbgmsg(tier, "Scrape error: %s", errmsg);
    tr_logAddTorInfo(tier->tor, "Scrape error: %s", errmsg);
    tr_strlcpy(tier->lastScrapeStr, errmsg, sizeof(tier->lastScrapeStr));

    /* switch to the next tracker */
    tierIncrementTracker(tier);

    /* schedule a rescrape */
    interval = getRetryInterval(tier->currentTracker);
    dbgmsg(tier, "Retrying scrape in %zu seconds.", (size_t)interval);
    tr_logAddTorInfo(tier->tor, "Retrying scrape in %zu seconds.", (size_t)interval);
    tier->lastScrapeSucceeded = false;
    tier->scrapeAt = get_next_scrape_time(session, tier, interval);
}

static tr_tier* find_tier(tr_torrent* tor, char const* scrape)
{
    struct tr_torrent_tiers* tt = tor->tiers;

    for (int i = 0; tt != NULL && i < tt->tier_count; ++i)
    {
        tr_tracker const* const tracker = tt->tiers[i].currentTracker;

        if (tracker != NULL &&
            tracker->scrape_info != NULL &&
            tr_strcmp0(scrape, tracker->scrape_info->url) == 0)
        {
            return &tt->tiers[i];
        }
    }

    return NULL;
}

static void on_scrape_done(tr_scrape_response const* response, void* vsession)
{
    time_t const now = tr_time();
    tr_session* session = vsession;
    tr_announcer* announcer = session->announcer;

    for (int i = 0; i < response->row_count; ++i)
    {
        struct tr_scrape_response_row const* row = &response->rows[i];
        tr_torrent* tor = tr_torrentFindFromHash(session, row->info_hash);

        if (tor != NULL)
        {
            tr_tier* tier = find_tier(tor, response->url);

            if (tier != NULL)
            {
                dbgmsg(tier,
                    "scraped url:%s -- "
                    "did_connect:%d "
                    "did_timeout:%d "
                    "seeders:%d "
                    "leechers:%d "
                    "downloads:%d "
                    "downloaders:%d "
                    "min_request_interval:%d "
                    "err:%s ",
                    response->url,
                    (int)response->did_connect,
                    (int)response->did_timeout,
                    row->seeders,
                    row->leechers,
                    row->downloads,
                    row->downloaders,
                    response->min_request_interval,
                    response->errmsg != NULL ? response->errmsg : "none");

                tier->isScraping = false;
                tier->lastScrapeTime = now;
                tier->lastScrapeSucceeded = false;
                tier->lastScrapeTimedOut = response->did_timeout;

                if (!response->did_connect)
                {
                    on_scrape_error(session, tier, _("Could not connect to tracker"));
                }
                else if (response->did_timeout)
                {
                    on_scrape_error(session, tier, _("Tracker did not respond"));
                }
                else if (response->errmsg != NULL)
                {
                    on_scrape_error(session, tier, response->errmsg);
                }
                else
                {
                    tr_tracker* tracker;

                    tier->lastScrapeSucceeded = true;
                    tier->scrapeIntervalSec = MAX(DEFAULT_SCRAPE_INTERVAL_SEC, response->min_request_interval);
                    tier->scrapeAt = get_next_scrape_time(session, tier, tier->scrapeIntervalSec);
                    tr_logAddTorDbg(tier->tor, "Scrape successful. Rescraping in %d seconds.", tier->scrapeIntervalSec);

                    if ((tracker = tier->currentTracker) != NULL)
                    {
                        if (row->seeders >= 0)
                        {
                            tracker->seederCount = row->seeders;
                        }

                        if (row->leechers >= 0)
                        {
                            tracker->leecherCount = row->leechers;
                        }

                        if (row->downloads >= 0)
                        {
                            tracker->downloadCount = row->downloads;
                        }

                        tracker->downloaderCount = row->downloaders;
                        tracker->consecutiveFailures = 0;
                    }
                }
            }
        }
    }

    /* Maybe reduce the number of torrents in a multiscrape req */
    if (multiscrape_too_big(response->errmsg))
    {
        char const* url = response->url;
        int* multiscrape_max = &tr_announcerGetScrapeInfo(announcer, url)->multiscrape_max;

        /* Lower the max only if it hasn't already lowered for a similar error.
           For example if N parallel multiscrapes all have the same `max` and
           error out, lower the value once for that batch, not N times. */
        if (*multiscrape_max >= response->row_count)
        {
            int const n = MAX(1, *multiscrape_max - TR_MULTISCRAPE_STEP);
            if (*multiscrape_max != n)
            {
                char* scheme = NULL;
                char* host = NULL;
                int port;
                if (tr_urlParse(url, strlen(url), &scheme, &host, &port, NULL))
                {
                    /* don't log the full URL, since that might have a personal announce id */
                    char* sanitized_url = tr_strdup_printf("%s://%s:%d", scheme, host, port);
                    tr_logAddNamedInfo(sanitized_url, "Reducing multiscrape max to %d", n);
                    tr_free(sanitized_url);
                    tr_free(host);
                    tr_free(scheme);
                }

                *multiscrape_max = n;
            }
        }
    }

    if (announcer != NULL)
    {
        ++announcer->slotsAvailable;
    }
}

static void scrape_request_delegate(tr_announcer* announcer, tr_scrape_request const* request, tr_scrape_response_func callback,
    void* callback_data)
{
    tr_session* session = announcer->session;

    if (strncmp(request->url, "http", 4) == 0)
    {
        tr_tracker_http_scrape(session, request, callback, callback_data);
    }
    else if (strncmp(request->url, "udp://", 6) == 0)
    {
        tr_tracker_udp_scrape(session, request, callback, callback_data);
    }
    else
    {
        tr_logAddError("Unsupported url: %s", request->url);
    }
}

static void multiscrape(tr_announcer* announcer, tr_ptrArray* tiers)
{
    int request_count = 0;
    time_t const now = tr_time();
    int const tier_count = tr_ptrArraySize(tiers);
    int const max_request_count = MIN(announcer->slotsAvailable, tier_count);
    tr_scrape_request* requests = tr_new0(tr_scrape_request, max_request_count);

    /* batch as many info_hashes into a request as we can */
    for (int i = 0; i < tier_count; ++i)
    {
        tr_tier* tier = tr_ptrArrayNth(tiers, i);
        struct tr_scrape_info* const scrape_info = tier->currentTracker->scrape_info;
        uint8_t const* hash = tier->tor->info.hash;
        bool found = false;

        TR_ASSERT(scrape_info != NULL);

        /* if there's a request with this scrape URL and a free slot, use it */
        for (int j = 0; !found && j < request_count; ++j)
        {
            tr_scrape_request* req = &requests[j];

            if (req->info_hash_count >= scrape_info->multiscrape_max)
            {
                continue;
            }

            if (tr_strcmp0(req->url, scrape_info->url) != 0)
            {
                continue;
            }

            memcpy(req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH);
            tier->isScraping = true;
            tier->lastScrapeStartTime = now;
            found = true;
        }

        /* otherwise, if there's room for another request, build a new one */
        if (!found && request_count < max_request_count)
        {
            tr_scrape_request* req = &requests[request_count++];
            req->url = scrape_info->url;
            tier_build_log_name(tier, req->log_name, sizeof(req->log_name));

            memcpy(req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH);
            tier->isScraping = true;
            tier->lastScrapeStartTime = now;
        }
    }

    /* send the requests we just built */
    for (int i = 0; i < request_count; ++i)
    {
        scrape_request_delegate(announcer, &requests[i], on_scrape_done, announcer->session);
    }

    /* cleanup */
    tr_free(requests);
}

static void flushCloseMessages(tr_announcer* announcer)
{
    for (int i = 0, n = tr_ptrArraySize(&announcer->stops); i < n; ++i)
    {
        announce_request_delegate(announcer, tr_ptrArrayNth(&announcer->stops, i), NULL, NULL);
    }

    tr_ptrArrayClear(&announcer->stops);
}

static bool tierNeedsToAnnounce(tr_tier const* tier, time_t const now)
{
    return !tier->isAnnouncing && !tier->isScraping && tier->announceAt != 0 && tier->announceAt <= now &&
        tier->announce_event_count > 0;
}

static bool tierNeedsToScrape(tr_tier const* tier, time_t const now)
{
    return !tier->isScraping && tier->scrapeAt != 0 && tier->scrapeAt <= now && tier->currentTracker != NULL &&
        tier->currentTracker->scrape_info != NULL;
}

static int compareTiers(void const* va, void const* vb)
{
    int ret;
    tr_tier const* a = *(tr_tier const**)va;
    tr_tier const* b = *(tr_tier const**)vb;

    /* primary key: larger stats come before smaller */
    ret = compareTransfer(a->byteCounts[TR_ANN_UP], a->byteCounts[TR_ANN_DOWN], b->byteCounts[TR_ANN_UP],
        b->byteCounts[TR_ANN_DOWN]);

    /* secondary key: announcements that have been waiting longer go first */
    if (ret == 0 && a->announceAt != b->announceAt)
    {
        ret = a->announceAt < b->announceAt ? -1 : 1;
    }

    return ret;
}

static void announceMore(tr_announcer* announcer)
{
    int n;
    tr_torrent* tor;
    tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
    tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
    time_t const now = tr_time();

    dbgmsg(NULL, "announceMore: slotsAvailable is %d", announcer->slotsAvailable);

    if (announcer->slotsAvailable < 1)
    {
        return;
    }

    /* build a list of tiers that need to be announced */
    tor = NULL;

    while ((tor = tr_torrentNext(announcer->session, tor)) != NULL)
    {
        struct tr_torrent_tiers* tt = tor->tiers;

        for (int i = 0; tt != NULL && i < tt->tier_count; ++i)
        {
            tr_tier* tier = &tt->tiers[i];

            if (tierNeedsToAnnounce(tier, now))
            {
                tr_ptrArrayAppend(&announceMe, tier);
            }
            else if (tierNeedsToScrape(tier, now))
            {
                tr_ptrArrayAppend(&scrapeMe, tier);
            }
        }
    }

    n = tr_ptrArraySize(&announceMe);

    /* if there are more tiers than slots available, prioritize */
    if (n > announcer->slotsAvailable)
    {
        qsort(tr_ptrArrayBase(&announceMe), n, sizeof(tr_tier*), compareTiers);
        n = announcer->slotsAvailable;
    }

    /* announce some */
    for (int i = 0; i < n; ++i)
    {
        tr_tier* tier = tr_ptrArrayNth(&announceMe, i);
        tr_logAddTorDbg(tier->tor, "%s", "Announcing to tracker");
        dbgmsg(tier, "announcing tier %d of %d", i, n);
        tierAnnounce(announcer, tier);
    }

    /* scrape some */
    multiscrape(announcer, &scrapeMe);

    /* cleanup */
    tr_ptrArrayDestruct(&scrapeMe, NULL);
    tr_ptrArrayDestruct(&announceMe, NULL);
}

static void onUpkeepTimer(evutil_socket_t foo UNUSED, short bar UNUSED, void* vannouncer)
{
    tr_announcer* announcer = vannouncer;
    tr_session* session = announcer->session;
    bool const is_closing = session->isClosed;
    time_t const now = tr_time();

    tr_sessionLock(session);

    /* maybe send out some "stopped" messages for closed torrents */
    flushCloseMessages(announcer);

    /* maybe send out some announcements to trackers */
    if (!is_closing)
    {
        announceMore(announcer);
    }

    /* TAU upkeep */
    if (announcer->tauUpkeepAt <= now)
    {
        announcer->tauUpkeepAt = now + TAU_UPKEEP_INTERVAL_SECS;
        tr_tracker_udp_upkeep(session);
    }

    /* set up the next timer */
    tr_timerAdd(announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0);

    tr_sessionUnlock(session);
}

/***
****
***/

tr_tracker_stat* tr_announcerStats(tr_torrent const* torrent, int* setmeTrackerCount)
{
    TR_ASSERT(tr_isTorrent(torrent));

    time_t const now = tr_time();

    int out = 0;
    tr_tracker_stat* ret;
    struct tr_torrent_tiers* tt = torrent->tiers;

    /* alloc the stats */
    *setmeTrackerCount = tt->tracker_count;
    ret = tr_new0(tr_tracker_stat, tt->tracker_count);

    /* populate the stats */
    for (int i = 0; i < tt->tier_count; ++i)
    {
        tr_tier const* const tier = &tt->tiers[i];

        for (int j = 0; j < tier->tracker_count; ++j)
        {
            tr_tracker const* const tracker = &tier->trackers[j];
            tr_tracker_stat* st = &ret[out++];

            st->id = tracker->id;
            tr_strlcpy(st->host, tracker->key, sizeof(st->host));
            tr_strlcpy(st->announce, tracker->announce, sizeof(st->announce));
            st->tier = i;
            st->isBackup = tracker != tier->currentTracker;
            st->lastScrapeStartTime = tier->lastScrapeStartTime;

            if (tracker->scrape_info != NULL)
            {
                tr_strlcpy(st->scrape, tracker->scrape_info->url, sizeof(st->scrape));
            }
            else
            {
                st->scrape[0] = '\0';
            }

            st->seederCount = tracker->seederCount;
            st->leecherCount = tracker->leecherCount;
            st->downloadCount = tracker->downloadCount;

            if (st->isBackup)
            {
                st->scrapeState = TR_TRACKER_INACTIVE;
                st->announceState = TR_TRACKER_INACTIVE;
                st->nextScrapeTime = 0;
                st->nextAnnounceTime = 0;
            }
            else
            {
                if ((st->hasScraped = tier->lastScrapeTime != 0))
                {
                    st->lastScrapeTime = tier->lastScrapeTime;
                    st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
                    st->lastScrapeTimedOut = tier->lastScrapeTimedOut;
                    tr_strlcpy(st->lastScrapeResult, tier->lastScrapeStr, sizeof(st->lastScrapeResult));
                }

                if (tier->isScraping)
                {
                    st->scrapeState = TR_TRACKER_ACTIVE;
                }
                else if (tier->scrapeAt == 0)
                {
                    st->scrapeState = TR_TRACKER_INACTIVE;
                }
                else if (tier->scrapeAt > now)
                {
                    st->scrapeState = TR_TRACKER_WAITING;
                    st->nextScrapeTime = tier->scrapeAt;
                }
                else
                {
                    st->scrapeState = TR_TRACKER_QUEUED;
                }

                st->lastAnnounceStartTime = tier->lastAnnounceStartTime;

                if ((st->hasAnnounced = tier->lastAnnounceTime != 0))
                {
                    st->lastAnnounceTime = tier->lastAnnounceTime;
                    tr_strlcpy(st->lastAnnounceResult, tier->lastAnnounceStr, sizeof(st->lastAnnounceResult));
                    st->lastAnnounceSucceeded = tier->lastAnnounceSucceeded;
                    st->lastAnnounceTimedOut = tier->lastAnnounceTimedOut;
                    st->lastAnnouncePeerCount = tier->lastAnnouncePeerCount;
                }

                if (tier->isAnnouncing)
                {
                    st->announceState = TR_TRACKER_ACTIVE;
                }
                else if (!torrent->isRunning || tier->announceAt == 0)
                {
                    st->announceState = TR_TRACKER_INACTIVE;
                }
                else if (tier->announceAt > now)
                {
                    st->announceState = TR_TRACKER_WAITING;
                    st->nextAnnounceTime = tier->announceAt;
                }
                else
                {
                    st->announceState = TR_TRACKER_QUEUED;
                }
            }
        }
    }

    return ret;
}

void tr_announcerStatsFree(tr_tracker_stat* trackers, int trackerCount UNUSED)
{
    tr_free(trackers);
}

/***
****
***/

static void copy_tier_attributes_impl(struct tr_tier* tgt, int trackerIndex, tr_tier const* src)
{
    /* sanity clause */
    TR_ASSERT(trackerIndex < tgt->tracker_count);
    TR_ASSERT(tr_strcmp0(tgt->trackers[trackerIndex].announce, src->currentTracker->announce) == 0);

    tr_tier const keep = *tgt;

    /* bitwise copy will handle most of tr_tier's fields... */
    *tgt = *src;

    /* ...fix the fields that can't be cleanly bitwise-copied */
    tgt->wasCopied = true;
    tgt->trackers = keep.trackers;
    tgt->tracker_count = keep.tracker_count;
    tgt->announce_events = tr_memdup(src->announce_events, sizeof(tr_announce_event) * src->announce_event_count);
    tgt->announce_event_count = src->announce_event_count;
    tgt->announce_event_alloc = src->announce_event_count;
    tgt->currentTrackerIndex = trackerIndex;
    tgt->currentTracker = &tgt->trackers[trackerIndex];
    tgt->currentTracker->seederCount = src->currentTracker->seederCount;
    tgt->currentTracker->leecherCount = src->currentTracker->leecherCount;
    tgt->currentTracker->downloadCount = src->currentTracker->downloadCount;
    tgt->currentTracker->downloaderCount = src->currentTracker->downloaderCount;
}

static void copy_tier_attributes(struct tr_torrent_tiers* tt, tr_tier const* src)
{
    bool found = false;

    /* find a tier (if any) which has a match for src->currentTracker */
    for (int i = 0; !found && i < tt->tier_count; ++i)
    {
        for (int j = 0; !found && j < tt->tiers[i].tracker_count; ++j)
        {
            if ((found = tr_strcmp0(src->currentTracker->announce, tt->tiers[i].trackers[j].announce) == 0))
            {
                copy_tier_attributes_impl(&tt->tiers[i], j, src);
            }
        }
    }
}

void tr_announcerResetTorrent(tr_announcer* announcer UNUSED, tr_torrent* tor)
{
    TR_ASSERT(tor->tiers != NULL);

    time_t const now = tr_time();

    struct tr_torrent_tiers* tt = tor->tiers;
    tr_torrent_tiers old = *tt;

    /* remove the old tiers / trackers */
    tt->tiers = NULL;
    tt->trackers = NULL;
    tt->tier_count = 0;
    tt->tracker_count = 0;

    /* create the new tiers / trackers */
    addTorrentToTier(tt, tor);

    /* copy the old tiers' states into their replacements */
    for (int i = 0; i < old.tier_count; ++i)
    {
        if (old.tiers[i].currentTracker != NULL)
        {
            copy_tier_attributes(tt, &old.tiers[i]);
        }
    }

    /* kickstart any tiers that didn't get started */
    if (tor->isRunning)
    {
        for (int i = 0; i < tt->tier_count; ++i)
        {
            if (!tt->tiers[i].wasCopied)
            {
                tier_announce_event_push(&tt->tiers[i], TR_ANNOUNCE_EVENT_STARTED, now);
            }
        }
    }

    /* cleanup */
    tiersDestruct(&old);
}