[Core] switch_sockaddr_info_get() will not resolve if the hostname is an IP address. Add new switch_is_ip_address() API. Add a unit-test. (#3055)

This commit is contained in:
Andrey Volk
2026-06-12 16:13:47 +03:00
committed by GitHub
parent 2e9b721d43
commit be554e4a08
4 changed files with 47 additions and 0 deletions
+7
View 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:
+4
View 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);
}
+18
View 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