Bug 1467141 - Improved Bogo shim IPv4 fallback, r=franziskus
authorJonas Allmann <jallmann@mozilla.com>
Wed, 20 Jun 2018 12:04:38 +0200
changeset 14404 1a13c19d7fab53fd62786e05d6546a4abf66e48d
parent 14403 20fe6e13b0fdc3c4419b1d4e608c3139ad17539e
child 14407 d99e54ca9b6df33025ee9a196b8b942428bbff91
push id3124
push userfranziskuskiefer@gmail.com
push dateWed, 20 Jun 2018 10:05:31 +0000
reviewersfranziskus
bugs1467141
Bug 1467141 - Improved Bogo shim IPv4 fallback, r=franziskus Differential Revision: https://phabricator.services.mozilla.com/D1567
gtests/nss_bogo_shim/nss_bogo_shim.cc
--- a/gtests/nss_bogo_shim/nss_bogo_shim.cc
+++ b/gtests/nss_bogo_shim/nss_bogo_shim.cc
@@ -83,42 +83,49 @@ class TestAgent {
 
     SECStatus rv = SSL_ResetHandshake(ssl_fd_, cfg_.get<bool>("server"));
     if (rv != SECSuccess) return false;
 
     return true;
   }
 
   bool ConnectTcp() {
+    // Try IPv6 first, then IPv4 in case of failure.
+    if (!OpenConnection("::1") && !OpenConnection("127.0.0.1")) {
+      return false;
+    }
+
+    ssl_fd_ = SSL_ImportFD(NULL, pr_fd_);
+    if (!ssl_fd_) {
+      return false;
+    }
+    pr_fd_ = nullptr;
+
+    return true;
+  }
+
+  bool OpenConnection(const char* ip) {
     PRStatus prv;
     PRNetAddr addr;
 
-    // Try IPv6 first.
-    prv = PR_StringToNetAddr("::1", &addr);
+    prv = PR_StringToNetAddr(ip, &addr);
+
     if (prv != PR_SUCCESS) {
-      // If that fails, try IPv4.
-      prv = PR_StringToNetAddr("127.0.0.1", &addr);
-      if (prv != PR_SUCCESS) {
-        return false;
-      }
+      return false;
     }
+
     addr.inet.port = PR_htons(cfg_.get<int>("port"));
 
     pr_fd_ = PR_OpenTCPSocket(addr.raw.family);
     if (!pr_fd_) return false;
 
     prv = PR_Connect(pr_fd_, &addr, PR_INTERVAL_NO_TIMEOUT);
     if (prv != PR_SUCCESS) {
       return false;
     }
-
-    ssl_fd_ = SSL_ImportFD(NULL, pr_fd_);
-    if (!ssl_fd_) return false;
-    pr_fd_ = nullptr;
-
     return true;
   }
 
   bool SetupKeys() {
     SECStatus rv;
 
     if (cfg_.get<std::string>("key-file") != "") {
       key_ = ReadPrivateKey(cfg_.get<std::string>("key-file"));