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