]> git.pond.sub.org Git - empserver/blob - src/lib/gen/tcp_listen.c
Break inclusion cycle: prototypes.h and commands.h included each
[empserver] / src / lib / gen / tcp_listen.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  tcp_listen.c: Create a socket and listen on it
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2005
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <sys/types.h>
40 #ifdef _WIN32
41 #define WIN32
42 #include "winsock2.h"
43 #undef NS_ALL
44 #else
45 #include <arpa/inet.h>
46 #include <netdb.h>
47 #include <netinet/in.h>
48 #include <sys/socket.h>
49 #include <unistd.h>
50 #endif
51 #include "prototypes.h"
52
53 static int cant_listen(char *, char *, const char *);
54
55 int
56 tcp_listen(char *host, char *serv, size_t *addrlenp)
57 {
58     int fd;
59     int on = 1;
60 #ifdef HAVE_GETADDRINFO
61     /*
62      * Inspired by example code from W. Richard Stevens: UNIX Network
63      * Programming, Vol. 1
64      */
65     int n;
66     struct addrinfo hints, *res, *ressave;
67
68     memset(&hints, 0, sizeof(struct addrinfo));
69     hints.ai_flags = AI_PASSIVE;
70     hints.ai_family = AF_UNSPEC;
71     hints.ai_socktype = SOCK_STREAM;
72
73     if ((n = getaddrinfo(host, serv, &hints, &res)) != 0)
74         cant_listen(host, serv, gai_strerror(n));
75     ressave = res;
76
77     do {
78         fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
79         if (fd < 0)
80             continue;           /* error, try next one */
81
82 #ifndef _WIN32
83         /*
84          * SO_REUSEADDR requests to permit another bind even when the
85          * port is still in state TIME_WAIT.  Windows' SO_REUSEADDR is
86          * broken: it makes bind() succeed no matter what, even if
87          * there's another server running on the same port.  Luckily,
88          * bind() seems to be broken as well: it seems to suceed while
89          * the port in state TIME_WAIT by default; thus we get the
90          * behavior we want by not setting SO_REUSEADDR.
91          */
92         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
93             cant_listen(host, serv, strerror(errno));
94 #endif
95         if (bind(fd, res->ai_addr, res->ai_addrlen) == 0)
96             break;              /* success */
97
98         close(fd);              /* error, close and try next one */
99     } while ((res = res->ai_next) != NULL);
100
101     if (res == NULL)         /* errno from final socket() or bind() */
102         cant_listen(host, serv, strerror(errno));
103
104     if (listen(fd, SOMAXCONN) < 0)
105         cant_listen(host, serv, strerror(errno));
106
107     if (addrlenp)
108         *addrlenp = res->ai_addrlen;
109
110     freeaddrinfo(ressave);
111
112 #else  /* !HAVE_GETADDRINFO */
113     struct sockaddr_in sin;
114     struct hostent *hp;
115     struct servent *sp;
116
117     memset(&sin, 0, sizeof(sin));
118     sin.sin_family = AF_INET;
119     if (!host)
120         sin.sin_addr.s_addr = htonl(INADDR_ANY);
121     else if (isdigit(*host))
122         sin.sin_addr.s_addr = inet_addr(host);
123     else {
124         hp = gethostbyname(host);
125         if (!hp)
126             cant_listen(host, serv, strerror(errno));
127         memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
128     }
129     if (isdigit(*serv))
130         sin.sin_port = htons(atoi(serv));
131     else {
132         sp = getservbyname(serv, "tcp");
133         if (!sp)
134             cant_listen(host, serv, strerror(errno));
135         sin.sin_port = sp->s_port;
136     }
137     if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
138         cant_listen(host, serv, strerror(errno));
139 #ifndef _WIN32
140     /* see comment on setsockopt() above */
141     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
142         cant_listen(host, serv, strerror(errno));
143 #endif
144     if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
145         cant_listen(host, serv, strerror(errno));
146     if (listen(fd, SOMAXCONN) < 0)
147         cant_listen(host, serv, strerror(errno));
148
149     if (addrlenp)
150         *addrlenp = sizeof(struct sockaddr_in);
151
152 #endif /* !HAVE_GETADDRINFO */
153
154     return fd;
155 }
156
157
158 static int
159 cant_listen(char *host, char *serv, const char *err)
160 {
161     fprintf(stderr, "Can't listen on %s%s%s: %s\n",
162             host ? host : "", host ? ":" : "", serv, err);
163     exit(1);
164 }