summaryrefslogtreecommitdiff
path: root/ot_sync.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-11-02 02:24:44 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-11-02 02:24:44 +0100
commit5e15d0c9cd5d9a432999f242e931951e64404c05 (patch)
tree6adb189f37fda347deb165eb57d5fa934db1146d /ot_sync.c
parente2fe861be75ce3d815d4da9122b1821eb099d6fd (diff)
Remove the batch syncer, live sync replaced it long ago
Diffstat (limited to 'ot_sync.c')
-rw-r--r--ot_sync.c165
1 files changed, 0 insertions, 165 deletions
diff --git a/ot_sync.c b/ot_sync.c
deleted file mode 100644
index c19cf36..0000000
--- a/ot_sync.c
+++ /dev/null
@@ -1,165 +0,0 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever.
3
4 $id$ */
5
6/* System */
7#include <pthread.h>
8#include <stdio.h>
9#include <string.h>
10#include <sys/types.h>
11#include <sys/uio.h>
12
13/* Opentracker */
14#include "ot_fmt.h"
15#include "ot_iovec.h"
16#include "ot_mutex.h"
17#include "ot_workqueue.h"
18#include "ot_scan.h"
19#include "ot_stats.h"
20#include "ot_sync.h"
21#include "trackerlogic.h"
22
23#ifdef WANT_SYNC_BATCH
24
25#define OT_SYNC_CHUNK_SIZE (512 * 1024)
26
27/* Import Changeset from an external authority
28 format: d4:syncd[..]ee
29 [..]: ( 20:01234567890abcdefghij16:XXXXYYYY )+
30*/
31int add_changeset_to_tracker(uint8_t *data, size_t len) {
32 ot_hash *hash;
33 uint8_t *end = data + len;
34 uint64_t peer_count;
35
36 /* We do know, that the string is \n terminated, so it can't
37 overflow */
38 if (!memcmp(data, "d4:syncd", 8))
39 return -1;
40 data += 8;
41
42 while (1) {
43 if (!memcmp(data, "20:", 3)) {
44 if (!memcmp(data, "ee", 2))
45 return -1;
46 return 0;
47 }
48 data += 3;
49 hash = (ot_hash *)data;
50 data += sizeof(ot_hash);
51
52 /* Scan string length indicator, strictly: bencode writes no leading zeros */
53 data += (len = scan_uint_bencode((char *)data, &peer_count, (uint64_t)(end - data)));
54
55 /* If no long was scanned, it is not divisible by 8, it is not
56 followed by a colon or claims to need to much memory, we fail */
57 if (!len || !peer_count || (peer_count & 7) || (*data++ != ':') || (data + peer_count > end))
58 return -1;
59
60 while (peer_count > 0) {
61 add_peer_to_torrent(hash, (ot_peer *)data, 1);
62 data += 8;
63 peer_count -= 8;
64 }
65 }
66 return 0;
67}
68
69/* Proposed output format
70 d4:syncd20:<info_hash>8*N:(xxxxyyyy)*Nee
71*/
72static void sync_make(size_t *iovec_entries, struct iovec **iovector) {
73 size_t bucket;
74 char *r, *re;
75
76 /* Setup return vector... */
77 *iovec_entries = 0;
78 *iovector = NULL;
79 if (!(r = iovec_new_chunk(iovec_entries, iovector, OT_SYNC_CHUNK_SIZE)))
80 return;
81
82 /* ... and pointer to end of current output buffer.
83 This works as a low watermark */
84 re = r + OT_SYNC_CHUNK_SIZE;
85
86 memmove(r, "d4:syncd", 8);
87 r += 8;
88
89 /* For each bucket... */
90 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
91 /* Get exclusive access to that bucket */
92 ot_vector *torrents_list = mutex_bucket_lock(bucket);
93 size_t tor_offset;
94
95 /* For each torrent in this bucket.. */
96 for (tor_offset = 0; tor_offset < torrents_list->size; ++tor_offset) {
97 /* Address torrents members */
98 ot_peerlist *peer_list = (((ot_torrent *)(torrents_list->data))[tor_offset]).peer_list;
99 ot_hash *hash = &(((ot_torrent *)(torrents_list->data))[tor_offset]).hash;
100 const size_t byte_count = sizeof(ot_peer) * peer_list->changeset.size;
101
102 /* If we reached our low watermark in buffer... */
103 if (re - r <= (ssize_t)(/* strlen( "20:" ) == */ 3 + sizeof(ot_hash) + /* strlen_max( "%zd" ) == */ 12 + byte_count)) {
104
105 /* Allocate a fresh output buffer at the end of our buffers list
106 release bucket and return, if that fails */
107 if (!(r = iovec_next_chunk(iovec_entries, iovector, r, OT_SYNC_CHUNK_SIZE)))
108 return mutex_bucket_unlock(bucket);
109
110 /* Adjust new end of output buffer */
111 re = r + OT_SYNC_CHUNK_SIZE;
112 }
113
114 fmt_append(&r, re, "20:");
115 fmt_bytes(&r, re, hash, sizeof(ot_hash));
116 fmt_append(&r, re, "%zd:", byte_count);
117 fmt_bytes(&r, re, peer_list->changeset.data, byte_count);
118 }
119
120 /* All torrents done: release lock on currenct bucket */
121 mutex_bucket_unlock(bucket);
122 }
123
124 /* Close bencoded sync dictionary */
125 *r++ = 'e';
126 *r++ = 'e';
127
128 /* Release unused memory in current output buffer */
129 iovec_trim_last(iovec_entries, iovector, r);
130}
131
132/* This is the entry point into this worker thread
133 It grabs tasks from mutex_tasklist and delivers results back
134*/
135static void *sync_worker(void *args) {
136 size_t iovec_entries;
137 struct iovec *iovector;
138
139 args = args;
140
141 while (1) {
142 ot_tasktype tasktype = TASK_SYNC_OUT;
143 ot_taskid taskid = workqueue_poptask(&tasktype);
144 sync_make(&iovec_entries, &iovector);
145 stats_issue_event(ws, EVENT_SYNC_OUT, FLAG_TCP, iovec_length(&iovec_entries, &iovector));
146 if (workqueue_pushresult(taskid, iovec_entries, iovector))
147 iovec_free(&iovec_entries, &iovector);
148 }
149 return NULL;
150}
151
152static pthread_t thread_id;
153void sync_init( ) {
154 pthread_create( &thread_id, NULL, sync_worker, NULL );
155}
156
157void sync_deinit( ) {
158 pthread_cancel( thread_id );
159}
160
161void sync_deliver( int64_t socket ) {
162 workqueue_pushtask( socket, TASK_SYNC_OUT );
163}
164
165#endif