[CRIU] [PATCHv4 4/7] remote: Refactor TCP client setup

Radostin Stoyanov rstoyanov1 at gmail.com
Thu Sep 13 20:28:08 MSK 2018


Remove setup_TCP_client_socket() and use setup_tcp_server() instead as
both functions have very similar functionality.

Signed-off-by: Radostin Stoyanov <rstoyanov1 at gmail.com>
---
 criu/img-proxy.c          |  3 ++-
 criu/img-remote.c         | 36 ------------------------------------
 criu/include/img-remote.h |  1 -
 criu/include/util.h       |  2 +-
 criu/page-xfer.c          |  2 +-
 criu/util.c               |  8 ++++----
 6 files changed, 8 insertions(+), 44 deletions(-)

diff --git a/criu/img-proxy.c b/criu/img-proxy.c
index b504eae5..e73620cd 100644
--- a/criu/img-proxy.c
+++ b/criu/img-proxy.c
@@ -5,6 +5,7 @@
 #include <fcntl.h>
 #include <sys/socket.h>
 #include "cr_options.h"
+#include "util.h"
 
 int image_proxy(bool background, char *local_proxy_path, char *fwd_host, unsigned short fwd_port)
 {
@@ -22,7 +23,7 @@ int image_proxy(bool background, char *local_proxy_path, char *fwd_host, unsigne
 		proxy_to_cache_fd = opts.ps_socket;
 		pr_info("Re-using ps socket %d\n", proxy_to_cache_fd);
 	} else {
-		proxy_to_cache_fd = setup_TCP_client_socket(fwd_host, fwd_port);
+		proxy_to_cache_fd = setup_tcp_client(fwd_host, fwd_port);
 		if (proxy_to_cache_fd < 0) {
 			pr_perror("Unable to open proxy to cache TCP socket");
 			return -1; // TODO - should close other sockets.
diff --git a/criu/img-remote.c b/criu/img-remote.c
index 4391a1f9..c16288bc 100644
--- a/criu/img-remote.c
+++ b/criu/img-remote.c
@@ -118,42 +118,6 @@ struct roperation *get_rop_by_name(
 	return NULL;
 }
 
-int setup_TCP_client_socket(char *hostname, int port)
-{
-	int sockfd;
-	struct sockaddr_in serv_addr;
-	struct hostent *server;
-
-	sockfd = socket(AF_INET, SOCK_STREAM, 0);
-	if (sockfd < 0) {
-		pr_perror("Unable to open remote image socket");
-		return -1;
-	}
-
-	server = gethostbyname(hostname);
-	if (server == NULL) {
-		pr_perror("Unable to get host by name (%s)", hostname);
-		goto err;
-	}
-
-	bzero((char *) &serv_addr, sizeof(serv_addr));
-	serv_addr.sin_family = AF_INET;
-	bcopy((char *) server->h_addr,
-		(char *) &serv_addr.sin_addr.s_addr,
-		server->h_length);
-	serv_addr.sin_port = htons(port);
-
-	if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
-		pr_perror("Unable to connect to remote %s", hostname);
-		goto err;
-	}
-
-	return sockfd;
-err:
-	close(sockfd);
-	return -1;
-}
-
 int event_set(int epoll_fd, int op, int fd, uint32_t events, void *data)
 {
 	int ret;
diff --git a/criu/include/img-remote.h b/criu/include/img-remote.h
index 99034ef8..eb071ca8 100644
--- a/criu/include/img-remote.h
+++ b/criu/include/img-remote.h
@@ -82,7 +82,6 @@ int64_t recv_image_async(struct roperation *op);
 int64_t read_remote_header(int fd, char *snapshot_id, char *path, int *open_mode, uint64_t *size);
 int64_t write_remote_header(int fd, char *snapshot_id, char *path, int open_mode, uint64_t size);
 
-int setup_TCP_client_socket(char *hostname, int port);
 int setup_UNIX_server_socket(char *path);
 void socket_set_non_blocking(int fd);
 
diff --git a/criu/include/util.h b/criu/include/util.h
index 1e2fec81..d9a1ff0f 100644
--- a/criu/include/util.h
+++ b/criu/include/util.h
@@ -291,7 +291,7 @@ void print_data(unsigned long addr, unsigned char *data, size_t size);
 
 int setup_tcp_server(char *type, char *addr, unsigned short *port);
 int run_tcp_server(bool daemon_mode, int *ask, int cfd, int sk);
-int setup_tcp_client(char *hostname);
+int setup_tcp_client(char *hostname, unsigned short port);
 
 #define LAST_PID_PATH		"sys/kernel/ns_last_pid"
 #define PID_MAX_PATH		"sys/kernel/pid_max"
diff --git a/criu/page-xfer.c b/criu/page-xfer.c
index a949f487..ed84bef5 100644
--- a/criu/page-xfer.c
+++ b/criu/page-xfer.c
@@ -1054,7 +1054,7 @@ static int connect_to_page_server(void)
 		goto out;
 	}
 
-	page_server_sk = setup_tcp_client(opts.addr);
+	page_server_sk = setup_tcp_client(opts.addr, opts.port);
 	if (page_server_sk == -1)
 		return -1;
 out:
diff --git a/criu/util.c b/criu/util.c
index bdb4aadf..b8bcf4a2 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -1392,7 +1392,7 @@ out:
 	return -1;
 }
 
-int setup_tcp_client(char *hostname)
+int setup_tcp_client(char *hostname, unsigned short port)
 {
 	struct sockaddr_storage saddr;
 	struct addrinfo addr_criteria, *addr_list, *p;
@@ -1429,9 +1429,9 @@ int setup_tcp_client(char *hostname)
 		}
 
 		inet_ntop(p->ai_family, ip, ipstr, sizeof(ipstr));
-		pr_info("Connecting to server %s:%u\n", ipstr, opts.port);
+		pr_info("Connecting to server %s:%u\n", ipstr, port);
 
-		if (get_sockaddr_in(&saddr, ipstr, opts.port))
+		if (get_sockaddr_in(&saddr, ipstr, port))
 			goto out;
 
 		sk = socket(saddr.ss_family, SOCK_STREAM, IPPROTO_TCP);
@@ -1441,7 +1441,7 @@ int setup_tcp_client(char *hostname)
 		}
 
 		if (connect(sk, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
-			pr_info("Can't connect to server %s:%u\n", ipstr, opts.port);
+			pr_info("Can't connect to server %s:%u\n", ipstr, port);
 			close(sk);
 			sk = -1;
 		} else {
-- 
2.17.1



More information about the CRIU mailing list