diff options
Diffstat (limited to 'opentracker.c')
-rw-r--r-- | opentracker.c | 223 |
1 files changed, 107 insertions, 116 deletions
diff --git a/opentracker.c b/opentracker.c index 0e499fe..58704bb 100644 --- a/opentracker.c +++ b/opentracker.c | |||
@@ -342,130 +342,121 @@ void help( char *name ) { | |||
342 | } | 342 | } |
343 | 343 | ||
344 | int main( int argc, char **argv ) { | 344 | int main( int argc, char **argv ) { |
345 | int s=socket_tcp4(); | 345 | int s=socket_tcp4(); |
346 | tai6464 t; | 346 | tai6464 t; |
347 | unsigned long ip; | 347 | unsigned long ip; |
348 | char *serverip = NULL; | 348 | char *serverip = NULL; |
349 | char *serverdir = "."; | 349 | char *serverdir = "."; |
350 | uint16 port = 6969; | 350 | uint16 port = 6969; |
351 | 351 | ||
352 | while( 1 ) { | 352 | while( 1 ) { |
353 | switch( getopt(argc,argv,":i:p:d:ocbBh") ) { | 353 | switch( getopt(argc,argv,":i:p:d:ocbBh") ) { |
354 | case -1: goto allparsed; | 354 | case -1: goto allparsed; |
355 | case 'i': serverip = optarg; break; | 355 | case 'i': serverip = optarg; break; |
356 | case 'p': port = (uint16)atol( optarg ); break; | 356 | case 'p': port = (uint16)atol( optarg ); break; |
357 | case 'd': serverdir = optarg; break; | 357 | case 'd': serverdir = optarg; break; |
358 | case 'h': help( argv[0]); exit(0); | 358 | case 'h': help( argv[0]); exit(0); |
359 | #ifdef WANT_CLOSED_TRACKER | 359 | #ifdef WANT_CLOSED_TRACKER |
360 | case 'o': g_closedtracker = 0; break; | 360 | case 'o': g_closedtracker = 0; break; |
361 | case 'c': g_closedtracker = 1; break; | 361 | case 'c': g_closedtracker = 1; break; |
362 | #endif | 362 | #endif |
363 | #ifdef WANT_BLACKLIST | 363 | #ifdef WANT_BLACKLIST |
364 | case 'b': g_check_blacklist = 1; break; | 364 | case 'b': g_check_blacklist = 1; break; |
365 | case 'B': g_check_blacklist = 0; break; | 365 | case 'B': g_check_blacklist = 0; break; |
366 | #endif | 366 | #endif |
367 | default: | 367 | default: |
368 | case '?': usage( argv[0] ); exit(1); | 368 | case '?': usage( argv[0] ); exit(1); |
369 | } | ||
370 | } | 369 | } |
370 | } | ||
371 | 371 | ||
372 | allparsed: | 372 | allparsed: |
373 | ot_start_time = time( NULL ); | 373 | ot_start_time = time( NULL ); |
374 | if (socket_bind4_reuse(s,serverip,port)==-1) | 374 | if (socket_bind4_reuse(s,serverip,port)==-1) |
375 | panic("socket_bind4_reuse"); | 375 | panic("socket_bind4_reuse"); |
376 | 376 | ||
377 | if (socket_listen(s,16)==-1) | 377 | if (socket_listen(s,16)==-1) |
378 | panic("socket_listen"); | 378 | panic("socket_listen"); |
379 | 379 | ||
380 | if (!io_fd(s)) | 380 | if (!io_fd(s)) |
381 | panic("io_fd"); | 381 | panic("io_fd"); |
382 | 382 | ||
383 | signal( SIGINT, graceful ); | 383 | signal( SIGINT, graceful ); |
384 | if( init_logic( serverdir ) == -1 ) | 384 | if( init_logic( serverdir ) == -1 ) |
385 | panic("Logic not started"); | 385 | panic("Logic not started"); |
386 | 386 | ||
387 | io_wantread(s); | 387 | io_wantread(s); |
388 | 388 | ||
389 | for (;;) | 389 | for (;;) { |
390 | { | 390 | int64 i; |
391 | int64 i; | 391 | io_wait(); |
392 | io_wait(); | 392 | |
393 | 393 | while ((i=io_canread())!=-1) { | |
394 | while ((i=io_canread())!=-1) | 394 | if (i==s) { // ist es der serversocket? |
395 | { | 395 | int n; |
396 | if (i==s) // ist es der serversocket? | 396 | while ((n=socket_accept4(s,(void*)&ip,&port))!=-1) { |
397 | { | 397 | if (io_fd(n)) { |
398 | int n; | 398 | struct http_data* h=(struct http_data*)malloc(sizeof(struct http_data)); |
399 | while ((n=socket_accept4(s,(void*)&ip,&port))!=-1) | 399 | io_wantread(n); |
400 | { | 400 | |
401 | if (io_fd(n)) | 401 | if (h) { |
402 | { | 402 | byte_zero(h,sizeof(struct http_data)); |
403 | struct http_data* h=(struct http_data*)malloc(sizeof(struct http_data)); | 403 | h->ip=ip; |
404 | io_wantread(n); | 404 | taia_now(&t); |
405 | 405 | taia_addsec(&t,&t,OT_CLIENT_TIMEOUT); | |
406 | if (h) | 406 | io_timeout(n,t); |
407 | { | 407 | io_setcookie(n,h); |
408 | byte_zero(h,sizeof(struct http_data)); | 408 | ++ot_overall_connections; |
409 | h->ip=ip; | 409 | } else |
410 | taia_now(&t); | 410 | io_close(n); |
411 | taia_addsec(&t,&t,OT_CLIENT_TIMEOUT); | 411 | } else |
412 | io_timeout(n,t); | 412 | io_close(n); |
413 | io_setcookie(n,h); | ||
414 | ++ot_overall_connections; | ||
415 | } else | ||
416 | io_close(n); | ||
417 | } else | ||
418 | io_close(n); | ||
419 | } | ||
420 | if (errno==EAGAIN) | ||
421 | io_eagain(s); | ||
422 | else | ||
423 | carp("socket_accept4"); | ||
424 | } | ||
425 | else | ||
426 | { | ||
427 | char buf[8192]; | ||
428 | struct http_data* h=io_getcookie(i); | ||
429 | |||
430 | int l=io_tryread(i,buf,sizeof buf); | ||
431 | if (l<=0) | ||
432 | { | ||
433 | if (h) | ||
434 | { | ||
435 | array_reset(&h->r); | ||
436 | free(h); | ||
437 | } | ||
438 | io_close(i); | ||
439 | } | ||
440 | else | ||
441 | { | ||
442 | array_catb(&h->r,buf,l); | ||
443 | |||
444 | if (array_failed(&h->r)) | ||
445 | httperror(i,h,"500 Server Error","Request too long."); | ||
446 | else if (array_bytes(&h->r)>8192) | ||
447 | httperror(i,h,"500 request too long","You sent too much headers"); | ||
448 | else if ((l=header_complete(h))) | ||
449 | httpresponse(i,h); | ||
450 | } | ||
451 | } | ||
452 | } | 413 | } |
453 | 414 | if( errno==EAGAIN ) | |
454 | while ((i=io_canwrite())!=-1) | 415 | io_eagain(s); |
455 | { | 416 | else |
456 | struct http_data* h=io_getcookie(i); | 417 | carp("socket_accept4"); |
457 | 418 | } else { | |
458 | int64 r=iob_send(i,&h->iob); | 419 | char buf[8192]; |
459 | if (r==-1) | 420 | struct http_data* h=io_getcookie(i); |
460 | io_eagain(i); | 421 | |
461 | else | 422 | int l=io_tryread(i,buf,sizeof buf); |
462 | if ((r<=0)||(h->iob.bytesleft==0)) | 423 | if( l <= 0 ) { |
463 | { | 424 | if( h ) { |
464 | iob_reset(&h->iob); | 425 | array_reset(&h->r); |
465 | free(h); | 426 | free(h); |
466 | io_close(i); | 427 | } |
467 | } | 428 | io_close(i); |
429 | } else { | ||
430 | array_catb(&h->r,buf,l); | ||
431 | |||
432 | if( array_failed(&h->r)) | ||
433 | httperror(i,h,"500 Server Error","Request too long."); | ||
434 | else if (array_bytes(&h->r)>8192) | ||
435 | httperror(i,h,"500 request too long","You sent too much headers"); | ||
436 | else if ((l=header_complete(h))) | ||
437 | httpresponse(i,h); | ||
438 | else { | ||
439 | taia_now(&t); | ||
440 | taia_addsec(&t,&t,OT_CLIENT_TIMEOUT); | ||
441 | io_timeout(i,t); | ||
442 | } | ||
468 | } | 443 | } |
444 | } | ||
469 | } | 445 | } |
470 | return 0; | 446 | |
447 | while ((i=io_canwrite())!=-1) { | ||
448 | struct http_data* h=io_getcookie(i); | ||
449 | |||
450 | int64 r=iob_send(i,&h->iob); | ||
451 | if (r==-1) | ||
452 | io_eagain(i); | ||
453 | else | ||
454 | if ((r<=0)||(h->iob.bytesleft==0)) { | ||
455 | iob_reset(&h->iob); | ||
456 | free(h); | ||
457 | io_close(i); | ||
458 | } | ||
459 | } | ||
460 | } | ||
461 | return 0; | ||
471 | } | 462 | } |