2 * Copyright (c) 2016-2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <poll.h> // for nfds_t
42 #include <tr_socket.h>
45 * Open sockets on all interface addresses
47 * Uses getaddrinfo() to find all TCP addresses and opens sockets in
48 * non-blocking modes. Binds to most max_fd sockets and stores file descriptors
49 * in fd_out. Unused entries in fd_out are not modified. Returns the actual
50 * number of sockets opened.
52 * @param port port to listen on
53 * @param fd_out output array, at least max_fd long
54 * @param max_fd maximum number of file descriptors to write
55 * @return number of file descriptors written into the output array
57 nfds_t tr_sock_listen_all(unsigned int port, int *fd_out, nfds_t max_fd)
63 struct addrinfo *ai=NULL;
64 struct addrinfo *ai_head=NULL;
65 struct addrinfo hints={
68 .ai_socktype=SOCK_STREAM,
69 .ai_protocol=IPPROTO_TCP
74 port_str=talloc_asprintf(NULL, "%d", port);
76 tr_err("tr_sock_listen_all: unable to allocate port");
80 gai_retval = getaddrinfo(NULL, port_str, &hints, &ai_head);
81 talloc_free(port_str);
82 if (gai_retval != 0) {
83 tr_err("tr_sock_listen_all: getaddrinfo() failed (%s)", gai_strerror(gai_retval));
86 tr_debug("tr_sock_listen_all: got address info");
88 /* TODO: listen on all ports - I don't recall what this means (jlr, 4/11/2018) */
89 for (ai=ai_head,n_opened=0; (ai!=NULL)&&(n_opened<max_fd); ai=ai->ai_next) {
90 if (0 > (conn = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol))) {
91 tr_debug("tr_sock_listen_all: unable to open socket");
96 if (0!=setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)))
97 tr_debug("tids_listen: unable to set SO_REUSEADDR"); /* not fatal? */
99 if (ai->ai_family==AF_INET6) {
100 /* don't allow IPv4-mapped IPv6 addresses (per RFC4942, not sure
101 * if still relevant) */
102 if (0!=setsockopt(conn, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))) {
103 tr_debug("tr_sock_listen_all: unable to set IPV6_V6ONLY, skipping interface");
109 rc=bind(conn, ai->ai_addr, ai->ai_addrlen);
111 tr_debug("tr_sock_listen_all: unable to bind to socket");
116 if (0>listen(conn, 512)) {
117 tr_debug("tr_sock_listen_all: unable to listen on bound socket");
122 /* ok, this one worked. Save it */
123 fd_out[n_opened++]=conn;
125 freeaddrinfo(ai_head);
128 tr_debug("tr_sock_listen_all: no addresses available for listening.");
132 tr_debug("tr_sock_listen_all: listening on port %d on %d socket%s",
135 (n_opened==1)?"":"s");