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