Module: kamailio
Branch: master
Commit: 1ae30cf9bd8fe8a9535957a0eec45d0a622ea7b5
URL: 
https://github.com/kamailio/kamailio/commit/1ae30cf9bd8fe8a9535957a0eec45d0a622ea7b5

Author: Daniel-Constantin Mierla <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2026-05-20T21:25:42+02:00

core: resolve - getaddrinfo() wrapper to simulate gethostbyname()

---

Modified: src/core/resolve.c
Modified: src/core/resolve.h

---

Diff:  
https://github.com/kamailio/kamailio/commit/1ae30cf9bd8fe8a9535957a0eec45d0a622ea7b5.diff
Patch: 
https://github.com/kamailio/kamailio/commit/1ae30cf9bd8fe8a9535957a0eec45d0a622ea7b5.patch

---

diff --git a/src/core/resolve.c b/src/core/resolve.c
index 5ad3aa97a4d..94aa3ad7393 100644
--- a/src/core/resolve.c
+++ b/src/core/resolve.c
@@ -30,6 +30,7 @@
 
 #include <sys/types.h>
 #include <netinet/in.h>
+#include <netdb.h>
 #include <arpa/nameser.h>
 #include <resolv.h>
 #include <string.h>
@@ -75,6 +76,99 @@ static ip_addr_t *get_next_ipaddr_buf(void)
        return ipb;
 }
 
+#define KSR_GETHOSTBYNAME_ADDRS 8
+
+struct hostent *ksr_gethostbyname(const char *name)
+{
+       static struct hostent he;
+       static char *aliases[1];
+       static char *addr_list[KSR_GETHOSTBYNAME_ADDRS + 1];
+       static unsigned char addr_buf[KSR_GETHOSTBYNAME_ADDRS]
+                                                                [sizeof(struct 
in6_addr)];
+       static char name_buf[MAX_DNS_NAME];
+       struct addrinfo hints;
+       struct addrinfo *res = NULL;
+       struct addrinfo *ai;
+       const char *hname;
+       int af;
+       int hlen;
+       int i;
+
+       if(name == NULL || *name == '\0') {
+               return NULL;
+       }
+
+       memset(&he, 0, sizeof(he));
+       memset(&hints, 0, sizeof(hints));
+       memset(addr_list, 0, sizeof(addr_list));
+       aliases[0] = NULL;
+
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_socktype = SOCK_DGRAM;
+       hints.ai_flags = AI_ADDRCONFIG;
+
+       if(getaddrinfo(name, NULL, &hints, &res) != 0 || res == NULL) {
+               return NULL;
+       }
+
+       af = AF_UNSPEC;
+       hlen = 0;
+       for(ai = res; ai != NULL; ai = ai->ai_next) {
+               if(ai->ai_family == AF_INET) {
+                       af = AF_INET;
+                       hlen = sizeof(struct in_addr);
+                       break;
+               }
+               if(ai->ai_family == AF_INET6) {
+                       af = AF_INET6;
+                       hlen = sizeof(struct in6_addr);
+                       break;
+               }
+       }
+
+       if(af == AF_UNSPEC) {
+               freeaddrinfo(res);
+               return NULL;
+       }
+
+       i = 0;
+       for(ai = res; ai != NULL && i < KSR_GETHOSTBYNAME_ADDRS; ai = 
ai->ai_next) {
+               if(ai->ai_family != af || ai->ai_addr == NULL) {
+                       continue;
+               }
+               if(af == AF_INET) {
+                       memcpy(addr_buf[i], &((struct sockaddr_in 
*)ai->ai_addr)->sin_addr,
+                                       hlen);
+               } else {
+                       memcpy(addr_buf[i],
+                                       &((struct sockaddr_in6 
*)ai->ai_addr)->sin6_addr, hlen);
+               }
+               addr_list[i] = (char *)addr_buf[i];
+               i++;
+       }
+       addr_list[i] = NULL;
+
+       if(i == 0) {
+               freeaddrinfo(res);
+               return NULL;
+       }
+
+       hname = (res->ai_canonname != NULL && res->ai_canonname[0] != '\0')
+                                       ? res->ai_canonname
+                                       : name;
+       strncpy(name_buf, hname, sizeof(name_buf) - 1);
+       name_buf[sizeof(name_buf) - 1] = '\0';
+
+       he.h_name = name_buf;
+       he.h_aliases = aliases;
+       he.h_addrtype = af;
+       he.h_length = hlen;
+       he.h_addr_list = addr_list;
+
+       freeaddrinfo(res);
+       return &he;
+}
+
 /* counters framework */
 struct dns_counters_h dns_cnts_h;
 counter_def_t dns_cnt_defs[] = {
diff --git a/src/core/resolve.h b/src/core/resolve.h
index 68d5c3eaaa0..6f67c20e43f 100644
--- a/src/core/resolve.h
+++ b/src/core/resolve.h
@@ -232,6 +232,8 @@ ip_addr_t *str2ip6(str *st);
 ip_addr_t *str2ipx(str *st);
 
 struct hostent *_sip_resolvehost(str *name, unsigned short *port, char *proto);
+/* alternative to gethostbyname() using internally getaddrinfo() */
+struct hostent *ksr_gethostbyname(const char *name);
 
 
 /* gethostbyname wrapper, handles ip/ipv6 automatically */

_______________________________________________
Kamailio - Development Mailing List -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to