hostname lookup for telnet-proxy
diff --git a/README b/README
index c3bd6de..adada85 100644
--- a/README
+++ b/README
@@ -287,17 +287,16 @@
then you must first edit the Makefile and remove the -DHAVE_ZLIB and
the -lz from the compile flags.
-To run telnet-proxy, you simply give it the server's IP address
-(telnet-proxy does not support hostname resolution, nor IPv6), the
-server's port number, and the port number that telnet-proxy should
-listen on. For example, to connect to the server on 209.85.171.100
-port 7800 and to listen on port 5000, run:
+To run telnet-proxy, you simply give it the server's host name or
+IP address, the server's port number, and the port number that
+telnet-proxy should listen on. For example, to connect to the server
+on mud.example.com port 7800 and to listen on port 5000, run:
- $ ./telnet-proxy 209.85.171.100 7800 5000
+ $ ./telnet-proxy mud.example.com 7800 5000
You can then connect to the host telnet-proxy is running on (e.g.
-127.0.0.1, or the machine's public IP) on port 500 and you will
-automatically be proxied into 209.85.171.100.
+127.0.0.1) on port 500 and you will automatically be proxied into
+mud.example.com.
telnet-proxy will display status information about the data
passing through both ends of the tunnel. Once either end
diff --git a/telnet-proxy.c b/telnet-proxy.c
index 4b8b13f..0cedaf9 100644
--- a/telnet-proxy.c
+++ b/telnet-proxy.c
@@ -12,6 +12,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <netdb.h>
#include <poll.h>
#include <errno.h>
#include <stdio.h>
@@ -226,6 +227,8 @@
struct conn_t server;
struct conn_t client;
struct libtelnet_cb_t cb_table;
+ struct addrinfo *ai;
+ struct addrinfo hints;
/* check usage */
if (argc != 4) {
@@ -255,7 +258,7 @@
}
/* wait for client */
- if (listen(listen_sock, 5) == -1) {
+ if (listen(listen_sock, 1) == -1) {
fprintf(stderr, "listen() failed: %s\n", strerror(errno));
return 1;
}
@@ -267,6 +270,16 @@
/* stop listening now that we have a client */
close(listen_sock);
+
+ /* look up server host */
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ if ((rs = getaddrinfo(argv[1], argv[2], &hints, &ai)) != 0) {
+ fprintf(stderr, "getaddrinfo() failed for %s: %s\n", argv[1],
+ gai_strerror(rs));
+ return 1;
+ }
/* create server socket */
if ((server.sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
@@ -274,25 +287,23 @@
return 1;
}
- /* connect to server */
+ /* bind server socket */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
if (bind(server.sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
fprintf(stderr, "bind() failed: %s\n", strerror(errno));
return 1;
}
- memset(&addr, 0, sizeof(addr));
- if (inet_pton(AF_INET, argv[1], &addr.sin_addr) != 1) {
- fprintf(stderr, "inet_pton() failed: %s\n", strerror(errno));
- return 1;
- }
- addr.sin_family = AF_INET;
- addr.sin_port = htons(strtol(argv[2], 0, 10));
- if (connect(server.sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+
+ /* connect */
+ if (connect(server.sock, ai->ai_addr, ai->ai_addrlen) == -1) {
fprintf(stderr, "server() failed: %s\n", strerror(errno));
return 1;
}
+ /* free address lookup info */
+ freeaddrinfo(ai);
+
/* initialize connection structs */
server.name = "\e[35mSERVER";
server.remote = &client;