linux-sctp.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo
@ 2021-08-04 11:29 Xin Long
  2021-08-04 11:29 ` [PATCH lksctp-tools 1/4] myftp: " Xin Long
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Xin Long @ 2021-08-04 11:29 UTC (permalink / raw)
  To: linux-sctp, Marcelo Ricardo Leitner

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(-)

-- 
2.27.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH lksctp-tools 1/4] myftp: replace use of deprecated gethostbyname with getaddrinfo
  2021-08-04 11:29 [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo Xin Long
@ 2021-08-04 11:29 ` Xin Long
  2021-08-04 11:29 ` [PATCH lksctp-tools 2/4] sctp_xconnect: " Xin Long
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Xin Long @ 2021-08-04 11:29 UTC (permalink / raw)
  To: linux-sctp, Marcelo Ricardo Leitner

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

Signed-off-by: Xin Long <lucien.xin@gmail.com>
---
 configure.ac     |  2 +-
 src/apps/myftp.c | 40 ++++++++++++++++++----------------------
 2 files changed, 19 insertions(+), 23 deletions(-)

diff --git a/configure.ac b/configure.ac
index 4e2f7b4..8345dab 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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
diff --git a/src/apps/myftp.c b/src/apps/myftp.c
index 64fa3f2..473fa03 100644
--- a/src/apps/myftp.c
+++ b/src/apps/myftp.c
@@ -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;
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH lksctp-tools 2/4] sctp_xconnect: replace use of deprecated gethostbyname with getaddrinfo
  2021-08-04 11:29 [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo Xin Long
  2021-08-04 11:29 ` [PATCH lksctp-tools 1/4] myftp: " Xin Long
@ 2021-08-04 11:29 ` Xin Long
  2021-08-04 11:29 ` [PATCH lksctp-tools 3/4] sctp_test: " Xin Long
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Xin Long @ 2021-08-04 11:29 UTC (permalink / raw)
  To: linux-sctp, Marcelo Ricardo Leitner

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

Signed-off-by: Xin Long <lucien.xin@gmail.com>
---
 src/apps/sctp_xconnect.c | 72 +++++++++++++++++-----------------------
 1 file changed, 31 insertions(+), 41 deletions(-)

diff --git a/src/apps/sctp_xconnect.c b/src/apps/sctp_xconnect.c
index 6759c0e..5cd1049 100644
--- a/src/apps/sctp_xconnect.c
+++ b/src/apps/sctp_xconnect.c
@@ -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]);
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH lksctp-tools 3/4] sctp_test: replace use of deprecated gethostbyname with getaddrinfo
  2021-08-04 11:29 [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo Xin Long
  2021-08-04 11:29 ` [PATCH lksctp-tools 1/4] myftp: " Xin Long
  2021-08-04 11:29 ` [PATCH lksctp-tools 2/4] sctp_xconnect: " Xin Long
@ 2021-08-04 11:29 ` Xin Long
  2021-08-04 11:29 ` [PATCH lksctp-tools 4/4] sctp_darn: " Xin Long
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Xin Long @ 2021-08-04 11:29 UTC (permalink / raw)
  To: linux-sctp, Marcelo Ricardo Leitner

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

Signed-off-by: Xin Long <lucien.xin@gmail.com>
---
 src/apps/sctp_test.c | 63 +++++++++++---------------------------------
 1 file changed, 16 insertions(+), 47 deletions(-)

diff --git a/src/apps/sctp_test.c b/src/apps/sctp_test.c
index cd7654b..e382804 100644
--- a/src/apps/sctp_test.c
+++ b/src/apps/sctp_test.c
@@ -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:
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH lksctp-tools 4/4] sctp_darn: replace use of deprecated gethostbyname with getaddrinfo
  2021-08-04 11:29 [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo Xin Long
                   ` (2 preceding siblings ...)
  2021-08-04 11:29 ` [PATCH lksctp-tools 3/4] sctp_test: " Xin Long
@ 2021-08-04 11:29 ` Xin Long
  2021-08-04 23:47 ` [PATCH lksctp-tools 0/4] lksctp-tools: " Xin Long
  2021-08-11 17:27 ` mleitner
  5 siblings, 0 replies; 7+ messages in thread
From: Xin Long @ 2021-08-04 11:29 UTC (permalink / raw)
  To: linux-sctp, Marcelo Ricardo Leitner

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.

Signed-off-by: Xin Long <lucien.xin@gmail.com>
---
 configure.ac         |   2 +-
 src/apps/sctp_darn.c | 246 +++++++++++++------------------------------
 2 files changed, 72 insertions(+), 176 deletions(-)

diff --git a/configure.ac b/configure.ac
index 8345dab..28132bf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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
diff --git a/src/apps/sctp_darn.c b/src/apps/sctp_darn.c
index bcfb822..133c1c6 100644
--- a/src/apps/sctp_darn.c
+++ b/src/apps/sctp_darn.c
@@ -126,7 +126,6 @@ int if_index = 0;
 sockaddr_storage_t remote_addr;
 sa_family_t ra_family;	/* What family is remote_addr? */
 int ra_len = 0;		/* How long is remote_addr? */
-void *ra_raw;		/* This is the addr part of remote_addr. */
 int new_connection = 1;
 
 enum inter_cmd_num {
@@ -491,49 +490,29 @@ int
 build_endpoint(char *argv0, int portnum)
 {
 	int retval;
-	struct hostent *hst;
+	struct addrinfo hints, *rp;
 	sockaddr_storage_t local_addr;
-	sa_family_t la_family;	/* What family is local_addr? */
-	int la_len;		/* How long is local_addr? */
-	void *la_raw;		/* This is the addr part of local_addr. */
 	int error;
 	struct sctp_event_subscribe subscribe;
 
 	/* Get the transport address for the local host name.  */
-	hst = gethostbyname(local_host);
-	if (hst == NULL) {
-		hst = gethostbyname2(local_host, AF_INET6);
-	}
-
-	if (hst == NULL || hst->h_length < 1) {
-		fprintf(stderr, "%s: bad hostname: %s\n", argv0, local_host);
-		exit(1);
+	memset(&hints, 0, sizeof(struct addrinfo));
+	hints.ai_family = AF_INET;
+	hints.ai_protocol = IPPROTO_SCTP;
+	if (getaddrinfo(local_host, NULL, &hints, &rp) != 0) {
+		hints.ai_family = AF_INET6;
+		if (getaddrinfo(local_host, NULL, &hints, &rp) != 0) {
+			fprintf(stderr, "%s: bad hostname: %s\n", argv0, local_host);
+			exit(1);
+		}
 	}
-
-	la_family = hst->h_addrtype;
-	switch (la_family) {
-	case AF_INET:
-		la_len = sizeof(local_addr.v4);
-		la_raw = &local_addr.v4.sin_addr;
-		local_addr.v4.sin_port = htons(portnum);
-		local_addr.v4.sin_family = AF_INET;
-		break;
-	case AF_INET6:
-		la_len = sizeof(local_addr.v6);
-		la_raw = &local_addr.v6.sin6_addr;
-		local_addr.v6.sin6_port = htons(portnum);
-		local_addr.v6.sin6_family = AF_INET6;
+	memcpy(&local_addr, rp->ai_addr, rp->ai_addrlen);
+	local_addr.v4.sin_port = htons(portnum); /* equal to v6.sin6_port */
+	if (rp->ai_family == AF_INET6)
 		local_addr.v6.sin6_scope_id = if_index;
-		break;
-	default:
-		fprintf(stderr, "Invalid address type.\n");
-		exit(1);
-		break;
-	}
-	memcpy(la_raw, hst->h_addr_list[0], hst->h_length);
 
 	/* Create the local endpoint.  */
-	retval = socket(la_family, socket_type, IPPROTO_SCTP);
+	retval = socket(rp->ai_family, socket_type, IPPROTO_SCTP);
 	if (retval < 0) {
 		fprintf(stderr, "%s: failed to create socket:  %s.\n",
 			argv0, strerror(errno));
@@ -553,7 +532,7 @@ build_endpoint(char *argv0, int portnum)
 	}
 
 	/* Bind this socket to the test port.  */
-	error = bind(retval, &local_addr.sa, la_len);
+	error = bind(retval, &local_addr.sa, rp->ai_addrlen);
 	if (error != 0) {
 		fprintf(stderr, "%s: can not bind to %s:%d: %s.\n",
 			argv0, local_host, portnum,
@@ -778,7 +757,7 @@ command_send(char *argv0, int *skp)
 	struct iovec iov;
 	int done = 0;
 	char message[REALLY_BIG];
-	struct hostent *hst;
+	struct addrinfo hints, *rp;
 	int c;
 	struct sockaddr *addrs;
 	int msglen;
@@ -787,38 +766,21 @@ command_send(char *argv0, int *skp)
 
 	/* Set up the destination.  */
 	if (remote_host != NULL) {
-		hst = gethostbyname(remote_host);
-		if (hst == NULL) {
-			hst = gethostbyname2(remote_host, AF_INET6);
-		}
-
-		if (hst == NULL || hst->h_length < 1) {
-			fprintf(stderr, "%s: bad hostname: %s\n",
-				argv0, remote_host);
-			exit(1);
+		memset(&hints, 0, sizeof(struct addrinfo));
+		hints.ai_family = AF_INET;
+		hints.ai_protocol = IPPROTO_SCTP;
+		if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) {
+			hints.ai_family = AF_INET6;
+			if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) {
+				fprintf(stderr, "%s: bad hostname: %s\n", argv0, remote_host);
+				exit(1);
+			}
 		}
-
-		ra_family = hst->h_addrtype;
-		switch (ra_family) {
-		case AF_INET:
-			ra_len = sizeof(remote_addr.v4);
-			ra_raw = &remote_addr.v4.sin_addr;
-			remote_addr.v4.sin_port = htons(remote_port);
-			remote_addr.v4.sin_family = AF_INET;
-			break;
-		case AF_INET6:
-			ra_len = sizeof(remote_addr.v6);
-			ra_raw = &remote_addr.v6.sin6_addr;
-			remote_addr.v6.sin6_port = htons(remote_port);
-			remote_addr.v6.sin6_family = AF_INET6;
+		memcpy(&remote_addr, rp->ai_addr, rp->ai_addrlen);
+		remote_addr.v4.sin_port = htons(remote_port); /* equal to v6.sin6_port */
+		if (rp->ai_family == AF_INET6)
 			remote_addr.v6.sin6_scope_id = if_index;
-			break;
-		default:
-			fprintf(stderr, "Invalid address type.\n");
-			exit(1);
-			break;
-		}
-		memcpy(ra_raw, hst->h_addr_list[0], hst->h_length);
+		ra_len = rp->ai_addrlen;
 	}
 
 	/* Initialize the global value for interactive mode functions.  */
@@ -1041,7 +1003,7 @@ command_poll(char *argv0)
 	fd_set *xbitsp = NULL;
 
 	struct msghdr outmsg;
-	struct hostent *hst;
+	struct addrinfo hints, *rp;
 	int msglen;
 	int temp_fd, temp_set;
 
@@ -1050,38 +1012,20 @@ command_poll(char *argv0)
 	/* If a remote host is specified, initialize the destination. */
 	if (remote_host) {
 		/* Set up the destination.  */
-		hst = gethostbyname(remote_host);
-		if (hst == NULL) {
-			hst = gethostbyname2(remote_host, AF_INET6);
-		}
-
-		if (hst == NULL || hst->h_length < 1) {
-			fprintf(stderr, "%s: bad hostname: %s\n",
-				argv0, remote_host);
-			exit(1);
+		memset(&hints, 0, sizeof(struct addrinfo));
+		hints.ai_family = AF_INET;
+		hints.ai_protocol = IPPROTO_SCTP;
+		if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) {
+			hints.ai_family = AF_INET6;
+			if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) {
+				fprintf(stderr, "%s: bad hostname: %s\n", argv0, remote_host);
+				exit(1);
+			}
 		}
-
-		ra_family = hst->h_addrtype;
-		switch (ra_family) {
-		case AF_INET:
-			ra_len = sizeof(remote_addr.v4);
-			ra_raw = &remote_addr.v4.sin_addr;
-			remote_addr.v4.sin_port = htons(remote_port);
-			remote_addr.v4.sin_family = AF_INET;
-			break;
-		case AF_INET6:
-			ra_len = sizeof(remote_addr.v6);
-			ra_raw = &remote_addr.v6.sin6_addr;
-			remote_addr.v6.sin6_port = htons(remote_port);
-			remote_addr.v6.sin6_family = AF_INET6;
+		memcpy(&remote_addr, rp->ai_addr, rp->ai_addrlen);
+		remote_addr.v4.sin_port = htons(remote_port); /* equal to v6.sin6_port */
+		if (rp->ai_family == AF_INET6)
 			remote_addr.v6.sin6_scope_id = if_index;
-			break;
-		default:
-			fprintf(stderr, "Invalid address type.\n");
-			exit(1);
-			break;
-		}
-		memcpy(ra_raw, hst->h_addr_list[0], hst->h_length);
 
 		/* Initialize the message struct we use to pass messages to
 	 	 * the remote socket.
@@ -1091,7 +1035,7 @@ command_poll(char *argv0)
 		outmsg.msg_control = NULL;
 		outmsg.msg_controllen = 0;
 		outmsg.msg_name = &remote_addr;
-		outmsg.msg_namelen = ra_len;
+		outmsg.msg_namelen = rp->ai_addrlen;
 		outmsg.msg_flags = 0;
 	}
 
@@ -1406,14 +1350,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;
@@ -1421,28 +1362,17 @@ append_addr(const char *parm, struct sockaddr *addrs, int *ret_count)
 
 	if (!parm)
 		return NULL;
-	/* Get the entries for this host.  */
-	hst4 = gethostbyname(parm);
-	hst6 = gethostbyname2(parm, 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(parm, NULL, &hints, &res) != 0) {
 		fprintf(stderr, "bad hostname: %s\n", parm);
 		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.
@@ -1473,31 +1403,14 @@ 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);
-
-			aptr += sizeof(struct sockaddr_in6);
-		} /* for (loop through the new v6 addresses) */
+		aptr += rp->ai_addrlen;
 	}
 
  finally:
@@ -2123,7 +2036,7 @@ shutdown_func(char *argv0, int *skp, int shutdown_type)
 	struct cmsghdr *cmsg;
         int error=0, bytes_sent;
 	struct sctp_sndrcvinfo *sinfo;
-	struct hostent *hst;
+	struct addrinfo hints, *rp;
 	char *sd_type;
 	int sk = *skp;
 
@@ -2142,37 +2055,20 @@ shutdown_func(char *argv0, int *skp, int shutdown_type)
 	if (socket_type == SOCK_SEQPACKET) {
 		/* Set up the destination.  */
 		if (remote_host) {
-			hst = gethostbyname(remote_host);
-			if (hst == NULL) {
-				hst = gethostbyname2(remote_host, AF_INET6);
-			}
-
-			if (hst == NULL || hst->h_length < 1) {
-				fprintf(stderr, "%s: bad hostname: %s\n",
-					argv0, remote_host);
-				exit(1);
-			}
-
-			ra_family = hst->h_addrtype;
-			switch (ra_family) {
-			case AF_INET:
-				ra_len = sizeof(remote_addr.v4);
-				ra_raw = &remote_addr.v4.sin_addr;
-				remote_addr.v4.sin_port = htons(remote_port);
-				remote_addr.v4.sin_family = AF_INET;
-				break;
-			case AF_INET6:
-				ra_len = sizeof(remote_addr.v6);
-				ra_raw = &remote_addr.v6.sin6_addr;
-				remote_addr.v6.sin6_port = htons(remote_port);
-				remote_addr.v6.sin6_family = AF_INET6;
-				break;
-			default:
-				fprintf(stderr, "Invalid address type.\n");
-				exit(1);
-				break;
+			memset(&hints, 0, sizeof(struct addrinfo));
+			hints.ai_family = AF_INET;
+			hints.ai_protocol = IPPROTO_SCTP;
+			if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) {
+				hints.ai_family = AF_INET6;
+				if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) {
+					fprintf(stderr, "%s: bad hostname: %s\n",
+						argv0, remote_host);
+					exit(1);
+				}
 			}
-			memcpy(ra_raw, hst->h_addr_list[0], hst->h_length);
+			memcpy(&remote_addr, rp->ai_addr, rp->ai_addrlen);
+			remote_addr.v4.sin_port = htons(remote_port); /* equal to v6.sin6_port */
+			ra_len = rp->ai_addrlen;
 		}
 
 		/* Initialize the message struct we use to pass messages to
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo
  2021-08-04 11:29 [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo Xin Long
                   ` (3 preceding siblings ...)
  2021-08-04 11:29 ` [PATCH lksctp-tools 4/4] sctp_darn: " Xin Long
@ 2021-08-04 23:47 ` Xin Long
  2021-08-11 17:27 ` mleitner
  5 siblings, 0 replies; 7+ messages in thread
From: Xin Long @ 2021-08-04 23:47 UTC (permalink / raw)
  To: linux-sctp @ vger . kernel . org, Marcelo Ricardo Leitner

On Wed, Aug 4, 2021 at 7:29 PM Xin Long <lucien.xin@gmail.com> wrote:
>
> 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...).

>
> 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
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo
  2021-08-04 11:29 [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo Xin Long
                   ` (4 preceding siblings ...)
  2021-08-04 23:47 ` [PATCH lksctp-tools 0/4] lksctp-tools: " Xin Long
@ 2021-08-11 17:27 ` mleitner
  5 siblings, 0 replies; 7+ messages in thread
From: mleitner @ 2021-08-11 17:27 UTC (permalink / raw)
  To: Xin Long; +Cc: linux-sctp

On Wed, Aug 04, 2021 at 07:29:12AM -0400, Xin Long wrote:
> This patchset is simply to replace use of deprecated gethostbyname
> with getaddrinfo in myftp, sctp_xconnect, sctp_test and sctp_darn.

Applied. Thanks Xin.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-08-11 17:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-04 11:29 [PATCH lksctp-tools 0/4] lksctp-tools: replace use of deprecated gethostbyname with getaddrinfo Xin Long
2021-08-04 11:29 ` [PATCH lksctp-tools 1/4] myftp: " Xin Long
2021-08-04 11:29 ` [PATCH lksctp-tools 2/4] sctp_xconnect: " Xin Long
2021-08-04 11:29 ` [PATCH lksctp-tools 3/4] sctp_test: " Xin Long
2021-08-04 11:29 ` [PATCH lksctp-tools 4/4] sctp_darn: " Xin Long
2021-08-04 23:47 ` [PATCH lksctp-tools 0/4] lksctp-tools: " Xin Long
2021-08-11 17:27 ` mleitner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).