From 519897a163eb266a397c3e96f97df87d8df0e026 Mon Sep 17 00:00:00 2001
From: "Christopher J. Morrone" <morrone2@llnl.gov>
Date: Thu, 7 Sep 2006 00:19:45 +0000
Subject: [PATCH] Have net_stream_listen() return the port number in HOST byte
 order, and KEEP the port in host byte order until absolutely necessary to
 change it back to network byte order.

---
 src/api/allocate.c                |  4 ++--
 src/api/spawn.c                   | 14 +++++---------
 src/api/step_ctx.c                |  2 +-
 src/api/step_io.c                 |  6 ++----
 src/api/step_launch.c             |  4 ++--
 src/common/net.c                  | 10 +++++++---
 src/common/net.h                  |  3 ++-
 src/plugins/mpi/mpichgm/mpichgm.c |  2 +-
 src/salloc/msg.c                  |  4 +---
 src/sattach/sattach.c             |  2 +-
 10 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/src/api/allocate.c b/src/api/allocate.c
index 8d93c8c1f33..c4dd22a1798 100644
--- a/src/api/allocate.c
+++ b/src/api/allocate.c
@@ -570,8 +570,8 @@ static listen_t *_create_allocation_response_socket(char *interface_hostname)
 		return NULL;
 	}
 	listen->hostname = xstrdup(interface_hostname);
-	/* FIXME - screw it!  I can't seem to get the port number through slurm_*
-	   functions */
+	/* FIXME - screw it!  I can't seem to get the port number through
+	   slurm_* functions */
 	listen->port = ntohs(listen->address.sin_port);
 	fd_set_nonblocking(listen->fd);
 
diff --git a/src/api/spawn.c b/src/api/spawn.c
index 8c235534d94..870ef5616f7 100644
--- a/src/api/spawn.c
+++ b/src/api/spawn.c
@@ -106,8 +106,6 @@ extern int slurm_spawn (slurm_step_ctx ctx, int *fd_array)
 	int i, rc = SLURM_SUCCESS;
 	uint16_t slurmd_debug = 0;
 	char *env_var;
-	/* hostlist_t hostlist = NULL; */
-/* 	hostlist_iterator_t itr = NULL; */
 	int task_cnt = 0;
 	uint32_t *cpus = NULL;
 	slurm_step_layout_t *step_layout = ctx->step_resp->step_layout;
@@ -154,9 +152,6 @@ extern int slurm_spawn (slurm_step_ctx ctx, int *fd_array)
 	req_array_ptr = xmalloc(sizeof(slurm_msg_t) * 
 				step_layout->node_cnt);
 
-	//hostlist = hostlist_create(step_layout->node_list);
-	//itr = hostlist_iterator_create(hostlist);
-
 	for (i=0; i<step_layout->node_cnt; i++) {
 		spawn_task_request_msg_t *r = &msg_array_ptr[i];
 		slurm_msg_t              *m = &req_array_ptr[i];
@@ -179,7 +174,7 @@ extern int slurm_spawn (slurm_step_ctx ctx, int *fd_array)
 		r->global_task_id	= step_layout->tids[i][0];
 		r->cpus_allocated	= cpus[i];
 		r->srun_node_id	= (uint32_t) i;
-		r->io_port	= ntohs(sock_array[i]);
+		r->io_port	= sock_array[i];
 		m->msg_type	= REQUEST_SPAWN_TASK;
 		m->data		= r;
 		
@@ -191,8 +186,6 @@ extern int slurm_spawn (slurm_step_ctx ctx, int *fd_array)
 		       fd_array[i], r->io_port, i);
 #endif
 	}
-	//hostlist_iterator_destroy(itr);
-	//hostlist_destroy(hostlist);
 	rc = _p_launch(req_array_ptr, ctx);
 
 	xfree(msg_array_ptr);
@@ -222,6 +215,9 @@ slurm_spawn_kill (slurm_step_ctx ctx, uint16_t signal)
 }
 
 
+/*
+ * Returns the port number in host byte order.
+ */
 static int _sock_bind_wild(int sockfd)
 {
 	socklen_t len;
@@ -237,7 +233,7 @@ static int _sock_bind_wild(int sockfd)
 	len = sizeof(sin);
 	if (getsockname(sockfd, (struct sockaddr *) &sin, &len) < 0)
 		return (-1);
-	return (sin.sin_port);
+	return ntohs(sin.sin_port);
 }
 
 
diff --git a/src/api/step_ctx.c b/src/api/step_ctx.c
index f045ced8a2d..cd60cc2d7ab 100644
--- a/src/api/step_ctx.c
+++ b/src/api/step_ctx.c
@@ -94,7 +94,7 @@ slurm_step_ctx_create (const job_step_create_request_msg_t *user_step_req)
 			_free_step_req(step_req);
 			goto fail;
 		}
-		step_req->port = ntohs(port);
+		step_req->port = port;
 		step_req->host = xshort_hostname();
 	}
 
diff --git a/src/api/step_io.c b/src/api/step_io.c
index 722961b13ca..dc52bf465fb 100644
--- a/src/api/step_io.c
+++ b/src/api/step_io.c
@@ -1195,15 +1195,13 @@ client_io_handler_create(slurm_step_io_fds_t fds,
 
 	for (i = 0; i < cio->num_listen; i++) {
 		eio_obj_t *obj;
-		short port;
 
 		if (net_stream_listen(&cio->listensock[i],
-				      &port) < 0) {
+				      &cio->listenport[i]) < 0) {
 			fatal("unable to initialize stdio listen socket: %m");
 		}
-		cio->listenport[i] = ntohs(port);
 		debug("initialized stdio listening socket, port %d\n",
-		      ntohs(cio->listenport[i]));
+		      cio->listenport[i]);
 		/*net_set_low_water(cio->listensock[i], 140);*/
 		obj = _create_listensock_eio(cio->listensock[i], cio);
 		eio_new_initial_obj(cio->eio, obj);
diff --git a/src/api/step_launch.c b/src/api/step_launch.c
index a9b2e8277a1..8743c872038 100644
--- a/src/api/step_launch.c
+++ b/src/api/step_launch.c
@@ -172,7 +172,7 @@ int slurm_step_launch (slurm_step_ctx ctx,
 		env_array_for_step(&env,
 				   ctx->step_resp,
 				   launcher_hostname,
-				   htons(ctx->launch_state->resp_port[0]),
+				   ctx->launch_state->resp_port[0],
 				   ent->h_addr_list[0]);
 		xfree(launcher_hostname);
 	}
@@ -234,7 +234,7 @@ int slurm_step_launch (slurm_step_ctx ctx,
 	launch.num_resp_port = ctx->launch_state->num_resp_port;
 	launch.resp_port = xmalloc(sizeof(uint16_t) * launch.num_resp_port);
 	for (i = 0; i < launch.num_resp_port; i++) {
-		launch.resp_port[i] = ntohs(ctx->launch_state->resp_port[i]);
+		launch.resp_port[i] = ctx->launch_state->resp_port[i];
 	}
 
 	_launch_tasks(ctx, &launch);
diff --git a/src/common/net.c b/src/common/net.c
index bda9b3a5782..856fc3527b1 100644
--- a/src/common/net.c
+++ b/src/common/net.c
@@ -57,7 +57,10 @@
 #  define NET_DEFAULT_BACKLOG	1024
 #endif 
 
-static uint16_t _sock_bind_wild(int sockfd)
+/*
+ * Returns the port number in host byte order.
+ */
+static short _sock_bind_wild(int sockfd)
 {
 	socklen_t len;
 	struct sockaddr_in sin;
@@ -72,14 +75,15 @@ static uint16_t _sock_bind_wild(int sockfd)
 	len = sizeof(sin);
 	if (getsockname(sockfd, (struct sockaddr *) &sin, &len) < 0)
 		return (-1);
-	return (uint16_t)sin.sin_port;
+	return ntohs(sin.sin_port);
 }
 
 /* open a stream socket on an ephemereal port and put it into 
  * the listen state. fd and port are filled in with the new
  * socket's file descriptor and port #.
  *
- * NOTE: port is in network byte order!
+ * OUT fd - listening socket file descriptor number
+ * OUT port - TCP port number in host byte order
  */
 int net_stream_listen(int *fd, short *port)
 {
diff --git a/src/common/net.h b/src/common/net.h
index e979457643e..7eeb52f9a3c 100644
--- a/src/common/net.h
+++ b/src/common/net.h
@@ -45,7 +45,8 @@
  * the listen state. fd and port are filled in with the new
  * socket's file descriptor and port #.
  *
- * NOTE: port is in network byte order!
+ * OUT fd - listening socket file descriptor number
+ * OUT port - TCP port number in host byte order
  */
 int net_stream_listen(int *fd, short *port);
 
diff --git a/src/plugins/mpi/mpichgm/mpichgm.c b/src/plugins/mpi/mpichgm/mpichgm.c
index 034ba4daccf..15276b5ca02 100644
--- a/src/plugins/mpi/mpichgm/mpichgm.c
+++ b/src/plugins/mpi/mpichgm/mpichgm.c
@@ -335,7 +335,7 @@ extern int gmpi_thr_create(srun_job_t *job)
 		return -1;
 	}
 	slurm_attr_destroy(&attr);
-	setenvf (NULL, "GMPI_PORT",  "%u", ntohs(port));
+	setenvf (NULL, "GMPI_PORT",  "%u", port);
 	setenvf (NULL, "GMPI_MAGIC", "%u", job->jobid);
 	setenvf (NULL, "GMPI_NP",    "%d", job->ntasks);
 	setenvf (NULL, "GMPI_SHMEM", "1");
diff --git a/src/salloc/msg.c b/src/salloc/msg.c
index a3b12b78b62..5ba1404e21e 100644
--- a/src/salloc/msg.c
+++ b/src/salloc/msg.c
@@ -80,19 +80,17 @@ extern salloc_msg_thread_t *msg_thr_create(uint16_t *port)
 {
 	int sock = -1;
 	eio_obj_t *obj;
-	short port_tmp;
 	salloc_msg_thread_t *msg_thr = NULL;
 
 	debug("Entering _msg_thr_create()");
 	slurm_uid = (uid_t) slurm_get_slurm_user_id();
 	msg_thr = (salloc_msg_thread_t *)xmalloc(sizeof(salloc_msg_thread_t));
 
-	if (net_stream_listen(&sock, &port_tmp) < 0) {
+	if (net_stream_listen(&sock, (short *)port) < 0) {
 		error("unable to intialize step launch listening socket: %m");
 		xfree(msg_thr);
 		return NULL;
 	}
-	*port = ntohs(port_tmp);
 	debug("port from net_stream_listen is %hu", *port);
 
 	obj = eio_obj_create(sock, &message_socket_ops, NULL);
diff --git a/src/sattach/sattach.c b/src/sattach/sattach.c
index a0244b93ce4..1d622a19742 100644
--- a/src/sattach/sattach.c
+++ b/src/sattach/sattach.c
@@ -418,7 +418,7 @@ static int _msg_thr_create(struct message_thread_state *mts, int num_nodes)
 			error("unable to intialize step launch listening socket: %m");
 			return SLURM_ERROR;
 		}
-		mts->resp_port[i] = ntohs(port);
+		mts->resp_port[i] = port;
 		obj = eio_obj_create(sock, &message_socket_ops, (void *)mts);
 		eio_new_initial_obj(mts->msg_handle, obj);
 	}
-- 
GitLab