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