]> git.pond.sub.org Git - empserver/blob - src/lib/gen/tcp_listen.c
Fix wildcard bind to at least bind IPv4 or else IPv6 on OpenBSD
[empserver] / src / lib / gen / tcp_listen.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  tcp_listen.c: Create a socket and listen on it
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2005-2010
31  */
32
33 #include <config.h>
34
35 #include <assert.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <netdb.h>
46 #include <unistd.h>
47 #include "prototypes.h"
48
49 /* Portability cruft, should become unnecessary eventually */
50 #ifndef AI_ADDRCONFIG
51 #define AI_ADDRCONFIG 0
52 #endif
53
54 static void cant_listen(char *, char *, const char *)
55     ATTRIBUTE((noreturn));
56
57 int
58 tcp_listen(char *host, char *serv, size_t *addrlenp)
59 {
60     int fd;
61     int on = 1;
62 #ifdef HAVE_GETADDRINFO
63     /*
64      * Inspired by example code from W. Richard Stevens: UNIX Network
65      * Programming, Vol. 1
66      */
67     int err;
68     struct addrinfo hints, *first_ai, *ai;
69     /* Crap necessary for OpenBSD, see below */
70     int try_v6only_off = 1, v6only_stuck = 0;
71
72     memset(&hints, 0, sizeof(struct addrinfo));
73     hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
74     hints.ai_family = AF_UNSPEC;
75     hints.ai_socktype = SOCK_STREAM;
76
77     if ((err = getaddrinfo(host, serv, &hints, &first_ai)) != 0)
78         cant_listen(host, serv, gai_strerror(err));
79     assert(first_ai);
80
81 again:
82     for (ai = first_ai; ai; ai = ai->ai_next) {
83         fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
84         if (fd < 0)
85             continue;           /* error, try next one */
86
87 #ifdef IPV6_V6ONLY
88         if (ai->ai_family == AF_INET6 && try_v6only_off) {
89             int off = 0;
90
91             if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
92                            &off, sizeof(off)) < 0) {
93                 /*
94                  * IPV6_V6ONLY is stuck on, violating RFC 3493 (gee,
95                  * thanks, OpenBSD!).  Address is good only for IPv6,
96                  * not for IPv4.  Means we can't have both on this
97                  * system.  Continue looking for one that's good for
98                  * IPv4.
99                  */
100                 v6only_stuck = 1;
101                 continue;
102             }
103         }
104 #else
105         (void)try_v6only_off;
106 #endif
107
108         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
109         if (bind(fd, ai->ai_addr, ai->ai_addrlen) == 0)
110             break;              /* success */
111
112         close(fd);              /* error, close and try next one */
113     }
114
115     /* More crap for OpenBSD */
116     if (ai == NULL && v6only_stuck) {
117         /*
118          * No go.  But we skipped IPv6 addresses that don't work for
119          * IPv4, but could for IPv6.  Try again without skipping
120          * these.
121          */
122         try_v6only_off = 0;
123         goto again;
124     }
125
126     if (ai == NULL)          /* errno from final socket() or bind() */
127         cant_listen(host, serv, strerror(errno));
128
129     if (listen(fd, SOMAXCONN) < 0)
130         cant_listen(host, serv, strerror(errno));
131
132     if (addrlenp)
133         *addrlenp = ai->ai_addrlen;
134
135     freeaddrinfo(first_ai);
136
137 #else  /* !HAVE_GETADDRINFO */
138     struct sockaddr_in sin;
139     struct hostent *hp;
140     struct servent *sp;
141
142     memset(&sin, 0, sizeof(sin));
143     sin.sin_family = AF_INET;
144     if (!host)
145         sin.sin_addr.s_addr = htonl(INADDR_ANY);
146     else if (isdigit(*host))
147         sin.sin_addr.s_addr = inet_addr(host);
148     else {
149         hp = gethostbyname(host);
150         if (!hp)
151             cant_listen(host, serv, strerror(errno));
152         memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
153     }
154     if (isdigit(*serv))
155         sin.sin_port = htons(atoi(serv));
156     else {
157         sp = getservbyname(serv, "tcp");
158         if (!sp)
159             cant_listen(host, serv, strerror(errno));
160         sin.sin_port = sp->s_port;
161     }
162     if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
163         cant_listen(host, serv, strerror(errno));
164     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
165     if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
166         cant_listen(host, serv, strerror(errno));
167     if (listen(fd, SOMAXCONN) < 0)
168         cant_listen(host, serv, strerror(errno));
169
170     if (addrlenp)
171         *addrlenp = sizeof(struct sockaddr_in);
172
173 #endif /* !HAVE_GETADDRINFO */
174
175     return fd;
176 }
177
178 static void
179 cant_listen(char *host, char *serv, const char *err)
180 {
181     fprintf(stderr, "Can't listen on %s%s%s: %s\n",
182             host ? host : "", host ? ":" : "", serv, err);
183     exit(1);
184 }