diff options
| -rw-r--r-- | opentracker.c | 60 |
1 files changed, 41 insertions, 19 deletions
diff --git a/opentracker.c b/opentracker.c index b7431d4..6c132be 100644 --- a/opentracker.c +++ b/opentracker.c | |||
| @@ -235,7 +235,12 @@ static void server_mainloop( ) { | |||
| 235 | 235 | ||
| 236 | int64_t ot_try_bind( char ip[4], uint16_t port, PROTO_FLAG proto ) { | 236 | int64_t ot_try_bind( char ip[4], uint16_t port, PROTO_FLAG proto ) { |
| 237 | int64 s = proto == FLAG_TCP ? socket_tcp4( ) : socket_udp4(); | 237 | int64 s = proto == FLAG_TCP ? socket_tcp4( ) : socket_udp4(); |
| 238 | 238 | ||
| 239 | #ifdef _DEBUG | ||
| 240 | char *protos[] = {"TCP","UDP","UDP mcast"}; | ||
| 241 | fprintf( stderr, "Binding socket type %s to address %d.%d.%d.%d:%d...", protos[proto],(int)ip[0],(int)ip[1],(int)ip[2],(int)ip[3],port); | ||
| 242 | #endif | ||
| 243 | |||
| 239 | if( socket_bind4_reuse( s, ip, port ) == -1 ) | 244 | if( socket_bind4_reuse( s, ip, port ) == -1 ) |
| 240 | panic( "socket_bind4_reuse" ); | 245 | panic( "socket_bind4_reuse" ); |
| 241 | 246 | ||
| @@ -248,28 +253,36 @@ int64_t ot_try_bind( char ip[4], uint16_t port, PROTO_FLAG proto ) { | |||
| 248 | io_setcookie( s, (void*)proto ); | 253 | io_setcookie( s, (void*)proto ); |
| 249 | 254 | ||
| 250 | io_wantread( s ); | 255 | io_wantread( s ); |
| 256 | |||
| 257 | #ifdef _DEBUG | ||
| 258 | fputs( " success.\n", stderr); | ||
| 259 | #endif | ||
| 251 | 260 | ||
| 252 | return s; | 261 | return s; |
| 253 | } | 262 | } |
| 254 | 263 | ||
| 255 | char * set_config_option( char **option, char *value ) { | 264 | char * set_config_option( char **option, char *value ) { |
| 265 | #ifdef _DEBUG | ||
| 266 | fprintf( stderr, "Setting config option: %s\n", value ); | ||
| 267 | #endif | ||
| 256 | while( isspace(*value) ) ++value; | 268 | while( isspace(*value) ) ++value; |
| 257 | if( *option ) free( *option ); | 269 | if( *option ) free( *option ); |
| 258 | return *option = strdup( value ); | 270 | return *option = strdup( value ); |
| 259 | } | 271 | } |
| 260 | 272 | ||
| 261 | /* WARNING! Does not behave like scan_ip4 regarding return values */ | ||
| 262 | static int scan_ip4_port( const char *src, char *ip, uint16 *port ) { | 273 | static int scan_ip4_port( const char *src, char *ip, uint16 *port ) { |
| 274 | const char *s = src; | ||
| 263 | int off; | 275 | int off; |
| 264 | while( isspace(*src) ) ++src; | 276 | while( isspace(*s) ) ++s; |
| 265 | if( !(off = scan_ip4( src, ip ) ) ) | 277 | if( !(off = scan_ip4( s, ip ) ) ) |
| 266 | return -1; | 278 | return 0; |
| 267 | src += off; | 279 | s += off; |
| 268 | if( *src == 0 ) return 0; | 280 | if( *s == 0 || isspace(*s)) return s-src; |
| 269 | if( *src != ':' ) | 281 | if( *(s++) != ':' ) |
| 270 | return -1; | 282 | return 0; |
| 271 | *port = atol(src+1); | 283 | if( !(off = scan_ushort (s, port ) ) ) |
| 272 | return 0; | 284 | return 0; |
| 285 | return off+s-src; | ||
| 273 | } | 286 | } |
| 274 | 287 | ||
| 275 | int parse_configfile( char * config_filename ) { | 288 | int parse_configfile( char * config_filename ) { |
| @@ -302,12 +315,12 @@ int parse_configfile( char * config_filename ) { | |||
| 302 | set_config_option( &g_serverdir, p+16 ); | 315 | set_config_option( &g_serverdir, p+16 ); |
| 303 | } else if(!byte_diff(p,10,"listen.tcp" ) && isspace(p[10])) { | 316 | } else if(!byte_diff(p,10,"listen.tcp" ) && isspace(p[10])) { |
| 304 | uint16_t tmpport = 6969; | 317 | uint16_t tmpport = 6969; |
| 305 | if( scan_ip4_port( p+11, tmpip, &tmpport )) goto parse_error; | 318 | if( !scan_ip4_port( p+11, tmpip, &tmpport )) goto parse_error; |
| 306 | ot_try_bind( tmpip, tmpport, FLAG_TCP ); | 319 | ot_try_bind( tmpip, tmpport, FLAG_TCP ); |
| 307 | ++bound; | 320 | ++bound; |
| 308 | } else if(!byte_diff(p, 10, "listen.udp" ) && isspace(p[10])) { | 321 | } else if(!byte_diff(p, 10, "listen.udp" ) && isspace(p[10])) { |
| 309 | uint16_t tmpport = 6969; | 322 | uint16_t tmpport = 6969; |
| 310 | if( scan_ip4_port( p+11, tmpip, &tmpport )) goto parse_error; | 323 | if( !scan_ip4_port( p+11, tmpip, &tmpport )) goto parse_error; |
| 311 | ot_try_bind( tmpip, tmpport, FLAG_UDP ); | 324 | ot_try_bind( tmpip, tmpport, FLAG_UDP ); |
| 312 | ++bound; | 325 | ++bound; |
| 313 | #ifdef WANT_ACCESSLIST_BLACK | 326 | #ifdef WANT_ACCESSLIST_BLACK |
| @@ -330,7 +343,7 @@ int parse_configfile( char * config_filename ) { | |||
| 330 | accesslist_blessip( tmpip, OT_PERMISSION_MAY_LIVESYNC ); | 343 | accesslist_blessip( tmpip, OT_PERMISSION_MAY_LIVESYNC ); |
| 331 | } else if(!byte_diff(p, 23, "livesync.cluster.listen" ) && isspace(p[23])) { | 344 | } else if(!byte_diff(p, 23, "livesync.cluster.listen" ) && isspace(p[23])) { |
| 332 | uint16_t tmpport = LIVESYNC_PORT; | 345 | uint16_t tmpport = LIVESYNC_PORT; |
| 333 | if( scan_ip4_port( p+24, tmpip, &tmpport )) goto parse_error; | 346 | if( !scan_ip4_port( p+24, tmpip, &tmpport )) goto parse_error; |
| 334 | livesync_bind_mcast( tmpip, tmpport ); | 347 | livesync_bind_mcast( tmpip, tmpport ); |
| 335 | #endif | 348 | #endif |
| 336 | } else | 349 | } else |
| @@ -347,6 +360,7 @@ int main( int argc, char **argv ) { | |||
| 347 | struct passwd *pws = NULL; | 360 | struct passwd *pws = NULL; |
| 348 | char serverip[4] = {0,0,0,0}, tmpip[4]; | 361 | char serverip[4] = {0,0,0,0}, tmpip[4]; |
| 349 | int bound = 0, scanon = 1; | 362 | int bound = 0, scanon = 1; |
| 363 | uint16_t tmpport; | ||
| 350 | 364 | ||
| 351 | while( scanon ) { | 365 | while( scanon ) { |
| 352 | switch( getopt( argc, argv, ":i:p:A:P:d:r:s:f:v" | 366 | switch( getopt( argc, argv, ":i:p:A:P:d:r:s:f:v" |
| @@ -357,21 +371,29 @@ while( scanon ) { | |||
| 357 | #endif | 371 | #endif |
| 358 | "h" ) ) { | 372 | "h" ) ) { |
| 359 | case -1 : scanon = 0; break; | 373 | case -1 : scanon = 0; break; |
| 360 | case 'i': scan_ip4( optarg, serverip ); break; | 374 | case 'i': |
| 375 | if( !scan_ip4( optarg, serverip )) { usage( argv[0] ); exit( 1 ); } | ||
| 376 | break; | ||
| 361 | #ifdef WANT_ACCESSLIST_BLACK | 377 | #ifdef WANT_ACCESSLIST_BLACK |
| 362 | case 'b': set_config_option( &g_accesslist_filename, optarg); break; | 378 | case 'b': set_config_option( &g_accesslist_filename, optarg); break; |
| 363 | #elif defined( WANT_ACCESSLIST_WHITE ) | 379 | #elif defined( WANT_ACCESSLIST_WHITE ) |
| 364 | case 'w': set_config_option( &g_accesslist_filename, optarg); break; | 380 | case 'w': set_config_option( &g_accesslist_filename, optarg); break; |
| 365 | #endif | 381 | #endif |
| 366 | case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), FLAG_TCP ); bound++; break; | 382 | case 'p': |
| 367 | case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), FLAG_UDP ); bound++; break; | 383 | if( !scan_ushort( optarg, &tmpport)) { usage( argv[0] ); exit( 1 ); } |
| 384 | ot_try_bind( serverip, tmpport, FLAG_TCP ); bound++; break; | ||
| 385 | case 'P': | ||
| 386 | if( !scan_ushort( optarg, &tmpport)) { usage( argv[0] ); exit( 1 ); } | ||
| 387 | ot_try_bind( serverip, tmpport, FLAG_UDP ); bound++; break; | ||
| 368 | #ifdef WANT_SYNC_LIVE | 388 | #ifdef WANT_SYNC_LIVE |
| 369 | case 's': livesync_bind_mcast( serverip, (uint16)atol( optarg )); break; | 389 | case 's': |
| 390 | if( !scan_ushort( optarg, &tmpport)) { usage( argv[0] ); exit( 1 ); } | ||
| 391 | livesync_bind_mcast( serverip, tmpport); break; | ||
| 370 | #endif | 392 | #endif |
| 371 | case 'd': set_config_option( &g_serverdir, optarg ); break; | 393 | case 'd': set_config_option( &g_serverdir, optarg ); break; |
| 372 | case 'r': set_config_option( &g_redirecturl, optarg ); break; | 394 | case 'r': set_config_option( &g_redirecturl, optarg ); break; |
| 373 | case 'A': | 395 | case 'A': |
| 374 | scan_ip4( optarg, tmpip ); | 396 | if( !scan_ip4( optarg, tmpip )) { usage( argv[0] ); exit( 1 ); } |
| 375 | accesslist_blessip( tmpip, 0xffff ); /* Allow everything for now */ | 397 | accesslist_blessip( tmpip, 0xffff ); /* Allow everything for now */ |
| 376 | break; | 398 | break; |
| 377 | case 'f': bound += parse_configfile( optarg ); break; | 399 | case 'f': bound += parse_configfile( optarg ); break; |
