**/
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:
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);
}
#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
}
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;