}
}
+/*
+ * Checks if a string contains a valid IP address (IPv4 or IPv6).
+ *
+ * Returns:
+ * 1: string is a valid IPv4 or IPv6
+ * 0: string is not a valid IP address
+ */
+
+int
+network_is_ip_address (const char *address)
+{
+ struct sockaddr_in server_addr;
+ struct sockaddr_in6 server_addr6;
+
+ if (!address || !address[0])
+ return 0;
+
+ /* valid IPv4? */
+ if (inet_pton (AF_INET, address, &server_addr.sin_addr))
+ return 1;
+
+ /* valid IPv6? */
+ if (inet_pton (AF_INET6, address, &server_addr6.sin6_addr))
+ return 1;
+
+ /* not a valid IP address */
+ return 0;
+}
+
/*
* Sends data on a socket with retry.
*
unhook (hook_connect);
return;
}
- rc = gnutls_server_name_set (*HOOK_CONNECT(hook_connect, gnutls_sess),
- GNUTLS_NAME_DNS,
- HOOK_CONNECT(hook_connect, address),
- strlen (HOOK_CONNECT(hook_connect, address)));
- if (rc != GNUTLS_E_SUCCESS)
+ if (!network_is_ip_address (HOOK_CONNECT(hook_connect, address)))
{
- (void) (HOOK_CONNECT(hook_connect, callback))
- (hook_connect->callback_pointer,
- hook_connect->callback_data,
- WEECHAT_HOOK_CONNECT_GNUTLS_INIT_ERROR,
- 0, -1, _("set server name indication (SNI) failed"), NULL);
- unhook (hook_connect);
- return;
+ /* set the server name (only if it's NOT an IPv4/IPv6) */
+ rc = gnutls_server_name_set (*HOOK_CONNECT(hook_connect, gnutls_sess),
+ GNUTLS_NAME_DNS,
+ HOOK_CONNECT(hook_connect, address),
+ strlen (HOOK_CONNECT(hook_connect, address)));
+ if (rc != GNUTLS_E_SUCCESS)
+ {
+ (void) (HOOK_CONNECT(hook_connect, callback))
+ (hook_connect->callback_pointer,
+ hook_connect->callback_data,
+ WEECHAT_HOOK_CONNECT_GNUTLS_INIT_ERROR,
+ 0, -1, _("set server name indication (SNI) failed"), NULL);
+ unhook (hook_connect);
+ return;
+ }
}
rc = gnutls_priority_set_direct (*HOOK_CONNECT(hook_connect, gnutls_sess),
HOOK_CONNECT(hook_connect, gnutls_priorities),
--- /dev/null
+/*
+ * test-core-network.cpp - test network functions
+ *
+ * Copyright (C) 2021 Sébastien Helleu <flashcode@flashtux.org>
+ *
+ * This file is part of WeeChat, the extensible chat client.
+ *
+ * WeeChat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * WeeChat is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "CppUTest/TestHarness.h"
+
+extern "C"
+{
+//#include <unistd.h>
+//#include <stdio.h>
+//#include <string.h>
+#include "src/core/wee-network.h"
+
+extern int network_is_ip_address (const char *address);
+}
+
+TEST_GROUP(CoreNetwork)
+{
+};
+
+/*
+ * Tests functions:
+ * network_init_gcrypt
+ */
+
+TEST(CoreNetwork, InitGcrypt)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_load_system_ca_file
+ */
+
+TEST(CoreNetwork, LoadSystemCaFile)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_load_user_ca_files
+ */
+
+TEST(CoreNetwork, LoadUserCaFiles)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_load_ca_files
+ */
+
+TEST(CoreNetwork, LoadCaFiles)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_reload_ca_files
+ */
+
+TEST(CoreNetwork, ReloadCaFiles)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_init_gnutls
+ */
+
+TEST(CoreNetwork, InitGnutls)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_end
+ */
+
+TEST(CoreNetwork, End)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_is_ip_address
+ */
+
+TEST(CoreNetwork, IsIpAddress)
+{
+ /* invalid address */
+ LONGS_EQUAL(0, network_is_ip_address (NULL));
+ LONGS_EQUAL(0, network_is_ip_address (""));
+ LONGS_EQUAL(0, network_is_ip_address ("abc"));
+ LONGS_EQUAL(0, network_is_ip_address ("1"));
+ LONGS_EQUAL(0, network_is_ip_address ("1.2"));
+ LONGS_EQUAL(0, network_is_ip_address ("1.2.3"));
+ LONGS_EQUAL(0, network_is_ip_address ("1.2.3.a"));
+ LONGS_EQUAL(0, network_is_ip_address ("1.2.3.4.5"));
+ LONGS_EQUAL(0, network_is_ip_address ("001.002.003.004"));
+
+ /* valid IPv4 */
+ LONGS_EQUAL(1, network_is_ip_address ("127.0.0.1"));
+ LONGS_EQUAL(1, network_is_ip_address ("1.2.3.4"));
+
+ /* valid IPv6 */
+ LONGS_EQUAL(1, network_is_ip_address ("::1"));
+ LONGS_EQUAL(1, network_is_ip_address ("2001:0db8:0000:85a3:0000:0000:ac1f:8001"));
+ LONGS_EQUAL(1, network_is_ip_address ("2001:db8:0:85a3:0:0:ac1f:8001"));
+ LONGS_EQUAL(1, network_is_ip_address ("2001:db8:0:85a3::ac1f:8001"));
+}
+
+/*
+ * Tests functions:
+ * network_send_with_retry
+ */
+
+TEST(CoreNetwork, SendWithRetry)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_recv_with_retry
+ */
+
+TEST(CoreNetwork, RecvWithRetry)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_pass_httpproxy
+ */
+
+TEST(CoreNetwork, PassHttpproxy)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_resolve
+ */
+
+TEST(CoreNetwork, Resolve)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_pass_socks4proxy
+ */
+
+TEST(CoreNetwork, PassSock4proxy)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_pass_socks5proxy
+ */
+
+TEST(CoreNetwork, PassSocks5proxy)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_pass_proxy
+ */
+
+TEST(CoreNetwork, PassProxy)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_connect
+ */
+
+TEST(CoreNetwork, Connect)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_connect_to
+ */
+
+TEST(CoreNetwork, ConnectTo)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_connect_child
+ */
+
+TEST(CoreNetwork, ConnectChild)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_connect_child_timer_cb
+ */
+
+TEST(CoreNetwork, ConnectChildTimerCb)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_connect_gnutls_handshake_fd_cb
+ */
+
+TEST(CoreNetwork, ConnectGnutlsHandshakeFdCb)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_connect_gnutls_handshake_timer_cb
+ */
+
+TEST(CoreNetwork, ConnectGnutlsHandshakeTimerCb)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_connect_child_read_cb
+ */
+
+TEST(CoreNetwork, ConnectChildReadCb)
+{
+ /* TODO: write tests */
+}
+
+/*
+ * Tests functions:
+ * network_connect_with_fork
+ */
+
+TEST(CoreNetwork, ConnectWithFork)
+{
+ /* TODO: write tests */
+}