]> xenbits.xensource.com Git - libvirt.git/commitdiff
rpc: allow selection of TCP address family
authorDaniel P. Berrange <berrange@redhat.com>
Thu, 21 May 2015 14:51:28 +0000 (15:51 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Thu, 11 Jun 2015 11:11:18 +0000 (12:11 +0100)
By default, getaddrinfo() will return addresses for both
IPv4 and IPv6 if both protocols are enabled, and so the
RPC code will listen/connect to both protocols too. There
may be cases where it is desirable to restrict this to
just one of the two protocols, so add an 'int family'
parameter to all the TCP related APIs.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
daemon/libvirtd.c
src/libxl/libxl_migration.c
src/qemu/qemu_migration.c
src/remote/remote_driver.c
src/rpc/virnetclient.c
src/rpc/virnetclient.h
src/rpc/virnetserverservice.c
src/rpc/virnetserverservice.h
src/rpc/virnetsocket.c
src/rpc/virnetsocket.h
tests/virnetsockettest.c

index 3e7f87c372137af871b31f543a994cfbad1aff7a..1e2c002ebacbcdd9583c4908289d1035c1715fcb 100644 (file)
@@ -509,6 +509,7 @@ daemonSetupNetworking(virNetServerPtr srv,
                       config->listen_addr, config->tcp_port);
             if (!(svcTCP = virNetServerServiceNewTCP(config->listen_addr,
                                                      config->tcp_port,
+                                                     AF_UNSPEC,
                                                      config->auth_tcp,
 #if WITH_GNUTLS
                                                      NULL,
@@ -552,6 +553,7 @@ daemonSetupNetworking(virNetServerPtr srv,
             if (!(svcTLS =
                   virNetServerServiceNewTCP(config->listen_addr,
                                             config->tls_port,
+                                            AF_UNSPEC,
                                             config->auth_tls,
                                             ctxt,
                                             false,
index 10b5bd6ee1cb4fc90d71d8acdc243543ad0b1339..39e4a65b93e26e671a577dd97d3dc8c840b865c3 100644 (file)
@@ -389,7 +389,9 @@ libxlDomainMigrationPrepare(virConnectPtr dconn,
 
     snprintf(portstr, sizeof(portstr), "%d", port);
 
-    if (virNetSocketNewListenTCP(hostname, portstr, &socks, &nsocks) < 0) {
+    if (virNetSocketNewListenTCP(hostname, portstr,
+                                 AF_UNSPEC,
+                                 &socks, &nsocks) < 0) {
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("Fail to create socket for incoming migration"));
         goto error;
@@ -491,7 +493,9 @@ libxlDomainMigrationPerform(libxlDriverPrivatePtr driver,
     snprintf(portstr, sizeof(portstr), "%d", port);
 
     /* socket connect to dst host:port */
-    if (virNetSocketNewConnectTCP(hostname, portstr, &sock) < 0) {
+    if (virNetSocketNewConnectTCP(hostname, portstr,
+                                  AF_UNSPEC,
+                                  &sock) < 0) {
         virReportSystemError(saved_errno,
                              _("unable to connect to '%s:%s'"),
                              hostname, portstr);
index 70400f379f7522bbfb3f8c14df8444ae363ebc83..b623fbcd82028c3696f907aef1a0d8ca07b2c192 100644 (file)
@@ -3866,7 +3866,9 @@ qemuMigrationConnect(virQEMUDriverPtr driver,
 
     if (virSecurityManagerSetSocketLabel(driver->securityManager, vm->def) < 0)
         goto cleanup;
-    if (virNetSocketNewConnectTCP(host, port, &sock) == 0) {
+    if (virNetSocketNewConnectTCP(host, port,
+                                  AF_UNSPEC,
+                                  &sock) == 0) {
         spec->dest.fd.qemu = virNetSocketDupFD(sock, true);
         virObjectUnref(sock);
     }
index dd8dab69f69244ac29b94aff905b5a91d64ec687..273799b4a4bc1ee2144886e6862559d351fd3b99 100644 (file)
@@ -819,7 +819,7 @@ doRemoteOpen(virConnectPtr conn,
 
         /*FALLTHROUGH*/
     case trans_tcp:
-        priv->client = virNetClientNewTCP(priv->hostname, port);
+        priv->client = virNetClientNewTCP(priv->hostname, port, AF_UNSPEC);
         if (!priv->client)
             goto failed;
 
@@ -854,6 +854,7 @@ doRemoteOpen(virConnectPtr conn,
 
         priv->client = virNetClientNewLibSSH2(priv->hostname,
                                               port,
+                                              AF_UNSPEC,
                                               username,
                                               keyfile,
                                               knownHosts,
index 7fca055e86051e52815fe93509df7885f14f9284..d0b96b654f38aac03b3e40d2b5370e230e38f207 100644 (file)
@@ -349,11 +349,14 @@ virNetClientPtr virNetClientNewUNIX(const char *path,
 
 
 virNetClientPtr virNetClientNewTCP(const char *nodename,
-                                   const char *service)
+                                   const char *service,
+                                   int family)
 {
     virNetSocketPtr sock;
 
-    if (virNetSocketNewConnectTCP(nodename, service, &sock) < 0)
+    if (virNetSocketNewConnectTCP(nodename, service,
+                                  family,
+                                  &sock) < 0)
         return NULL;
 
     return virNetClientNew(sock, nodename);
@@ -383,6 +386,7 @@ virNetClientPtr virNetClientNewSSH(const char *nodename,
         VAR = VAL;
 virNetClientPtr virNetClientNewLibSSH2(const char *host,
                                        const char *port,
+                                       int family,
                                        const char *username,
                                        const char *privkeyPath,
                                        const char *knownHostsPath,
@@ -473,7 +477,9 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host,
     if (!(command = virBufferContentAndReset(&buf)))
         goto no_memory;
 
-    if (virNetSocketNewConnectLibSSH2(host, port, username, privkey,
+    if (virNetSocketNewConnectLibSSH2(host, port,
+                                      family,
+                                      username, privkey,
                                       knownhosts, knownHostsVerify, authMethods,
                                       command, authPtr, uri, &sock) != 0)
         goto cleanup;
index 3bcde6324361ab7ff5c7424816ffcfe1e3e7a4a1..38f929ca558353b859e278cd46f0cedb20f90abc 100644 (file)
@@ -41,7 +41,8 @@ virNetClientPtr virNetClientNewUNIX(const char *path,
                                     const char *binary);
 
 virNetClientPtr virNetClientNewTCP(const char *nodename,
-                                   const char *service);
+                                   const char *service,
+                                   int family);
 
 virNetClientPtr virNetClientNewSSH(const char *nodename,
                                    const char *service,
@@ -55,6 +56,7 @@ virNetClientPtr virNetClientNewSSH(const char *nodename,
 
 virNetClientPtr virNetClientNewLibSSH2(const char *host,
                                        const char *port,
+                                       int family,
                                        const char *username,
                                        const char *privkeyPath,
                                        const char *knownHostsPath,
index 9087473efd399a26756d2103d7519f2b74c5187f..4df26cb52f43101de13e392619aa7a59c3496479 100644 (file)
@@ -143,6 +143,7 @@ virNetServerServiceNewFDOrUNIX(const char *path,
 
 virNetServerServicePtr virNetServerServiceNewTCP(const char *nodename,
                                                  const char *service,
+                                                 int family,
                                                  int auth,
 #if WITH_GNUTLS
                                                  virNetTLSContextPtr tls,
@@ -169,6 +170,7 @@ virNetServerServicePtr virNetServerServiceNewTCP(const char *nodename,
 
     if (virNetSocketNewListenTCP(nodename,
                                  service,
+                                 family,
                                  &svc->socks,
                                  &svc->nsocks) < 0)
         goto error;
index b1d6c2d3c51954cd795b74f5476fdde6967db0d4..9afa0b13dbc6e4a90212f8a2c8718f96ef1deebd 100644 (file)
@@ -51,6 +51,7 @@ virNetServerServicePtr virNetServerServiceNewFDOrUNIX(const char *path,
                                                       unsigned int *cur_fd);
 virNetServerServicePtr virNetServerServiceNewTCP(const char *nodename,
                                                  const char *service,
+                                                 int family,
                                                  int auth,
 # if WITH_GNUTLS
                                                  virNetTLSContextPtr tls,
index 51f94d4bf1d7453e2cd2f82ecac94e7a26b7c7a4..d9f3e11b0fa0e3cc50e12889407ed0d503ccfcf8 100644 (file)
@@ -219,6 +219,7 @@ static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
 
 int virNetSocketNewListenTCP(const char *nodename,
                              const char *service,
+                             int family,
                              virNetSocketPtr **retsocks,
                              size_t *nretsocks)
 {
@@ -236,6 +237,7 @@ int virNetSocketNewListenTCP(const char *nodename,
     *nretsocks = 0;
 
     memset(&hints, 0, sizeof(hints));
+    hints.ai_family = family;
     hints.ai_flags = AI_PASSIVE;
     hints.ai_socktype = SOCK_STREAM;
 
@@ -454,6 +456,7 @@ int virNetSocketNewListenFD(int fd,
 
 int virNetSocketNewConnectTCP(const char *nodename,
                               const char *service,
+                              int family,
                               virNetSocketPtr *retsock)
 {
     struct addrinfo *ai = NULL;
@@ -470,6 +473,7 @@ int virNetSocketNewConnectTCP(const char *nodename,
     memset(&remoteAddr, 0, sizeof(remoteAddr));
 
     memset(&hints, 0, sizeof(hints));
+    hints.ai_family = family;
     hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
     hints.ai_socktype = SOCK_STREAM;
 
@@ -801,6 +805,7 @@ int virNetSocketNewConnectSSH(const char *nodename,
 int
 virNetSocketNewConnectLibSSH2(const char *host,
                               const char *port,
+                              int family,
                               const char *username,
                               const char *privkey,
                               const char *knownHosts,
@@ -892,7 +897,7 @@ virNetSocketNewConnectLibSSH2(const char *host,
     }
 
     /* connect to remote server */
-    if ((ret = virNetSocketNewConnectTCP(host, port, &sock)) < 0)
+    if ((ret = virNetSocketNewConnectTCP(host, port, family, &sock)) < 0)
         goto error;
 
     /* connect to the host using ssh */
@@ -915,6 +920,7 @@ virNetSocketNewConnectLibSSH2(const char *host,
 int
 virNetSocketNewConnectLibSSH2(const char *host ATTRIBUTE_UNUSED,
                               const char *port ATTRIBUTE_UNUSED,
+                              int family ATTRIBUTE_UNUSED,
                               const char *username ATTRIBUTE_UNUSED,
                               const char *privkey ATTRIBUTE_UNUSED,
                               const char *knownHosts ATTRIBUTE_UNUSED,
index 86bc2f6cdac81366171e740441ed7e0411fea9a4..a8ff8a9e488b1640517abbaf32a7efe07f62ed96 100644 (file)
@@ -47,6 +47,7 @@ typedef void (*virNetSocketIOFunc)(virNetSocketPtr sock,
 
 int virNetSocketNewListenTCP(const char *nodename,
                              const char *service,
+                             int family,
                              virNetSocketPtr **addrs,
                              size_t *naddrs);
 
@@ -61,6 +62,7 @@ int virNetSocketNewListenFD(int fd,
 
 int virNetSocketNewConnectTCP(const char *nodename,
                               const char *service,
+                              int family,
                               virNetSocketPtr *addr);
 
 int virNetSocketNewConnectUNIX(const char *path,
@@ -84,6 +86,7 @@ int virNetSocketNewConnectSSH(const char *nodename,
 
 int virNetSocketNewConnectLibSSH2(const char *host,
                                   const char *port,
+                                  int family,
                                   const char *username,
                                   const char *privkey,
                                   const char *knownHosts,
index 5d91f26b1a87447e93dfc303a6fb0ed20f1a0cda..f609484c20e30a1a41b63d2a612bb4309da1d1fa 100644 (file)
@@ -166,7 +166,9 @@ static int testSocketTCPAccept(const void *opaque)
 
     snprintf(portstr, sizeof(portstr), "%d", data->port);
 
-    if (virNetSocketNewListenTCP(data->lnode, portstr, &lsock, &nlsock) < 0)
+    if (virNetSocketNewListenTCP(data->lnode, portstr,
+                                 AF_UNSPEC,
+                                 &lsock, &nlsock) < 0)
         goto cleanup;
 
     for (i = 0; i < nlsock; i++) {
@@ -174,7 +176,9 @@ static int testSocketTCPAccept(const void *opaque)
             goto cleanup;
     }
 
-    if (virNetSocketNewConnectTCP(data->cnode, portstr, &csock) < 0)
+    if (virNetSocketNewConnectTCP(data->cnode, portstr,
+                                  AF_UNSPEC,
+                                  &csock) < 0)
         goto cleanup;
 
     virObjectUnref(csock);