aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xcs/xcs.c
blob: f0b1a96a8868b46a596c505c362425d4826cacca (plain)
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/* xcs.c 
 *
 * xcs - Xen Control Switch
 *
 * Copyright (c) 2004, Andrew Warfield
 */
 
/*

  Things we need to select on in xcs:
  
  1. Events arriving on /dev/evtchn
  
    These will kick a function to read everything off the fd, and scan the
    associated control message rings, resulting in notifications sent on
    data channels to connected clients.
    
  2. New TCP connections on XCS_PORT.
  
    These will either be control (intially) or associated data connections.
    
    Control connections will instantiate or rebind to an existing connnection
    struct.  The control channel is used to configure what events will be 
    received on an associated data channel.  These two channels are split
    out because the control channel is synchronous, all messages will return
    a result from XCS.  The data channel is effectively asynchronous, events
    may arrive in the middle of a control message exchange.  Additionally, 
    Having two TCP connections allows the client side to have a blocking
    listen loop for data messages, while independently interacting on the 
    control channel at other places in the code.
    
    Data connections attach to an existing control struct, using a session
    id that is passed during the control connect.  There is currently a 
    one-to-one relationship between data and control channels, but there
    could just as easily be many data channels, if there were a set of 
    clients with identical interests, or if you wanted to trace an existing
    client's data traffic.
    
 3. Messages arriving on open TCP connections.
    There are three types of open connections:
     
    3a. Messages arriving on open control channel file descriptors.
 
        [description of the control protocol here]
 
    3b. Messages arriving on open data channel file descriptors.
 
        [description of the data protocol here]
        
    3c. Messages arriving on (new) unbound connections.
    
        A connection must issue a XCS_CONNECT message to specify what
        it is, after which the connection is moved into one of the above 
        two groups.
 
 Additionally, we need a periodic timer to do housekeeping.
 
 4. Every XCS_GC_INTERVAL seconds, we need to clean up outstanding state. 
    Specifically, we garbage collect any sessions (connection_t structs)
    that have been unconnected for a period of time (XCS_SESSION_TIMEOUT), 
    and close any connections that have been openned, but not connected
    as a control or data connection (XCS_UFD_TIMEOUT).

*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <malloc.h>
#include <fcntl.h>
#include "xcs.h"

#undef fd_max
#define fd_max(x,y) ((x) > (y) ? (x) : (y))

/* ------[ Control channel interfaces ]------------------------------------*/

static control_channel_t *cc_list[NR_EVENT_CHANNELS];
static int *dom_port_map = 0;
static int dom_port_map_size = 0;

static void map_dom_to_port(u32 dom, int port)
{
	if (dom >= dom_port_map_size) {
		dom_port_map = (int *)realloc(dom_port_map,
					      (dom + 10) * sizeof(dom_port_map[0]));

		if (dom_port_map == NULL) {
			perror("realloc(dom_port_map)");
			exit(1);
		}

		for (; dom_port_map_size < dom + 10; dom_port_map_size++) {
			dom_port_map[dom_port_map_size] = -1;
		}
	}

	dom_port_map[dom] = port;
}

static int dom_to_port(u32 dom) {
	if (dom >= dom_port_map_size) return -1;

	return dom_port_map[dom];
}

static void init_interfaces(void)
{
    memset(cc_list, 0, sizeof cc_list);
}

static control_channel_t *add_interface(u32 dom, int local_port, 
                                        int remote_port)
{
    control_channel_t *cc=NULL, *oldcc;
    int ret;
    
    if (cc_list[dom_to_port(dom)] != NULL)
    {
        return(cc_list[dom_to_port(dom)]);
    }
    
    if (cc_list[local_port] == NULL) 
    {
        cc = ctrl_chan_new(dom, local_port, remote_port);
    }
    
    if (cc == NULL)
        return NULL;
    
    DPRINTF("added a new interface: dom: %u (l:%d,r:%d): %p\n",
            dom, local_port, remote_port, cc);
    DPRINTF("added a new interface: dom: %u (l:%d,r:%d): %p\n",
            dom, cc->local_port, cc->remote_port, cc);
    
    if ((ret = evtchn_bind(cc->local_port)) != 0)
    {
        DPRINTF("Got control interface, but couldn't bind evtchan!(%d)\n", ret);
        ctrl_chan_free(cc);
        return NULL;
    }
    
    if ( cc_list[cc->local_port] != NULL )
    {
        oldcc = cc_list[cc->local_port];
        
        if ((oldcc->remote_dom != cc->remote_dom) ||
            (oldcc->remote_port != cc->remote_port))
        {
            DPRINTF("CC conflict! (port: %d, old dom: %u, new dom: %u)\n",
                    cc->local_port, oldcc->remote_dom, cc->remote_dom);
            map_dom_to_port(oldcc->remote_dom, -1);
            ctrl_chan_free(cc_list[cc->local_port]);
        }
    }
     
    cc_list[cc->local_port] = cc;
    map_dom_to_port(cc->remote_dom, cc->local_port);
    cc->type = CC_TYPE_INTERDOMAIN;
    cc->ref_count = 0;
    return cc;
}

control_channel_t *add_virq(int virq)
{
    control_channel_t *cc;
    int virq_port;
    
    if (ctrl_chan_bind_virq(virq, &virq_port) == -1)
        return NULL;
    
    if ((cc_list[virq_port]       != NULL) && 
        (cc_list[virq_port]->type != CC_TYPE_VIRQ))
        return NULL; 
    
    if ((cc_list[virq_port]       != NULL) && 
        (cc_list[virq_port]->type == CC_TYPE_VIRQ))
        return cc_list[virq_port]; 
    
    cc = (control_channel_t *)malloc(sizeof(control_channel_t));
    if ( cc == NULL ) return NULL;

    cc->type       = CC_TYPE_VIRQ;
    cc->local_port = virq_port;
    cc->virq       = virq;
    
    return cc;
}

void get_interface(control_channel_t *cc)
{
    if (cc != NULL)
        cc->ref_count++;
}
    
void put_interface(control_channel_t *cc)
{
    if (cc != NULL)
    {
        cc->ref_count--;
        if (cc->ref_count <= 0)
        {
            DPRINTF("Freeing cc on port %d.\n", cc->local_port);
            (void)evtchn_unbind(cc->local_port);
            ctrl_chan_free(cc);
        }
    }
}

/* ------[ Simple helpers ]------------------------------------------------*/

/* listen_socket() is straight from paul sheer's useful select_tut manpage. */
static int listen_socket (int listen_port) 
{
    struct sockaddr_in a;
    int s;
    int yes;

    if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0) 
    {
        perror ("socket");
        return -1;
    }
    
    yes = 1;
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
        (char *) &yes, sizeof (yes)) < 0) 
    {
        perror ("setsockopt");
        close (s);
        return -1;
    }

    memset (&a, 0, sizeof (a));
    a.sin_port = htons (listen_port);
    a.sin_family = AF_INET;
    if (bind(s, (struct sockaddr *) &a, sizeof (a)) < 0) 
    {
        perror ("bind");
        close (s);
        return -1;
    }
    printf ("accepting connections on port %d\n", (int) listen_port);
    listen (s, 10);
    return s;
}

/* ------[ Message handlers ]----------------------------------------------*/

#define NO_CHANGE     0
#define CONNECTED     1
#define DISCONNECTED  2
int handle_connect_msg( xcs_msg_t *msg, int fd )
{
    xcs_connect_msg_t *cmsg = &msg->u.connect;
    connection_t *con;
    int ret = NO_CHANGE;
    
    switch (msg->type)
    {
        case XCS_CONNECT_CTRL:
        {
            if ( cmsg->session_id == 0 )
            {
                con = connection_new();
                if ( con == NULL)
                {
                    msg->result = XCS_RSLT_FAILED;
                    break;
                }
                msg->result      = XCS_RSLT_OK;
                cmsg->session_id = con->id;
                con->ctrl_fd     = fd;
                ret = CONNECTED;
                DPRINTF("New control connection\n");
                break;
            }

            con = get_con_by_session(cmsg->session_id);
            if ( con == NULL )
            {
                msg->result = XCS_RSLT_BADSESSION;
                break;
            }
            if ( con->ctrl_fd != -1 )
            {
                msg->result = XCS_RSLT_CONINUSE;
                break;
            }
            con->ctrl_fd   = fd;
            msg->result   = XCS_RSLT_OK;
            ret = CONNECTED;
            DPRINTF("Rebound to control connection\n");
            break;
        }
        case XCS_CONNECT_DATA:
        {
            con = get_con_by_session(cmsg->session_id);
            if ( con == NULL )
            {
                msg->result = XCS_RSLT_BADSESSION;
                break;
            }
            if ( con->data_fd != -1 )
            {
                msg->result = XCS_RSLT_CONINUSE;
                break;
            }
            con->data_fd   = fd;
            msg->result   = XCS_RSLT_OK;
            ret = CONNECTED;
            DPRINTF("Attached data connection\n");
            break;

        }
        case XCS_CONNECT_BYE:
        {
            close ( fd );
            ret = DISCONNECTED;
            break;
        }
    }   
    
    return ret;
}

int handle_control_message( connection_t *con, xcs_msg_t *msg )
{
    int ret;
    int reply_needed = 1;
            
    DPRINTF("Got message, type %u.\n", msg->type);

    switch (msg->type)
    {
        case XCS_MSG_BIND:
        {
            xcs_bind_msg_t *bmsg = &msg->u.bind;

            if ( ! BIND_MSG_VALID(bmsg) )
            {
                msg->result = XCS_RSLT_BADREQUEST;
                break;
            }
            
            ret = xcs_bind(con, bmsg->port, bmsg->type);
            if (ret == 0) {
                msg->result = XCS_RSLT_OK;
            } else {
                msg->result = XCS_RSLT_FAILED;
            }
            break;
        }
        case XCS_MSG_UNBIND:
        {
            xcs_bind_msg_t *bmsg = &msg->u.bind;

            if ( ! BIND_MSG_VALID(bmsg) )
            {
                msg->result = XCS_RSLT_BADREQUEST;
                break;
            }
            
            ret = xcs_unbind(con, bmsg->port, bmsg->type);
            if (ret == 0) {
                msg->result = XCS_RSLT_OK;
            } else {
                msg->result = XCS_RSLT_FAILED;
            }
            break;
        }    
        case XCS_VIRQ_BIND:
        {
            control_channel_t *cc;
            xcs_virq_msg_t *vmsg = &msg->u.virq;
            if ( ! VIRQ_MSG_VALID(vmsg) )
            {
                msg->result = XCS_RSLT_BADREQUEST;
                break;
            }

            cc = add_virq(vmsg->virq);
            if (cc == NULL)
            {
                msg->result = XCS_RSLT_FAILED;
                break;
            }
            ret = xcs_bind(con, cc->local_port, TYPE_VIRQ);
            if (ret == 0) {
                vmsg->port   = cc->local_port;
                msg->result  = XCS_RSLT_OK;
            } else {
                msg->result = XCS_RSLT_FAILED;
            }
            break;
        }

        case XCS_CIF_NEW_CC:
        {
            control_channel_t *cc;
            xcs_interface_msg_t *imsg = &msg->u.interface;

            if ( ! INTERFACE_MSG_VALID(imsg) )
            {
                msg->result = XCS_RSLT_BADREQUEST;
                break;
            }

            cc = add_interface(imsg->dom, imsg->local_port, imsg->remote_port);
            if (cc != NULL) {
                get_interface(cc);
                msg->result       = XCS_RSLT_OK;
                imsg->local_port  = cc->local_port;
                imsg->remote_port = cc->remote_port;
            } else {
                msg->result = XCS_RSLT_FAILED;
            }
            break;
        }

        case XCS_CIF_FREE_CC:
        {
            control_channel_t *cc;
            xcs_interface_msg_t *imsg = &msg->u.interface;

            if ( ! INTERFACE_MSG_VALID(imsg) )
            {
                msg->result = XCS_RSLT_BADREQUEST;
                break;
            }

            cc = add_interface(imsg->dom, imsg->local_port, imsg->remote_port);
            if (cc != NULL) {
                put_interface(cc);
            } 
            msg->result       = XCS_RSLT_OK;
            break;
        }
    }
    return reply_needed;
}

void handle_data_message( connection_t *con, xcs_msg_t *msg )
{
    control_channel_t *cc;
    xcs_control_msg_t *cmsg = &msg->u.control;
    int port;
    
    switch (msg->type)
    {
    case XCS_REQUEST:
        if ( cmsg->remote_dom > MAX_DOMS )
            break;
        
        port = dom_to_port(cmsg->remote_dom);
        if (port == -1) break;
        cc = cc_list[port];
        if ((cc != NULL) && ( cc->type == CC_TYPE_INTERDOMAIN ))
        {
            DPRINTF("DN:REQ: dom:%d port: %d type: %d\n", 
                    cc->remote_dom, cc->local_port, 
                    cmsg->msg.type);
            ctrl_chan_write_request(cc, cmsg);
            ctrl_chan_notify(cc);
        } else {
            DPRINTF("tried to send a REQ to a null cc\n.");
        }
        break;

    case XCS_RESPONSE:
        if ( cmsg->remote_dom > MAX_DOMS )
            break;
        
        port = dom_to_port(cmsg->remote_dom);
        if (port == -1) break;
        cc = cc_list[port];
        if ((cc != NULL) && ( cc->type == CC_TYPE_INTERDOMAIN ))
        {
            DPRINTF("DN:RSP: dom:%d port: %d type: %d\n", 
                    cc->remote_dom, cc->local_port, 
                    cmsg->msg.type);
            ctrl_chan_write_response(cc, cmsg);
            ctrl_chan_notify(cc);
        }
        break;

    case XCS_VIRQ:
        if ( !(PORT_VALID(cmsg->local_port)) )
            break;
            
        cc = cc_list[cmsg->local_port];
        
        if ((cc != NULL) && ( cc->type == CC_TYPE_VIRQ ))
        {
            DPRINTF("DN:VIRQ:  virq: %d port: %d\n", 
                    cc->virq, cc->local_port);
            ctrl_chan_notify(cc);
        }
        break;
    }
}
    
/* ------[ Control interface handler ]-------------------------------------*/

/* passed as a function pointer to the lookup. */
void send_kmsg(connection_t *c, void *arg)
{
    xcs_msg_t *msg = (xcs_msg_t *)arg;

    DPRINTF("       -> CONNECTION %d\n", c->data_fd);
    if (c->data_fd > 0)
    {
      send(c->data_fd, msg, sizeof(xcs_msg_t), 0);
    }
}

int handle_ctrl_if(void)
{
    control_channel_t *cc;
    control_msg_t     *msg;
    xcs_msg_t          kmsg;
    int                chan, ret;
    
    DPRINTF("Event thread kicked!\n");
again:
    while ((chan = evtchn_read()) > 0)
    {
        evtchn_unmask(chan);
        cc = cc_list[chan];
        if (cc_list[chan] == NULL) {
            DPRINTF("event from unknown channel (%d)\n", chan);
            continue;
        }

        if ( cc_list[chan]->type == CC_TYPE_VIRQ )
        {
            DPRINTF("UP:VIRQ: virq:%d port: %d\n",
                    cc->virq, cc->local_port);
            kmsg.type = XCS_VIRQ;
            kmsg.u.control.local_port = cc->local_port;
            xcs_lookup(cc->local_port, TYPE_VIRQ, send_kmsg, &kmsg);
            continue;
        }

        while (ctrl_chan_request_to_read(cc))
        {
            msg = &kmsg.u.control.msg;
            kmsg.type = XCS_REQUEST;
            kmsg.u.control.remote_dom = cc->remote_dom;
            kmsg.u.control.local_port = cc->local_port;
            ret = ctrl_chan_read_request(cc, &kmsg.u.control);
            DPRINTF("UP:REQ: dom:%d port: %d type: %d len: %d\n", 
                    cc->remote_dom, cc->local_port, 
                    msg->type, msg->length);
            if (ret == 0)
                xcs_lookup(cc->local_port, msg->type, send_kmsg, &kmsg);
        }

        while (ctrl_chan_response_to_read(cc))
        {
            msg = &kmsg.u.control.msg;
            kmsg.type = XCS_RESPONSE;
            kmsg.u.control.remote_dom = cc->remote_dom;
            kmsg.u.control.local_port = cc->local_port;
            ret = ctrl_chan_read_response(cc, &kmsg.u.control);
            DPRINTF("UP:RSP: dom:%d port: %d type: %d len: %d\n", 
                    cc->remote_dom, cc->local_port, 
                    msg->type, msg->length);
            if (ret == 0)
                xcs_lookup(cc->local_port, msg->type, send_kmsg, &kmsg);
        }
    }
    
    if (chan == -EINTR)
        goto again;
    
    return chan;
}

  
/* ------[ Main xcs code / big select loop ]-------------------------------*/

                
typedef struct unbound_fd_st {
    int                   fd;
    struct timeval        born;
    struct unbound_fd_st *next;
} unbound_fd_t;

/* This makes ufd point to the next entry in the list, so need to   *
 * break/continue if called while iterating.                        */
void delete_ufd(unbound_fd_t **ufd)
{
    unbound_fd_t *del_ufd;
    
    del_ufd = *ufd;
    *ufd    = (*ufd)->next;
    free( del_ufd );
}

void gc_ufd_list( unbound_fd_t **ufd )
{
    struct timeval now, delta;
    
    gettimeofday(&now, NULL);
    
    while ( *ufd != NULL )
    {
        timersub(&now, &(*ufd)->born, &delta);
        if (delta.tv_sec > XCS_UFD_TIMEOUT)
        {
            DPRINTF("GC-UFD: closing fd: %d\n", (*ufd)->fd);
            close((*ufd)->fd);
            delete_ufd(ufd);
            continue;
        }
        ufd = &(*ufd)->next;
    }
}

int main (int argc, char*argv[])
{
    int listen_fd, evtchn_fd;
    unbound_fd_t *unbound_fd_list = NULL, **ufd;
    struct timeval timeout = { XCS_GC_INTERVAL, 0 };
    connection_t **con;
    
    /* Initialize xc and event connections. */
    if (ctrl_chan_init() != 0)
    {
        printf("Couldn't open conneciton to libxc.\n");
        exit(-1);
    }
    
    if ((evtchn_fd = evtchn_open()) < 0)
    {
        printf("Couldn't open event channel driver interface.\n");
        exit(-1);
    }
   
    /* Initialize control interfaces, bindings. */
    init_interfaces();
    init_bindings();
    
    listen_fd = listen_socket(XCS_TCP_PORT);
   
    /* detach from our controlling tty so that a shell does hang waiting for
       stopped jobs. */
    /* we should use getopt() here */

    if (!(argc == 2 && !strcmp(argv[1], "-i"))) {
	pid_t pid = fork();
	int fd;

	if (pid == -1) {
		perror("fork()");
	} else if (pid) {
		exit(0);
	}

    	setsid();
	close(2);
	close(1);
	close(0);
	fd = open("/dev/null", O_RDWR);
	dup(fd);
	dup(fd);
    }
 
    for (;;)
    {
        int n, ret;
        fd_set rd, wr, er;
        FD_ZERO ( &rd );
        FD_ZERO ( &wr );
        FD_ZERO ( &er );
        
        /* TCP listen fd: */
        FD_SET ( listen_fd, &rd );
        n = fd_max ( n, listen_fd );
        
        /* Evtchn fd: */
        FD_SET ( evtchn_fd, &rd );
        n = fd_max ( n, evtchn_fd );
        
        /* unbound connection fds: */
        ufd = &unbound_fd_list;
        while ((*ufd) != NULL) 
        {
            FD_SET ( (*ufd)->fd, &rd );
            n = fd_max ( n, (*ufd)->fd );
            ufd = &(*ufd)->next;
        }
        
        /* control and data fds: */
        con = &connection_list;
        while ((*con) != NULL)
        {
            if ((*con)->ctrl_fd > 0)
            {
                FD_SET ( (*con)->ctrl_fd, &rd );
                n = fd_max ( n, (*con)->ctrl_fd );
            }
            if ((*con)->data_fd > 0)
            {
                FD_SET ( (*con)->data_fd, &rd );
                n = fd_max ( n, (*con)->data_fd );
            }
            con = &(*con)->next;
        }
        
        ret = select ( n + 1, &rd, &wr, &er, &timeout );
        
        if ( (timeout.tv_sec == 0) && (timeout.tv_usec == 0) )
        {
            gc_ufd_list(&unbound_fd_list);
            gc_connection_list();
            timeout.tv_sec = XCS_GC_INTERVAL;
        }
        
        if ( (ret == -1) && (errno == EINTR) )
            continue;
        if ( ret < 0 )
        {
            perror ("select()");
            exit(-1);
        }
        
        /* CASE 1: Events arriving on /dev/evtchn. */
        
        if ( FD_ISSET (evtchn_fd, &rd ))
            handle_ctrl_if();
        
        /* CASE 2: New connection on the listen port. */
        if ( FD_ISSET ( listen_fd, &rd ))
        {
            struct sockaddr_in remote_addr;
            int size;
            memset (&remote_addr, 0, sizeof (remote_addr));
            size = sizeof remote_addr;
            ret = accept(listen_fd, (struct sockaddr *)&remote_addr, &size);
            if ( ret < 0 )
            {
                perror("accept()");
            } else {
                unbound_fd_t *new_ufd;
                
                new_ufd = (unbound_fd_t *)malloc(sizeof(*new_ufd));
                
                if (new_ufd != NULL)
                {
                    gettimeofday(&new_ufd->born, NULL);
                    new_ufd->fd     = ret;
                    new_ufd->next   = unbound_fd_list;
                    unbound_fd_list = new_ufd; 
                } else {
                    perror("malloc unbound connection");
                    close(ret);
                }
            }
        }
        
        /* CASE 3a: Handle messages on control connections. */
        
        con = &connection_list;
        while ( *con != NULL )
        {
            if ( ((*con)->ctrl_fd > 0) && (FD_ISSET((*con)->ctrl_fd, &rd)) )
            {
                xcs_msg_t msg;
                memset (&msg, 0, sizeof(msg));
                ret = read( (*con)->ctrl_fd, &msg, sizeof(msg) );
                
                if ( ret < 0 )
                {
                    perror("reading ctrl fd.");
                } else if ( ret == 0 )
                {
                    DPRINTF("Control connection dropped.\n");
                    close ( (*con)->ctrl_fd );
                    (*con)->ctrl_fd = -1;
                    gettimeofday(&(*con)->disconnect_time, NULL);
                } else 
                {
                    if ( ret != sizeof(msg) )
                    {
                        DPRINTF("Unexpected frame size!\n");
                        continue;
                    }
                    
                    ret = handle_control_message( *con, &msg );
                    
                    if ( ret == 1 )
                        send( (*con)->ctrl_fd, &msg, sizeof(msg), 0 );
                }
            }
            con = &(*con)->next;
        }
        
        /* CASE 3b: Handle messages on data connections. */
        
        con = &connection_list;
        while ( *con != NULL )
        {
            if ( ((*con)->data_fd > 0) && (FD_ISSET((*con)->data_fd, &rd)) )
            {
                xcs_msg_t msg;
                memset (&msg, 0, sizeof(msg));
                ret = read( (*con)->data_fd, &msg, sizeof(msg) );
                
                if ( ret < 0 )
                {
                    perror("reading data fd.");
                } else if ( ret == 0 )
                {
                    DPRINTF("Data connection dropped.\n");
                    close ( (*con)->data_fd );
                    (*con)->data_fd = -1;
                    gettimeofday(&(*con)->disconnect_time, NULL);
                } else 
                {
                    if ( ret != sizeof(msg) )
                    {
                        DPRINTF("Unexpected frame size!\n");
                        continue;
                    }
                    
                    handle_data_message( *con, &msg );
                }
            }
            con = &(*con)->next;
        }
        
        /* CASE 3c: Handle messages arriving on unbound connections. */
        ufd = &unbound_fd_list;
        while ((*ufd) != NULL)
        {
            if ( FD_ISSET( (*ufd)->fd, &rd ) )
            {
                xcs_msg_t msg;
                memset (&msg, 0, sizeof(msg));
                ret = read( (*ufd)->fd, &msg, sizeof(msg) );
                
                if ( ret == 0 )
                {
                    close ( (*ufd)->fd );
                    delete_ufd(ufd);
                    continue; /* we just advanced ufd */
                } else {
                    if ( ret != sizeof(msg) )
                    {
                        DPRINTF("Unexpected frame size!\n");
                        continue;
                    }
                    
                    ret = handle_connect_msg( &msg, (*ufd)->fd );
                    
                    if ( (ret == CONNECTED) || (ret == NO_CHANGE) )
                        send( (*ufd)->fd, &msg, sizeof(msg), 0 );
                    
                    if ( (ret = CONNECTED) || (ret = DISCONNECTED) )
                    {
                        delete_ufd( ufd );
                        continue;
                    }
                }
            }
            ufd = &(*ufd)->next;
        }
    }
}