]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - tools/squidclient/Transport.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / tools / squidclient / Transport.cc
index 1abeada100a9730dd4327adebac107533a6accf3..658da8b48176e1b0d4a9b213534ffdd5b9b459ac 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
  *
  * Squid software is distributed under GPLv2+ license and includes
  * contributions from numerous individuals and organizations.
@@ -8,6 +8,7 @@
 
 #include "squid.h"
 #include "ip/Address.h"
+#include "ip/tools.h"
 #include "tools/squidclient/Ping.h"
 #include "tools/squidclient/Transport.h"
 
@@ -52,7 +53,7 @@ bool
 Transport::TheConfig::parseCommandOpts(int argc, char *argv[], int c, int &optIndex)
 {
     bool tls = false;
-    const char *shortOpStr = "A:C:h:l:p:P:T:?";
+    const char *shortOpStr = "h:l:p:T:?";
 
     // options for controlling squidclient transport connection
     static struct option longOptions[] = {
@@ -139,7 +140,7 @@ client_comm_bind(int sock, const Ip::Address &addr)
     static struct addrinfo *AI = NULL;
     addr.getAddrInfo(AI);
     int res = bind(sock, AI->ai_addr, AI->ai_addrlen);
-    Ip::Address::FreeAddrInfo(AI);
+    Ip::Address::FreeAddr(AI);
     return res;
 }
 
@@ -148,6 +149,11 @@ resolveDestination(Ip::Address &iaddr)
 {
     struct addrinfo *AI = NULL;
 
+    debugVerbose(2, "Transport detected: IPv4" <<
+                 ((Ip::EnableIpv6 & IPV6_SPECIAL_V4MAPPING) ? "-mapped " : "") <<
+                 (Ip::EnableIpv6 == IPV6_OFF ? "-only" : " and IPv6") <<
+                 ((Ip::EnableIpv6 & IPV6_SPECIAL_SPLITSTACK) ? " split-stack" : ""));
+
     if (Transport::Config.localHost) {
         debugVerbose(2, "Resolving " << Transport::Config.localHost << " ...");
 
@@ -168,10 +174,10 @@ resolveDestination(Ip::Address &iaddr)
     iaddr.getAddrInfo(AI);
     if ((conn = socket(AI->ai_family, AI->ai_socktype, 0)) < 0) {
         std::cerr << "ERROR: could not open socket to " << iaddr << std::endl;
-        Ip::Address::FreeAddrInfo(AI);
+        Ip::Address::FreeAddr(AI);
         exit(1);
     }
-    Ip::Address::FreeAddrInfo(AI);
+    Ip::Address::FreeAddr(AI);
 
     if (Transport::Config.localHost) {
         if (client_comm_bind(conn, iaddr) < 0) {
@@ -199,7 +205,7 @@ client_comm_connect(int sock, const Ip::Address &addr)
     static struct addrinfo *AI = NULL;
     addr.getAddrInfo(AI);
     int res = connect(sock, AI->ai_addr, AI->ai_addrlen);
-    Ip::Address::FreeAddrInfo(AI);
+    Ip::Address::FreeAddr(AI);
     Ping::TimerStart();
     return res;
 }
@@ -229,7 +235,7 @@ Transport::Connect()
 }
 
 ssize_t
-Transport::Write(void *buf, size_t len)
+Transport::Write(const void *buf, size_t len)
 {
     if (conn < 0)
         return -1;
@@ -335,6 +341,14 @@ verifyTlsCertificate(gnutls_session_t session)
 }
 #endif
 
+#if USE_GNUTLS
+static void
+gnutlsDebugHandler(int level, const char *msg)
+{
+    debugVerbose(level, "GnuTLS: " << msg);
+}
+#endif
+
 void
 Transport::InitTls()
 {
@@ -342,12 +356,18 @@ Transport::InitTls()
     debugVerbose(3, "Initializing TLS library...");
     // NP: gnutls init is re-entrant and lock-counted with deinit but not thread safe.
     if (gnutls_global_init() != GNUTLS_E_SUCCESS) {
-        std::cerr << "FATAL ERROR: TLS Initialize failed: " << xstrerror() << std::endl;
+        int xerrno = errno;
+        std::cerr << "FATAL ERROR: TLS Initialize failed: " << xstrerr(xerrno) << std::endl;
         exit(1);
     }
 
     Config.tlsEnabled = true;
 
+#if USE_GNUTLS
+    gnutls_global_set_log_function(&gnutlsDebugHandler);
+    gnutls_global_set_log_level(scParams.verbosityLevel);
+#endif
+
     // Initialize for anonymous TLS
     gnutls_anon_allocate_client_credentials(&Config.anonCredentials);
 
@@ -506,3 +526,4 @@ Transport::ShutdownTls()
     Config.tlsEnabled = false;
 #endif
 }
+