summaryrefslogtreecommitdiff
path: root/src/ot_livesync.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ot_livesync.c')
-rw-r--r--src/ot_livesync.c271
1 files changed, 271 insertions, 0 deletions
diff --git a/src/ot_livesync.c b/src/ot_livesync.c
new file mode 100644
index 0000000..1381b3b
--- /dev/null
+++ b/src/ot_livesync.c
@@ -0,0 +1,271 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <arpa/inet.h>
6#include <pthread.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/socket.h>
11#include <sys/types.h>
12#include <unistd.h>
13
14/* Opentracker */
15#include "ot_accesslist.h"
16#include "ot_ip.h"
17#include "ot_socket.h"
18#include "ot_livesync.h"
19#include "ot_mutex.h"
20#include "ot_stats.h"
21#include "trackerlogic.h"
22
23#ifdef WANT_SYNC_LIVE
24
25/* The multicast group, legacy opentrackers joined by default 224.0.23.5, so it
26 stays the default */
27static ot_ip6 g_group = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5};
28static uint16_t g_port = LIVESYNC_PORT;
29
30#define LIVESYNC_INCOMING_BUFFSIZE (256 * 256)
31
32#define LIVESYNC_OUTGOING_BUFFSIZE_PEERS 1480
33#define LIVESYNC_OUTGOING_WATERMARK_PEERS (sizeof(ot_hash) + OT_PEER_SIZE6)
34
35#define LIVESYNC_MAXDELAY 15 /* seconds */
36
37enum { OT_SYNC_PEER4, OT_SYNC_PEER6 };
38
39/* Forward declaration */
40static void *livesync_worker(void *args);
41
42/* For incoming packets */
43static int g_socket_in = -1;
44
45/* For outgoing packets */
46static int g_socket_out = -1;
47
48static pthread_mutex_t g_outbuf_mutex = PTHREAD_MUTEX_INITIALIZER;
49typedef struct {
50 uint8_t data[LIVESYNC_OUTGOING_BUFFSIZE_PEERS];
51 size_t fill;
52 ot_time next_packet_time;
53} sync_buffer;
54
55static sync_buffer g_v6_buf;
56static sync_buffer g_v4_buf;
57
58static pthread_t thread_id;
59static int thread_started;
60static _Atomic int thread_left;
61void livesync_init(void) {
62 uint32_t packet_type6 = htonl(OT_SYNC_PEER6);
63 uint32_t packet_type4 = htonl(OT_SYNC_PEER4);
64
65 if (g_socket_in == -1)
66 return;
67
68 /* Prepare outgoing peers buffer */
69 memcpy(g_v6_buf.data, &g_tracker_id, sizeof(g_tracker_id));
70 memcpy(g_v4_buf.data, &g_tracker_id, sizeof(g_tracker_id));
71
72 memcpy(g_v6_buf.data + sizeof(g_tracker_id), &packet_type6, sizeof(packet_type6));
73 memcpy(g_v4_buf.data + sizeof(g_tracker_id), &packet_type4, sizeof(packet_type4));
74
75 g_v6_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t);
76 g_v4_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t);
77
78 g_v6_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
79 g_v4_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
80
81 thread_started = !pthread_create(&thread_id, NULL, livesync_worker, NULL);
82}
83
84void livesync_deinit(void) {
85 if (thread_started) {
86 /* Repeats so it interrupts a read already under way */
87 while (!atomic_load(&thread_left)) {
88 pthread_kill(thread_id, OT_SIGNAL_WAKE);
89 usleep(1000);
90 }
91 pthread_join(thread_id, NULL);
92 thread_started = 0;
93 }
94
95 if (g_socket_in != -1)
96 close(g_socket_in);
97 if (g_socket_out != -1)
98 close(g_socket_out);
99}
100
101int livesync_set_group(const ot_ip6 group) {
102 /* v4 multicast is 224.0.0.0/4, and the socket layer has nothing else */
103 if (!ip_is_v4mapped(group) || (group[12] & 0xf0) != 0xe0)
104 return -1;
105 memcpy(g_group, group, sizeof(ot_ip6));
106 return 0;
107}
108
109void livesync_bind_mcast(ot_ip6 ip, uint16_t port) {
110 if (!ip_is_v4mapped(ip))
111 exerr("v6 mcast support not yet available.");
112
113 if (g_socket_in != -1)
114 exerr("Error: Livesync listen ip specified twice.");
115
116 g_port = port;
117
118 if ((g_socket_in = socket_mcast_join(g_group, ip, port)) == -1)
119 exerr("Error: Cant bind live sync incoming socket.");
120
121 if ((g_socket_out = socket_mcast_bind(ip, port, 0)) == -1)
122 exerr("Error: Cant bind live sync outgoing socket.");
123}
124
125/* Caller MUST hold g_outbuf_mutex. Returns with g_outbuf_mutex unlocked */
126static void livesync_issue_peersync(sync_buffer *buf) {
127 char mycopy[LIVESYNC_OUTGOING_BUFFSIZE_PEERS];
128 size_t fill = buf->fill;
129
130 /* The flush watermark keeps fill within the buffer */
131 if (fill > LIVESYNC_OUTGOING_BUFFSIZE_PEERS)
132 fill = LIVESYNC_OUTGOING_BUFFSIZE_PEERS;
133
134 memcpy(mycopy, buf->data, fill);
135 buf->fill = sizeof(g_tracker_id) + sizeof(uint32_t);
136 buf->next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
137
138 /* From now this thread has a local copy of the buffer and
139 has modified the protected element */
140 pthread_mutex_unlock(&g_outbuf_mutex);
141
142 socket_mcast_send(g_socket_out, mycopy, fill, g_group, g_port);
143}
144
145static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size) {
146 size_t off = sizeof(g_tracker_id) + sizeof(uint32_t);
147
148 /* Prepare peer record for v4mapped expansion */
149 if (peer_size == OT_PEER_SIZE4)
150 memcpy(&ws->peer, ip_v4mapped_prefix, sizeof(ip_v4mapped_prefix));
151
152 /* Now basic sanity checks have been done on the live sync packet
153 We might add more testing and logging. */
154 while ((ssize_t)(off + sizeof(ot_hash) + peer_size) <= ws->request_size) {
155 char *peer_ip = ws->request + off + sizeof(ot_hash);
156 if (peer_size == OT_PEER_SIZE4)
157 memcpy(&ws->peer[0] + sizeof(ip_v4mapped_prefix), peer_ip, peer_size);
158 else
159 memcpy(&ws->peer, peer_ip, peer_size);
160 ws->hash = (ot_hash *)(ws->request + off);
161
162 if (!g_opentracker_running)
163 return;
164
165 if (OT_PEERFLAG(ws->peer) & PEER_FLAG_STOPPED)
166 remove_peer_from_torrent(FLAG_MCA, ws);
167 else
168 add_peer_to_torrent_and_return_peers(FLAG_MCA, ws, /* amount = */ 0);
169
170 off += sizeof(ot_hash) + peer_size;
171 }
172
173 stats_issue_event(ws, EVENT_SYNC, 0, (ws->request_size - sizeof(g_tracker_id) - sizeof(uint32_t)) / ((ssize_t)sizeof(ot_hash) + peer_size));
174}
175
176/* Tickle the live sync module from time to time, so no events get
177 stuck when there's not enough traffic to fill udp packets fast
178 enough */
179void livesync_ticker(void) {
180 if (g_socket_out == -1)
181 return;
182
183 /* livesync_issue_peersync sets g_next_packet_time */
184 pthread_mutex_lock(&g_outbuf_mutex);
185 if (time_is_past(g_v6_buf.next_packet_time) && g_v6_buf.fill > sizeof(g_tracker_id) + sizeof(uint32_t))
186 livesync_issue_peersync(&g_v6_buf);
187 else
188 pthread_mutex_unlock(&g_outbuf_mutex);
189
190 pthread_mutex_lock(&g_outbuf_mutex);
191 if (time_is_past(g_v4_buf.next_packet_time) && g_v4_buf.fill > sizeof(g_tracker_id) + sizeof(uint32_t))
192 livesync_issue_peersync(&g_v4_buf);
193 else
194 pthread_mutex_unlock(&g_outbuf_mutex);
195}
196
197/* Inform live sync about whats going on. */
198void livesync_tell(struct ot_workstruct *ws) {
199 size_t peer_size; /* initialized in next line */
200 ot_peer *peer_src;
201 sync_buffer *dest_buf;
202
203 if (g_socket_out == -1)
204 return;
205
206 peer_src = peer_from_peer6(&ws->peer, &peer_size);
207 dest_buf = peer_size == OT_PEER_SIZE6 ? &g_v6_buf : &g_v4_buf;
208
209 pthread_mutex_lock(&g_outbuf_mutex);
210
211 memcpy(dest_buf->data + dest_buf->fill, ws->hash, sizeof(ot_hash));
212 dest_buf->fill += sizeof(ot_hash);
213
214 memcpy(dest_buf->data + dest_buf->fill, peer_src, peer_size);
215 dest_buf->fill += peer_size;
216
217 if (dest_buf->fill >= LIVESYNC_OUTGOING_BUFFSIZE_PEERS - LIVESYNC_OUTGOING_WATERMARK_PEERS)
218 livesync_issue_peersync(dest_buf);
219 else
220 pthread_mutex_unlock(&g_outbuf_mutex);
221}
222
223static void *livesync_worker(void *args) {
224 struct ot_workstruct ws;
225 ot_ip6 in_ip;
226 uint16_t in_port;
227
228 (void)args;
229
230 if (workstruct_init(&ws, LIVESYNC_INCOMING_BUFFSIZE) == -1) {
231 fprintf(stderr, "Warning: live sync could not get its buffers and will not run.\n");
232 atomic_store(&thread_left, 1);
233 return NULL;
234 }
235 ws.request = ws.inbuf;
236
237 while (g_opentracker_running) {
238 uint32_t packet_type;
239
240 ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port);
241
242 /* Expect at least tracker id and packet type */
243 if (ws.request_size <= (ssize_t)(sizeof(g_tracker_id) + sizeof(uint32_t)))
244 continue;
245 if (!accesslist_is_blessed(in_ip, OT_PERMISSION_MAY_LIVESYNC))
246 continue;
247 if (!memcmp(ws.inbuf, &g_tracker_id, sizeof(g_tracker_id))) {
248 /* TODO: log packet coming from ourselves */
249 continue;
250 }
251
252 memcpy(&packet_type, ws.inbuf + sizeof(g_tracker_id), sizeof(packet_type));
253
254 switch (ntohl(packet_type)) {
255 case OT_SYNC_PEER6:
256 livesync_handle_peersync(&ws, OT_PEER_SIZE6);
257 break;
258 case OT_SYNC_PEER4:
259 livesync_handle_peersync(&ws, OT_PEER_SIZE4);
260 break;
261 default:
262 break;
263 }
264 }
265
266 workstruct_deinit(&ws);
267 atomic_store(&thread_left, 1);
268 return NULL;
269}
270
271#endif