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