diff options
| author | erdgeist <> | 2009-01-13 22:41:17 +0000 |
|---|---|---|
| committer | erdgeist <> | 2009-01-13 22:41:17 +0000 |
| commit | 131211b4daf83b7c594337f4e7c71e4711094d71 (patch) | |
| tree | 63dc942c426103c01d61aa15959a81df91f2d4e1 /ot_livesync.c | |
| parent | 779d6c235ff8fe5284fd10dc82a9b99e7fa38d06 (diff) | |
V6
Diffstat (limited to 'ot_livesync.c')
| -rw-r--r-- | ot_livesync.c | 60 |
1 files changed, 32 insertions, 28 deletions
diff --git a/ot_livesync.c b/ot_livesync.c index a47edba..d577f7c 100644 --- a/ot_livesync.c +++ b/ot_livesync.c | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "socket.h" | 15 | #include "socket.h" |
| 16 | #include "ndelay.h" | 16 | #include "ndelay.h" |
| 17 | #include "byte.h" | 17 | #include "byte.h" |
| 18 | #include "ip6.h" | ||
| 18 | 19 | ||
| 19 | /* Opentracker */ | 20 | /* Opentracker */ |
| 20 | #include "trackerlogic.h" | 21 | #include "trackerlogic.h" |
| @@ -88,14 +89,14 @@ void livesync_init( ) { | |||
| 88 | 89 | ||
| 89 | /* Prepare outgoing peers buffer */ | 90 | /* Prepare outgoing peers buffer */ |
| 90 | g_peerbuffer_pos = g_peerbuffer_start; | 91 | g_peerbuffer_pos = g_peerbuffer_start; |
| 91 | memmove( g_peerbuffer_pos, &g_tracker_id, sizeof( g_tracker_id ) ); | 92 | memcpy( g_peerbuffer_pos, &g_tracker_id, sizeof( g_tracker_id ) ); |
| 92 | uint32_pack_big( (char*)g_peerbuffer_pos + sizeof( g_tracker_id ), OT_SYNC_PEER); | 93 | uint32_pack_big( (char*)g_peerbuffer_pos + sizeof( g_tracker_id ), OT_SYNC_PEER); |
| 93 | g_peerbuffer_pos += sizeof( g_tracker_id ) + sizeof( uint32_t); | 94 | g_peerbuffer_pos += sizeof( g_tracker_id ) + sizeof( uint32_t); |
| 94 | 95 | ||
| 95 | #ifdef WANT_SYNC_SCRAPE | 96 | #ifdef WANT_SYNC_SCRAPE |
| 96 | /* Prepare outgoing scrape buffer */ | 97 | /* Prepare outgoing scrape buffer */ |
| 97 | g_scrapebuffer_pos = g_scrapebuffer_start; | 98 | g_scrapebuffer_pos = g_scrapebuffer_start; |
| 98 | memmove( g_scrapebuffer_pos, &g_tracker_id, sizeof( g_tracker_id ) ); | 99 | memcpy( g_scrapebuffer_pos, &g_tracker_id, sizeof( g_tracker_id ) ); |
| 99 | uint32_pack_big( (char*)g_scrapebuffer_pos + sizeof( g_tracker_id ), OT_SYNC_SCRAPE_TELL); | 100 | uint32_pack_big( (char*)g_scrapebuffer_pos + sizeof( g_tracker_id ), OT_SYNC_SCRAPE_TELL); |
| 100 | g_scrapebuffer_pos += sizeof( g_tracker_id ) + sizeof( uint32_t); | 101 | g_scrapebuffer_pos += sizeof( g_tracker_id ) + sizeof( uint32_t); |
| 101 | 102 | ||
| @@ -116,8 +117,13 @@ void livesync_deinit() { | |||
| 116 | pthread_cancel( thread_id ); | 117 | pthread_cancel( thread_id ); |
| 117 | } | 118 | } |
| 118 | 119 | ||
| 119 | void livesync_bind_mcast( char *ip, uint16_t port) { | 120 | void livesync_bind_mcast( ot_ip6 ip, uint16_t port) { |
| 120 | char tmpip[4] = {0,0,0,0}; | 121 | char tmpip[4] = {0,0,0,0}; |
| 122 | char *v4ip; | ||
| 123 | |||
| 124 | if( !ip6_isv4mapped(ip)) | ||
| 125 | exerr("v6 mcast support not yet available."); | ||
| 126 | v4ip = ip+12; | ||
| 121 | 127 | ||
| 122 | if( g_socket_in != -1 ) | 128 | if( g_socket_in != -1 ) |
| 123 | exerr("Error: Livesync listen ip specified twice."); | 129 | exerr("Error: Livesync listen ip specified twice."); |
| @@ -129,12 +135,12 @@ void livesync_bind_mcast( char *ip, uint16_t port) { | |||
| 129 | if( socket_bind4_reuse( g_socket_in, tmpip, port ) == -1 ) | 135 | if( socket_bind4_reuse( g_socket_in, tmpip, port ) == -1 ) |
| 130 | exerr("Error: Cant bind live sync incoming socket." ); | 136 | exerr("Error: Cant bind live sync incoming socket." ); |
| 131 | 137 | ||
| 132 | if( socket_mcjoin4( g_socket_in, groupip_1, ip ) ) | 138 | if( socket_mcjoin4( g_socket_in, groupip_1, v4ip ) ) |
| 133 | exerr("Error: Cant make live sync incoming socket join mcast group."); | 139 | exerr("Error: Cant make live sync incoming socket join mcast group."); |
| 134 | 140 | ||
| 135 | if( ( g_socket_out = socket_udp4()) < 0) | 141 | if( ( g_socket_out = socket_udp4()) < 0) |
| 136 | exerr("Error: Cant create live sync outgoing socket." ); | 142 | exerr("Error: Cant create live sync outgoing socket." ); |
| 137 | if( socket_bind4_reuse( g_socket_out, ip, port ) == -1 ) | 143 | if( socket_bind4_reuse( g_socket_out, v4ip, port ) == -1 ) |
| 138 | exerr("Error: Cant bind live sync outgoing socket." ); | 144 | exerr("Error: Cant bind live sync outgoing socket." ); |
| 139 | 145 | ||
| 140 | socket_mcttl4(g_socket_out, 1); | 146 | socket_mcttl4(g_socket_out, 1); |
| @@ -160,9 +166,9 @@ static void livesync_handle_peersync( ssize_t datalen ) { | |||
| 160 | if( !g_opentracker_running ) return; | 166 | if( !g_opentracker_running ) return; |
| 161 | 167 | ||
| 162 | if( OT_PEERFLAG(peer) & PEER_FLAG_STOPPED ) | 168 | if( OT_PEERFLAG(peer) & PEER_FLAG_STOPPED ) |
| 163 | remove_peer_from_torrent(hash, peer, NULL, FLAG_MCA ); | 169 | remove_peer_from_torrent( *hash, peer, NULL, FLAG_MCA ); |
| 164 | else | 170 | else |
| 165 | add_peer_to_torrent( hash, peer, FLAG_MCA ); | 171 | add_peer_to_torrent( *hash, peer, FLAG_MCA ); |
| 166 | 172 | ||
| 167 | off += sizeof( ot_hash ) + sizeof( ot_peer ); | 173 | off += sizeof( ot_hash ) + sizeof( ot_peer ); |
| 168 | } | 174 | } |
| @@ -175,7 +181,7 @@ void livesync_issue_beacon( ) { | |||
| 175 | size_t torrent_count = mutex_get_torrent_count(); | 181 | size_t torrent_count = mutex_get_torrent_count(); |
| 176 | uint8_t beacon[ sizeof(g_tracker_id) + sizeof(uint32_t) + sizeof( uint64_t ) ]; | 182 | uint8_t beacon[ sizeof(g_tracker_id) + sizeof(uint32_t) + sizeof( uint64_t ) ]; |
| 177 | 183 | ||
| 178 | memmove( beacon, &g_tracker_id, sizeof( g_tracker_id ) ); | 184 | memcpy( beacon, &g_tracker_id, sizeof( g_tracker_id ) ); |
| 179 | uint32_pack_big( (char*)beacon + sizeof( g_tracker_id ), OT_SYNC_SCRAPE_BEACON); | 185 | uint32_pack_big( (char*)beacon + sizeof( g_tracker_id ), OT_SYNC_SCRAPE_BEACON); |
| 180 | uint32_pack_big( (char*)beacon + sizeof( g_tracker_id ) + sizeof(uint32_t), (uint32_t)((uint64_t)(torrent_count)>>32) ); | 186 | uint32_pack_big( (char*)beacon + sizeof( g_tracker_id ) + sizeof(uint32_t), (uint32_t)((uint64_t)(torrent_count)>>32) ); |
| 181 | uint32_pack_big( (char*)beacon + sizeof( g_tracker_id ) + 2 * sizeof(uint32_t), (uint32_t)torrent_count ); | 187 | uint32_pack_big( (char*)beacon + sizeof( g_tracker_id ) + 2 * sizeof(uint32_t), (uint32_t)torrent_count ); |
| @@ -202,7 +208,7 @@ void livesync_handle_beacon( ssize_t datalen ) { | |||
| 202 | 208 | ||
| 203 | if( torrent_count_remote > g_inquire_remote_count ) { | 209 | if( torrent_count_remote > g_inquire_remote_count ) { |
| 204 | g_inquire_remote_count = torrent_count_remote; | 210 | g_inquire_remote_count = torrent_count_remote; |
| 205 | memmove( &g_inquire_remote_host, g_inbuffer, sizeof( g_tracker_id ) ); | 211 | memcpy( &g_inquire_remote_host, g_inbuffer, sizeof( g_tracker_id ) ); |
| 206 | } | 212 | } |
| 207 | } | 213 | } |
| 208 | } | 214 | } |
| @@ -210,9 +216,9 @@ void livesync_handle_beacon( ssize_t datalen ) { | |||
| 210 | void livesync_issue_inquire( ) { | 216 | void livesync_issue_inquire( ) { |
| 211 | uint8_t inquire[ sizeof(g_tracker_id) + sizeof(uint32_t) + sizeof(g_tracker_id)]; | 217 | uint8_t inquire[ sizeof(g_tracker_id) + sizeof(uint32_t) + sizeof(g_tracker_id)]; |
| 212 | 218 | ||
| 213 | memmove( inquire, &g_tracker_id, sizeof( g_tracker_id ) ); | 219 | memcpy( inquire, &g_tracker_id, sizeof( g_tracker_id ) ); |
| 214 | uint32_pack_big( (char*)inquire + sizeof( g_tracker_id ), OT_SYNC_SCRAPE_INQUIRE); | 220 | uint32_pack_big( (char*)inquire + sizeof( g_tracker_id ), OT_SYNC_SCRAPE_INQUIRE); |
| 215 | memmove( inquire + sizeof(g_tracker_id) + sizeof(uint32_t), &g_inquire_remote_host, sizeof( g_tracker_id ) ); | 221 | memcpy( inquire + sizeof(g_tracker_id) + sizeof(uint32_t), &g_inquire_remote_host, sizeof( g_tracker_id ) ); |
| 216 | 222 | ||
| 217 | socket_send4(g_socket_out, (char*)inquire, sizeof(inquire), groupip_1, LIVESYNC_PORT); | 223 | socket_send4(g_socket_out, (char*)inquire, sizeof(inquire), groupip_1, LIVESYNC_PORT); |
| 218 | } | 224 | } |
| @@ -239,7 +245,7 @@ void livesync_issue_tell( ) { | |||
| 239 | unsigned int j; | 245 | unsigned int j; |
| 240 | for( j=0; j<torrents_list->size; ++j ) { | 246 | for( j=0; j<torrents_list->size; ++j ) { |
| 241 | ot_torrent *torrent = (ot_torrent*)(torrents_list->data) + j; | 247 | ot_torrent *torrent = (ot_torrent*)(torrents_list->data) + j; |
| 242 | memmove(g_scrapebuffer_pos, torrent->hash, sizeof(ot_hash)); | 248 | memcpy(g_scrapebuffer_pos, torrent->hash, sizeof(ot_hash)); |
| 243 | g_scrapebuffer_pos += sizeof(ot_hash); | 249 | g_scrapebuffer_pos += sizeof(ot_hash); |
| 244 | uint32_pack_big( (char*)g_scrapebuffer_pos , (uint32_t)(g_now_minutes - torrent->peer_list->base )); | 250 | uint32_pack_big( (char*)g_scrapebuffer_pos , (uint32_t)(g_now_minutes - torrent->peer_list->base )); |
| 245 | uint32_pack_big( (char*)g_scrapebuffer_pos + 4, (uint32_t)((uint64_t)(torrent->peer_list->down_count)>>32) ); | 251 | uint32_pack_big( (char*)g_scrapebuffer_pos + 4, (uint32_t)((uint64_t)(torrent->peer_list->down_count)>>32) ); |
| @@ -268,29 +274,29 @@ void livesync_handle_tell( ssize_t datalen ) { | |||
| 268 | /* Some instance is in progress of telling. Our inquiry was successful. | 274 | /* Some instance is in progress of telling. Our inquiry was successful. |
| 269 | Don't ask again until we see next beacon. */ | 275 | Don't ask again until we see next beacon. */ |
| 270 | g_next_inquire_time = 0; | 276 | g_next_inquire_time = 0; |
| 271 | 277 | ||
| 272 | /* Don't cause any new inquiries during another tracker's tell */ | 278 | /* Don't cause any new inquiries during another tracker's tell */ |
| 273 | if( g_next_beacon_time - g_now_seconds < LIVESYNC_BEACON_INTERVAL ) | 279 | if( g_next_beacon_time - g_now_seconds < LIVESYNC_BEACON_INTERVAL ) |
| 274 | g_next_beacon_time = g_now_seconds + LIVESYNC_BEACON_INTERVAL; | 280 | g_next_beacon_time = g_now_seconds + LIVESYNC_BEACON_INTERVAL; |
| 275 | 281 | ||
| 276 | while( off + sizeof(ot_hash) + 12 <= (size_t)datalen ) { | 282 | while( off + sizeof(ot_hash) + 12 <= (size_t)datalen ) { |
| 277 | ot_hash *hash = (ot_hash*)(g_inbuffer+off); | 283 | ot_hash *hash = (ot_hash*)(g_inbuffer+off); |
| 278 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); | 284 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(*hash); |
| 279 | size_t down_count_remote; | 285 | size_t down_count_remote; |
| 280 | int exactmatch; | 286 | int exactmatch; |
| 281 | ot_torrent * torrent = vector_find_or_insert(torrents_list, hash, sizeof(ot_hash), OT_HASH_COMPARE_SIZE, &exactmatch); | 287 | ot_torrent * torrent = vector_find_or_insert(torrents_list, hash, sizeof(ot_hash), OT_HASH_COMPARE_SIZE, &exactmatch); |
| 282 | if( !torrent ) { | 288 | if( !torrent ) { |
| 283 | mutex_bucket_unlock_by_hash( hash, 0 ); | 289 | mutex_bucket_unlock_by_hash( *hash, 0 ); |
| 284 | continue; | 290 | continue; |
| 285 | } | 291 | } |
| 286 | 292 | ||
| 287 | if( !exactmatch ) { | 293 | if( !exactmatch ) { |
| 288 | /* Create a new torrent entry, then */ | 294 | /* Create a new torrent entry, then */ |
| 289 | int i; for(i=0;i<20;i+=4) WRITE32(&torrent->hash,i,READ32(hash,i)); | 295 | memcpy( &torrent->hash, hash, sizeof(ot_hash)); |
| 290 | 296 | ||
| 291 | if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { | 297 | if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { |
| 292 | vector_remove_torrent( torrents_list, torrent ); | 298 | vector_remove_torrent( torrents_list, torrent ); |
| 293 | mutex_bucket_unlock_by_hash( hash, 0 ); | 299 | mutex_bucket_unlock_by_hash( *hash, 0 ); |
| 294 | continue; | 300 | continue; |
| 295 | } | 301 | } |
| 296 | 302 | ||
| @@ -298,8 +304,8 @@ void livesync_handle_tell( ssize_t datalen ) { | |||
| 298 | torrent->peer_list->base = g_now_minutes - uint32_read_big((char*)g_inbuffer+off+sizeof(ot_hash)); | 304 | torrent->peer_list->base = g_now_minutes - uint32_read_big((char*)g_inbuffer+off+sizeof(ot_hash)); |
| 299 | } | 305 | } |
| 300 | 306 | ||
| 301 | down_count_remote = (size_t)(((uint64_t)uint32_read_big((char*)g_inbuffer+off+sizeof( ot_hash ) + sizeof(uint32_t))) << 32); | 307 | down_count_remote = (size_t)(((uint64_t)uint32_read_big((char*)g_inbuffer+off+sizeof(ot_hash ) + sizeof(uint32_t))) << 32); |
| 302 | down_count_remote |= (size_t) uint32_read_big((char*)g_inbuffer+off+sizeof( ot_hash ) + 2 * sizeof(uint32_t)); | 308 | down_count_remote |= (size_t) uint32_read_big((char*)g_inbuffer+off+sizeof(ot_hash ) + 2 * sizeof(uint32_t)); |
| 303 | 309 | ||
| 304 | if( down_count_remote > torrent->peer_list->down_count ) | 310 | if( down_count_remote > torrent->peer_list->down_count ) |
| 305 | torrent->peer_list->down_count = down_count_remote; | 311 | torrent->peer_list->down_count = down_count_remote; |
| @@ -319,7 +325,7 @@ void livesync_handle_tell( ssize_t datalen ) { | |||
| 319 | stuck when there's not enough traffic to fill udp packets fast | 325 | stuck when there's not enough traffic to fill udp packets fast |
| 320 | enough */ | 326 | enough */ |
| 321 | void livesync_ticker( ) { | 327 | void livesync_ticker( ) { |
| 322 | 328 | ||
| 323 | /* livesync_issue_peersync sets g_next_packet_time */ | 329 | /* livesync_issue_peersync sets g_next_packet_time */ |
| 324 | if( g_now_seconds > g_next_packet_time && | 330 | if( g_now_seconds > g_next_packet_time && |
| 325 | g_peerbuffer_pos > g_peerbuffer_start + sizeof( g_tracker_id ) ) | 331 | g_peerbuffer_pos > g_peerbuffer_start + sizeof( g_tracker_id ) ) |
| @@ -350,21 +356,19 @@ void livesync_ticker( ) { | |||
| 350 | } | 356 | } |
| 351 | 357 | ||
| 352 | /* Inform live sync about whats going on. */ | 358 | /* Inform live sync about whats going on. */ |
| 353 | void livesync_tell( ot_hash * const info_hash, const ot_peer * const peer ) { | 359 | void livesync_tell( ot_hash const info_hash, const ot_peer * const peer ) { |
| 354 | unsigned int i; | ||
| 355 | for(i=0;i<sizeof(ot_hash)/4;i+=4) WRITE32(g_peerbuffer_pos,i,READ32(info_hash,i)); | ||
| 356 | 360 | ||
| 357 | WRITE32(g_peerbuffer_pos,sizeof(ot_hash) ,READ32(peer,0)); | 361 | memcpy( g_peerbuffer_pos, info_hash, sizeof(ot_hash) ); |
| 358 | WRITE32(g_peerbuffer_pos,sizeof(ot_hash)+4,READ32(peer,4)); | 362 | memcpy( g_peerbuffer_pos+sizeof(ot_hash), peer, sizeof(ot_peer) ); |
| 359 | 363 | ||
| 360 | g_peerbuffer_pos += sizeof(ot_hash)+8; | 364 | g_peerbuffer_pos += sizeof(ot_hash)+sizeof(ot_peer); |
| 361 | 365 | ||
| 362 | if( g_peerbuffer_pos >= g_peerbuffer_highwater ) | 366 | if( g_peerbuffer_pos >= g_peerbuffer_highwater ) |
| 363 | livesync_issue_peersync(); | 367 | livesync_issue_peersync(); |
| 364 | } | 368 | } |
| 365 | 369 | ||
| 366 | static void * livesync_worker( void * args ) { | 370 | static void * livesync_worker( void * args ) { |
| 367 | uint8_t in_ip[4]; uint16_t in_port; | 371 | ot_ip6 in_ip; uint16_t in_port; |
| 368 | ssize_t datalen; | 372 | ssize_t datalen; |
| 369 | 373 | ||
| 370 | (void)args; | 374 | (void)args; |
| @@ -375,7 +379,7 @@ static void * livesync_worker( void * args ) { | |||
| 375 | /* Expect at least tracker id and packet type */ | 379 | /* Expect at least tracker id and packet type */ |
| 376 | if( datalen <= (ssize_t)(sizeof( g_tracker_id ) + sizeof( uint32_t )) ) | 380 | if( datalen <= (ssize_t)(sizeof( g_tracker_id ) + sizeof( uint32_t )) ) |
| 377 | continue; | 381 | continue; |
| 378 | if( !accesslist_isblessed((char*)in_ip, OT_PERMISSION_MAY_LIVESYNC)) | 382 | if( !accesslist_isblessed(in_ip, OT_PERMISSION_MAY_LIVESYNC)) |
| 379 | continue; | 383 | continue; |
| 380 | if( !memcmp( g_inbuffer, &g_tracker_id, sizeof( g_tracker_id ) ) ) { | 384 | if( !memcmp( g_inbuffer, &g_tracker_id, sizeof( g_tracker_id ) ) ) { |
| 381 | /* TODO: log packet coming from ourselves */ | 385 | /* TODO: log packet coming from ourselves */ |
