]> git.pond.sub.org Git - empserver/blob - src/client/host.c
12d89e71983cedaf37bb07e9dd7824619d130e78
[empserver] / src / client / host.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  host.c: make stream connection to empire
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2005-2013
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <netdb.h>
47 #include <unistd.h>
48 #include "misc.h"
49
50 /* Portability cruft, should become unnecessary eventually */
51 #ifndef AI_ADDRCONFIG
52 #define AI_ADDRCONFIG 0
53 #endif
54
55 #ifdef HAVE_GETADDRINFO
56 /*
57  * Inspired by example code from W. Richard Stevens: UNIX Network
58  * Programming, Vol. 1
59  */
60
61 int
62 tcp_connect(char *host, char *serv)
63 {
64     int sockfd, n;
65     struct addrinfo hints, *res, *ressave;
66
67     memset(&hints, 0, sizeof(struct addrinfo));
68     hints.ai_flags = AI_ADDRCONFIG;
69     hints.ai_family = AF_UNSPEC;
70     hints.ai_socktype = SOCK_STREAM;
71
72     if ((n = getaddrinfo(host, serv, &hints, &res)) != 0) {
73         fprintf(stderr, "Can't connect to %s:%s: %s\n",
74                 host, serv, gai_strerror(n));
75         exit(1);
76     }
77     ressave = res;
78
79     do {
80         sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
81         if (sockfd < 0)
82             continue;           /* ignore this one */
83
84         if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
85             break;              /* success */
86         close(sockfd);          /* ignore this one */
87     } while ((res = res->ai_next) != NULL);
88
89     if (res == NULL) {    /* errno from final socket() or connect() */
90         fprintf(stderr, "Can't connect to %s:%s: %s\n",
91                 host, serv, strerror(errno));
92         exit(1);
93     }
94
95     freeaddrinfo(ressave);
96
97     return sockfd;
98 }
99
100 #else  /* !HAVE_GETADDRINFO */
101
102 static int
103 hostaddr(char *name, struct sockaddr_in *addr)
104 {
105     struct hostent *hp;
106
107     if (name == NULL || *name == 0)
108         return 0;
109     if (isdigit(*name)) {
110         addr->sin_addr.s_addr = inet_addr(name);
111     } else {
112         hp = gethostbyname(name);
113         if (hp == NULL)
114             return 0;
115         memcpy(&addr->sin_addr, hp->h_addr, sizeof(addr->sin_addr));
116     }
117     return 1;
118 }
119
120 static int
121 hostport(char *name, struct sockaddr_in *addr)
122 {
123     struct servent *sp;
124
125     if (name == NULL || *name == 0)
126         return 0;
127     if (isdigit(*name)) {
128         addr->sin_port = htons(atoi(name));
129     } else {
130         sp = getservbyname(name, "tcp");
131         if (sp == NULL)
132             return 0;
133         addr->sin_port = sp->s_port;
134     }
135     return 1;
136 }
137
138 static int
139 hostconnect(struct sockaddr_in *addr)
140 {
141     /* FIXME should attempt connect to all addresses of multi-homed host, not just 1st */
142     int s;
143
144     s = socket(AF_INET, SOCK_STREAM, 0);
145     if (s < 0) {
146         return -1;
147     }
148     addr->sin_family = AF_INET;
149     if (connect(s, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
150         (void)close(s);
151         return -1;
152     }
153     return s;
154 }
155
156 int
157 tcp_connect(char *host, char *serv)
158 {
159     struct sockaddr_in sin;
160     int sock;
161
162     if (!hostport(serv, &sin)) {
163         fprintf(stderr, "Can't resolve Empire port %s\n", serv);
164         exit(1);
165     }
166     if (!hostaddr(host, &sin)) {
167         fprintf(stderr, "Can't resolve Empire host %s\n", host);
168         exit(1);
169     }
170     if ((sock = hostconnect(&sin)) < 0) {
171         fprintf(stderr, "Can't connect to %s:%s: %s\n",
172                 serv, host, strerror(errno));
173         exit(1);
174     }
175     return sock;
176 }
177 #endif