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

Author: Daniel-Constantin Mierla <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2024-04-06T08:19:19+02:00

corex: added dns_file modparam

- can be set to the path of a file containing dns_cache records
- each line has to contain a dns_cache-param-formatted record
- empty lines are allowed
- comment line has to start with #, comments can be only on separate
  line, not supported after dns_cache record

---

Modified: src/modules/corex/corex_mod.c

---

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

---

diff --git a/src/modules/corex/corex_mod.c b/src/modules/corex/corex_mod.c
index 4152fbf5df4..6739e4cee16 100644
--- a/src/modules/corex/corex_mod.c
+++ b/src/modules/corex/corex_mod.c
@@ -32,6 +32,7 @@
 #include "../../core/fmsg.h"
 #include "../../core/kemi.h"
 #include "../../core/str_list.h"
+#include "../../core/trim.h"
 #include "../../core/events.h"
 #include "../../core/onsend.h"
 #include "../../core/forward.h"
@@ -82,12 +83,15 @@ static int corex_evrt_reply_out_no = -1;
 
 int corex_alias_subdomains_param(modparam_t type, void *val);
 int corex_dns_cache_param(modparam_t type, void *val);
+int corex_dns_file_param(modparam_t type, void *val);
+int corex_dns_file_load(void);
 
 static int mod_init(void);
 static int child_init(int);
 static void mod_destroy(void);
 
 static str_list_t *corex_dns_cache_list = NULL;
+static str_list_t *corex_dns_file_list = NULL;
 
 static int corex_dns_cache_param_add(str *pval);
 
@@ -176,6 +180,8 @@ static param_export_t params[] = {
                                (void *)corex_alias_subdomains_param},
        {"dns_cache", PARAM_STR | USE_FUNC_PARAM,
                                (void *)corex_dns_cache_param},
+       {"dns_file", PARAM_STR | USE_FUNC_PARAM,
+                               (void *)corex_dns_file_param},
        {"nio_intercept", INT_PARAM, &nio_intercept},
        {"nio_min_msg_len", INT_PARAM, &nio_min_msg_len},
        {"nio_msg_avp", PARAM_STR, &nio_msg_avp_param},
@@ -226,6 +232,9 @@ static int mod_init(void)
                        return -1;
                }
        }
+       if(corex_dns_file_load() < 0) {
+               return -1;
+       }
 
        if((nio_intercept > 0) && (nio_intercept_init() < 0)) {
                LM_ERR("failed to register network io intercept callback\n");
@@ -385,6 +394,67 @@ int corex_dns_cache_param(modparam_t type, void *val)
        return 0;
 }
 
+int corex_dns_file_param(modparam_t type, void *val)
+{
+       str_list_t *sit;
+
+       if(val == NULL || ((str *)val)->s == NULL || ((str *)val)->len == 0) {
+               LM_ERR("invalid parameter\n");
+               return -1;
+       }
+
+       sit = (str_list_t *)pkg_mallocxz(sizeof(str_list_t));
+       if(sit == NULL) {
+               PKG_MEM_ERROR;
+               return -1;
+       }
+       sit->s = *((str *)val);
+       if(corex_dns_file_list != NULL) {
+               sit->next = corex_dns_file_list;
+       }
+       corex_dns_file_list = sit;
+
+       return 0;
+}
+
+int corex_dns_file_load(void)
+{
+       str_list_t *sit;
+       str sline;
+       char lbuf[512];
+       FILE *FP;
+
+       for(sit = corex_dns_file_list; sit != NULL; sit = sit->next) {
+               FP = fopen(sit->s.s, "r");
+               if(FP == NULL) {
+                       LM_ERR("failed to open file '%.*s'\n", sit->s.len, 
sit->s.s);
+                       return -1;
+               }
+               while(fgets(lbuf, 512, FP)) {
+                       sline.s = lbuf;
+                       sline.len = strlen(sline.s);
+                       trim(&sline);
+                       if(sline.len <= 0) {
+                               /* empty line */
+                               continue;
+                       }
+                       if(sline.s[0] == '#') {
+                               /* comment */
+                               continue;
+                       }
+                       if(corex_dns_cache_param_add(&sline) < 0) {
+                               LM_ERR("failed to add record: '%.*s' (%.*s)\n", 
sline.len,
+                                               sline.s, sit->s.len, sit->s.s);
+                               fclose(FP);
+                               return -1;
+                       }
+               }
+               fclose(FP);
+       }
+
+       return 0;
+}
+
 static int corex_dns_cache_param_add(str *pval)
 {
        str sval;

_______________________________________________
Kamailio (SER) - Development Mailing List
To unsubscribe send an email to [email protected]

Reply via email to