]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[Core] switch_sockaddr_info_get() will not resolve if the hostname is an IP address...
authorAndrey Volk <andywolk@gmail.com>
Fri, 12 Jun 2026 13:13:47 +0000 (16:13 +0300)
committerGitHub <noreply@github.com>
Fri, 12 Jun 2026 13:13:47 +0000 (16:13 +0300)
src/include/switch_utils.h
src/switch_apr.c
src/switch_utils.c
tests/unit/switch_core.c

index a0c91e6384f34cc9018aa7caa838225180a16a49..68667ee4773f3990065345ee8f4e9b411e2a0dde 100644 (file)
@@ -1519,6 +1519,13 @@ SWITCH_DECLARE(const char *) switch_memory_usage_stream(switch_stream_handle_t *
 **/
 SWITCH_DECLARE(int) switch_rand(void);
 
+/*!
+ * \brief Check if a hostname is a valid IP address (IPv4 or IPv6)
+ * \param hostname The hostname to check
+ * \return 1 if a valid IP address, 0 - otherwise
+ */
+SWITCH_DECLARE(int) switch_is_ip_address(const char *hostname);
+
 SWITCH_END_EXTERN_C
 #endif
 /* For Emacs:
index 60660d6e2ebe748a597c7749cf751e81798f9ef5..09e4ba51e4ad05a7e01a52b8fa78d118f4b7e827 100644 (file)
@@ -841,6 +841,10 @@ SWITCH_DECLARE(switch_status_t) switch_sockaddr_create(switch_sockaddr_t **sa, s
 SWITCH_DECLARE(switch_status_t) switch_sockaddr_info_get(switch_sockaddr_t ** sa, const char *hostname, int32_t family,
                                                                                                                 switch_port_t port, int32_t flags, switch_memory_pool_t *pool)
 {
+       if (!zstr(hostname) && switch_is_ip_address(hostname)) {
+               return switch_sockaddr_new(sa, hostname, port, pool);
+       }
+
        return fspr_sockaddr_info_get(sa, hostname, family, port, flags, pool);
 }
 
index dc855c8deff17673c7921c5fb999f48925586656..854659ab4a8566ad47f16fcc025f7d5b430e59b9 100644 (file)
@@ -4888,6 +4888,24 @@ SWITCH_DECLARE(int) switch_rand(void)
 #endif
 }
 
+SWITCH_DECLARE(int) switch_is_ip_address(const char *hostname)
+{
+       struct sockaddr_in sa;
+       struct sockaddr_in6 sa6;
+
+       if (!hostname) return 0;
+
+       if (inet_pton(AF_INET, hostname, &(sa.sin_addr)) == 1) {
+               return 1; /* It is a valid IPv4 address */
+       }
+
+       if (inet_pton(AF_INET6, hostname, &(sa6.sin6_addr)) == 1) {
+               return 1; /* It is a valid IPv6 address */
+       }
+
+       return 0; /* Not a valid IPv4 or IPv6 address */
+}
+
 /* For Emacs:
  * Local Variables:
  * mode:c
index 6b4ba12e04685732b6ed8d9881f7060263b7802a..096c8d014a668a62a68329192c78829a6ab44755 100644 (file)
@@ -72,6 +72,24 @@ FST_CORE_BEGIN("./conf")
                }
                FST_TEARDOWN_END()
 
+               FST_TEST_BEGIN(test_is_ip_address)
+               {                       
+                       const char *test_ips[] = {
+                               "192.168.1.1",              // Valid IPv4
+                               "2001:db8::ff00:42:8329",   // Valid IPv6
+                               "www.google.com",           // Hostname
+                               "not.an.ip.address",
+                               NULL
+                       };
+
+                       fst_check_int_equals(switch_is_ip_address(test_ips[0]), 1);
+                       fst_check_int_equals(switch_is_ip_address(test_ips[1]), 1);
+                       fst_check_int_equals(switch_is_ip_address(test_ips[2]), 0);
+                       fst_check_int_equals(switch_is_ip_address(test_ips[3]), 0);
+                       fst_check_int_equals(switch_is_ip_address(test_ips[4]), 0);
+               }
+               FST_TEST_END();
+
                FST_TEST_BEGIN(test_switch_regex)
                {
                        switch_regex_match_t *match_data = NULL;