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>
46 * Open sockets on all interface addresses
48 * Uses getaddrinfo() to find all TCP addresses and opens sockets in
49 * non-blocking modes. Binds to most max_fd sockets and stores file descriptors
50 * in fd_out. Unused entries in fd_out are not modified. Returns the actual
51 * number of sockets opened.
53 * @param port port to listen on
54 * @param fd_out output array, at least max_fd long
55 * @param max_fd maximum number of file descriptors to write
56 * @return number of file descriptors written into the output array
58 nfds_t tr_sock_listen_all(int port, int *fd_out, nfds_t max_fd)
64 struct addrinfo *ai=NULL;
65 struct addrinfo *ai_head=NULL;
66 struct addrinfo hints={
69 .ai_socktype=SOCK_STREAM,
70 .ai_protocol=IPPROTO_TCP
75 port_str=talloc_asprintf(NULL, "%d", port);
77 tr_err("tr_sock_listen_all: unable to allocate port");
81 gai_retval = getaddrinfo(NULL, port_str, &hints, &ai_head);
82 talloc_free(port_str);
83 if (gai_retval != 0) {
84 tr_err("tr_sock_listen_all: getaddrinfo() failed (%s)", gai_strerror(gai_retval));
87 tr_debug("tr_sock_listen_all: got address info");
89 /* TODO: listen on all ports - I don't recall what this means (jlr, 4/11/2018) */
90 for (ai=ai_head,n_opened=0; (ai!=NULL)&&(n_opened<max_fd); ai=ai->ai_next) {
91 if (0 > (conn = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol))) {
92 tr_debug("tr_sock_listen_all: unable to open socket");
97 if (0!=setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)))
98 tr_debug("tids_listen: unable to set SO_REUSEADDR"); /* not fatal? */
100 if (ai->ai_family==AF_INET6) {
101 /* don't allow IPv4-mapped IPv6 addresses (per RFC4942, not sure
102 * if still relevant) */
103 if (0!=setsockopt(conn, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))) {
104 tr_debug("tr_sock_listen_all: unable to set IPV6_V6ONLY, skipping interface");
110 rc=bind(conn, ai->ai_addr, ai->ai_addrlen);
112 tr_debug("tr_sock_listen_all: unable to bind to socket");
117 if (0>listen(conn, 512)) {
118 tr_debug("tr_sock_listen_all: unable to listen on bound socket");
123 /* ok, this one worked. Save it */
124 fd_out[n_opened++]=conn;
126 freeaddrinfo(ai_head);
129 tr_debug("tr_sock_listen_all: no addresses available for listening.");
133 tr_debug("tr_sock_listen_all: listening on port %d on %d socket%s",
136 (n_opened==1)?"":"s");
142 * Extract a string-formatted socket address from a struct sockaddr
145 * @param dst pointer to allocated space of at least INET6_ADDRSLEN bytes
146 * @param dst_len size of space allocated at dst
147 * @return pointer to dst or null on error
149 static const char *tr_sock_ip_address(struct sockaddr *s, char *dst, size_t dst_len)
151 switch (s->sa_family) {
154 &(((struct sockaddr_in *)s)->sin_addr),
156 (socklen_t) dst_len);
161 &(((struct sockaddr_in6 *)s)->sin6_addr),
163 (socklen_t) dst_len);
167 snprintf(dst, dst_len, "addr family %u", s->sa_family);
175 * Accept a socket connection
178 * @return -1 on error, connection fd on success
180 int tr_sock_accept(int sock)
183 struct sockaddr_storage peeraddr;
184 socklen_t addr_len = sizeof(peeraddr);
185 char peeraddr_string[INET6_ADDRSTRLEN];
188 if (0 > (conn = accept(sock, (struct sockaddr *)&(peeraddr), &addr_len))) {
189 if (strerror_r(errno, err, sizeof(err)))
190 snprintf(err, sizeof(err), "errno = %d", errno);
191 tr_debug("tr_sock_accept: Unable to accept connection: %s", err);
193 tr_info("tr_sock_accept: Incoming connection on fd %d from %s",
195 tr_sock_ip_address((struct sockaddr *)&peeraddr,
197 sizeof(peeraddr_string)));