]> git.pond.sub.org Git - empserver/blob - src/lib/gen/tcp_listen.c
Update copyright notice.
[empserver] / src / lib / gen / tcp_listen.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 <stdlib.h>
40 #include <string.h>
41 #ifdef _WIN32
42 #define WIN32
43 #include "winsock2.h"
44 #undef NS_ALL
45 #else
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51 #include <unistd.h>
52 #endif
53 #include "prototypes.h"
54
55 static int cant_listen(char *, char *, const char *);
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 n;
68     struct addrinfo hints, *res, *ressave;
69
70     memset(&hints, 0, sizeof(struct addrinfo));
71     hints.ai_flags = AI_PASSIVE;
72     hints.ai_family = AF_UNSPEC;
73     hints.ai_socktype = SOCK_STREAM;
74
75     if ((n = getaddrinfo(host, serv, &hints, &res)) != 0)
76         cant_listen(host, serv, gai_strerror(n));
77     ressave = res;
78
79     do {
80         fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
81         if (fd < 0)
82             continue;           /* error, try next one */
83
84 #ifndef _WIN32
85         /*
86          * SO_REUSEADDR requests to permit another bind even when the
87          * port is still in state TIME_WAIT.  Windows' SO_REUSEADDR is
88          * broken: it makes bind() succeed no matter what, even if
89          * there's another server running on the same port.  Luckily,
90          * bind() seems to be broken as well: it seems to suceed while
91          * the port in state TIME_WAIT by default; thus we get the
92          * behavior we want by not setting SO_REUSEADDR.
93          */
94         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
95             cant_listen(host, serv, strerror(errno));
96 #endif
97         if (bind(fd, res->ai_addr, res->ai_addrlen) == 0)
98             break;              /* success */
99
100         close(fd);              /* error, close and try next one */
101     } while ((res = res->ai_next) != NULL);
102
103     if (res == NULL)         /* errno from final socket() or bind() */
104         cant_listen(host, serv, strerror(errno));
105
106     if (listen(fd, SOMAXCONN) < 0)
107         cant_listen(host, serv, strerror(errno));
108
109     if (addrlenp)
110         *addrlenp = res->ai_addrlen;
111
112     freeaddrinfo(ressave);
113
114 #else  /* !HAVE_GETADDRINFO */
115     struct sockaddr_in sin;
116     struct hostent *hp;
117     struct servent *sp;
118
119     memset(&sin, 0, sizeof(sin));
120     sin.sin_family = AF_INET;
121     if (!host)
122         sin.sin_addr.s_addr = htonl(INADDR_ANY);
123     else if (isdigit(*host))
124         sin.sin_addr.s_addr = inet_addr(host);
125     else {
126         hp = gethostbyname(host);
127         if (!hp)
128             cant_listen(host, serv, strerror(errno));
129         memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
130     }
131     if (isdigit(*serv))
132         sin.sin_port = htons(atoi(serv));
133     else {
134         sp = getservbyname(serv, "tcp");
135         if (!sp)
136             cant_listen(host, serv, strerror(errno));
137         sin.sin_port = sp->s_port;
138     }
139     if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
140         cant_listen(host, serv, strerror(errno));
141 #ifndef _WIN32
142     /* see comment on setsockopt() above */
143     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
144         cant_listen(host, serv, strerror(errno));
145 #endif
146     if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
147         cant_listen(host, serv, strerror(errno));
148     if (listen(fd, SOMAXCONN) < 0)
149         cant_listen(host, serv, strerror(errno));
150
151     if (addrlenp)
152         *addrlenp = sizeof(struct sockaddr_in);
153
154 #endif /* !HAVE_GETADDRINFO */
155
156     return fd;
157 }
158
159
160 static int
161 cant_listen(char *host, char *serv, const char *err)
162 {
163     fprintf(stderr, "Can't listen on %s%s%s: %s\n",
164             host ? host : "", host ? ":" : "", serv, err);
165     exit(1);
166 }