]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-09-connect.c
io: update to use time_mono() for timers.
[ccan] / ccan / io / test / run-09-connect.c
1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
6 #include <sys/wait.h>
7 #include <stdio.h>
8
9 #ifdef DEBUG_CONN
10 #define PORT "64009"
11 #else
12 #define PORT "65009"
13 #endif
14
15 static struct io_listener *l;
16 static struct data *d2;
17
18 struct data {
19         int state;
20         char buf[10];
21 };
22
23 static struct io_plan *closer(struct io_conn *conn, struct data *d)
24 {
25         d->state++;
26         return io_close(conn);
27 }
28
29 static struct io_plan *connected(struct io_conn *conn, struct data *d2)
30 {
31         ok1(d2->state == 0);
32         d2->state++;
33         return io_read(conn, d2->buf, sizeof(d2->buf), closer, d2);
34 }
35
36 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
37 {
38 #ifdef DEBUG_CONN
39         io_set_debug(conn, true);
40 #endif
41         ok1(d->state == 0);
42         d->state++;
43         io_close_listener(l);
44
45         return io_write(conn, d->buf, sizeof(d->buf), closer, d);
46 }
47
48 static int make_listen_fd(const char *port, struct addrinfo **info)
49 {
50         int fd, on = 1;
51         struct addrinfo *addrinfo, hints;
52
53         memset(&hints, 0, sizeof(hints));
54         hints.ai_family = AF_UNSPEC;
55         hints.ai_socktype = SOCK_STREAM;
56         hints.ai_flags = AI_PASSIVE;
57         hints.ai_protocol = 0;
58
59         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
60                 return -1;
61
62         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
63                     addrinfo->ai_protocol);
64         if (fd < 0)
65                 return -1;
66
67         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
68         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
69                 close(fd);
70                 return -1;
71         }
72         if (listen(fd, 1) != 0) {
73                 close(fd);
74                 return -1;
75         }
76         *info = addrinfo;
77         return fd;
78 }
79
80 static struct io_plan *setup_connect(struct io_conn *conn,
81                                      struct addrinfo *addrinfo)
82 {
83         d2 = malloc(sizeof(*d2));
84         d2->state = 0;
85         return io_connect(conn, addrinfo, connected, d2);
86 }
87
88 int main(void)
89 {
90         struct data *d = malloc(sizeof(*d));
91         struct addrinfo *addrinfo;
92         int fd;
93
94         /* This is how many tests you plan to run */
95         plan_tests(8);
96         d->state = 0;
97         memset(d->buf, 'a', sizeof(d->buf));
98         fd = make_listen_fd(PORT, &addrinfo);
99         ok1(fd >= 0);
100         l = io_new_listener(NULL, fd, init_conn, d);
101         ok1(l);
102
103         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
104                     addrinfo->ai_protocol);
105         ok1(io_new_conn(NULL, fd, setup_connect, addrinfo));
106
107         ok1(io_loop(NULL, NULL) == NULL);
108         ok1(d->state == 2);
109         ok1(d2->state == 2);
110
111         freeaddrinfo(addrinfo);
112         free(d);
113         free(d2);
114
115         /* This exits depending on whether all tests passed */
116         return exit_status();
117 }