1 #include <ccan/net/net.h>
2 #include <ccan/net/net.c>
3 #include <ccan/tap/tap.h>
9 static unsigned int server(int protocol, int type)
14 struct sockaddr_in ipv4;
15 struct sockaddr_in6 ipv6;
17 socklen_t addlen = sizeof(addr);
19 sock = socket(protocol, type, 0);
21 /* Bind to free port. */
24 /* Figure out what port it gave us. */
25 getsockname(sock, &addr.addr, &addlen);
32 fd = accept(sock, NULL, 0);
34 err(1, "Accepting from socket %i", sock);
36 ret = write(fd, "Yay!", strlen("Yay!"));
37 if (ret != strlen("Yay!"))
38 err(1, "Write returned %i", ret);
42 return ntohs(protocol == AF_INET
43 ? addr.ipv4.sin_port : addr.ipv6.sin6_port);
48 struct addrinfo *addr, *addr2;
50 struct sockaddr saddr;
51 socklen_t slen = sizeof(saddr);
56 port = server(AF_INET, SOCK_STREAM);
57 sprintf(buf, "%u", port);
59 addr = net_client_lookup("localhost", buf, AF_UNSPEC, SOCK_STREAM);
60 addr2 = net_client_lookup("ip6-localhost", buf,
61 AF_UNSPEC, SOCK_STREAM);
64 /* Join them as if they were from one lookup. */
65 addr->ai_next = addr2;
67 fd = net_connect(addr);
70 ok1(getsockname(fd, &saddr, &slen) == 0);
71 ok1(saddr.sa_family == AF_INET);
72 status = read(fd, buf, sizeof(buf));
73 ok(status == strlen("Yay!"),
74 "Read returned %i (%s)", status, strerror(errno));
75 ok1(strncmp(buf, "Yay!", strlen("Yay!")) == 0);
81 port = server(AF_INET6, SOCK_STREAM);
82 sprintf(buf, "%u", port);
84 addr = net_client_lookup("localhost", buf, AF_UNSPEC, SOCK_STREAM);
85 addr2 = net_client_lookup("ip6-localhost", buf,
86 AF_UNSPEC, SOCK_STREAM);
89 /* Join them as if they were from one lookup. */
90 addr->ai_next = addr2;
92 fd = net_connect(addr);
95 ok1(getsockname(fd, &saddr, &slen) == 0);
96 ok1(saddr.sa_family == AF_INET6);
97 status = read(fd, buf, sizeof(buf));
98 ok(status == strlen("Yay!"),
99 "Read returned %i (%s)", status, strerror(errno));
100 ok1(strncmp(buf, "Yay!", strlen("Yay!")) == 0);
102 addr->ai_next = NULL;
107 ok1(WIFEXITED(status));
108 ok1(WEXITSTATUS(status) == 0);
110 /* This exits depending on whether all tests passed */
111 return exit_status();