summaryrefslogtreecommitdiff
path: root/tools/socktest.c
blob: b0c45c51a21a4e773704fadc8ffe20dd77f25a43 (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
/*
** Copyright 2009 The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

/** socket testing  */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/uio.h>
#include <unistd.h>

#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/un.h>
#include <netinet/in.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/sco.h>
#include <bluetooth/l2cap.h>

enum sock_type {
    UNIX = 0,
    RFCOMM,
    SCO,
    L2CAP,
    TCP,
};

struct thread_args {
    int fd;
    int type;
    int delay;
};

struct sockaddr_un  local_addr_un  = {AF_UNIX, "/data/foo"};
struct sockaddr_rc  local_addr_rc  = {AF_BLUETOOTH, *BDADDR_ANY, 4};
struct sockaddr_sco local_addr_sco = {AF_BLUETOOTH, *BDADDR_LOCAL};
struct sockaddr_l2  local_addr_l2  = {AF_BLUETOOTH, htobs(0x1001), *BDADDR_ANY, 0};
struct sockaddr_in  local_addr_in  = {AF_INET, 9999, {0}, {0}};

struct sockaddr_un  remote_addr_un  ;
struct sockaddr_rc  remote_addr_rc  ;
struct sockaddr_sco remote_addr_sco ;
struct sockaddr_l2  remote_addr_l2  ;
struct sockaddr_in  remote_addr_in  ;

static void print_events(int events) {
    if (events & POLLIN) printf("POLLIN ");
    if (events & POLLPRI) printf("POLLPRI ");
    if (events & POLLOUT) printf("POLLOUT ");
    if (events & POLLERR) printf("POLLERR ");
    if (events & POLLHUP) printf("POLLHUP ");
    if (events & POLLNVAL) printf("POLLNVAL ");
    printf("\n");
}

static void print_fds(struct pollfd *ufds, nfds_t nfds) {
    unsigned int i;
    for (i=0; i<nfds; i++)
        printf("%d ", ufds[i].fd);
}

static int _socket(int type) {
    int ret;
    int family = -1;
    int typ = -1;
    int protocol = -1;

    switch (type) {
    case UNIX:
        family = PF_UNIX;
        typ = SOCK_STREAM;
        protocol = 0;
        break;
    case RFCOMM:
        family = PF_BLUETOOTH;
        typ = SOCK_STREAM;
        protocol = BTPROTO_RFCOMM;
        break;
    case SCO:
        family = PF_BLUETOOTH;
        typ = SOCK_SEQPACKET;
        protocol = BTPROTO_SCO;
        break;
    case L2CAP:
        family = PF_BLUETOOTH;
        typ = SOCK_SEQPACKET;
        protocol = BTPROTO_L2CAP;
        break;
    case TCP:
        family = PF_INET;
        typ = SOCK_STREAM;
        protocol = 0;
        break;
    }

    printf("%ld: socket()\n", pthread_self());
    ret = socket(family, typ, protocol);
    printf("%ld: socket() = %d\n", pthread_self(), ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));

    return ret;
}

static int _close(int fd, int type) {
    int ret;

    printf("%ld: close(%d)\n", pthread_self(), fd);
    ret = close(fd);
    printf("%ld: close(%d) = %d\n", pthread_self(), fd, ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));

    return ret;
}

static int _bind(int fd, int type) {
    int len = 0;
    int ret;
    struct sockaddr *addr = NULL;

    switch (type) {
    case UNIX:
        unlink(local_addr_un.sun_path);
        addr = (struct sockaddr *) &local_addr_un;
        len = sizeof(local_addr_un);
        break;
    case RFCOMM:
        addr = (struct sockaddr *) &local_addr_rc;
        len = sizeof(local_addr_rc);
        break;
    case SCO:
        addr = (struct sockaddr *) &local_addr_sco;
        len = sizeof(local_addr_sco);
        break;
    case L2CAP:
        addr = (struct sockaddr *) &local_addr_l2;
        len = sizeof(local_addr_l2);
        break;
    case TCP:
        addr = (struct sockaddr *) &local_addr_in;
        len = sizeof(local_addr_in);
        break;
    }

    printf("%ld: bind(%d)\n", pthread_self(), fd);
    ret = bind(fd, addr, len);
    printf("%ld: bind(%d) = %d\n", pthread_self(), fd, ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));

    return ret;
}

static int _listen(int fd, int type) {
    int ret;

    printf("%ld: listen(%d)\n", pthread_self(), fd);
    ret = listen(fd, 1);
    printf("%ld: listen(%d) = %d\n", pthread_self(), fd, ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));

    return ret;
}

static int _read(int fd) {
    int ret;
    char buf;

    printf("%ld: read(%d)\n", pthread_self(), fd);
    ret = read(fd, &buf, 1);
    printf("%ld: read(%d) = %d [%d]\n", pthread_self(), fd, ret, (int)buf);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));

    return ret;
}


static int _accept(int fd, int type) {
    int ret;
    int len;
    struct sockaddr *addr = NULL;

    switch (type) {
    case UNIX:
        addr = (struct sockaddr *) &remote_addr_un;
        len = sizeof(remote_addr_un);
        break;
    case RFCOMM:
        addr = (struct sockaddr *) &remote_addr_rc;
        len = sizeof(remote_addr_rc);
        break;
    case SCO:
        addr = (struct sockaddr *) &remote_addr_sco;
        len = sizeof(remote_addr_sco);
        break;
    case L2CAP:
        addr = (struct sockaddr *) &remote_addr_l2;
        len = sizeof(remote_addr_l2);
        break;
    case TCP:
        addr = (struct sockaddr *) &remote_addr_in;
        len = sizeof(remote_addr_in);
        break;
    }

    printf("%ld: accept(%d)\n", pthread_self(), fd);
    ret = accept(fd, addr, &len);
    printf("%ld: accept(%d) = %d\n", pthread_self(), fd, ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
    else {
        printf("\tlen = %d\n", len);
    }

    return ret;
}

int get_bdaddr(const char *str, bdaddr_t *ba) {
    char *d = ((char *)ba) + 5, *endp;
    int i;
    for(i = 0; i < 6; i++) {
        *d-- = strtol(str, &endp, 16);
        if (*endp != ':' && i != 5) {
            memset(ba, 0, sizeof(bdaddr_t));
            return -1;
        }
        str = endp + 1;
    }
    return 0;
}

static int _connect(int fd, int type) {
    int ret;
    int len = 0;
    struct sockaddr *addr = NULL;

    switch (type) {
    case UNIX:
        addr = (struct sockaddr *) &local_addr_un;
        len = sizeof(local_addr_un);
        break;
    case RFCOMM:
        get_bdaddr("00:11:22:33:44:55", &local_addr_rc.rc_bdaddr);
        addr = (struct sockaddr *) &local_addr_rc;
        len = sizeof(local_addr_rc);
        break;
    case SCO:
        addr = (struct sockaddr *) &local_addr_sco;
        len = sizeof(local_addr_sco);
        break;
    case L2CAP:
        addr = (struct sockaddr *) &local_addr_l2;
        len = sizeof(local_addr_l2);
        break;
    case TCP:
        addr = (struct sockaddr *) &local_addr_in;
        len = sizeof(local_addr_in);
        break;
    }

    printf("%ld: connect(%d)\n", pthread_self(), fd);
    ret = connect(fd, addr, len);
    printf("%ld: connect(%d) = %d\n", pthread_self(), fd, ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));

    return ret;
}

static int _write(int fd, int type) {
    int ret;
    char buf = 69;

    printf("%ld: write(%d)\n", pthread_self(), fd);
    ret = write(fd, &buf, 1);
    printf("%ld: write(%d) = %d\n", pthread_self(), fd, ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));

    return ret;
}

static int _shutdown(int fd, int how) {
    int ret;

    printf("%ld: shutdown(%d)\n", pthread_self(), fd);
    ret = shutdown(fd, how);
    printf("%ld: shutdown(%d) = %d\n", pthread_self(), fd, ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));

    return ret;
}

static int _poll(struct pollfd *ufds, nfds_t nfds, int timeout) {
    int ret;
    unsigned int i;

    printf("%ld: poll(", pthread_self());
    print_fds(ufds, nfds);
    printf(")\n");
    ret = poll(ufds, nfds, timeout);
    printf("%ld: poll() = %d\n", pthread_self(), ret);
    if (ret < 0) printf("\terr %d (%s)\n", errno, strerror(errno));
    if (ret > 0) {
        for (i=0; i<nfds; i++) {
            if (ufds[i].revents) {
                printf("\tfd %d ", ufds[i].fd); print_events(ufds[i].revents);
            }
        }
    }
    return ret;
}

static void thread_delay_close(struct thread_args *args) {
    printf("%ld: START\n", pthread_self());
    sleep(args->delay);
    _close(args->fd, args->type);
    printf("%ld: END\n", pthread_self());
}

static void thread_poll(void *args) {
    int fd = (int)args;
    struct pollfd pfd;
    printf("%ld: START\n", pthread_self());
    pfd.fd = fd;
    pfd.events = 0;
    _poll(&pfd, 1, -1);
    printf("%ld: END\n", pthread_self());
}

static void thread_read(void *args) {
    int fd = (int)args;
    printf("%ld: START\n", pthread_self());
    _read(fd);
    printf("%ld: END\n", pthread_self());
}

static void thread_pollin(void *args) {
    int fd = (int)args;
    struct pollfd pfd;
    printf("%ld: START\n", pthread_self());
    pfd.fd = fd;
    pfd.events = POLLIN;
    _poll(&pfd, 1, -1);
    printf("%ld: END\n", pthread_self());
}

static void thread_shutdown(int fd) {
    printf("%ld: START\n", pthread_self());
    sleep(4);
    _shutdown(fd, SHUT_RDWR);
    printf("%ld: END\n", pthread_self());
}

static void thread_accept(struct thread_args *args) {
    printf("%ld: START\n", pthread_self());
    sleep(args->delay);
    _accept(args->fd, args->type);
    printf("%ld: END\n", pthread_self());
}

static void thread_connect(struct thread_args *args) {
    printf("%ld: START\n", pthread_self());
    sleep(args->delay);
    _connect(args->fd, args->type);
    printf("%ld: END\n", pthread_self());
}

static void thread_delay_close_write(struct thread_args *args) {
    printf("%ld: START\n", pthread_self());
    sleep(args->delay);
    _close(args->fd, args->type);
    sleep(args->delay);
    _write(args->fd, args->type);
    printf("%ld: END\n", pthread_self());
}

static void thread_accept_write(struct thread_args *args) {
    printf("%ld: START\n", pthread_self());
    sleep(args->delay);
    _accept(args->fd, args->type);
    sleep(args->delay);
    _write(args->fd, args->type);
    printf("%ld: END\n", pthread_self());
}

static void thread_delay_connect(struct thread_args *args) {
    printf("%ld: START\n", pthread_self());
    sleep(args->delay);
    args->fd = _socket(args->type);
    _connect(args->fd, args->type);
    printf("%ld: END\n", pthread_self());
}

static int do_accept_accept_accept(int type) {
    int fd;

    fd = _socket(type);
    if (fd < 0) goto error;

    if (_bind(fd, type) < 0) goto error;

    if (_listen(fd, type) < 0) goto error;

    while (1) {
        _accept(fd, type);
    }

    return 0;

error:
    return -1;
}

static int do_accept_and_close(int type) {
    int fd;
    pthread_t thread;
    struct thread_args args = {-1, type, 1};

    fd = _socket(type);
    if (fd < 0) goto error;

    if (_bind(fd, type) < 0) goto error;

    if (_listen(fd, type) < 0) goto error;

    args.fd = fd;
    pthread_create(&thread, NULL, (void *)thread_delay_close, (void *)&args);

    _accept(fd, type);

    pthread_join(thread, NULL);

    return 0;

error:
    return -1;
}

static int do_accept_shutdown(int type) {
    int fd;
    pthread_t thread;
    struct thread_args args = {-1, type, 0};

    fd = _socket(type);
    if (fd < 0) goto error;

    if (_bind(fd, type) < 0) goto error;

    if (_listen(fd, type) < 0) goto error;

    args.fd = fd;
    pthread_create(&thread, NULL, (void *)thread_accept, (void *)&args);

    sleep(4);
    _shutdown(fd, SHUT_RDWR);

    pthread_join(thread, NULL);

    _close(fd, type);

    return 0;

error:
    return -1;
}

static int do_connect_shutdown(int type) {
    int fd;
    pthread_t thread;
    struct thread_args args = {-1, type, 0};

    fd = _socket(type);
    if (fd < 0) goto error;

    args.fd = fd;
    pthread_create(&thread, NULL, (void *)thread_connect, (void *)&args);

    sleep(4);
    _shutdown(fd, SHUT_RDWR);

    pthread_join(thread, NULL);

    _close(fd, type);

    return 0;

error:
    return -1;
}

static int do_connectnb_shutdown(int type) {
    int fd;
    int flags;
    pthread_t thread;
    struct thread_args args = {-1, type, 0};


    fd = _socket(type);
    if (fd < 0) goto error;

    flags = fcntl(fd, F_GETFL);
    if (flags == -1)
        return -1;
    if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
        return -1;

    _connect(fd, type);

    sleep(1);
    _shutdown(fd, SHUT_RDWR);

    sleep(2);

    _close(fd, type);

    return 0;

error:
    return -1;
}

static int do_connectnb_close(int type) {
    int fd;
    pthread_t thread;
    struct thread_args args = {-1, type, 0};
    int flags;

    fd = _socket(type);
    if (fd < 0) goto error;

    flags = fcntl(fd, F_GETFL);
    if (flags == -1)
        return -1;
    if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
        return -1;

    _connect(fd, type);

    sleep(2);

    _close(fd, type);

    return 0;

error:
    return -1;
}

// accept in one thread. close then write in another
static int do_accept_close_write(int type) {
    int fd;
    pthread_t thread;
    struct thread_args args = {-1, type, 1};

    fd = _socket(type);
    if (fd < 0) goto error;

    if (_bind(fd, type) < 0) goto error;

    if (_listen(fd, type) < 0) goto error;

    args.fd = fd;
    pthread_create(&thread, NULL, (void *)thread_delay_close_write, (void *)&args);

    _accept(fd, type);

    pthread_join(thread, NULL);

    return 0;

error:
    return -1;
}

static int do_poll_poll_poll_shutdown(int type) {
    const int MAX_T = 32;
    int fd;
    pthread_t t[MAX_T];
    int i;

    fd = _socket(type);

    for (i=0; i<MAX_T; i++)
        pthread_create(&t[i], NULL, (void *)thread_poll, (void *)fd);

    sleep(1);

    _shutdown(fd, SHUT_RDWR);

    for (i=0; i<MAX_T; i++)
        pthread_join(t[i], NULL);

    _close(fd, type);

    return 0;
}

static int do_poll_poll_poll_close(int type) {
    const int MAX_T = 32;
    int fd;
    pthread_t t[MAX_T];
    int i;

    fd = _socket(type);

    for (i=0; i<MAX_T; i++)
        pthread_create(&t[i], NULL, (void *)thread_poll, (void *)fd);

    sleep(1);

    _close(fd, type);

    for (i=0; i<MAX_T; i++)
        pthread_join(t[i], NULL);

    return 0;
}

static int do_read_read_read_close(int type) {
    const int MAX_T = 32;
    int fd;
    pthread_t t[MAX_T];
    int i;

    fd = _socket(type);

    for (i=0; i<MAX_T; i++)
        pthread_create(&t[i], NULL, (void *)thread_read, (void *)fd);

    sleep(1);

    _close(fd, type);

    for (i=0; i<MAX_T; i++)
        pthread_join(t[i], NULL);

    return 0;
}

static int do_read_read_read_shutdown(int type) {
    const int MAX_T = 32;
    int fd;
    pthread_t t[MAX_T];
    int i;

    fd = _socket(type);

    for (i=0; i<MAX_T; i++)
        pthread_create(&t[i], NULL, (void *)thread_read, (void *)fd);

    sleep(1);

    _shutdown(fd, SHUT_RDWR);

    for (i=0; i<MAX_T; i++)
        pthread_join(t[i], NULL);

    _close(fd, type);

    return 0;
}

static int do_connected_read1_shutdown1(int type) {
    int fd1, fd2;
    pthread_t t1;
    pthread_t t2;
    struct thread_args a1 = {-1, type, 0};
    struct thread_args a2 = {-1, type, 2};

    fd1 = _socket(type);
    if (fd1 < 0) goto error;

    if (_bind(fd1, type) < 0) goto error;

    if (_listen(fd1, type) < 0) goto error;

    a1.fd = fd1;
    pthread_create(&t1, NULL, (void *)thread_accept_write, (void *)&a1);

    fd2 = _socket(type);
    if (_connect(fd2, type)) goto error;

    pthread_create(&t2, NULL, (void *)thread_shutdown, (void *)&fd2);
    
    while (1) if (_read(fd2)) break;

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    return 0;

error:
    return -1;
}

// accept in one thread, connect from two different threads
static int do_accept_connect_connect(int type) {
    int fd;
    pthread_t t1;
    pthread_t t2;
    struct thread_args a1 = {-1, type, 1};
    struct thread_args a2 = {-1, type, 2};

    fd = _socket(type);
    if (fd < 0) goto error;

    if (_bind(fd, type) < 0) goto error;

    if (_listen(fd, type) < 0) goto error;

    pthread_create(&t1, NULL, (void *)thread_delay_connect, (void *)&a1);
    pthread_create(&t2, NULL, (void *)thread_delay_connect, (void *)&a2);

    _accept(fd, type);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    return 0;

error:
    return -1;
}

struct {
    char *name;
    int (*ptr)(int);
} action_table[]  = {
    {"accept_accept_accept", do_accept_accept_accept},
    {"accept_and_close", do_accept_and_close},
    {"accept_shutdown", do_accept_shutdown},
    {"connect_shutdown", do_connect_shutdown},
    {"connectnb_shutdown", do_connectnb_shutdown},
    {"connectnb_close", do_connectnb_close},
    {"accept_close_write", do_accept_close_write},
    {"accept_connect_connect", do_accept_connect_connect},
    {"poll_poll_poll_shutdown", do_poll_poll_poll_shutdown},
    {"poll_poll_poll_close", do_poll_poll_poll_close},
    {"read_read_read_shutdown", do_read_read_read_shutdown},
    {"read_read_read_close", do_read_read_read_close},
    {"connected_read1_shutdown1", do_connected_read1_shutdown1},
    {NULL, NULL},
};

struct {
    char *name;
    enum sock_type type;
} type_table[]  = {
    {"unix", UNIX},
    {"rfcomm", RFCOMM},
    {"sco", SCO},
    {"l2cap", L2CAP},
    {"tcp", TCP},
    {NULL, -1},
};

static void usage() {
    int i;

    printf("socktest TYPE ACTION\n");
    printf("\nTYPE:\n");
    for (i = 0; type_table[i].name; i++) {
        printf("\t%s\n", type_table[i].name);
    }
    printf("\nACTION:\n");
    for (i = 0; action_table[i].name; i++) {
        printf("\t%s\n", action_table[i].name);
    }
}

int main(int argc, char **argv) {
    int i;
    int type = -1;

    if (argc != 3) {
        usage();
        return -1;
    }
    for (i = 0; type_table[i].name; i++) {
        if (!strcmp(argv[1], type_table[i].name)) {
            type = type_table[i].type;
            break;
        }
    }
    if (type == -1) {
        usage();
        return -1;
    }
    for (i = 0; action_table[i].name; i++) {
        if (!strcmp(argv[2], action_table[i].name)) {
            printf("TYPE = %s ACTION = %s\n", type_table[type].name,
                    action_table[i].name);
            return (*action_table[i].ptr)(type);
        }
    }
    usage();
    return -1;
}