Blame SOURCES/myftp-replace-use-of-deprecated-gethostbyname-with-g.patch

cf5772
From 166c26dca2a5004f83c5dbc1cb9870667fa8e301 Mon Sep 17 00:00:00 2001
cf5772
From: Xin Long <lucien.xin@gmail.com>
cf5772
Date: Wed, 4 Aug 2021 07:29:13 -0400
cf5772
Subject: [PATCH 1/4] myftp: replace use of deprecated gethostbyname with
cf5772
 getaddrinfo
cf5772
cf5772
This patch is to replace use of deprecated gethostbyname with
cf5772
getaddrinfo in the file src/apps/myftp.c.
cf5772
cf5772
Signed-off-by: Xin Long <lucien.xin@gmail.com>
cf5772
Signed-off-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
cf5772
---
cf5772
 configure.ac     |  2 +-
cf5772
 src/apps/myftp.c | 40 ++++++++++++++++++----------------------
cf5772
 2 files changed, 19 insertions(+), 23 deletions(-)
cf5772
cf5772
diff --git a/configure.ac b/configure.ac
cf5772
index 4e2f7b4..8345dab 100644
cf5772
--- a/configure.ac
cf5772
+++ b/configure.ac
cf5772
@@ -75,7 +75,7 @@ AC_FUNC_REALLOC
cf5772
 AC_FUNC_SELECT_ARGTYPES
cf5772
 AC_FUNC_SETVBUF_REVERSED
cf5772
 AC_FUNC_VPRINTF
cf5772
-AC_CHECK_FUNCS([bzero gethostbyname gettimeofday memmove memset select socket \
cf5772
+AC_CHECK_FUNCS([bzero getaddrinfo gethostbyname gettimeofday memmove memset select socket \
cf5772
 		strchr strerror strtol strtoul])
cf5772
 
cf5772
 # Support for stream reset even, added on v4.11, 35ea82d611da
cf5772
diff --git a/src/apps/myftp.c b/src/apps/myftp.c
cf5772
index 64fa3f2..473fa03 100644
cf5772
--- a/src/apps/myftp.c
cf5772
+++ b/src/apps/myftp.c
cf5772
@@ -64,11 +64,11 @@ typedef enum { COMMAND_NONE, COMMAND_RECV, COMMAND_SEND } command_t;
cf5772
 #define MAX_NUM_HOST	5
cf5772
 static char *local_host[MAX_NUM_HOST];
cf5772
 static int num_local_host = 0;
cf5772
-static int local_port = 4444;
cf5772
+static char *local_port = "4444";
cf5772
 
cf5772
 static int buffer_size = BUFSIZE;
cf5772
 static char *remote_host = NULL;
cf5772
-static int remote_port = 4444;
cf5772
+static char *remote_port = "4444";
cf5772
 static command_t command = COMMAND_NONE;
cf5772
 static char *filename = NULL;
cf5772
 static int interactive = 0;
cf5772
@@ -133,7 +133,7 @@ static int parse_arguments(int argc, char *argv[])
cf5772
 			break;
cf5772
 		case 2:		/* local port */
cf5772
 		case 'P':
cf5772
-			local_port = atoi(optarg);
cf5772
+			local_port = optarg;
cf5772
 			break;
cf5772
 		case 3:		/* remote host */
cf5772
 		case 'h':
cf5772
@@ -141,7 +141,7 @@ static int parse_arguments(int argc, char *argv[])
cf5772
 			break;
cf5772
 		case 4:		/* remote port */
cf5772
 		case 'p':
cf5772
-			remote_port = atoi(optarg);
cf5772
+			remote_port = optarg;
cf5772
 			break;
cf5772
 		case 5:
cf5772
 		case 'f':
cf5772
@@ -236,6 +236,7 @@ emsg(char *prog,char *s)
cf5772
 
cf5772
 static int build_endpoint(char *argv0)
cf5772
 {
cf5772
+	struct addrinfo hints, *rp;
cf5772
 	int retval,i;
cf5772
 
cf5772
 	/* Create the local endpoint.  */
cf5772
@@ -245,22 +246,19 @@ static int build_endpoint(char *argv0)
cf5772
 	}
cf5772
 
cf5772
 	for ( i = 0;i < num_local_host;i++ ) {
cf5772
-		struct hostent *hst;
cf5772
-		struct sockaddr_in laddr;
cf5772
-
cf5772
-		memset(&laddr, 0, sizeof(laddr));
cf5772
 		/* Get the transport address for the local host name.  */
cf5772
 		fprintf(stderr,"Hostname %d is %s\n",i+1,local_host[i]);
cf5772
-		if ( (hst = gethostbyname(local_host[i])) == NULL ) {
cf5772
+
cf5772
+		memset(&hints, 0, sizeof(struct addrinfo));
cf5772
+		hints.ai_family = AF_INET;
cf5772
+		hints.ai_protocol = IPPROTO_SCTP;
cf5772
+		if (getaddrinfo(local_host[i], local_port, &hints, &rp) != 0) {
cf5772
 			fprintf(stderr, "%s: bad hostname: %s\n", argv0, local_host[i]);
cf5772
 			exit(1);
cf5772
 		}
cf5772
-		memcpy(&laddr.sin_addr, hst->h_addr_list[0],sizeof(laddr.sin_addr));
cf5772
-		laddr.sin_port = htons(local_port);
cf5772
-		laddr.sin_family = AF_INET;
cf5772
 
cf5772
 		/* Bind this socket to the test port.  */
cf5772
-		if ( bind(retval, (struct sockaddr *)&laddr, sizeof(laddr)) ) {
cf5772
+		if (bind(retval, rp->ai_addr, rp->ai_addrlen)) {
cf5772
 			emsg(argv0,"bind");
cf5772
 			exit(-1);
cf5772
 		}
cf5772
@@ -339,21 +337,19 @@ command_send(char *argv0, int sk)
cf5772
 {
cf5772
 	struct msghdr outmsg;
cf5772
 	struct iovec iov;
cf5772
-	struct hostent *hst;
cf5772
-	struct sockaddr_in remote_addr;
cf5772
+	struct addrinfo hints, *rp;
cf5772
 	int fd;
cf5772
 	int msglen;
cf5772
 	int ct;
cf5772
 
cf5772
 	/* Set up the destination.  */
cf5772
-	hst = gethostbyname(remote_host);
cf5772
-	if (hst == NULL || hst->h_length < 1) {
cf5772
+	memset(&hints, 0, sizeof(struct addrinfo));
cf5772
+	hints.ai_family = AF_INET;
cf5772
+	hints.ai_protocol = IPPROTO_SCTP;
cf5772
+	if (getaddrinfo(remote_host, remote_port, &hints, &rp) != 0) {
cf5772
 		fprintf(stderr, "%s: bad hostname: %s\n", argv0, remote_host);
cf5772
 		exit(1);
cf5772
 	}
cf5772
-	memcpy(&remote_addr.sin_addr, hst->h_addr_list[0], sizeof(remote_addr.sin_addr));
cf5772
-	remote_addr.sin_port = htons(remote_port);
cf5772
-	remote_addr.sin_family = AF_INET;
cf5772
 
cf5772
 	/* Initialize the message struct we use to pass messages to
cf5772
 	 * the remote socket.
cf5772
@@ -364,8 +360,8 @@ command_send(char *argv0, int sk)
cf5772
 	outmsg.msg_iovlen = 1;
cf5772
 	outmsg.msg_control = NULL;
cf5772
 	outmsg.msg_controllen = 0;
cf5772
-	outmsg.msg_name = &remote_addr;
cf5772
-	outmsg.msg_namelen = sizeof(remote_addr);
cf5772
+	outmsg.msg_name = rp->ai_addr;
cf5772
+	outmsg.msg_namelen = rp->ai_addrlen;
cf5772
 
cf5772
 	/* open the file */
cf5772
 	if ( filename == NULL ) fd = 0;
cf5772
-- 
cf5772
2.27.0
cf5772