1 #include <ccan/net/net.h>
2 #include <ccan/net/net.c>
3 #include <ccan/tap/tap.h>
8 static unsigned int server(int protocol, int type)
13 struct sockaddr_in ipv4;
14 struct sockaddr_in6 ipv6;
16 socklen_t addlen = sizeof(addr);
18 sock = socket(protocol, type, 0);
20 /* Bind to free port. */
23 /* Figure out what port it gave us. */
24 getsockname(sock, &addr.addr, &addlen);
31 fd = accept(sock, NULL, 0);
33 err(1, "Accepting from socket %i", sock);
35 ret = write(fd, "Yay!", strlen("Yay!"));
36 if (ret != strlen("Yay!"))
37 err(1, "Write returned %i", ret);
41 return ntohs(protocol == AF_INET
42 ? addr.ipv4.sin_port : addr.ipv6.sin6_port);
47 struct addrinfo *addr, *addr2;
49 struct sockaddr saddr;
50 socklen_t slen = sizeof(saddr);
55 port = server(AF_INET, SOCK_STREAM);
56 sprintf(buf, "%u", port);
58 addr = net_client_lookup("localhost", buf, AF_UNSPEC, SOCK_STREAM);
59 addr2 = net_client_lookup("ip6-localhost", buf,
60 AF_UNSPEC, SOCK_STREAM);
63 /* Join them as if they were from one lookup. */
64 addr->ai_next = addr2;
66 fd = net_connect(addr);
69 ok1(getsockname(fd, &saddr, &slen) == 0);
70 ok1(saddr.sa_family == AF_INET);
71 status = read(fd, buf, sizeof(buf));
72 ok(status == strlen("Yay!"),
73 "Read returned %i (%s)", status, strerror(errno));
74 ok1(strncmp(buf, "Yay!", strlen("Yay!")) == 0);
80 port = server(AF_INET6, SOCK_STREAM);
81 sprintf(buf, "%u", port);
83 addr = net_client_lookup("localhost", buf, AF_UNSPEC, SOCK_STREAM);
84 addr2 = net_client_lookup("ip6-localhost", buf,
85 AF_UNSPEC, SOCK_STREAM);
88 /* Join them as if they were from one lookup. */
89 addr->ai_next = addr2;
91 fd = net_connect(addr);
94 ok1(getsockname(fd, &saddr, &slen) == 0);
95 ok1(saddr.sa_family == AF_INET6);
96 status = read(fd, buf, sizeof(buf));
97 ok(status == strlen("Yay!"),
98 "Read returned %i (%s)", status, strerror(errno));
99 ok1(strncmp(buf, "Yay!", strlen("Yay!")) == 0);
101 addr->ai_next = NULL;
106 ok1(WIFEXITED(status));
107 ok1(WEXITSTATUS(status) == 0);
109 /* This exits depending on whether all tests passed */
110 return exit_status();