[sr-dev] git:master:fa0e1684: dispatcher: dns resolving of destination addresses done on timer

Daniel-Constantin Mierla miconda at gmail.com
Thu Apr 7 20:13:38 CEST 2022


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

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2022-04-07T20:13:27+02:00

dispatcher: dns resolving of destination addresses done on timer

- new option to refresh periodically the ip addresses corresponding to
  destination addresses

---

Modified: src/modules/dispatcher/dispatch.c
Modified: src/modules/dispatcher/dispatch.h
Modified: src/modules/dispatcher/dispatcher.c

---

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

---

diff --git a/src/modules/dispatcher/dispatch.c b/src/modules/dispatcher/dispatch.c
index 66356878ac..b82bc89b90 100644
--- a/src/modules/dispatcher/dispatch.c
+++ b/src/modules/dispatcher/dispatch.c
@@ -3835,6 +3835,85 @@ void ds_ht_timer(unsigned int ticks, void *param)
 	return;
 }
 
+
+
+/**
+ *
+ */
+void ds_dns_update_set(ds_set_t *node)
+{
+	int i, j;
+	char hn[DS_HN_SIZE];
+	struct hostent *he;
+	unsigned short sport = 0;
+	char sproto = PROTO_NONE;
+
+	if(!node)
+		return;
+
+	for(i = 0; i < 2; ++i)
+		ds_dns_update_set(node->next[i]);
+
+	for(j = 0; j < node->nr; j++) {
+		/* do a DNS qookup for the host part, if not disabled via dst flags */
+		if(node->dlist[j].flags & DS_NODNSARES_DST) {
+			continue;
+		}
+		if(node->dlist[j].host.len <= 0) {
+			continue;
+		}
+		LM_DBG("resolving [%.*s] - mode: %d\n", node->dlist[j].host.len,
+				node->dlist[j].host.s, ds_dns_mode);
+		if (ds_dns_mode & DS_DNS_MODE_QSRV) {
+			sport = node->dlist[j].port;
+			sproto = (char)node->dlist[j].proto;
+			he = sip_resolvehost(&node->dlist[j].host, &sport, &sproto);
+			if(he != 0) {
+				if(sport != 0) {
+					node->dlist[j].port = sport;
+				}
+				if(sproto != PROTO_NONE) {
+					node->dlist[j].proto = sproto;
+				}
+			}
+		} else {
+			/* The Hostname needs to be \0 terminated for resolvehost, so we
+			 * make a copy here. */
+			memcpy(hn, node->dlist[j].host.s, node->dlist[j].host.len);
+			hn[node->dlist[j].host.len] = '\0';
+			he = resolvehost(hn);
+		}
+		if(he == 0) {
+			LM_ERR("could not resolve %.*s\n", node->dlist[j].host.len,
+					node->dlist[j].host.s);
+			continue;
+		} else {
+			/* Store hostent in the dispatcher structure */
+			hostent2ip_addr(&node->dlist[j].ip_address, he, 0);
+		}
+	}
+}
+
+/*! \brief
+ * Timer for DNS query of destination addresses
+ *
+ * This timer is regularly fired.
+ */
+void ds_dns_timer(unsigned int ticks, void *param)
+{
+	if(!(ds_dns_mode & DS_DNS_MODE_TIMER)) {
+		return;
+	}
+
+	/* Check for the list. */
+	if(_ds_list == NULL || _ds_list_nr <= 0) {
+		LM_DBG("no destination sets\n");
+		return;
+	}
+
+	ds_dns_update_set(_ds_list);
+}
+
 int ds_next_dst_api(sip_msg_t *msg, int mode)
 {
 	return ds_update_dst(msg, DS_USE_NEXT, mode);
diff --git a/src/modules/dispatcher/dispatch.h b/src/modules/dispatcher/dispatch.h
index d00bc42e47..fec204bfff 100644
--- a/src/modules/dispatcher/dispatch.h
+++ b/src/modules/dispatcher/dispatch.h
@@ -173,12 +173,16 @@ int ds_is_addr_from_list(sip_msg_t *_m, int group, str *uri, int mode);
  */
 void ds_check_timer(unsigned int ticks, void *param);
 
-
 /*! \brief
  * Timer for checking active calls load
  */
 void ds_ht_timer(unsigned int ticks, void *param);
 
+/*! \brief
+ * Timer for DNS query of destination addresses
+ */
+void ds_dns_timer(unsigned int ticks, void *param);
+
 /*! \brief
  * Check if the reply-code is valid:
  */
diff --git a/src/modules/dispatcher/dispatcher.c b/src/modules/dispatcher/dispatcher.c
index 8ae2fb7b68..471070fe4e 100644
--- a/src/modules/dispatcher/dispatcher.c
+++ b/src/modules/dispatcher/dispatcher.c
@@ -122,6 +122,7 @@ int ds_attrs_none = 0;
 int ds_load_mode = 0;
 uint32_t ds_dns_mode = DS_DNS_MODE_INIT;
 static int ds_dns_mode_param = 0;
+static int ds_dns_interval = 0;
 
 str ds_outbound_proxy = STR_NULL;
 
@@ -299,6 +300,7 @@ static param_export_t params[]={
 	{"ds_load_mode",       PARAM_INT, &ds_load_mode},
 	{"reload_delta",       PARAM_INT, &ds_reload_delta },
 	{"ds_dns_mode",        PARAM_INT, &ds_dns_mode_param},
+	{"ds_dns_interval",    PARAM_INT, &ds_dns_interval},
 	{0,0,0}
 };
 
@@ -330,6 +332,15 @@ static int mod_init(void)
 
 	ds_dns_mode = 1U<<(unsigned int)ds_dns_mode_param;
 
+	if(ds_dns_mode & DS_DNS_MODE_TIMER) {
+		if(ds_dns_interval<=0) {
+			LM_WARN("dns interval parameter not set - using 600\n");
+			ds_dns_interval = 600;
+		}
+		if(sr_wtimer_add(ds_dns_timer, NULL, ds_dns_interval) < 0) {
+			return -1;
+		}
+	}
 	if(ds_ping_active_init() < 0) {
 		return -1;
 	}




More information about the sr-dev mailing list