]> git.ozlabs.org Git - ccan/blob - ccan/net/test/run.c
955550bcf8f88b010aa89dac8479aa5d1143e777
[ccan] / ccan / net / test / run.c
1 #include <ccan/net/net.h>
2 #include <ccan/net/net.c>
3 #include <ccan/tap/tap.h>
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <stdio.h>
7 #include <err.h>
8
9 static unsigned int server(int protocol, int type)
10 {
11         int sock;
12         union {
13                 struct sockaddr addr;
14                 struct sockaddr_in ipv4;
15                 struct sockaddr_in6 ipv6;
16         } addr;
17         socklen_t addlen = sizeof(addr);
18
19         sock = socket(protocol, type, 0);
20
21         /* Bind to free port. */
22         listen(sock, 0);
23
24         /* Figure out what port it gave us. */
25         getsockname(sock, &addr.addr, &addlen);
26         fflush(stdout);
27
28         if (fork() == 0) {
29                 int ret, fd;
30
31                 alarm(3);
32                 fd = accept(sock, NULL, 0);
33                 if (fd < 0)
34                         err(1, "Accepting from socket %i", sock);
35
36                 ret = write(fd, "Yay!", strlen("Yay!"));
37                 if (ret != strlen("Yay!"))
38                         err(1, "Write returned %i", ret);
39                 exit(0);
40         }
41         close(sock);
42         return ntohs(protocol == AF_INET
43                      ? addr.ipv4.sin_port : addr.ipv6.sin6_port);
44 }
45
46 static bool we_faked_double = false;
47
48 /* Get a localhost on ipv4 and IPv6.  Fake it if we have to. */
49 static struct addrinfo* double_addr_lookup(char* buf)
50 {
51         struct addrinfo *addr, *addr2;
52
53         addr = net_client_lookup("localhost", buf, AF_UNSPEC, SOCK_STREAM);
54         if (!addr)
55                 return addr;
56
57         /* If we only got one, we need to fake up the other one. */
58         if (addr->ai_next) {
59                 addr2 = addr->ai_next;
60         } else {
61                 we_faked_double = true;
62
63                 /* OK, IPv4 only? */
64                 if (addr->ai_family == AF_INET) {
65                         /* These are the names I found on my Ubuntu system. */
66                         addr2 = net_client_lookup("ip6-localhost", buf,
67                                                   AF_UNSPEC, SOCK_STREAM);
68                         if (!addr2)
69                                 addr2 = net_client_lookup("localhost6", buf,
70                                                           AF_UNSPEC,
71                                                           SOCK_STREAM);
72                 } else if (addr->ai_family == AF_INET6)
73                         /* IPv6 only?  This is a guess... */
74                         addr2 = net_client_lookup("ip4-localhost", buf,
75                                                   AF_UNSPEC, SOCK_STREAM);
76                 if (!addr2)
77                         return NULL;
78                 addr->ai_next = addr2;
79         }
80
81         /* More than two? */
82         if (addr2->ai_next)
83                 return NULL;
84         /* One IPv4 and one IPv6? */
85         if (addr->ai_family == AF_INET && addr2->ai_family == AF_INET6)
86                 return addr;
87         if (addr->ai_family == AF_INET6 && addr2->ai_family == AF_INET)
88                 return addr;
89         return NULL;
90 }
91
92 static void double_addr_free(struct addrinfo* addr)
93 {
94         struct addrinfo *addr2;
95         if (we_faked_double) {
96                 addr2 = addr->ai_next;
97                 addr->ai_next = NULL;
98         }
99         freeaddrinfo(addr);
100         if (we_faked_double)
101                 freeaddrinfo(addr2);
102 }
103
104 int main(void)
105 {
106         struct addrinfo *addr;
107         int fd, status;
108         struct sockaddr saddr;
109         socklen_t slen = sizeof(saddr);
110         char buf[20];
111         unsigned int port;
112
113         plan_tests(14);
114         port = server(AF_INET, SOCK_STREAM);
115         sprintf(buf, "%u", port);
116
117         addr = double_addr_lookup(buf);
118         ok1(addr);
119         fd = net_connect(addr);
120         ok1(fd >= 0);
121
122         ok1(getsockname(fd, &saddr, &slen) == 0);
123         diag("family = %d", saddr.sa_family);
124         ok1(saddr.sa_family == AF_INET);
125         status = read(fd, buf, sizeof(buf));
126         ok(status == strlen("Yay!"),
127            "Read returned %i (%s)", status, strerror(errno));
128         ok1(strncmp(buf, "Yay!", strlen("Yay!")) == 0);
129         close(fd);
130         double_addr_free(addr);
131
132         port = server(AF_INET6, SOCK_STREAM);
133         sprintf(buf, "%u", port);
134
135         addr = double_addr_lookup(buf);
136         ok1(addr);
137         fd = net_connect(addr);
138         ok1(fd >= 0);
139
140         ok1(getsockname(fd, &saddr, &slen) == 0);
141         ok1(saddr.sa_family == AF_INET6);
142         status = read(fd, buf, sizeof(buf));
143         ok(status == strlen("Yay!"),
144            "Read returned %i (%s)", status, strerror(errno));
145         ok1(strncmp(buf, "Yay!", strlen("Yay!")) == 0);
146         close(fd);
147         double_addr_free(addr);
148
149         wait(&status);
150         ok1(WIFEXITED(status));
151         ok1(WEXITSTATUS(status) == 0);
152
153         /* This exits depending on whether all tests passed */
154         return exit_status();
155 }