summaryrefslogtreecommitdiff
path: root/ot_sync.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_sync.c')
-rw-r--r--ot_sync.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/ot_sync.c b/ot_sync.c
new file mode 100644
index 0000000..6e95a98
--- /dev/null
+++ b/ot_sync.c
@@ -0,0 +1,107 @@
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 <sys/types.h>
6#include <sys/mman.h>
7#include <stdio.h>
8#include <string.h>
9
10/* Libowfat */
11#include "scan.h"
12#include "byte.h"
13
14/* Opentracker */
15#include "trackerlogic.h"
16#include "ot_mutex.h"
17#include "ot_sync.h"
18
19#ifdef WANT_TRACKER_SYNC
20/* Import Changeset from an external authority
21 format: d4:syncd[..]ee
22 [..]: ( 20:01234567890abcdefghij16:XXXXYYYY )+
23*/
24int add_changeset_to_tracker( ot_byte *data, size_t len ) {
25 ot_hash *hash;
26 ot_byte *end = data + len;
27 unsigned long peer_count;
28
29 /* We do know, that the string is \n terminated, so it cant
30 overflow */
31 if( byte_diff( data, 8, "d4:syncd" ) ) return -1;
32 data += 8;
33
34 while( 1 ) {
35 if( byte_diff( data, 3, "20:" ) ) {
36 if( byte_diff( data, 2, "ee" ) )
37 return -1;
38 return 0;
39 }
40 data += 3;
41 hash = (ot_hash*)data;
42 data += sizeof( ot_hash );
43
44 /* Scan string length indicator */
45 data += ( len = scan_ulong( (char*)data, &peer_count ) );
46
47 /* If no long was scanned, it is not divisible by 8, it is not
48 followed by a colon or claims to need to much memory, we fail */
49 if( !len || !peer_count || ( peer_count & 7 ) || ( *data++ != ':' ) || ( data + peer_count > end ) )
50 return -1;
51
52 while( peer_count > 0 ) {
53 add_peer_to_torrent( hash, (ot_peer*)data, 1 );
54 data += 8; peer_count -= 8;
55 }
56 }
57 return 0;
58}
59
60/* Proposed output format
61 d4:syncd20:<info_hash>8*N:(xxxxyyyy)*Nee
62*/
63size_t return_changeset_for_tracker( char **reply ) {
64 size_t allocated = 0, i, replysize;
65 ot_vector *torrents_list;
66 int bucket;
67 char *r;
68
69 /* Maybe there is time to clean_all_torrents(); */
70
71 /* Determine space needed for whole changeset */
72 for( bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket ) {
73 torrents_list = mutex_bucket_lock(bucket);
74 for( i=0; i<torrents_list->size; ++i ) {
75 ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + i;
76 allocated += sizeof( ot_hash ) + sizeof(ot_peer) * torrent->peer_list->changeset.size + 13;
77 }
78 mutex_bucket_unlock(bucket);
79 }
80
81 /* add "d4:syncd" and "ee" */
82 allocated += 8 + 2;
83
84 if( !( r = *reply = mmap( NULL, allocated, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ) ) )
85 return 0;
86
87 memmove( r, "d4:syncd", 8 ); r += 8;
88 for( bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket ) {
89 torrents_list = mutex_bucket_lock(bucket);
90 for( i=0; i<torrents_list->size; ++i ) {
91 ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + i;
92 const size_t byte_count = sizeof(ot_peer) * torrent->peer_list->changeset.size;
93 *r++ = '2'; *r++ = '0'; *r++ = ':';
94 memmove( r, torrent->hash, sizeof( ot_hash ) ); r += sizeof( ot_hash );
95 r += sprintf( r, "%zd:", byte_count );
96 memmove( r, torrent->peer_list->changeset.data, byte_count ); r += byte_count;
97 }
98 mutex_bucket_unlock(bucket);
99 }
100 *r++ = 'e'; *r++ = 'e';
101
102 replysize = ( r - *reply );
103 fix_mmapallocation( *reply, allocated, replysize );
104
105 return replysize;
106}
107#endif