[CRIU] [PATCHv4] remote: Fix incorrect handling of `port` option

Radostin Stoyanov rstoyanov1 at gmail.com
Wed Jan 31 13:24:44 MSK 2018


The `port` option is converted from unsigned short integer to
network byte order twice. Unfortunately the 2nd conversion
reverses the 1st one.

Example:

	#include <stdio.h>
	#include <arpa/inet.h>
	#include <stdlib.h>

	int main()
	{
	    printf("%d\n", htons(atoi("1234"))); /* 53764 */
	    printf("%d\n", htons(htons(atoi("1234")))); /* 1234 */
	    return 0;
	}

Signed-off-by: Radostin Stoyanov <rstoyanov1 at gmail.com>
---
 criu/cr-service.c | 2 +-
 criu/crtools.c    | 2 +-
 criu/page-xfer.c  | 2 +-
 criu/util.c       | 6 +++---
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/criu/cr-service.c b/criu/cr-service.c
index 33d41732..a1e637af 100644
--- a/criu/cr-service.c
+++ b/criu/cr-service.c
@@ -377,7 +377,7 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
 	}
 
 	if (req->ps) {
-		opts.port = htons((short)req->ps->port);
+		opts.port = (short)req->ps->port;
 
 		if (!opts.lazy_pages) {
 			opts.use_page_server = true;
diff --git a/criu/crtools.c b/criu/crtools.c
index 07bb2655..dffae5c8 100644
--- a/criu/crtools.c
+++ b/criu/crtools.c
@@ -719,7 +719,7 @@ int main(int argc, char *argv[], char *envp[])
 			opts.addr = optarg;
 			break;
 		case 1052:
-			opts.port = htons(atoi(optarg));
+			opts.port = atoi(optarg);
 			if (!opts.port)
 				goto bad_arg;
 			break;
diff --git a/criu/page-xfer.c b/criu/page-xfer.c
index 9ae21161..b1624059 100644
--- a/criu/page-xfer.c
+++ b/criu/page-xfer.c
@@ -1077,7 +1077,7 @@ int disconnect_from_page_server(void)
 		return 0;
 
 	pr_info("Disconnect from the page server %s:%u\n",
-			opts.addr, (int)ntohs(opts.port));
+			opts.addr, opts.port);
 
 	if (opts.ps_socket != -1)
 		/*
diff --git a/criu/util.c b/criu/util.c
index b252e93d..41eb15ac 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -1275,7 +1275,7 @@ static int get_sockaddr_in(struct sockaddr_in *addr, char *host)
 		return -1;
 	}
 
-	addr->sin_port = opts.port;
+	addr->sin_port = htons(opts.port);
 	return 0;
 }
 
@@ -1285,7 +1285,7 @@ int setup_tcp_server(char *type)
 	struct sockaddr_in saddr;
 	socklen_t slen = sizeof(saddr);
 
-	pr_info("Starting %s server on port %u\n", type, (int)ntohs(opts.port));
+	pr_info("Starting %s server on port %u\n", type, opts.port);
 
 	sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 	if (sk < 0) {
@@ -1375,7 +1375,7 @@ int setup_tcp_client(char *addr)
 	struct sockaddr_in saddr;
 	int sk;
 
-	pr_info("Connecting to server %s:%u\n", addr, (int)ntohs(opts.port));
+	pr_info("Connecting to server %s:%u\n", addr, opts.port);
 
 	if (get_sockaddr_in(&saddr, addr))
 		return -1;
-- 
2.14.3



More information about the CRIU mailing list