]> git.pond.sub.org Git - empserver/blob - src/lib/gen/tcp_listen.c
Use IPv4 and v6 only when suitable interfaces are configured
[empserver] / src / lib / gen / tcp_listen.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 #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
56 int
57 tcp_listen(char *host, char *serv, size_t *addrlenp)
58 {
59     int fd;
60     int on = 1;
61 #ifdef HAVE_GETADDRINFO
62     /*
63      * Inspired by example code from W. Richard Stevens: UNIX Network
64      * Programming, Vol. 1
65      */
66     int n;
67     struct addrinfo hints, *res, *ressave;
68
69     memset(&hints, 0, sizeof(struct addrinfo));
70     hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
71     hints.ai_family = AF_UNSPEC;
72     hints.ai_socktype = SOCK_STREAM;
73
74     if ((n = getaddrinfo(host, serv, &hints, &res)) != 0)
75         cant_listen(host, serv, gai_strerror(n));
76     ressave = res;
77
78     do {
79         fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
80         if (fd < 0)
81             continue;           /* error, try next one */
82
83         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
84             cant_listen(host, serv, strerror(errno));
85         if (bind(fd, res->ai_addr, res->ai_addrlen) == 0)
86             break;              /* success */
87
88         close(fd);              /* error, close and try next one */
89     } while ((res = res->ai_next) != NULL);
90
91     if (res == NULL)         /* errno from final socket() or bind() */
92         cant_listen(host, serv, strerror(errno));
93
94     if (listen(fd, SOMAXCONN) < 0)
95         cant_listen(host, serv, strerror(errno));
96
97     if (addrlenp)
98         *addrlenp = res->ai_addrlen;
99
100     freeaddrinfo(ressave);
101
102 #else  /* !HAVE_GETADDRINFO */
103     struct sockaddr_in sin;
104     struct hostent *hp;
105     struct servent *sp;
106
107     memset(&sin, 0, sizeof(sin));
108     sin.sin_family = AF_INET;
109     if (!host)
110         sin.sin_addr.s_addr = htonl(INADDR_ANY);
111     else if (isdigit(*host))
112         sin.sin_addr.s_addr = inet_addr(host);
113     else {
114         hp = gethostbyname(host);
115         if (!hp)
116             cant_listen(host, serv, strerror(errno));
117         memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
118     }
119     if (isdigit(*serv))
120         sin.sin_port = htons(atoi(serv));
121     else {
122         sp = getservbyname(serv, "tcp");
123         if (!sp)
124             cant_listen(host, serv, strerror(errno));
125         sin.sin_port = sp->s_port;
126     }
127     if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
128         cant_listen(host, serv, strerror(errno));
129     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
130         cant_listen(host, serv, strerror(errno));
131     if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
132         cant_listen(host, serv, strerror(errno));
133     if (listen(fd, SOMAXCONN) < 0)
134         cant_listen(host, serv, strerror(errno));
135
136     if (addrlenp)
137         *addrlenp = sizeof(struct sockaddr_in);
138
139 #endif /* !HAVE_GETADDRINFO */
140
141     return fd;
142 }
143
144 static void
145 cant_listen(char *host, char *serv, const char *err)
146 {
147     fprintf(stderr, "Can't listen on %s%s%s: %s\n",
148             host ? host : "", host ? ":" : "", serv, err);
149     exit(1);
150 }