2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * host.c: make stream connection to empire
30 * Known contributors to this file:
33 * Markus Armbruster, 2005
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
51 #define close(fd) w32_close_socket((fd))
55 #ifdef HAVE_GETADDRINFO
57 * Inspired by example code from W. Richard Stevens: UNIX Network
62 tcp_connect(char *host, char *serv)
65 struct addrinfo hints, *res, *ressave;
67 memset(&hints, 0, sizeof(struct addrinfo));
68 hints.ai_family = AF_UNSPEC;
69 hints.ai_socktype = SOCK_STREAM;
71 if ((n = getaddrinfo(host, serv, &hints, &res)) != 0) {
72 fprintf(stderr, "Can't connect to %s:%s: %s\n",
73 host, serv, gai_strerror(n));
79 sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
81 continue; /* ignore this one */
83 if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
85 close(sockfd); /* ignore this one */
86 } while ((res = res->ai_next) != NULL);
88 if (res == NULL) { /* errno set from final connect() */
89 fprintf(stderr, "Can't connect to %s:%s: %s\n",
90 host, serv, strerror(errno));
94 freeaddrinfo(ressave);
99 #else /* !HAVE_GETADDRINFO */
102 hostaddr(char *name, struct sockaddr_in *addr)
106 if (name == NULL || *name == 0)
108 if (isdigit(*name)) {
109 addr->sin_addr.s_addr = inet_addr(name);
111 hp = gethostbyname(name);
114 memcpy(&addr->sin_addr, hp->h_addr, sizeof(addr->sin_addr));
120 hostport(char *name, struct sockaddr_in *addr)
124 if (name == NULL || *name == 0)
126 if (isdigit(*name)) {
127 addr->sin_port = htons(atoi(name));
129 sp = getservbyname(name, "tcp");
132 addr->sin_port = sp->s_port;
138 hostconnect(struct sockaddr_in *addr)
140 /* FIXME should attempt connect to all addresses of multi-homed host, not just 1st */
143 s = socket(AF_INET, SOCK_STREAM, 0);
147 addr->sin_family = AF_INET;
148 if (connect(s, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
156 tcp_connect(char *host, char *serv)
158 struct sockaddr_in sin;
161 if (!hostport(serv, &sin)) {
162 fprintf(stderr, "Can't resolve Empire port %s\n", serv);
165 if (!hostaddr(host, &sin)) {
166 fprintf(stderr, "Can't resolve Empire host %s\n", host);
169 if ((sock = hostconnect(&sin)) < 0) {
170 fprintf(stderr, "Can't connect to %s:%s: %s\n",
171 serv, host, strerror(errno));