X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fnet%2Fnet.h;h=9ad6de56a498b12aab9a5372c9114b5def47b505;hp=a336281ef609417ca02b5c758534cefdb81f5664;hb=6abc867fce8c3eba40eb26092d31e34b47dd9165;hpb=74257cee33ae3033f961d5f22a0313b8cb1b18d4 diff --git a/ccan/net/net.h b/ccan/net/net.h index a336281e..9ad6de56 100644 --- a/ccan/net/net.h +++ b/ccan/net/net.h @@ -47,4 +47,55 @@ struct addrinfo *net_client_lookup(const char *hostname, * freeaddrinfo(addr); */ int net_connect(const struct addrinfo *addrinfo); + +/** + * net_server_lookup - look up a service name to bind to. + * @service: the service to look up + * @family: Usually AF_UNSPEC, otherwise AF_INET or AF_INET6. + * @socktype: SOCK_DGRAM or SOCK_STREAM. + * + * This will do a synchronous lookup of a given name, returning a linked list + * of results, or NULL on error. You should use freeaddrinfo() to free it. + * + * Example: + * #include + * #include + * #include + * #include + * #include + * ... + * struct addrinfo *addr; + * + * // Get address(es) to bind for our service. + * addr = net_server_lookup("8888", AF_UNSPEC, SOCK_STREAM); + * if (!addr) + * errx(1, "Failed to look up 8888 to bind to"); + */ +struct addrinfo *net_server_lookup(const char *service, + int family, + int socktype); + +/** + * net_bind - create listening socket(s) + * @addrinfo: the address(es) to bind to. + * @fds: array of two fds. + * + * This will create one (or if necessary) two sockets, mark them + * SO_REUSEADDR, bind them to the given address(es), and make them + * listen() (if the socket type is SOCK_STREAM or SOCK_SEQPACKET). + * + * Returns -1 (and sets errno) on error, or 1 or 2 depending on how many + * @fds are valid. + * + * Example: + * int fds[2], i, num_fds; + * + * num_fds = net_bind(addr, fds); + * if (num_fds < 0) + * err(1, "Failed to listen on port 8888"); + * + * for (i = 0; i < num_fds; i++) + * printf(" Got fd %u/%u: %i\n", i, num_fds, fds[i]); + */ +int net_bind(const struct addrinfo *addrinfo, int fds[2]); #endif /* CCAN_NET_H */