mirror of
				git://erdgeist.org/opentracker
				synced 2025-10-31 18:03:24 +08:00 
			
		
		
		
	Indent. Sorry. Plus timeout on every connection, for every byte we got.
This commit is contained in:
		
							parent
							
								
									bef60daf2b
								
							
						
					
					
						commit
						1d66041fd5
					
				
							
								
								
									
										223
									
								
								opentracker.c
									
									
									
									
									
								
							
							
						
						
									
										223
									
								
								opentracker.c
									
									
									
									
									
								
							| @ -342,130 +342,121 @@ void help( char *name ) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int main( int argc, char **argv ) { | int main( int argc, char **argv ) { | ||||||
|     int s=socket_tcp4(); |   int s=socket_tcp4(); | ||||||
|     tai6464 t; |   tai6464 t; | ||||||
|     unsigned long ip; |   unsigned long ip; | ||||||
|     char *serverip = NULL; |   char *serverip = NULL; | ||||||
|     char *serverdir = "."; |   char *serverdir = "."; | ||||||
|     uint16 port = 6969; |   uint16 port = 6969; | ||||||
| 
 | 
 | ||||||
|     while( 1 ) { |   while( 1 ) { | ||||||
|       switch( getopt(argc,argv,":i:p:d:ocbBh") ) { |     switch( getopt(argc,argv,":i:p:d:ocbBh") ) { | ||||||
|         case -1: goto allparsed; |       case -1: goto allparsed; | ||||||
|         case 'i': serverip = optarg; break; |       case 'i': serverip = optarg; break; | ||||||
|         case 'p': port = (uint16)atol( optarg ); break; |       case 'p': port = (uint16)atol( optarg ); break; | ||||||
|         case 'd': serverdir = optarg; break; |       case 'd': serverdir = optarg; break; | ||||||
|         case 'h': help( argv[0]); exit(0); |       case 'h': help( argv[0]); exit(0); | ||||||
| #ifdef WANT_CLOSED_TRACKER | #ifdef WANT_CLOSED_TRACKER | ||||||
|         case 'o': g_closedtracker = 0; break; |       case 'o': g_closedtracker = 0; break; | ||||||
|         case 'c': g_closedtracker = 1; break; |       case 'c': g_closedtracker = 1; break; | ||||||
| #endif | #endif | ||||||
| #ifdef WANT_BLACKLIST | #ifdef WANT_BLACKLIST | ||||||
|         case 'b': g_check_blacklist = 1; break; |       case 'b': g_check_blacklist = 1; break; | ||||||
|         case 'B': g_check_blacklist = 0; break; |       case 'B': g_check_blacklist = 0; break; | ||||||
| #endif | #endif | ||||||
|         default: |       default: | ||||||
|         case '?': usage( argv[0] ); exit(1); |       case '?': usage( argv[0] ); exit(1); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | allparsed: | ||||||
|  |   ot_start_time = time( NULL ); | ||||||
|  |   if (socket_bind4_reuse(s,serverip,port)==-1) | ||||||
|  |     panic("socket_bind4_reuse"); | ||||||
|  | 
 | ||||||
|  |   if (socket_listen(s,16)==-1) | ||||||
|  |     panic("socket_listen"); | ||||||
|  | 
 | ||||||
|  |   if (!io_fd(s)) | ||||||
|  |     panic("io_fd"); | ||||||
|  | 
 | ||||||
|  |   signal( SIGINT, graceful ); | ||||||
|  |   if( init_logic( serverdir ) == -1 ) | ||||||
|  |     panic("Logic not started"); | ||||||
|  | 
 | ||||||
|  |   io_wantread(s); | ||||||
|  | 
 | ||||||
|  |   for (;;) { | ||||||
|  |     int64 i; | ||||||
|  |     io_wait(); | ||||||
|  | 
 | ||||||
|  |     while ((i=io_canread())!=-1) { | ||||||
|  |       if (i==s) { // ist es der serversocket?
 | ||||||
|  |         int n; | ||||||
|  |         while ((n=socket_accept4(s,(void*)&ip,&port))!=-1) { | ||||||
|  |           if (io_fd(n)) { | ||||||
|  |             struct http_data* h=(struct http_data*)malloc(sizeof(struct http_data)); | ||||||
|  |             io_wantread(n); | ||||||
|  | 
 | ||||||
|  |             if (h) { | ||||||
|  |               byte_zero(h,sizeof(struct http_data)); | ||||||
|  |               h->ip=ip; | ||||||
|  |               taia_now(&t); | ||||||
|  |               taia_addsec(&t,&t,OT_CLIENT_TIMEOUT); | ||||||
|  |               io_timeout(n,t); | ||||||
|  |               io_setcookie(n,h); | ||||||
|  |               ++ot_overall_connections; | ||||||
|  |             } else | ||||||
|  |               io_close(n); | ||||||
|  |           } else | ||||||
|  |             io_close(n); | ||||||
|  |         } | ||||||
|  |         if( errno==EAGAIN ) | ||||||
|  |           io_eagain(s); | ||||||
|  |         else | ||||||
|  |           carp("socket_accept4"); | ||||||
|  |       } else { | ||||||
|  |         char buf[8192]; | ||||||
|  |         struct http_data* h=io_getcookie(i); | ||||||
|  | 
 | ||||||
|  |         int l=io_tryread(i,buf,sizeof buf); | ||||||
|  |         if( l <= 0 ) { | ||||||
|  |           if( h ) { | ||||||
|  |             array_reset(&h->r); | ||||||
|  |             free(h); | ||||||
|  |           } | ||||||
|  |           io_close(i); | ||||||
|  |         } else { | ||||||
|  |           array_catb(&h->r,buf,l); | ||||||
|  | 
 | ||||||
|  |           if( array_failed(&h->r)) | ||||||
|  |             httperror(i,h,"500 Server Error","Request too long."); | ||||||
|  |           else if (array_bytes(&h->r)>8192) | ||||||
|  |             httperror(i,h,"500 request too long","You sent too much headers"); | ||||||
|  |           else if ((l=header_complete(h))) | ||||||
|  |             httpresponse(i,h); | ||||||
|  |           else { | ||||||
|  |             taia_now(&t); | ||||||
|  |             taia_addsec(&t,&t,OT_CLIENT_TIMEOUT); | ||||||
|  |             io_timeout(i,t); | ||||||
|  |           } | ||||||
|  |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| allparsed: |     while ((i=io_canwrite())!=-1) { | ||||||
|     ot_start_time = time( NULL ); |       struct http_data* h=io_getcookie(i); | ||||||
|     if (socket_bind4_reuse(s,serverip,port)==-1) |  | ||||||
|         panic("socket_bind4_reuse"); |  | ||||||
| 
 | 
 | ||||||
|     if (socket_listen(s,16)==-1) |       int64 r=iob_send(i,&h->iob); | ||||||
|         panic("socket_listen"); |       if (r==-1) | ||||||
| 
 |         io_eagain(i); | ||||||
|     if (!io_fd(s)) |       else | ||||||
|         panic("io_fd"); |         if ((r<=0)||(h->iob.bytesleft==0)) { | ||||||
| 
 |           iob_reset(&h->iob); | ||||||
|     signal( SIGINT, graceful ); |           free(h); | ||||||
|     if( init_logic( serverdir ) == -1 ) |           io_close(i); | ||||||
|       panic("Logic not started"); |  | ||||||
| 
 |  | ||||||
|     io_wantread(s); |  | ||||||
| 
 |  | ||||||
|     for (;;) |  | ||||||
|     { |  | ||||||
|         int64 i; |  | ||||||
|         io_wait(); |  | ||||||
| 
 |  | ||||||
|         while ((i=io_canread())!=-1) |  | ||||||
|         { |  | ||||||
|             if (i==s)    // ist es der serversocket?
 |  | ||||||
|             { |  | ||||||
|                 int n; |  | ||||||
|                 while ((n=socket_accept4(s,(void*)&ip,&port))!=-1) |  | ||||||
|                 { |  | ||||||
|                     if (io_fd(n)) |  | ||||||
|                     { |  | ||||||
|                         struct http_data* h=(struct http_data*)malloc(sizeof(struct http_data)); |  | ||||||
|                         io_wantread(n); |  | ||||||
| 
 |  | ||||||
|                         if (h) |  | ||||||
|                         { |  | ||||||
|                             byte_zero(h,sizeof(struct http_data)); |  | ||||||
|                             h->ip=ip; |  | ||||||
|                             taia_now(&t); |  | ||||||
|                             taia_addsec(&t,&t,OT_CLIENT_TIMEOUT); |  | ||||||
|                             io_timeout(n,t); |  | ||||||
|                             io_setcookie(n,h); |  | ||||||
|                             ++ot_overall_connections; |  | ||||||
|                         } else |  | ||||||
|                             io_close(n); |  | ||||||
|                     } else |  | ||||||
|                         io_close(n); |  | ||||||
|                 } |  | ||||||
|                 if (errno==EAGAIN) |  | ||||||
|                     io_eagain(s); |  | ||||||
|                 else |  | ||||||
|                     carp("socket_accept4"); |  | ||||||
|             } |  | ||||||
|             else |  | ||||||
|             { |  | ||||||
|                 char buf[8192]; |  | ||||||
|                 struct http_data* h=io_getcookie(i); |  | ||||||
| 
 |  | ||||||
|                 int l=io_tryread(i,buf,sizeof buf); |  | ||||||
|                 if (l<=0) |  | ||||||
|                 { |  | ||||||
|                     if (h) |  | ||||||
|                     { |  | ||||||
|                         array_reset(&h->r); |  | ||||||
|                         free(h); |  | ||||||
|                     } |  | ||||||
|                     io_close(i); |  | ||||||
|                 } |  | ||||||
|                 else |  | ||||||
|                 { |  | ||||||
|                     array_catb(&h->r,buf,l); |  | ||||||
| 
 |  | ||||||
|                     if (array_failed(&h->r)) |  | ||||||
|                         httperror(i,h,"500 Server Error","Request too long."); |  | ||||||
|                     else if (array_bytes(&h->r)>8192) |  | ||||||
|                         httperror(i,h,"500 request too long","You sent too much headers"); |  | ||||||
|                     else if ((l=header_complete(h))) |  | ||||||
|                         httpresponse(i,h); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 |       } | ||||||
|         while ((i=io_canwrite())!=-1) |   } | ||||||
|         { |   return 0; | ||||||
|             struct http_data* h=io_getcookie(i); |  | ||||||
| 
 |  | ||||||
|             int64 r=iob_send(i,&h->iob); |  | ||||||
|             if (r==-1) |  | ||||||
|                 io_eagain(i); |  | ||||||
|             else |  | ||||||
|                 if ((r<=0)||(h->iob.bytesleft==0)) |  | ||||||
|                 { |  | ||||||
|                     iob_reset(&h->iob); |  | ||||||
|                     free(h); |  | ||||||
|                     io_close(i); |  | ||||||
|                 } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     return 0; |  | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 erdgeist
						erdgeist