summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c49
1 files changed, 32 insertions, 17 deletions
diff --git a/opentracker.c b/opentracker.c
index 03a9076..10e9f65 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -67,14 +67,20 @@ static size_t ot_sockets_count = 0;
67static char debug_request[8192]; 67static char debug_request[8192];
68#endif 68#endif
69 69
70typedef enum {
71 STRUCT_HTTP_FLAG_ARRAY_USED = 1,
72 STRUCT_HTTP_FLAG_IOB_USED = 2
73} STRUCT_HTTP_FLAG;
74
70struct http_data { 75struct http_data {
71 union { 76 union {
72 array request; 77 array request;
73 io_batch batch; 78 io_batch batch;
74 }; 79 };
75 unsigned char ip[4]; 80 unsigned char ip[4];
76 int blessed; 81 STRUCT_HTTP_FLAG flag;
77}; 82};
83#define NOTBLESSED( h ) byte_diff( &h->ip, 4, g_adminip )
78 84
79/* Prototypes */ 85/* Prototypes */
80 86
@@ -140,7 +146,10 @@ static void sendmmapdata( const int64 s, char *buffer, size_t size ) {
140 146
141 if( !h ) 147 if( !h )
142 return free( buffer ); 148 return free( buffer );
143 array_reset( &h->request ); 149 if( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) {
150 h->flag &= ~STRUCT_HTTP_FLAG_ARRAY_USED;
151 array_reset( &h->request );
152 }
144 153
145 header = malloc( SUCCESS_HTTP_HEADER_LENGTH ); 154 header = malloc( SUCCESS_HTTP_HEADER_LENGTH );
146 if( !header ) { 155 if( !header ) {
@@ -153,6 +162,7 @@ static void sendmmapdata( const int64 s, char *buffer, size_t size ) {
153 iob_reset( &h->batch ); 162 iob_reset( &h->batch );
154 iob_addbuf_free( &h->batch, header, header_size ); 163 iob_addbuf_free( &h->batch, header, header_size );
155 iob_addbuf_munmap( &h->batch, buffer, size ); 164 iob_addbuf_munmap( &h->batch, buffer, size );
165 h->flag |= STRUCT_HTTP_FLAG_IOB_USED;
156 166
157 /* writeable sockets timeout after twice the pool timeout 167 /* writeable sockets timeout after twice the pool timeout
158 which defaults to 5 minutes (e.g. after 10 minutes) */ 168 which defaults to 5 minutes (e.g. after 10 minutes) */
@@ -166,17 +176,20 @@ static void senddata( const int64 s, char *buffer, size_t size ) {
166 ssize_t written_size; 176 ssize_t written_size;
167 177
168 /* whoever sends data is not interested in its input-array */ 178 /* whoever sends data is not interested in its input-array */
169 if( h ) 179 if( h && ( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) ) {
180 h->flag &= ~STRUCT_HTTP_FLAG_ARRAY_USED;
170 array_reset( &h->request ); 181 array_reset( &h->request );
182 }
171 183
172 written_size = write( s, buffer, size ); 184 written_size = write( s, buffer, size );
173 if( ( written_size < 0 ) || ( (size_t)written_size == size ) ) { 185 if( ( written_size < 0 ) || ( (size_t)written_size == size ) ) {
174 free( h ); io_close( s ); 186 free( h ); io_close( s );
175 } else { 187 } else {
176 char * outbuf = malloc( size - written_size ); 188 char * outbuf;
177 tai6464 t; 189 tai6464 t;
178 190
179 if( !outbuf ) { 191 if( !h ) return;
192 if( !( outbuf = malloc( size - written_size ) ) ) {
180 free(h); io_close( s ); 193 free(h); io_close( s );
181 return; 194 return;
182 } 195 }
@@ -184,6 +197,7 @@ static void senddata( const int64 s, char *buffer, size_t size ) {
184 iob_reset( &h->batch ); 197 iob_reset( &h->batch );
185 memmove( outbuf, buffer + written_size, size - written_size ); 198 memmove( outbuf, buffer + written_size, size - written_size );
186 iob_addbuf_free( &h->batch, outbuf, size - written_size ); 199 iob_addbuf_free( &h->batch, outbuf, size - written_size );
200 h->flag |= STRUCT_HTTP_FLAG_IOB_USED;
187 201
188 /* writeable sockets timeout after twice the pool timeout 202 /* writeable sockets timeout after twice the pool timeout
189 which defaults to 5 minutes (e.g. after 10 minutes) */ 203 which defaults to 5 minutes (e.g. after 10 minutes) */
@@ -226,7 +240,7 @@ static void httpresponse( const int64 s, char *data ) {
226 ******************************/ 240 ******************************/
227 case 4: /* sync ? */ 241 case 4: /* sync ? */
228 if( byte_diff( data, 4, "sync") ) HTTPERROR_404; 242 if( byte_diff( data, 4, "sync") ) HTTPERROR_404;
229 if( !h->blessed ) HTTPERROR_403_IP; 243 if( NOTBLESSED( h ) ) HTTPERROR_403_IP;
230 244
231LOG_TO_STDERR( "sync: %d.%d.%d.%d\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] ); 245LOG_TO_STDERR( "sync: %d.%d.%d.%d\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] );
232 246
@@ -567,7 +581,7 @@ static void handle_read( const int64 clientsocket ) {
567 ssize_t l; 581 ssize_t l;
568 582
569 if( ( l = io_tryread( clientsocket, static_inbuf, sizeof static_inbuf ) ) <= 0 ) { 583 if( ( l = io_tryread( clientsocket, static_inbuf, sizeof static_inbuf ) ) <= 0 ) {
570 if( h ) { 584 if( h && ( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) ) {
571 array_reset( &h->request ); 585 array_reset( &h->request );
572 free( h ); 586 free( h );
573 } 587 }
@@ -583,16 +597,18 @@ static void handle_read( const int64 clientsocket ) {
583 if( !array_start( &h->request ) ) { 597 if( !array_start( &h->request ) ) {
584 if( memchr( static_inbuf, '\n', l ) ) 598 if( memchr( static_inbuf, '\n', l ) )
585 return httpresponse( clientsocket, static_inbuf ); 599 return httpresponse( clientsocket, static_inbuf );
600 h->flag |= STRUCT_HTTP_FLAG_ARRAY_USED;
586 return array_catb( &h->request, static_inbuf, l ); 601 return array_catb( &h->request, static_inbuf, l );
587 } 602 }
588 603
604 h->flag |= STRUCT_HTTP_FLAG_ARRAY_USED;
589 array_catb( &h->request, static_inbuf, l ); 605 array_catb( &h->request, static_inbuf, l );
590 606
591 if( array_failed( &h->request ) ) 607 if( array_failed( &h->request ) )
592 return httperror( clientsocket, "500 Server Error", "Request too long."); 608 return httperror( clientsocket, "500 Server Error", "Request too long.");
593 609
594 if( ( !h->blessed ) && ( array_bytes( &h->request ) > 8192 ) ) 610 if( ( array_bytes( &h->request ) > 8192 ) && NOTBLESSED( h ) )
595 return httperror( clientsocket, "500 request too long", "You sent too much headers"); 611 return httperror( clientsocket, "500 request too long", "You sent too much headers");
596 612
597 if( memchr( array_start( &h->request ), '\n', array_length( &h->request, 1 ) ) ) 613 if( memchr( array_start( &h->request ), '\n', array_length( &h->request, 1 ) ) )
598 return httpresponse( clientsocket, array_start( &h->request ) ); 614 return httpresponse( clientsocket, array_start( &h->request ) );
@@ -627,9 +643,6 @@ static void handle_accept( const int64 serversocket ) {
627 byte_zero( h, sizeof( struct http_data ) ); 643 byte_zero( h, sizeof( struct http_data ) );
628 memmove( h->ip, ip, sizeof( ip ) ); 644 memmove( h->ip, ip, sizeof( ip ) );
629 645
630 if( !byte_diff( &h->ip, 4, g_adminip ) )
631 h->blessed = 1;
632
633 io_setcookie( i, h ); 646 io_setcookie( i, h );
634 647
635 ++ot_overall_tcp_connections; 648 ++ot_overall_tcp_connections;
@@ -647,8 +660,10 @@ static void handle_timeouted( void ) {
647 while( ( i = io_timeouted() ) != -1 ) { 660 while( ( i = io_timeouted() ) != -1 ) {
648 struct http_data* h=io_getcookie( i ); 661 struct http_data* h=io_getcookie( i );
649 if( h ) { 662 if( h ) {
650 iob_reset( &h->batch ); 663 if( h->flag & STRUCT_HTTP_FLAG_IOB_USED )
651 array_reset( &h->request ); 664 iob_reset( &h->batch );
665 if( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED )
666 array_reset( &h->request );
652 free( h ); 667 free( h );
653 } 668 }
654 io_close(i); 669 io_close(i);