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.
41 #include <tr_socket.h>
44 * Open sockets on all interface addresses
46 * Uses getaddrinfo() to find all TCP addresses and opens sockets in
47 * non-blocking modes. Binds to most max_fd sockets and stores file descriptors
48 * in fd_out. Unused entries in fd_out are not modified. Returns the actual
49 * number of sockets opened.
51 * @param port port to listen on
52 * @param fd_out output array, at least max_fd long
53 * @param max_fd maximum number of file descriptors to write
54 * @return number of file descriptors written into the output array
56 ssize_t listen_on_all_addrs(unsigned int port, int *fd_out, size_t max_fd)
62 struct addrinfo *ai=NULL;
63 struct addrinfo *ai_head=NULL;
64 struct addrinfo hints={
67 .ai_socktype=SOCK_STREAM,
68 .ai_protocol=IPPROTO_TCP
73 port_str=talloc_asprintf(NULL, "%d", port);
75 tr_err("listen_on_all_addrs: unable to allocate port");
79 gai_retval = getaddrinfo(NULL, port_str, &hints, &ai_head);
80 talloc_free(port_str);
81 if (gai_retval != 0) {
82 tr_err("listen_on_all_addrs: getaddrinfo() failed (%s)", gai_strerror(gai_retval));
85 tr_debug("listen_on_all_addrs: got address info");
87 /* TODO: listen on all ports - I don't recall what this means (jlr, 4/11/2018) */
88 for (ai=ai_head,n_opened=0; (ai!=NULL)&&(n_opened<max_fd); ai=ai->ai_next) {
89 if (0 > (conn = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol))) {
90 tr_debug("listen_on_all_addrs: unable to open socket");
95 if (0!=setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)))
96 tr_debug("tids_listen: unable to set SO_REUSEADDR"); /* not fatal? */
98 if (ai->ai_family==AF_INET6) {
99 /* don't allow IPv4-mapped IPv6 addresses (per RFC4942, not sure
100 * if still relevant) */
101 if (0!=setsockopt(conn, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))) {
102 tr_debug("listen_on_all_addrs: unable to set IPV6_V6ONLY, skipping interface");
108 rc=bind(conn, ai->ai_addr, ai->ai_addrlen);
110 tr_debug("listen_on_all_addrs: unable to bind to socket");
115 if (0>listen(conn, 512)) {
116 tr_debug("listen_on_all_addrs: unable to listen on bound socket");
121 /* ok, this one worked. Save it */
122 fd_out[n_opened++]=conn;
124 freeaddrinfo(ai_head);
127 tr_debug("listen_on_all_addrs: no addresses available for listening.");
131 tr_debug("listen_on_all_addrs: monitoring interface listening on port %d on %d socket%s",
134 (n_opened==1)?"":"s");