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