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
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.
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.
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/>.
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.
27 * tcp_listen.c: Create a socket and listen on it
29 * Known contributors to this file:
30 * Markus Armbruster, 2005-2010
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include "prototypes.h"
49 /* Portability cruft, should become unnecessary eventually */
51 #define AI_ADDRCONFIG 0
54 static void cant_listen(char *, char *, const char *)
55 ATTRIBUTE((noreturn));
58 tcp_listen(char *host, char *serv, size_t *addrlenp)
62 #ifdef HAVE_GETADDRINFO
64 * Inspired by example code from W. Richard Stevens: UNIX Network
68 struct addrinfo hints, *first_ai, *ai;
70 memset(&hints, 0, sizeof(struct addrinfo));
71 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
72 hints.ai_family = AF_UNSPEC;
73 hints.ai_socktype = SOCK_STREAM;
75 if ((err = getaddrinfo(host, serv, &hints, &first_ai)) != 0)
76 cant_listen(host, serv, gai_strerror(err));
79 for (ai = first_ai; ai; ai = ai->ai_next) {
80 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
82 continue; /* error, try next one */
85 if (ai->ai_family == AF_INET6) {
88 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
92 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
93 if (bind(fd, ai->ai_addr, ai->ai_addrlen) == 0)
96 close(fd); /* error, close and try next one */
99 if (ai == NULL) /* errno from final socket() or bind() */
100 cant_listen(host, serv, strerror(errno));
102 if (listen(fd, SOMAXCONN) < 0)
103 cant_listen(host, serv, strerror(errno));
106 *addrlenp = ai->ai_addrlen;
108 freeaddrinfo(first_ai);
110 #else /* !HAVE_GETADDRINFO */
111 struct sockaddr_in sin;
115 memset(&sin, 0, sizeof(sin));
116 sin.sin_family = AF_INET;
118 sin.sin_addr.s_addr = htonl(INADDR_ANY);
119 else if (isdigit(*host))
120 sin.sin_addr.s_addr = inet_addr(host);
122 hp = gethostbyname(host);
124 cant_listen(host, serv, strerror(errno));
125 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
128 sin.sin_port = htons(atoi(serv));
130 sp = getservbyname(serv, "tcp");
132 cant_listen(host, serv, strerror(errno));
133 sin.sin_port = sp->s_port;
135 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
136 cant_listen(host, serv, strerror(errno));
137 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
138 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
139 cant_listen(host, serv, strerror(errno));
140 if (listen(fd, SOMAXCONN) < 0)
141 cant_listen(host, serv, strerror(errno));
144 *addrlenp = sizeof(struct sockaddr_in);
146 #endif /* !HAVE_GETADDRINFO */
152 cant_listen(char *host, char *serv, const char *err)
154 fprintf(stderr, "Can't listen on %s%s%s: %s\n",
155 host ? host : "", host ? ":" : "", serv, err);