[CRIU] [PATCHv3 4/8] util: Enable hostname resolution of tcp address
Radostin Stoyanov
rstoyanov1 at gmail.com
Wed Sep 5 00:26:53 MSK 2018
Add hostname resolution in setup_tcp_client(). This change allows a
valid hostname to be provided as value for the --address option when
connecting to page server.
Signed-off-by: Radostin Stoyanov <rstoyanov1 at gmail.com>
---
Documentation/criu.txt | 2 +-
criu/include/util.h | 2 +-
criu/util.c | 56 +++++++++++++++++++++++++++++++-----------
3 files changed, 44 insertions(+), 16 deletions(-)
diff --git a/Documentation/criu.txt b/Documentation/criu.txt
index 6bb3f0f5..04e7a610 100644
--- a/Documentation/criu.txt
+++ b/Documentation/criu.txt
@@ -550,7 +550,7 @@ Launches *criu* in page server mode.
It isn't supposed to use --daemon and --status-fd together.
*--address* 'address'::
- Page server IP address.
+ Page server IP address or hostname.
*--port* 'number'::
Page server port number.
diff --git a/criu/include/util.h b/criu/include/util.h
index b286b3e9..aba16818 100644
--- a/criu/include/util.h
+++ b/criu/include/util.h
@@ -293,7 +293,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 *addr);
+int setup_tcp_client(char *hostname);
#define LAST_PID_PATH "sys/kernel/ns_last_pid"
#define PID_MAX_PATH "sys/kernel/pid_max"
diff --git a/criu/util.c b/criu/util.c
index 71bb2f6e..5c9fb306 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -30,6 +30,7 @@
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/socket.h>
+#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sched.h>
@@ -1384,28 +1385,55 @@ out:
return -1;
}
-int setup_tcp_client(char *addr)
+int setup_tcp_client(char *hostname)
{
struct sockaddr_storage saddr;
- int sk;
-
- pr_info("Connecting to server %s:%u\n", addr, opts.port);
+ struct addrinfo addr_criteria, *addr_list, *p;
+ char ipstr[INET6_ADDRSTRLEN];
+ int sk = -1;
+ void *ip;
- if (get_sockaddr_in(&saddr, addr, opts.port))
- return -1;
+ memset(&addr_criteria, 0, sizeof(addr_criteria));
+ addr_criteria.ai_family = AF_UNSPEC;
+ addr_criteria.ai_socktype = SOCK_STREAM;
+ addr_criteria.ai_protocol = IPPROTO_TCP;
- sk = socket(saddr.ss_family, SOCK_STREAM, IPPROTO_TCP);
- if (sk < 0) {
- pr_perror("Can't create socket");
- return -1;
+ if (getaddrinfo(hostname, NULL, &addr_criteria, &addr_list)) {
+ pr_perror("Failed to resolve hostname: %s", hostname);
+ goto out;
}
- if (connect(sk, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
- pr_perror("Can't connect to server");
- close(sk);
- return -1;
+ for(p = addr_list; p != NULL; p = p->ai_next) {
+
+ if (p->ai_family == AF_INET) {
+ struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
+ ip = &(ipv4->sin_addr);
+ } else {
+ struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
+ ip = &(ipv6->sin6_addr);
+ }
+
+ inet_ntop(p->ai_family, ip, ipstr, sizeof(ipstr));
+ pr_info("Connecting to server %s:%u\n", ipstr, opts.port);
+
+ if (get_sockaddr_in(&saddr, ipstr, opts.port))
+ goto out;
+
+ sk = socket(saddr.ss_family, SOCK_STREAM, IPPROTO_TCP);
+ if (sk < 0) {
+ pr_perror("Can't create socket");
+ goto out;
+ }
+
+ if (connect(sk, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
+ pr_info("Can't connect to server %s:%u\n", ipstr, opts.port);
+ close(sk);
+ sk = -1;
+ }
}
+out:
+ freeaddrinfo(addr_list);
return sk;
}
--
2.17.1
More information about the CRIU
mailing list