summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2024-04-15 00:41:43 +0200
committerDirk Engling <erdgeist@erdgeist.org>2024-04-15 00:41:43 +0200
commit7a48a693450af7fd9cc4b61180ff4afb969c272a (patch)
treecc1e578d4d3419327befb9cf9a4e1830309a6421
parent7c633c259ebc4a863c5076462c5792ecb8b9f617 (diff)
clang-format
-rw-r--r--ot_sync.c116
-rw-r--r--ot_sync.h8
2 files changed, 66 insertions, 58 deletions
diff --git a/ot_sync.c b/ot_sync.c
index cd66a46..259d4de 100644
--- a/ot_sync.c
+++ b/ot_sync.c
@@ -4,64 +4,66 @@
4 $id$ */ 4 $id$ */
5 5
6/* System */ 6/* System */
7#include <sys/types.h> 7#include <pthread.h>
8#include <sys/mman.h>
9#include <sys/uio.h>
10#include <stdio.h> 8#include <stdio.h>
11#include <string.h> 9#include <string.h>
12#include <pthread.h> 10#include <sys/mman.h>
11#include <sys/types.h>
12#include <sys/uio.h>
13 13
14/* Libowfat */ 14/* Libowfat */
15#include "scan.h"
16#include "byte.h" 15#include "byte.h"
17#include "io.h" 16#include "io.h"
17#include "scan.h"
18 18
19/* Opentracker */ 19/* Opentracker */
20#include "trackerlogic.h" 20#include "ot_iovec.h"
21#include "ot_mutex.h" 21#include "ot_mutex.h"
22#include "ot_sync.h"
23#include "ot_stats.h" 22#include "ot_stats.h"
24#include "ot_iovec.h" 23#include "ot_sync.h"
24#include "trackerlogic.h"
25 25
26#ifdef WANT_SYNC_BATCH 26#ifdef WANT_SYNC_BATCH
27 27
28#define OT_SYNC_CHUNK_SIZE (512*1024) 28#define OT_SYNC_CHUNK_SIZE (512 * 1024)
29 29
30/* Import Changeset from an external authority 30/* Import Changeset from an external authority
31 format: d4:syncd[..]ee 31 format: d4:syncd[..]ee
32 [..]: ( 20:01234567890abcdefghij16:XXXXYYYY )+ 32 [..]: ( 20:01234567890abcdefghij16:XXXXYYYY )+
33*/ 33*/
34int add_changeset_to_tracker( uint8_t *data, size_t len ) { 34int add_changeset_to_tracker(uint8_t *data, size_t len) {
35 ot_hash *hash; 35 ot_hash *hash;
36 uint8_t *end = data + len; 36 uint8_t *end = data + len;
37 unsigned long peer_count; 37 unsigned long peer_count;
38 38
39 /* We do know, that the string is \n terminated, so it cant 39 /* We do know, that the string is \n terminated, so it cant
40 overflow */ 40 overflow */
41 if( byte_diff( data, 8, "d4:syncd" ) ) return -1; 41 if (byte_diff(data, 8, "d4:syncd"))
42 return -1;
42 data += 8; 43 data += 8;
43 44
44 while( 1 ) { 45 while (1) {
45 if( byte_diff( data, 3, "20:" ) ) { 46 if (byte_diff(data, 3, "20:")) {
46 if( byte_diff( data, 2, "ee" ) ) 47 if (byte_diff(data, 2, "ee"))
47 return -1; 48 return -1;
48 return 0; 49 return 0;
49 } 50 }
50 data += 3; 51 data += 3;
51 hash = (ot_hash*)data; 52 hash = (ot_hash *)data;
52 data += sizeof( ot_hash ); 53 data += sizeof(ot_hash);
53 54
54 /* Scan string length indicator */ 55 /* Scan string length indicator */
55 data += ( len = scan_ulong( (char*)data, &peer_count ) ); 56 data += (len = scan_ulong((char *)data, &peer_count));
56 57
57 /* If no long was scanned, it is not divisible by 8, it is not 58 /* If no long was scanned, it is not divisible by 8, it is not
58 followed by a colon or claims to need to much memory, we fail */ 59 followed by a colon or claims to need to much memory, we fail */
59 if( !len || !peer_count || ( peer_count & 7 ) || ( *data++ != ':' ) || ( data + peer_count > end ) ) 60 if (!len || !peer_count || (peer_count & 7) || (*data++ != ':') || (data + peer_count > end))
60 return -1; 61 return -1;
61 62
62 while( peer_count > 0 ) { 63 while (peer_count > 0) {
63 add_peer_to_torrent( hash, (ot_peer*)data, 1 ); 64 add_peer_to_torrent(hash, (ot_peer *)data, 1);
64 data += 8; peer_count -= 8; 65 data += 8;
66 peer_count -= 8;
65 } 67 }
66 } 68 }
67 return 0; 69 return 0;
@@ -70,80 +72,86 @@ int add_changeset_to_tracker( uint8_t *data, size_t len ) {
70/* Proposed output format 72/* Proposed output format
71 d4:syncd20:<info_hash>8*N:(xxxxyyyy)*Nee 73 d4:syncd20:<info_hash>8*N:(xxxxyyyy)*Nee
72*/ 74*/
73static void sync_make( int *iovec_entries, struct iovec **iovector ) { 75static void sync_make(int *iovec_entries, struct iovec **iovector) {
74 int bucket; 76 int bucket;
75 char *r, *re; 77 char *r, *re;
76 78
77 /* Setup return vector... */ 79 /* Setup return vector... */
78 *iovec_entries = 0; 80 *iovec_entries = 0;
79 *iovector = NULL; 81 *iovector = NULL;
80 if( !( r = iovec_increase( iovec_entries, iovector, OT_SYNC_CHUNK_SIZE ) ) ) 82 if (!(r = iovec_increase(iovec_entries, iovector, OT_SYNC_CHUNK_SIZE)))
81 return; 83 return;
82 84
83 /* ... and pointer to end of current output buffer. 85 /* ... and pointer to end of current output buffer.
84 This works as a low watermark */ 86 This works as a low watermark */
85 re = r + OT_SYNC_CHUNK_SIZE; 87 re = r + OT_SYNC_CHUNK_SIZE;
86 88
87 memmove( r, "d4:syncd", 8 ); r += 8; 89 memmove(r, "d4:syncd", 8);
90 r += 8;
88 91
89 /* For each bucket... */ 92 /* For each bucket... */
90 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { 93 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
91 /* Get exclusive access to that bucket */ 94 /* Get exclusive access to that bucket */
92 ot_vector *torrents_list = mutex_bucket_lock( bucket ); 95 ot_vector *torrents_list = mutex_bucket_lock(bucket);
93 size_t tor_offset; 96 size_t tor_offset;
94 97
95 /* For each torrent in this bucket.. */ 98 /* For each torrent in this bucket.. */
96 for( tor_offset=0; tor_offset<torrents_list->size; ++tor_offset ) { 99 for (tor_offset = 0; tor_offset < torrents_list->size; ++tor_offset) {
97 /* Address torrents members */ 100 /* Address torrents members */
98 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[tor_offset] ).peer_list; 101 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; 102 ot_hash *hash = &(((ot_torrent *)(torrents_list->data))[tor_offset]).hash;
100 const size_t byte_count = sizeof(ot_peer) * peer_list->changeset.size; 103 const size_t byte_count = sizeof(ot_peer) * peer_list->changeset.size;
101 104
102 /* If we reached our low watermark in buffer... */ 105 /* 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 ) ) { 106 if (re - r <= (ssize_t)(/* strlen( "20:" ) == */ 3 + sizeof(ot_hash) + /* strlen_max( "%zd" ) == */ 12 + byte_count)) {
104 107
105 /* Allocate a fresh output buffer at the end of our buffers list 108 /* Allocate a fresh output buffer at the end of our buffers list
106 release bucket and return, if that fails */ 109 release bucket and return, if that fails */
107 if( !( r = iovec_fix_increase_or_free( iovec_entries, iovector, r, OT_SYNC_CHUNK_SIZE ) ) ) 110 if (!(r = iovec_fix_increase_or_free(iovec_entries, iovector, r, OT_SYNC_CHUNK_SIZE)))
108 return mutex_bucket_unlock( bucket ); 111 return mutex_bucket_unlock(bucket);
109 112
110 /* Adjust new end of output buffer */ 113 /* Adjust new end of output buffer */
111 re = r + OT_SYNC_CHUNK_SIZE; 114 re = r + OT_SYNC_CHUNK_SIZE;
112 } 115 }
113 116
114 *r++ = '2'; *r++ = '0'; *r++ = ':'; 117 *r++ = '2';
115 memmove( r, hash, sizeof( ot_hash ) ); r += sizeof( ot_hash ); 118 *r++ = '0';
116 r += sprintf( r, "%zd:", byte_count ); 119 *r++ = ':';
117 memmove( r, peer_list->changeset.data, byte_count ); r += byte_count; 120 memmove(r, hash, sizeof(ot_hash));
121 r += sizeof(ot_hash);
122 r += sprintf(r, "%zd:", byte_count);
123 memmove(r, peer_list->changeset.data, byte_count);
124 r += byte_count;
118 } 125 }
119 126
120 /* All torrents done: release lock on currenct bucket */ 127 /* All torrents done: release lock on currenct bucket */
121 mutex_bucket_unlock( bucket ); 128 mutex_bucket_unlock(bucket);
122 } 129 }
123 130
124 /* Close bencoded sync dictionary */ 131 /* Close bencoded sync dictionary */
125 *r++='e'; *r++='e'; 132 *r++ = 'e';
133 *r++ = 'e';
126 134
127 /* Release unused memory in current output buffer */ 135 /* Release unused memory in current output buffer */
128 iovec_fixlast( iovec_entries, iovector, r ); 136 iovec_fixlast(iovec_entries, iovector, r);
129} 137}
130 138
131/* This is the entry point into this worker thread 139/* This is the entry point into this worker thread
132 It grabs tasks from mutex_tasklist and delivers results back 140 It grabs tasks from mutex_tasklist and delivers results back
133*/ 141*/
134static void * sync_worker( void * args) { 142static void *sync_worker(void *args) {
135 int iovec_entries; 143 int iovec_entries;
136 struct iovec *iovector; 144 struct iovec *iovector;
137 145
138 args = args; 146 args = args;
139 147
140 while( 1 ) { 148 while (1) {
141 ot_tasktype tasktype = TASK_SYNC_OUT; 149 ot_tasktype tasktype = TASK_SYNC_OUT;
142 ot_taskid taskid = mutex_workqueue_poptask( &tasktype ); 150 ot_taskid taskid = mutex_workqueue_poptask(&tasktype);
143 sync_make( &iovec_entries, &iovector ); 151 sync_make(&iovec_entries, &iovector);
144 stats_issue_event( EVENT_SYNC_OUT, FLAG_TCP, iovec_length( &iovec_entries, &iovector) ); 152 stats_issue_event(EVENT_SYNC_OUT, FLAG_TCP, iovec_length(&iovec_entries, &iovector));
145 if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) ) 153 if (mutex_workqueue_pushresult(taskid, iovec_entries, iovector))
146 iovec_free( &iovec_entries, &iovector ); 154 iovec_free(&iovec_entries, &iovector);
147 } 155 }
148 return NULL; 156 return NULL;
149} 157}
diff --git a/ot_sync.h b/ot_sync.h
index 441010d..0fe0739 100644
--- a/ot_sync.h
+++ b/ot_sync.h
@@ -9,11 +9,11 @@
9#ifdef WANT_SYNC_BATCH 9#ifdef WANT_SYNC_BATCH
10enum { SYNC_IN, SYNC_OUT }; 10enum { SYNC_IN, SYNC_OUT };
11 11
12void sync_init( ); 12void sync_init();
13void sync_deinit( ); 13void sync_deinit();
14void sync_deliver( int64 socket ); 14void sync_deliver(int64 socket);
15 15
16int add_changeset_to_tracker( uint8_t *data, size_t len ); 16int add_changeset_to_tracker(uint8_t *data, size_t len);
17#else 17#else
18 18
19#define sync_init() 19#define sync_init()