Skip to contentSkip to conversation

lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo

PATCH Applied Xin Long 2 participants 7 messages Aug 4, 2021 – Aug 11, 2021 SCTP protocol

lore
Share & tools
lore

Xin Long

author maintainer
#0 lore raw

cover lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo

This patchset is simply to replace use of deprecated gethostbyname with getaddrinfo in myftp, sctp_xconnect, sctp_test and sctp_darn.

Xin Long (4):
myftp: replace use of deprecated gethostbyname with getaddrinfo
sctp_xconnect: replace use of deprecated gethostbyname with
getaddrinfo
sctp_test: replace use of deprecated gethostbyname with getaddrinfo
sctp_darn: replace use of deprecated gethostbyname with getaddrinfo

 configure.ac             |   2 +-
 src/apps/myftp.c         |  40 +++----
 src/apps/sctp_darn.c     | 246 +++++++++++----------------------------
 src/apps/sctp_test.c     |  63 +++-------
 src/apps/sctp_xconnect.c |  72 +++++-------
 5 files changed, 137 insertions(+), 286 deletions(-)
Signature · 1 line
2.27.0
author maintainer
#1 lore raw

PATCH 1/4 myftp: replace use of deprecated gethostbyname with getaddrinfo

This patch is to replace use of deprecated gethostbyname with getaddrinfo in the file src/apps/myftp.c.

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

PATCH 2/4 sctp_xconnect: replace use of deprecated gethostbyname with getaddrinfo

In reply to Xin Long

This patch is to replace use of deprecated gethostbyname with getaddrinfo in the file src/apps/sctp_xconnect.c.

---
 src/apps/sctp_xconnect.c | 72 +++++++++++++++++-----------------------
 1 file changed, 31 insertions(+), 41 deletions(-)
src/apps/sctp_xconnect.c +31 -41
index 6759c0e..5cd1049 100644
@@ -63,13 +63,13 @@ int TST_CNT = 0;
 
 int mode = NOT_DEFINED;
 
-int	assoc_num,
-	remote_port,
-	local_port;
+int assoc_num;
 int active = 0;
 
-char *local_host = NULL;
-char *remote_host = NULL;
+char *local_host;
+char *remote_host;
+char *local_port;
+char *remote_port;
 sockaddr_storage_t client_loop,
 		server_loop;
 struct hostent *hst;
@@ -102,6 +102,7 @@ void usage(char *argv0)
 
 /* Parse command line options */
 void parse_arguments(int argc, char*argv[]) {
+	struct addrinfo hints, *rp;
 	int c;
 
 	while ((c = getopt(argc, argv, ":H:P:ach:ln:p:")) >= 0) {
@@ -110,7 +111,7 @@ void parse_arguments(int argc, char*argv[]) {
 				local_host = optarg;
 				break;
 			case 'P':
-				local_port = atoi(optarg);
+				local_port = optarg;
 				break;
 			case 'c':
 			    if (mode == NOT_DEFINED)
@@ -138,7 +139,7 @@ void parse_arguments(int argc, char*argv[]) {
 				assoc_num = atoi(optarg);
 				break;
 			case 'p':
-				remote_port = atoi(optarg);
+				remote_port = optarg;
 				break;
 			default:
 				usage(argv[0]);
@@ -146,6 +147,9 @@ void parse_arguments(int argc, char*argv[]) {
 		}
 	} /* while() */
 
+	memset(&hints, 0, sizeof(struct addrinfo));
+	hints.ai_family = AF_INET;
+	hints.ai_protocol = IPPROTO_SCTP;
 	if (mode == CLIENT) {
 		if (assoc_num) {
 			if (assoc_num > MAXCLIENTNUM) {
@@ -160,13 +164,13 @@ void parse_arguments(int argc, char*argv[]) {
 			assoc_num = 1;
 
 		if (remote_host && remote_port) {
-			hst = gethostbyname(remote_host);
-
-			memcpy(&server_loop.v4.sin_addr, hst->h_addr_list[0],
-				   sizeof(server_loop.v4.sin_addr));
-
-			server_loop.v4.sin_family = AF_INET;
-server_loop.v4.sin_port = htons(remote_port);
+			if (getaddrinfo(remote_host, remote_port, &hints, &rp) != 0) {
+				printf("%s: bad remote hostname or port: %s, %s\n",
+				       argv[0], remote_host, remote_port);
+				usage(argv[0]);
+				exit(0);
+			}
+			memcpy(&server_loop, rp->ai_addr, rp->ai_addrlen);
 		} else {
 			printf("Remote host and remote port must be defined "
 				"in client mode\n");
@@ -174,20 +178,12 @@ server_loop.v4.sin_port = htons(remote_port);
 			exit(0);
 		}
 
-		if (local_host) {
-			hst = gethostbyname(local_host);
-
-			memcpy(&client_loop.v4.sin_addr, hst->h_addr_list[0],
-				   sizeof(client_loop.v4.sin_addr));
-		} else
-			client_loop.v4.sin_addr.s_addr = INADDR_ANY;
-
-		if (local_port)
-			client_loop.v4.sin_port = htons(local_port);
-		else
-			client_loop.v4.sin_port = 0;
-
-		client_loop.v4.sin_family = AF_INET;
+		if (getaddrinfo(local_host, local_port, &hints, &rp) != 0) {
+			printf("%s: bad local hostname or port: %s, %s\n",
+			       argv[0], local_host, local_port);
+			exit(0);
+		}
+		memcpy(&client_loop, rp->ai_addr, rp->ai_addrlen);
 	} else if (mode == SERVER) {
 		if (active) {
 			printf("This option if for client use only");
@@ -201,23 +197,17 @@ server_loop.v4.sin_port = htons(remote_port);
 			exit(0);
 		}
 
-		if (local_host) {
-			hst = gethostbyname(local_host);
-
-			memcpy(&server_loop.v4.sin_addr, hst->h_addr_list[0],
-				   sizeof(server_loop.v4.sin_addr));
-		} else
-			server_loop.v4.sin_addr.s_addr = INADDR_ANY;
-
-		if (local_port)
-			server_loop.v4.sin_port = htons(local_port);
-		else {
+		if (!local_port) {
 			printf("Specify a local port in server mode.\n");
 			usage(argv[0]);
 			exit(0);
 		}
-
-		server_loop.v4.sin_family = AF_INET;
+		if (getaddrinfo(local_host, local_port, &hints, &rp) != 0) {
+			printf("%s: bad local hostname or port: %s, %s\n",
+			       argv[0], local_host, local_port);
+			exit(0);
+		}
+		memcpy(&server_loop, rp->ai_addr, rp->ai_addrlen);
 	} else {
 		printf("Must assisgn a client or server mode.\n");
 		usage(argv[0]);
Signature · 1 line
2.27.0
author maintainer
#3 lore raw

PATCH 3/4 sctp_test: replace use of deprecated gethostbyname with getaddrinfo

In reply to Xin Long

This patch is to replace use of deprecated gethostbyname with getaddrinfo in the file src/apps/sctp_test.c.

---
 src/apps/sctp_test.c | 63 +++++++++++---------------------------------
 1 file changed, 16 insertions(+), 47 deletions(-)
src/apps/sctp_test.c +16 -47
index cd7654b..e382804 100644
@@ -487,14 +487,11 @@ struct sockaddr *
 append_addr(const char *parm, struct sockaddr *addrs, int *ret_count)
 {
 	struct sockaddr *new_addrs = NULL;
+	struct addrinfo hints, *res, *rp;
 	void *aptr;
 	struct sockaddr *sa_addr;
 	struct sockaddr_in *b4ap;
 	struct sockaddr_in6 *b6ap;
-	struct hostent *hst4 = NULL;
-	struct hostent *hst6 = NULL;
-	int i4 = 0;
-	int i6 = 0;
 	int j;
 	int orig_count = *ret_count;
 	int count = orig_count;
@@ -514,27 +511,16 @@ append_addr(const char *parm, struct sockaddr *addrs, int *ret_count)
 		}
 	}
 
-	/* Get the entries for this host.  */
-	hst4 = gethostbyname(ipaddr);
-	hst6 = gethostbyname2(ipaddr, AF_INET6);
-
-	if ((NULL == hst4 || hst4->h_length < 1)
-	    && (NULL == hst6 || hst6->h_length < 1)) {
+	memset(&hints, 0, sizeof(struct addrinfo));
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_protocol = IPPROTO_SCTP;
+	if (getaddrinfo(ipaddr, NULL, &hints, &res) != 0) {
 		fprintf(stderr, "bad hostname: %s\n", ipaddr);
 		goto finally;
 	}
 
-	/* Figure out the number of addresses.  */
-	if (NULL != hst4) {
-		for (i4 = 0; NULL != hst4->h_addr_list[i4]; ++i4) {
-			count++;
-		}
-	}
-	if (NULL != hst6) {
-		for (i6 = 0; NULL != hst6->h_addr_list[i6]; ++i6) {
-			count++;
-		}
-	}
+	for (rp = res; rp != NULL; rp = rp->ai_next)
+		count++;
 
 	/* Expand memory for the new addresses.  Assume all the addresses
 	 * are v6 addresses.
@@ -565,34 +551,17 @@ append_addr(const char *parm, struct sockaddr *addrs, int *ret_count)
 	}
 
 	/* Put the new addresses away.  */
-	if (NULL != hst4) {
-		for (j = 0; j < i4; ++j) {
-			b4ap = (struct sockaddr_in *)aptr;
-			memset(b4ap, 0x00, sizeof(*b4ap));
-			b4ap->sin_family = AF_INET;
-			b4ap->sin_port = htons(local_port);
-			bcopy(hst4->h_addr_list[j], &b4ap->sin_addr,
-			      hst4->h_length);
-
-			aptr += sizeof(struct sockaddr_in);
-		} /* for (loop through the new v4 addresses) */
-	}
-
-	if (NULL != hst6) {
-		for (j = 0; j < i6; ++j) {
-			b6ap = (struct sockaddr_in6 *)aptr;
-			memset(b6ap, 0x00, sizeof(*b6ap));
-			b6ap->sin6_family = AF_INET6;
-			b6ap->sin6_port =  htons(local_port);
+	for (rp = res; rp != NULL; rp = rp->ai_next) {
+		b4ap = (struct sockaddr_in *)aptr;
+		b6ap = (struct sockaddr_in6 *)aptr;
+		bcopy(rp->ai_addr, aptr, rp->ai_addrlen);
+		b4ap->sin_port = htons(local_port); /* equal to b6ap.v6.sin6_port */
+		if (rp->ai_family == AF_INET6) {
 			b6ap->sin6_scope_id = if_index;
-			bcopy(hst6->h_addr_list[j], &b6ap->sin6_addr,
-			      hst6->h_length);
-			if (!ifindex) {
+			if (!ifindex)
 				b6ap->sin6_scope_id = ifindex;
-			}
-
-			aptr += sizeof(struct sockaddr_in6);
-		} /* for (loop through the new v6 addresses) */
+		}
+		aptr += rp->ai_addrlen;
 	}
 
  finally:
Signature · 1 line
2.27.0
author maintainer
#4 lore raw

PATCH 4/4 sctp_darn: replace use of deprecated gethostbyname with getaddrinfo

In reply to Xin Long

This patch is to replace use of deprecated gethostbyname with getaddrinfo in the file src/apps/sctp_darn.c.

Note that different from sctp_test, sctp_darn prefers v4 address to v6 address when getting addrinfo from hostname. To keep the compatibility, we don't change it.

---
 configure.ac         |   2 +-
 src/apps/sctp_darn.c | 246 +++++++++++++------------------------------
 2 files changed, 72 insertions(+), 176 deletions(-)
configure.ac +1 -1
index 8345dab..28132bf 100644
@@ -75,7 +75,7 @@ AC_FUNC_REALLOC
 AC_FUNC_SELECT_ARGTYPES
 AC_FUNC_SETVBUF_REVERSED
 AC_FUNC_VPRINTF
-AC_CHECK_FUNCS([bzero getaddrinfo gethostbyname gettimeofday memmove memset select socket \
+AC_CHECK_FUNCS([bzero getaddrinfo gettimeofday memmove memset select socket \
 		strchr strerror strtol strtoul])
 
 # Support for stream reset even, added on v4.11, 35ea82d611da
src/apps/sctp_darn.c +71 -175

348 lines not on this page · raw message

Signature · 1 line
2.27.0
author maintainer
#5 lore raw
In reply to Xin Long
Quoted 3 lines · Xin Long

This patchset is simply to replace use of deprecated gethostbyname with getaddrinfo in myftp, sctp_xconnect, sctp_test and sctp_darn.

There are three ways to use this function in this patchset:

a. get IPv4 addresses by a hostname:

               hints.ai_family = AF_INET;
               hints.ai_protocol = IPPROTO_SCTP;
               getaddrinfo(remote_host, NULL, &hints, &rp)

b. get IPv6 addresses by a hostname:

               hints.ai_family = AF_INET6;
               hints.ai_protocol = IPPROTO_SCTP;
               getaddrinfo(remote_host, NULL, &hints, &rp)

c. get IPv4 and IPv6 addresses by a hostname:

               hints.ai_family = AF_UNSPEC;
               hints.ai_protocol = IPPROTO_SCTP;
               getaddrinfo(remote_host, NULL, &hints, &rp)

Note ai_protocol = IPPROTO_SCTP is necessary, especially for case c. Otherwise, it will return many addresses with different protocol(TCP, UDP...).

Quoted 18 lines

Xin Long (4):
myftp: replace use of deprecated gethostbyname with getaddrinfo
sctp_xconnect: replace use of deprecated gethostbyname with
getaddrinfo
sctp_test: replace use of deprecated gethostbyname with getaddrinfo
sctp_darn: replace use of deprecated gethostbyname with getaddrinfo

 configure.ac             |   2 +-
 src/apps/myftp.c         |  40 +++----
 src/apps/sctp_darn.c     | 246 +++++++++++----------------------------
 src/apps/sctp_test.c     |  63 +++-------
 src/apps/sctp_xconnect.c |  72 +++++-------
 5 files changed, 137 insertions(+), 286 deletions(-)

--
2.27.0

#6 lore raw
In reply to Xin Long
Quoted 2 lines · Xin Long

This patchset is simply to replace use of deprecated gethostbyname with getaddrinfo in myftp, sctp_xconnect, sctp_test and sctp_darn.

Applied. Thanks Xin.