[PATCH 29/65] remove obsolete gethostbyname, use getaddrinfo (adds support for IPV6)

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Thu, 17 Apr 2014 18:49:11 +0200

Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
---
 ii.c | 48 ++++++++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 22 deletions(-)
diff --git a/ii.c b/ii.c
index b399a4a..eaf7102 100644
--- a/ii.c
+++ b/ii.c
_AT_@ -22,7 +22,6 @@
 #define IRC_CHANNEL_MAX    50
 #define IRC_MSG_MAX       512
 #define PING_TIMEOUT      300
-#define SERVER_PORT      6667
 
 enum { TOK_NICKSRV = 0, TOK_USER, TOK_CMD, TOK_CHAN, TOK_ARG, TOK_TEXT, TOK_LAST };
 
_AT_@ -167,24 +166,29 @@ login(int ircfd, char *host, char *key, char *fullname) {
 }
 
 static int
-tcpopen(const char *host, unsigned short port) {
-	int fd;
-	struct sockaddr_in sin;
-	struct hostent *hp;
-
-	if(!(hp = gethostbyname(host)))
-		eprintf("ii: cannot retrieve host information:");
-	memset(&sin, 0, sizeof(struct sockaddr_in));
-	sin.sin_family = AF_INET;
-	/* TODO: don't use h_addr or h_addr_list here, but getaddrinfo(),
-	 * also support IPV6. */
-	memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length);
-	sin.sin_port = htons(port);
-	if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-		eprintf("ii: cannot create socket:");
-	if(connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0)
-		eprintf("ii: cannot connect to host:");
+tcpopen(const char *host, const char *service) {
+	struct addrinfo hints, *res = NULL, *rp;
+	int fd, e;
+
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_UNSPEC; /* allow IPv4 or IPv6 */
+	hints.ai_flags = AI_NUMERICSERV; /* avoid name lookup for port */
+	hints.ai_socktype = SOCK_STREAM;
+
+	if((e = getaddrinfo(host, service, &hints, &res)))
+		eprintf("ii: getaddrinfo() failed : %s\n", gai_strerror(e));
+
+	for (rp = res; rp; rp = rp->ai_next) {
+		fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+		if(fd == -1)
+			continue; /* retry */
+		if(connect(fd, res->ai_addr, res->ai_addrlen) != 1)
+			break; /* success */
+	}
+	if(!rp)
+		eprintf("ii: could not connect:");
 
+	freeaddrinfo(res);
 	return fd;
 }
 
_AT_@ -515,8 +519,7 @@ int
 main(int argc, char *argv[]) {
 	struct passwd *spw;
 	char *key = NULL, *fullname = NULL, *host = "", *keyname = "";
-	char prefix[_POSIX_PATH_MAX];
-	long port = SERVER_PORT;
+	char prefix[_POSIX_PATH_MAX], *service = "6667";
 	int ircfd;
 
 	/* use nickname and home dir of user for ii by default */
_AT_@ -537,7 +540,8 @@ main(int argc, char *argv[]) {
 			host = EARGF(usage());
 			break;
 		case 'p':
-			port = estrtol(EARGF(usage()), 10);
+			service = EARGF(usage());
+			estrtol(service, 10);
 			break;
 		case 'n':
 			strlcpy(nick, EARGF(usage()), sizeof(nick));
_AT_@ -558,7 +562,7 @@ main(int argc, char *argv[]) {
 	if(!*host || !*nick)
 		usage();
 
-	ircfd = tcpopen(host, port);
+	ircfd = tcpopen(host, service);
 	if(snprintf(path, sizeof(path), "%s/%s", prefix, host) <= 0)
 		eprintf("ii: path to irc directory too long\n");
 	create_dirtree(path);
-- 
2.4.10
--Multipart=_Mon__9_May_2016_17_21_10_+0200_I.6cpFVydhq75aaE
Content-Type: text/x-diff;
 name="0030-update-man-page-this-was-not-supported.patch"
Content-Disposition: attachment;
 filename="0030-update-man-page-this-was-not-supported.patch"
Content-Transfer-Encoding: 7bit
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon May 09 2016 - 17:24:22 CEST