(hostaddr, hostconnect): Leave printing messages to caller.

(main): Clarify messages.

(hostport) [_WIN32]: Remove useless conditional code.

(main): Fall back to compiled-in host only if environment doesn't
supply one, not if it can't be resolved.  Same for port.
This commit is contained in:
Markus Armbruster 2005-09-23 17:55:24 +00:00
parent 5357aa15b9
commit d253e1266f
2 changed files with 17 additions and 26 deletions

View file

@ -32,7 +32,7 @@
* Steve McClure, 1998 * Steve McClure, 1998
*/ */
#include <stdio.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
@ -46,7 +46,6 @@
#include <io.h> #include <io.h>
#include <winsock.h> #include <winsock.h>
#endif #endif
#include <ctype.h>
#include "misc.h" #include "misc.h"
int int
@ -60,15 +59,9 @@ hostaddr(char *name, struct sockaddr_in *addr)
addr->sin_addr.s_addr = inet_addr(name); addr->sin_addr.s_addr = inet_addr(name);
} else { } else {
hp = gethostbyname(name); hp = gethostbyname(name);
if (hp == NULL) { if (hp == NULL)
fprintf(stderr, "%s: No such host\n", name);
return 0; return 0;
}
memcpy(&addr->sin_addr, hp->h_addr, sizeof(addr->sin_addr)); memcpy(&addr->sin_addr, hp->h_addr, sizeof(addr->sin_addr));
#ifdef _WIN32
printf("Trying to connect to '%s'\n", inet_ntoa(addr->sin_addr));
fflush(stdout);
#endif
} }
return 1; return 1;
} }
@ -81,12 +74,7 @@ hostport(char *name, struct sockaddr_in *addr)
if (name == NULL || *name == 0) if (name == NULL || *name == 0)
return 0; return 0;
if (isdigit(*name)) { if (isdigit(*name)) {
#ifndef _WIN32
addr->sin_port = htons(atoi(name)); addr->sin_port = htons(atoi(name));
#else
addr->sin_port = atoi(name);
addr->sin_port = htons(addr->sin_port);
#endif
} else { } else {
sp = getservbyname(name, "tcp"); sp = getservbyname(name, "tcp");
if (sp == NULL) if (sp == NULL)
@ -106,17 +94,12 @@ hostconnect(struct sockaddr_in *addr)
#ifdef _WIN32 #ifdef _WIN32
errno = WSAGetLastError(); errno = WSAGetLastError();
#endif #endif
perror("socket");
return -1; return -1;
} }
addr->sin_family = AF_INET; addr->sin_family = AF_INET;
if (connect(s, (struct sockaddr *)addr, sizeof(*addr)) < 0) { if (connect(s, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
#ifdef _WIN32 #ifdef _WIN32
errno = WSAGetLastError(); errno = WSAGetLastError();
#endif
perror("connect");
#ifdef _WIN32
printf("Check that your EMPIREHOST and EMPIREPORT are correct.\n");
#endif #endif
(void)close(s); (void)close(s);
return -1; return -1;

View file

@ -105,6 +105,8 @@ main(int ac, char **av)
char *cname; char *cname;
char *pname; char *pname;
char *uname; char *uname;
char *host;
char *port;
int send_kill = 0; int send_kill = 0;
int utf8 = 0; int utf8 = 0;
@ -157,18 +159,24 @@ main(int ac, char **av)
exit(1); exit(1);
} }
getsose(); getsose();
if (!hostport(getenv("EMPIREPORT"), &sin) && port = getenv("EMPIREPORT");
!hostport("empire", &sin) && !hostport(empireport, &sin)) { if (!port)
fprintf(stderr, "No empire port\n"); port = empireport;
if (!hostport(port, &sin)) {
fprintf(stderr, "Can't resolve Empire port %s\n", port);
exit(1); exit(1);
} }
if (!hostaddr(getenv("EMPIREHOST"), &sin) && host = getenv("EMPIREHOST");
!hostaddr(empirehost, &sin)) { if (!host)
fprintf(stderr, "No empire host\n"); host = empirehost;
if (!hostaddr(host, &sin)) {
fprintf(stderr, "Can't resolve Empire host %s\n", host);
exit(1); exit(1);
} }
if ((sock = hostconnect(&sin)) < 0) if ((sock = hostconnect(&sin)) < 0) {
perror("Can't connect to Empire server");
exit(1); exit(1);
}
cname = getenv("COUNTRY"); cname = getenv("COUNTRY");
if (ac > 1) if (ac > 1)
cname = argv[1]; cname = argv[1];