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>
13 static struct io_listener *l;
20 static struct io_plan closer(struct io_conn *conn, struct data *d)
26 static struct io_plan connected(struct io_conn *conn, struct data *d2)
30 return io_read(d2->buf, sizeof(d2->buf), closer, d2);
33 static void init_conn(int fd, struct data *d)
37 io_new_conn(fd, io_write(d->buf, sizeof(d->buf), closer, d));
41 static int make_listen_fd(const char *port, struct addrinfo **info)
44 struct addrinfo *addrinfo, hints;
46 memset(&hints, 0, sizeof(hints));
47 hints.ai_family = AF_UNSPEC;
48 hints.ai_socktype = SOCK_STREAM;
49 hints.ai_flags = AI_PASSIVE;
50 hints.ai_protocol = 0;
52 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
55 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
56 addrinfo->ai_protocol);
60 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
61 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
65 if (listen(fd, 1) != 0) {
75 struct data *d = malloc(sizeof(*d)), *d2 = malloc(sizeof(*d2));
76 struct addrinfo *addrinfo;
79 /* This is how many tests you plan to run */
82 memset(d->buf, 'a', sizeof(d->buf));
83 fd = make_listen_fd(PORT, &addrinfo);
85 l = io_new_listener(fd, init_conn, d);
88 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
89 addrinfo->ai_protocol);
91 ok1(io_new_conn(fd, io_connect(fd, addrinfo, connected, d2)));
93 ok1(io_loop() == NULL);
97 freeaddrinfo(addrinfo);
101 /* This exits depending on whether all tests passed */
102 return exit_status();