-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathannounce_thread.hpp
117 lines (94 loc) · 2.9 KB
/
announce_thread.hpp
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
/*
utrack is a very small an efficient BitTorrent tracker
Copyright (C) 2010-2013 Arvid Norberg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _ANNOUNCE_THREAD_HPP_
#define _ANNOUNCE_THREAD_HPP_
#include "messages.hpp"
#include "swarm.hpp"
#include "socket.hpp"
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#endif
#include <thread>
#include <condition_variable>
#include <queue>
#include <unordered_map>
#include <array>
#include <vector>
struct announce_msg
{
union
{
udp_announce_message announce;
udp_scrape_message scrape;
} bits;
sockaddr_in from;
};
extern "C" int siphash(unsigned char *out, const unsigned char *in
, unsigned long long inlen, const unsigned char *k);
std::array<uint8_t, 16> gen_random_key();
struct siphash_fun
{
size_t operator()(sha1_hash const& h) const
{
// this is the secret key used in siphash to prevent hashcolision
// attacks. It's initialized to random bytes on startup (or first use)
static std::array<uint8_t, 16> hash_key = gen_random_key();
std::uint64_t ret;
siphash((std::uint8_t*)&ret, (std::uint8_t const*)h.val, sizeof(h.val)
, hash_key.data());
return ret;
}
};
// this is a thread that handles the announce for a specific
// set of info-hashes, and then sends a response over its own
// UDP socket
struct announce_thread
{
#ifdef USE_PCAP
announce_thread(packet_socket& s);
#else
announce_thread(int listen_port);
#endif
// disallow copy
announce_thread(announce_thread const&) = delete;
announce_thread& operator=(announce_thread const&) = delete;
void thread_fun();
void post_announces(std::vector<announce_msg> m);
~announce_thread();
std::thread::native_handle_type native_handle() { return m_thread.native_handle(); }
private:
// job queue
std::mutex m_mutex;
std::condition_variable m_cond;
// this is the queue new jobs are posted to
std::vector<std::vector<announce_msg>> m_queue;
// the swarm hash table. Each thread has its own hash table of swarms.
// swarms are pinned to certain threads based on their info-hash
typedef std::unordered_map<sha1_hash, swarm, siphash_fun> swarm_map_t;
swarm_map_t m_swarms;
#ifdef USE_PCAP
packet_socket& m_sock;
#else
// socket used to send responses to
packet_socket m_sock;
#endif
bool m_quit;
int m_queue_size;
std::thread m_thread;
};
#endif