-#include <string.h>
#include "config.h"
+#include <stdio.h>
+#include <string.h>
/**
- * net - simple IPv4/IPv6 client library
+ * net - simple IPv4/IPv6 socket library
*
- * This code makes it simple to support IPv4 and IPv6 without speed penalty.
+ * This code makes it simple to support IPv4 and IPv6 without speed penalty
+ * in clients by using non-blocking simultaneous connect, and using
+ * a convenience function to create both IPv4 and IPv6 sockets for servers.
*
* License: MIT
*
*
* Example:
* #include <ccan/net/net.h>
- * #include <sys/types.h>
- * #include <sys/socket.h>
+ * #include <netinet/in.h>
* #include <stdio.h>
* #include <err.h>
*
* struct addrinfo *addr;
* const char *dest, *port;
* int fd;
- * struct sockaddr saddr;
- * socklen_t slen = sizeof(saddr);
+ * union {
+ * struct sockaddr s;
+ * struct sockaddr_in v4;
+ * struct sockaddr_in6 v6;
+ * } u;
+ * socklen_t slen = sizeof(u);
*
* if (argc == 2) {
* dest = argv[1];
* fd = net_connect(addr);
* if (fd < 0)
* err(1, "Failed to connect to %s", dest);
+ * freeaddrinfo(addr);
*
- * if (getsockname(fd, &saddr, &slen) == 0)
+ * if (getsockname(fd, &u.s, &slen) == 0)
* printf("Connected via %s\n",
- * saddr.sa_family == AF_INET6 ? "IPv6"
- * : saddr.sa_family == AF_INET ? "IPv4"
+ * u.s.sa_family == AF_INET6 ? "IPv6"
+ * : u.s.sa_family == AF_INET ? "IPv4"
* : "UNKNOWN??");
* else
* err(1, "Failed to get socket type for connection");