2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * tcp_listen.c: Create a socket and listen on it
30 * Known contributors to this file:
31 * Markus Armbruster, 2005
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 static void cant_listen(char *, char *, const char *);
52 tcp_listen(char *host, char *serv, size_t *addrlenp)
56 #ifdef HAVE_GETADDRINFO
58 * Inspired by example code from W. Richard Stevens: UNIX Network
62 struct addrinfo hints, *res, *ressave;
64 memset(&hints, 0, sizeof(struct addrinfo));
65 hints.ai_flags = AI_PASSIVE;
66 hints.ai_family = AF_UNSPEC;
67 hints.ai_socktype = SOCK_STREAM;
69 if ((n = getaddrinfo(host, serv, &hints, &res)) != 0)
70 cant_listen(host, serv, gai_strerror(n));
74 fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
76 continue; /* error, try next one */
78 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
79 cant_listen(host, serv, strerror(errno));
80 if (bind(fd, res->ai_addr, res->ai_addrlen) == 0)
83 close(fd); /* error, close and try next one */
84 } while ((res = res->ai_next) != NULL);
86 if (res == NULL) /* errno from final socket() or bind() */
87 cant_listen(host, serv, strerror(errno));
89 if (listen(fd, SOMAXCONN) < 0)
90 cant_listen(host, serv, strerror(errno));
93 *addrlenp = res->ai_addrlen;
95 freeaddrinfo(ressave);
97 #else /* !HAVE_GETADDRINFO */
98 struct sockaddr_in sin;
102 memset(&sin, 0, sizeof(sin));
103 sin.sin_family = AF_INET;
105 sin.sin_addr.s_addr = htonl(INADDR_ANY);
106 else if (isdigit(*host))
107 sin.sin_addr.s_addr = inet_addr(host);
109 hp = gethostbyname(host);
111 cant_listen(host, serv, strerror(errno));
112 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
115 sin.sin_port = htons(atoi(serv));
117 sp = getservbyname(serv, "tcp");
119 cant_listen(host, serv, strerror(errno));
120 sin.sin_port = sp->s_port;
122 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
123 cant_listen(host, serv, strerror(errno));
124 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
125 cant_listen(host, serv, strerror(errno));
126 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
127 cant_listen(host, serv, strerror(errno));
128 if (listen(fd, SOMAXCONN) < 0)
129 cant_listen(host, serv, strerror(errno));
132 *addrlenp = sizeof(struct sockaddr_in);
134 #endif /* !HAVE_GETADDRINFO */
140 cant_listen(char *host, char *serv, const char *err)
142 fprintf(stderr, "Can't listen on %s%s%s: %s\n",
143 host ? host : "", host ? ":" : "", serv, err);