File: main_loop.c

package info (click to toggle)
camserv 0.42-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,884 kB
  • ctags: 1,123
  • sloc: ansic: 19,694; sh: 7,454; makefile: 206
file content (623 lines) | stat: -rw-r--r-- 17,874 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>

#include "camconfig.h"
#include "socket.h"
#include "mainloop.h"
#include "sockset.h"
#include "manager.h"
#include "databuf.h"
#include "list.h"
#include "log.h"

extern int errno;

static int Abort = 0;

#define MAX(x,y) ( (x) > (y) ? (x) : (y) )

#define CINFO_STATE_PREAMBLE    0
#define CINFO_STATE_PICTURE     1
#define CINFO_STATE_SEPERATOR   2
#define CINFO_STATE_SENDSIZE    3
#define CINFO_STATE_UNINIT      4

#define CLIENT_T_PROXY          0
#define CLIENT_T_BROWSER        1
#define CLIENT_T_UNINIT         2
#define CLIENT_T_SINGLE         3

#define RANDOMSTRING "ThisRandomString"
#define CONTENTTYPE  "image/jpeg"

#define MODNAME "mainloop"

typedef struct client_info_st {
  Socket *socket;           
  DataBuf *writebuf;
  void *management_data;
  int state;                    /* One of CINFO_STATE_* */

  time_t create_time;           /* Resource management */
  unsigned int bytes;
  unsigned int frames;

  unsigned int max_seconds;              /* Max resource info */
  unsigned int max_bytes;
  unsigned int max_frames;

  /********** Proxy only *********/
  unsigned long proxypic_size;  /* network order size of picture to send */
  char *proxypic_data;
  /*******************************/

  int last_picture_id;
  int client_type;              /* One of CLIENT_T_*    */
} ClientInfo;

static
char *get_multi_preamble_text( size_t *len){
#define MPREAMBLE_STR "HTTP/1.0 200 OK\n"  \
  "Content-type: multipart/x-mixed-replace;boundary=" RANDOMSTRING "\n" \
  "Cache-Control: no-cache\n" \
  "Cache-Control: private\n" \
  "Pragma: no-cache\n\n" \
  "--" RANDOMSTRING "\n" \
  "Content-type: " CONTENTTYPE "\n\n"
  
  if( len != NULL ) *len = sizeof( MPREAMBLE_STR ) - 1;
  return MPREAMBLE_STR;
}

static
char *get_single_preamble_text( size_t *len){
#define SPREAMBLE_STR "HTTP/1.0 200 OK\n"  \
  "Content-type: " CONTENTTYPE "\n" \
  "Cache-Control: no-cache\n" \
  "Cache-Control: private\n" \
  "Pragma: no-cache\n\n" 
  
  if( len != NULL ) *len = sizeof( SPREAMBLE_STR ) - 1;
  return SPREAMBLE_STR;
}

static
char *get_seperator_text( size_t *len ){
#define SEPERATOR_TEXT "\n--" RANDOMSTRING "\n"  \
  "Content-type: " CONTENTTYPE "\n\n" /* XXX */

  if( len != NULL ) *len = sizeof( SEPERATOR_TEXT ) - 1;
  return SEPERATOR_TEXT;
}

static 
ClientInfo *clientinfo_new( Socket *sock ){
  ClientInfo *res;

  if( (res = malloc( sizeof( *res ))) == NULL )
    return NULL;

  if( (res->writebuf = databuf_new()) == NULL ){
    free( res );
    return NULL;
  }

  res->management_data = NULL;
  res->socket = sock;
  res->state = CINFO_STATE_PREAMBLE;
  res->last_picture_id = -1;
  res->client_type = CLIENT_T_UNINIT;

  return res;
}

static
void clientinfo_dest( ClientInfo *cinfo ){
  databuf_dest( cinfo->writebuf );
  socket_dest( cinfo->socket );
  if( cinfo->management_data ){
    manager_dest_client( cinfo->management_data );
  }
  free( cinfo );
}

/*
 * accept_client:  Accept a client from the listen_socket, and return
 *                 a clientinfo structure containing it.  The new socket
 *                 will be set to non-blocking IO mode.
 *
 * Arguments:      listen_socket = Socket requiring an accept() operation.
 *
 * Return values:  Returns NULL on failure, else a valid pointer to a freshly
 *                 allocated ClientInfo structure.
 */

static
ClientInfo *accept_client( const Socket *listen_socket ){
  Socket *new_sock;
  ClientInfo *res;

  if( (new_sock = socket_accept( listen_socket )) == NULL ){
    camserv_log( MODNAME, "Could not accept new client socket: %s",
		 strerror( errno ));
    return NULL;
  }

  camserv_log( MODNAME, "Accepted new socket: %s", 
	       socket_query_remote_name( new_sock ));

  if( fcntl( socket_query_fd( new_sock ), F_SETFL, O_NONBLOCK ) == -1){
    camserv_log( MODNAME, "Unable to set socket to nonblocking mode!");
    socket_dest( new_sock );
    return NULL;
  }
  
  if( (res = clientinfo_new( new_sock )) == NULL ){
    camserv_log( MODNAME, "Error creating clientinfo structure!");
    socket_dest( new_sock );
    return NULL;
  }

  return res;
}
  
static
int dispatch_pictaker( const char *cmdbuf, const char *picture_mem ){
  int command;

  if( sscanf( cmdbuf, "%d", &command ) != 1 ){
    camserv_log( MODNAME, "Parse error reading data from pictaker!");
    return -1;
  }

  if( command == 0 ) {
    char *new_picture_data;
    int nbytes;

    sscanf( cmdbuf, "%*d %d", &nbytes );
    if( (new_picture_data = malloc( nbytes )) == NULL ){
      camserv_log( MODNAME, "Error allocating memory to manage picture!");
      return -1;
    }

    memcpy( new_picture_data, picture_mem, nbytes );
    if( manager_new_picture( new_picture_data, nbytes, 100 ) == -1 ){
      camserv_log( MODNAME, "Unable to manage picture!");
      free( new_picture_data );
      return -1;
    }
  } else {
    camserv_log( MODNAME, "Unknown pictaker dispatch command: %d", command );
    return -1;
  }
  return 0;
}

static
int write_proxy_client( ClientInfo *cinfo, SockSet *writeset ){
  int bufwrite;

  bufwrite = databuf_write( cinfo->writebuf, socket_query_fd( cinfo->socket ));
  if( bufwrite == -1 ) return -1;
  if( bufwrite == 1 ) return 0;

  /* In this case, we are done sending whatever it was that
     'state' referred to */
  if( cinfo->state == CINFO_STATE_SENDSIZE )
  {
    databuf_buf_set( cinfo->writebuf, cinfo->proxypic_data,
		     ntohl( cinfo->proxypic_size ));
    cinfo->state = CINFO_STATE_PICTURE;
  } else { /* Just finished sending a picture */
    char *pic_data;
    size_t pic_size;
    int pic_id;
    
    /* Remove old info */
    if( cinfo->management_data != NULL ) {
      if( manager_dest_client( cinfo->management_data ) == -1 ){
	camserv_log( MODNAME, "Error destroying client management!");
      }
    }

    cinfo->management_data = manager_new_client(&pic_data, &pic_size, &pic_id);
    if( cinfo->management_data == NULL ){
      camserv_log( MODNAME, "Error managing proxy!  (Picture may not be "
	       "taken yet)");
      sockset_hold( writeset, cinfo->socket );
      return 0;
    }

    if( pic_id == cinfo->last_picture_id ) {
      manager_dest_client( cinfo->management_data );
      cinfo->management_data = NULL;
      /* Proxy sucking too fast */
      sockset_hold( writeset, cinfo->socket );
      return 0;
    }

    camserv_log( MODNAME, "Writing picsize %d to client", (int) pic_size );
    cinfo->proxypic_data = pic_data;
    cinfo->proxypic_size = htonl( pic_size );
    cinfo->last_picture_id = pic_id;
    databuf_buf_set( cinfo->writebuf, &cinfo->proxypic_size,
		     sizeof( cinfo->proxypic_size ));
    cinfo->state = CINFO_STATE_SENDSIZE;
  }

  return 0;
}


static
int write_regular_client( ClientInfo *cinfo, SockSet *writeset ){
  switch(databuf_write( cinfo->writebuf, socket_query_fd( cinfo->socket )))
  {
  case -1 : /* Error */
    return -1;
    break;
  case 0:  /* All done feeding client the current data */
    if( cinfo->state == CINFO_STATE_PREAMBLE   || 
	cinfo->state == CINFO_STATE_SEPERATOR )
    {
      char *pic_data;
      size_t pic_size;
      int pic_id;
      
      cinfo->management_data = manager_new_client( &pic_data, &pic_size,
						   &pic_id );
      if( cinfo->management_data == NULL ){
	camserv_log( MODNAME, "Error managing client! (Picture may not"
		 " be taken yet)");
	/* Wait for the next successful picture to come around */
	sockset_hold( writeset, cinfo->socket );
	return 0;
      }
      
      if( pic_id == cinfo->last_picture_id ) {
	manager_dest_client( cinfo->management_data );
	cinfo->management_data = NULL;
	/* Whoa boy!  Hold on a second! */
	sockset_hold( writeset, cinfo->socket );
	return 0;
      }
      
      databuf_buf_set( cinfo->writebuf, pic_data, pic_size );
      cinfo->last_picture_id = pic_id;
      cinfo->state = CINFO_STATE_PICTURE;

      /* Accounting stuff */
      cinfo->frames++;
      cinfo->bytes += pic_size;
    } else {
      /* Just finished sending a picture */
      char *sep_data;
      size_t sep_size;

      if( manager_dest_client( cinfo->management_data ) == -1 ){
	camserv_log( MODNAME, "Error destroying client management!");
      }
      cinfo->management_data = NULL;
      cinfo->state = CINFO_STATE_SEPERATOR;

      /* For single-frame clients we are done now, and drop them */
      if( cinfo->client_type == CLIENT_T_SINGLE )
	return 1;
      
      /* Check resource limits */
      if( cinfo->max_seconds && 
	  (time( NULL ) - cinfo->create_time) > cinfo->max_seconds ) {
	camserv_log( MODNAME, "Dropping client \"%s\", time limit exceeded",
		     socket_query_remote_name( cinfo->socket ));
	return 1;
      }
      if( cinfo->max_bytes && cinfo->bytes > cinfo->max_bytes ) {
	camserv_log( MODNAME, "Dropping client \"%s\", byte limit exceeded",
		     socket_query_remote_name( cinfo->socket ));
	return 1;
      }
      if( cinfo->max_frames && cinfo->frames > cinfo->max_frames ) {
	camserv_log( MODNAME, "Dropping client \"%s\", frame limit exceeded",
		     socket_query_remote_name( cinfo->socket ));
	return 1;
      }

      /* send a seperator */
      sep_data = get_seperator_text( &sep_size );
      databuf_buf_set( cinfo->writebuf, sep_data, sep_size );
    }
    break;
  case 1:  /* Keep feeding data to the client */
    break;
  }
  return 0;
}

static
void client_remove( list_t *client_sockets, ClientInfo *cinfo ){
  lnode_t *node;

  for( node = list_first( client_sockets ); node != NULL; node=node->next ){
    if( node->data == cinfo ) {
      list_delete( client_sockets, node );
      lnode_destroy( node );
      return;
    }
  }
}

static
void sighandler( int signum ){
  camserv_log( MODNAME, "Received signal: %d",
	   signum );
  if( signum == SIGPIPE )
    return;

  camserv_log( MODNAME, "Aborting!");
  Abort = 1;
}

static
void setup_signals(){
  signal( SIGTERM, sighandler );
  camserv_log( MODNAME, "Setup signals");
}

int main_loop( CamConfig *ccfg, Socket *picture_sock, char *picture_mem  ){
  Socket *listen_socket;
  SockSet *readset = NULL, *writeset = NULL;
  list_t *client_sockets;
  lnode_t *node;
  int cfg_listen_port, highest_fd, num_clients, picture_client_ready;
  ClientInfo *clientinfo, *clientinfo2;

  if( (client_sockets = list_create( -1 )) == NULL)
    return -1;

  cfg_listen_port = camconfig_query_def_int( ccfg, SEC_SOCKET, 
					     "listen_port",
					     CAMCONFIG_DEF_LISTEN_PORT );

  if( (readset = sockset_new()) == NULL ||
      (writeset = sockset_new()) == NULL )
  {
    camserv_log( MODNAME, "Error allocating memory for socksets!");
    if( readset ) sockset_dest( readset );
    if( writeset ) sockset_dest( writeset );
    list_destroy( client_sockets );
    return -1;
  }

  if((listen_socket = socket_serve_tcp( NULL, cfg_listen_port, 100 )) == NULL )
  {
      camserv_log( MODNAME, "Error setting up socket on port \"%d\".  Exiting",
	       cfg_listen_port  );
      list_destroy( client_sockets );
      sockset_dest( readset );
      sockset_dest( writeset );
      return -1;
  }

  highest_fd = MAX( socket_query_fd( listen_socket ), 
		    socket_query_fd( picture_sock ));
  clientinfo = clientinfo_new( listen_socket );
  clientinfo2 = clientinfo_new( picture_sock );

  if( !clientinfo || !clientinfo2 ||
      sockset_add_fd( readset, listen_socket, clientinfo ) == -1 ||
      sockset_add_fd( readset, picture_sock, clientinfo2 ) == -1 )
  {
    camserv_log( MODNAME, "Error adding initial sockets to sockset!");
    sockset_dest( readset );
    sockset_dest( writeset );
    if( clientinfo )  clientinfo_dest( clientinfo );
    if( clientinfo2 ) clientinfo_dest( clientinfo2 );
    list_destroy( client_sockets );
    return -1;
  }

  num_clients = 0;
  picture_client_ready = 1;

  setup_signals();
  Abort = 0;
  while( !Abort ){
    int sel_res, i, nset_socks;
    void **set_socks;

    if( num_clients > 0 && picture_client_ready == 1 ){
      send( socket_query_fd( picture_sock ), "0", sizeof( "0" ), 0 );
      picture_client_ready = 0;
    }

    sockset_reset( readset );
    sockset_reset( writeset );

    sel_res = sockset_select( highest_fd + 1, readset, writeset, NULL );
    /* Service the event */
    if( sel_res == -1 ){
      camserv_log( MODNAME, "select() failure: %s", strerror( errno ));
      break;
    } else if( sel_res == 0 ){
      camserv_log( MODNAME, "Unexpected select() fall through!" );
      continue;
    } 

    /* Readable sockets */
    set_socks = sockset_query_socks( readset );
    nset_socks = sockset_query_nsocks( readset );
    for( i=0; i< nset_socks; i++ ){
      ClientInfo *new_cinfo;

      clientinfo = set_socks[ i ];

      if( clientinfo->socket == listen_socket ) {
	/* New client */

	if( (new_cinfo = accept_client( listen_socket )) == NULL )
	  continue;

	if( (node = lnode_create( new_cinfo )) == NULL ){
	  clientinfo_dest( new_cinfo );
	  continue;
	}

	if( sockset_add_fd( readset, new_cinfo->socket, new_cinfo ) == -1 ){
	  camserv_log( MODNAME, "Failed to add socket %d to socket read set!",
		   socket_query_fd( new_cinfo->socket ));
	  clientinfo_dest( new_cinfo );
	  lnode_destroy( node );
	  continue;
	}

	if( socket_query_fd( new_cinfo->socket ) > highest_fd )
	  highest_fd = socket_query_fd( new_cinfo->socket );

	list_append( client_sockets, node );
	num_clients++;
	/* Init resource limit for this client */
	new_cinfo->create_time = time( NULL );
	new_cinfo->bytes       = 0;
	new_cinfo->frames      = 0;
	new_cinfo->max_seconds = camconfig_query_def_int( ccfg, SEC_SOCKET,
							  "max_seconds", 0 );
	new_cinfo->max_bytes   = camconfig_query_def_int( ccfg, SEC_SOCKET,
							  "max_bytes", 0 );
	new_cinfo->max_frames  = camconfig_query_def_int( ccfg, SEC_SOCKET,
							  "max_frames", 0 );
      } else {
	char cmdbuf[ 1024 ];
	int readlen;

	clientinfo = set_socks[ i ];

	/* Regular joe client, set readable */
	if( (readlen = read( socket_query_fd( clientinfo->socket), cmdbuf, 
			     sizeof( cmdbuf ) - 1)) <= 0 )
	{
	  camserv_log( MODNAME, "Closing socket :%s", 
		       socket_query_remote_name( clientinfo->socket ));

	  
	  client_remove( client_sockets, clientinfo );
	  sockset_del_fd( readset, clientinfo->socket );
	  sockset_unhold_all( writeset );
	  sockset_del_fd( writeset, clientinfo->socket );
	  clientinfo_dest( clientinfo );
	  num_clients--;
	} else {
	  if( clientinfo->socket == picture_sock ) {
	    if( dispatch_pictaker( cmdbuf, picture_mem ) == -1 )
	      camserv_log( MODNAME, "Pictaker dispatch failure!");
	    sockset_unhold_all( writeset );
	    picture_client_ready = 1;
	  } else {
	    /* Information from a regular client */
	    cmdbuf[ readlen ] = '\0';
	    if( clientinfo->client_type == CLIENT_T_UNINIT ) {
	      char *preamble;
	      int pre_size;

	      /* Figure out what type of client we have */
	      if( !strncmp( cmdbuf, "GET", 3 )) {
		if( strstr( cmdbuf, "/singleframe" ))
		  clientinfo->client_type = CLIENT_T_SINGLE;
		else
		  clientinfo->client_type = CLIENT_T_BROWSER;
	      } else if( !strncmp( cmdbuf, "PROXY", 5 )) {
		clientinfo->client_type = CLIENT_T_PROXY;
		/* Here we are in the same state as being done writing a pic */
		clientinfo->state       = CINFO_STATE_PICTURE;
		databuf_buf_set( clientinfo->writebuf, NULL, 0 ); 
	      } else 
		clientinfo->client_type = CLIENT_T_BROWSER;

	      if( clientinfo->client_type != CLIENT_T_PROXY ) {
		/* Send the initial preamble.  Only now we can decide which 
		   type of preamble to send (single vs. multi-part) */
		if( clientinfo->client_type == CLIENT_T_SINGLE )
		  preamble = get_single_preamble_text( &pre_size );
		else
		  preamble = get_multi_preamble_text( &pre_size );
		databuf_buf_set( clientinfo->writebuf, preamble, pre_size );
	      }

	      if( sockset_add_fd( writeset, clientinfo->socket, 
				  clientinfo ) == -1 )
	      {
  		  camserv_log( MODNAME, "Failed to add socket %d to write set!",
			   socket_query_fd( clientinfo->socket ));
	      }
	    }
	  }
	}
      }
    }

    if( set_socks != NULL ) free( set_socks );

    /* Writable sockets */
    set_socks = sockset_query_socks( writeset );
    nset_socks = sockset_query_nsocks( writeset );
    for( i=0; i< nset_socks; i++ ){
      ClientInfo *cinfo;

      cinfo = set_socks[ i ];
      if( cinfo->client_type == CLIENT_T_BROWSER ||
	  cinfo->client_type == CLIENT_T_SINGLE ) 
      {
	int result;

	if( (result = write_regular_client( cinfo, writeset )) != 0 ){
	  /* result: 1=close requested, -1=error detected */
	  if( result == -1 )
	    camserv_log( MODNAME, "Databuf write error on socket: %s\n",
			 socket_query_remote_name( cinfo->socket ));
	  
	  client_remove( client_sockets, cinfo );
	  sockset_del_fd( readset, cinfo->socket );
	  sockset_del_fd( writeset, cinfo->socket );
	  clientinfo_dest( cinfo );
	  num_clients--;
	}
      } else {
	if( write_proxy_client( cinfo, writeset ) == -1 ){
	  camserv_log( MODNAME, "Databuf write error on socket: %d",
		   socket_query_fd( cinfo->socket ));
	  client_remove( client_sockets, cinfo );
	  sockset_del_fd( readset, cinfo->socket );
	  sockset_del_fd( writeset, cinfo->socket );
	  clientinfo_dest( cinfo );
	  num_clients--;
	}
      }
    }
    if( set_socks != NULL ) free( set_socks );
  }

  camserv_log( MODNAME, "Aborting.");
  sockset_dest( readset );
  sockset_dest( writeset );

  for( node = list_first( client_sockets) ; node; 
       node=list_next( client_sockets, node ))
  {
    clientinfo_dest( node->data );
  }

  /* Tell the picture taker to get out!  Get out! */
  camserv_log( MODNAME, "Closing picture taker");
  send( socket_query_fd( picture_sock ), "9", sizeof( "9" ), 0 );
  sleep( 3 );
  camserv_log( MODNAME, "done\n");

  list_destroy_nodes( client_sockets );
  list_destroy( client_sockets );
  socket_dest( listen_socket );
  return 0;
}