diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2017-12-20 00:40:58 +0100 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2017-12-20 00:40:58 +0100 |
commit | dbfa491594e0c84079bebc991107e1b51a228433 (patch) | |
tree | 45c42b4abb81067f139b2acdffed18c8612577f8 /src | |
parent | 8c051422c1673e57ce70cc9dccb6e136d901cc09 (diff) |
ptunnel-ng:
* fixed mingw64 cross compile issues
* using getaddrinfo instead of obsolete gethostbyname
* removed IS_WINDOWS AM_CONDITIONAL
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/options.c | 18 | ||||
-rw-r--r-- | src/utils.c | 25 | ||||
-rw-r--r-- | src/utils.h | 4 |
4 files changed, 39 insertions, 11 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 5d51807..8aaa52a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,9 +9,6 @@ endif if HAVE_SELINUX ptunnel_ng_CFLAGS += -DHAVE_SELINUX=1 endif -if IS_WINDOWS -ptunnel_ng_LDADD += -lws2_32 -endif ptunnel_ng_SOURCES = \ md5.c \ diff --git a/src/options.c b/src/options.c index b8487e0..b0af314 100644 --- a/src/options.c +++ b/src/options.c @@ -5,6 +5,9 @@ #include <getopt.h> #include <ctype.h> #include <assert.h> +#ifdef WIN32 +#include <ws2tcpip.h> +#endif #ifdef HAVE_CONFIG_H #include "config.h" @@ -329,8 +332,7 @@ void print_usage(const char *arg0) { } int parse_options(int argc, char **argv) { - int c = 0, optind = -1, has_logfile = 0; - struct hostent *host_ent; + int c = 0, optind = -1, has_logfile = 0, ret; md5_state_t state; #ifndef WIN32 struct passwd *pwnam; @@ -491,18 +493,18 @@ int parse_options(int argc, char **argv) { } if (opts.given_proxy_hostname) { - if (NULL == (host_ent = gethostbyname(opts.given_proxy_hostname))) { - pt_log(kLog_error, "Failed to look up %s as proxy address\n", opts.given_proxy_hostname); + if ((ret = host_to_addr(opts.given_proxy_hostname, &opts.given_proxy_ip)) != 0) { + pt_log(kLog_error, "Failed to look up %s as destination address: %s\n", + opts.given_proxy_hostname, gai_strerror(ret)); return 1; } - opts.given_proxy_ip = *(uint32_t*)host_ent->h_addr_list[0]; } - if (NULL == (host_ent = gethostbyname(opts.given_dst_hostname))) { - pt_log(kLog_error, "Failed to look up %s as destination address\n", opts.given_dst_hostname); + if ((ret = host_to_addr(opts.given_dst_hostname, &opts.given_dst_ip)) != 0) { + pt_log(kLog_error, "Failed to look up %s as destination address: %s\n", + opts.given_dst_hostname, gai_strerror(ret)); return 1; } - opts.given_dst_ip = *(uint32_t*)host_ent->h_addr_list[0]; #ifndef WIN32 if (NULL == (opts.pid_file = fopen(opts.pid_path, "w"))) diff --git a/src/utils.c b/src/utils.c index 996baf7..3305456 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,7 +1,13 @@ #include <stdarg.h> +#include <string.h> #ifndef WIN32 #include <syslog.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#else +#include <ws2tcpip.h> #endif #include <sys/time.h> @@ -52,6 +58,25 @@ double time_as_double(void) { return result; } +int host_to_addr(const char *hostname, uint32_t *result) +{ + int ret; + struct addrinfo *addrs = NULL; + struct addrinfo hints; + struct sockaddr_in *addr; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + + if ((ret = getaddrinfo(hostname, NULL, &hints, &addrs)) != 0) + return ret; + addr = (struct sockaddr_in *) addrs->ai_addr; + *result = *(uint32_t *) &addr->sin_addr; + freeaddrinfo(addrs); + + return 0; +} + #if 0 static const char hextab[] = "0123456789ABCDEF"; diff --git a/src/utils.h b/src/utils.h index 7a2b551..e0355d1 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,12 +1,16 @@ #ifndef UTILS_H #define UTILS_H 1 +#include <stdint.h> + #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) void pt_log(int level, const char *fmt, ...); double time_as_double(void); +int host_to_addr(const char *hostname, uint32_t *result); + #if 0 void print_hexstr(unsigned char *buf, size_t siz); #endif |