summaryrefslogtreecommitdiffstats
path: root/src/util/inet_listen.c
blob: 31800cdc3793a5811cf0f89391f6e8e93c7e47c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*++
/* NAME
/*	inet_listen 3
/* SUMMARY
/*	start TCP listener
/* SYNOPSIS
/*	#include <listen.h>
/*
/*	int	inet_windowsize;
/*
/*	int	inet_listen(addr, backlog, block_mode)
/*	const char *addr;
/*	int	backlog;
/*	int	block_mode;
/*
/*	int	inet_accept(fd)
/*	int	fd;
/* DESCRIPTION
/*	The \fBinet_listen\fR routine starts a TCP listener
/*	on the specified address, with the specified backlog, and returns
/*	the resulting file descriptor.
/*
/*	inet_accept() accepts a connection and sanitizes error results.
/*
/*	Specify an inet_windowsize value > 0 to override the TCP
/*	window size that the server advertises to the client.
/*
/*	Arguments:
/* .IP addr
/*	The communication endpoint to listen on. The syntax is "host:port".
/*	Host and port may be specified in symbolic form or numerically.
/*	A null host field means listen on all network interfaces.
/* .IP backlog
/*	This argument is passed on to the \fIlisten(2)\fR routine.
/* .IP block_mode
/*	Either NON_BLOCKING for a non-blocking socket, or BLOCKING for
/*	blocking mode.
/* .IP fd
/*	File descriptor returned by inet_listen().
/* DIAGNOSTICS
/*	Fatal errors: inet_listen() aborts upon any system call failure.
/*	inet_accept() leaves all error handling up to the caller.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System libraries. */

#include <sys_defs.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifndef MAXHOSTNAMELEN
#include <sys/param.h>
#endif
#include <errno.h>
#include <string.h>
#include <unistd.h>

/* Utility library. */

#include "mymalloc.h"
#include "msg.h"
#include "host_port.h"
#include "iostuff.h"
#include "listen.h"
#include "sane_accept.h"
#include "myaddrinfo.h"
#include "sock_addr.h"
#include "inet_proto.h"

/* inet_listen - create TCP listener */

int     inet_listen(const char *addr, int backlog, int block_mode)
{
    struct addrinfo *res;
    struct addrinfo *res0;
    int     aierr;
    int     sock;
    int     on = 1;
    char   *buf;
    char   *host;
    char   *port;
    const char *parse_err;
    MAI_HOSTADDR_STR hostaddr;
    MAI_SERVPORT_STR portnum;
    const INET_PROTO_INFO *proto_info;

    /*
     * Translate address information to internal form.
     */
    buf = mystrdup(addr);
    if ((parse_err = host_port(buf, &host, "", &port, (char *) 0)) != 0)
	msg_fatal("%s: %s", addr, parse_err);
    if (*host == 0)
	host = 0;
    if ((aierr = hostname_to_sockaddr(host, port, SOCK_STREAM, &res0)) != 0)
	msg_fatal("%s: %s", addr, MAI_STRERROR(aierr));
    myfree(buf);
    /* No early returns or res0 leaks. */

    proto_info = inet_proto_info();
    for (res = res0; /* see below */ ; res = res->ai_next) {

	/*
	 * No usable address found.
	 */
	if (res == 0)
	    msg_fatal("%s: host found but no usable address", addr);

	/*
	 * Safety net.
	 */
	if (strchr((char *) proto_info->sa_family_list, res->ai_family) != 0)
	    break;

	msg_info("skipping address family %d for %s", res->ai_family, addr);
    }

    /*
     * Show what address we're trying.
     */
    if (msg_verbose) {
	SOCKADDR_TO_HOSTADDR(res->ai_addr, res->ai_addrlen,
			     &hostaddr, &portnum, 0);
	msg_info("trying... [%s]:%s", hostaddr.buf, portnum.buf);
    }

    /*
     * Create a listener socket.
     */
    if ((sock = socket(res->ai_family, res->ai_socktype, 0)) < 0)
	msg_fatal("socket: %m");
#ifdef HAS_IPV6
#if defined(IPV6_V6ONLY) && !defined(BROKEN_AI_PASSIVE_NULL_HOST)
    if (res->ai_family == AF_INET6
	&& setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
		      (void *) &on, sizeof(on)) < 0)
	msg_fatal("setsockopt(IPV6_V6ONLY): %m");
#endif
#endif
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
		   (void *) &on, sizeof(on)) < 0)
	msg_fatal("setsockopt(SO_REUSEADDR): %m");
#if defined(SO_REUSEPORT_LB)
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT_LB,
		   (void *) &on, sizeof(on)) < 0)
	msg_fatal("setsockopt(SO_REUSEPORT_LB): %m");
#elif defined(SO_REUSEPORT)
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
		   (void *) &on, sizeof(on)) < 0)
	msg_fatal("setsockopt(SO_REUSEPORT): %m");
#endif
    if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
	SOCKADDR_TO_HOSTADDR(res->ai_addr, res->ai_addrlen,
			     &hostaddr, &portnum, 0);
	msg_fatal("bind %s port %s: %m", hostaddr.buf, portnum.buf);
    }
    freeaddrinfo(res0);
    non_blocking(sock, block_mode);
    if (inet_windowsize > 0)
	set_inet_windowsize(sock, inet_windowsize);
    if (listen(sock, backlog) < 0)
	msg_fatal("listen: %m");
    return (sock);
}

/* inet_accept - accept connection */

int     inet_accept(int fd)
{
    struct sockaddr_storage ss;
    SOCKADDR_SIZE ss_len = sizeof(ss);

    return (sane_accept(fd, (struct sockaddr *) &ss, &ss_len));
}