Browse code

corex: new param dns_cache to add values to internal dns cache

- it can be set many times
- the value is a param-like string with the prototype
"type=rectype;name=domain;addr=ip;ttl=ival;flags=ival"
- type valye can be 'A' (IPv4) or 'AAAA' (IPv6)
- flags can be:
1 - the domain is unresolvable (like blacklisted)
2 - the record is permanent (never times out, never deleted, never overwritten)

Daniel-Constantin Mierla authored on 26/05/2020 13:45:10
Showing 1 changed files
... ...
@@ -31,7 +31,9 @@
31 31
 #include "../../core/pvar.h"
32 32
 #include "../../core/fmsg.h"
33 33
 #include "../../core/kemi.h"
34
+#include "../../core/dns_cache.h"
34 35
 #include "../../core/parser/parse_uri.h"
36
+#include "../../core/parser/parse_param.h"
35 37
 
36 38
 #include "corex_lib.h"
37 39
 #include "corex_rpc.h"
... ...
@@ -67,6 +69,7 @@ static int w_is_faked_msg(sip_msg_t *msg, char *p1, char *p2);
67 69
 static int fixup_file_op(void** param, int param_no);
68 70
 
69 71
 int corex_alias_subdomains_param(modparam_t type, void *val);
72
+int corex_dns_cache_param(modparam_t type, void *val);
70 73
 
71 74
 static int  mod_init(void);
72 75
 static int  child_init(int);
... ...
@@ -141,6 +144,8 @@ static cmd_export_t cmds[]={
141 144
 static param_export_t params[]={
142 145
 	{"alias_subdomains",		STR_PARAM|USE_FUNC_PARAM,
143 146
 		(void*)corex_alias_subdomains_param},
147
+	{"dns_cache",		PARAM_STR|USE_FUNC_PARAM,
148
+		(void*)corex_dns_cache_param},
144 149
 	{"nio_intercept",	INT_PARAM, &nio_intercept},
145 150
 	{"nio_min_msg_len",	INT_PARAM, &nio_min_msg_len},
146 151
 	{"nio_msg_avp",		PARAM_STR, &nio_msg_avp_param},
... ...
@@ -304,9 +309,88 @@ int corex_alias_subdomains_param(modparam_t type, void *val)
304 309
 	return corex_add_alias_subdomains((char*)val);
305 310
 error:
306 311
 	return -1;
312
+}
313
+
314
+int corex_dns_cache_param(modparam_t type, void *val)
315
+{
316
+	str sval;
317
+	param_t* params_list = NULL;
318
+	param_hooks_t phooks;
319
+	param_t *pit=NULL;
320
+	str dns_name = STR_NULL;
321
+	str dns_addr = STR_NULL;
322
+	unsigned short dns_type = 0;
323
+	int dns_ttl = 0;
324
+	int dns_flags = 0;
325
+
326
+	if(val==NULL) {
327
+		LM_ERR("invalid parameter\n");
328
+		goto error;
329
+	}
330
+	sval = *((str*)val);
331
+	if(sval.s==NULL || sval.len<=0) {
332
+		LM_ERR("invalid parameter value\n");
333
+		goto error;
334
+	}
335
+
336
+	if(sval.s[sval.len-1]==';') {
337
+		sval.len--;
338
+	}
339
+	if (parse_params(&sval, CLASS_ANY, &phooks, &params_list)<0) {
340
+		return -1;
341
+	}
342
+
343
+	for (pit = params_list; pit; pit=pit->next) {
344
+		if (pit->name.len==4
345
+				&& strncasecmp(pit->name.s, "name", 4)==0) {
346
+			dns_name = pit->body;
347
+		} else if (pit->name.len==4
348
+				&& strncasecmp(pit->name.s, "addr", 4)==0) {
349
+			dns_addr = pit->body;
350
+		} else if (pit->name.len==4
351
+				&& strncasecmp(pit->name.s, "type", 4)==0) {
352
+			if((pit->body.len == 1) && ((pit->body.s[0]=='a')
353
+						|| (pit->body.s[0]=='A'))) {
354
+				dns_type = T_A;
355
+			} else if((pit->body.len == 4)
356
+					&& strncasecmp(pit->name.s, "aaaa", 4)==0) {
357
+				dns_type = T_AAAA;
358
+			}
359
+		} else if(pit->name.len==3
360
+				&& strncasecmp(pit->name.s, "ttl", 3)==0) {
361
+			if(dns_ttl==0) {
362
+				if (str2sint(&pit->body, &dns_ttl) < 0) {
363
+					LM_ERR("invalid ttl: %.*s\n", pit->body.len, pit->body.s);
364
+					return -1;
365
+				}
366
+			}
367
+		} else if(pit->name.len==5
368
+				&& strncasecmp(pit->name.s, "flags", 5)==0) {
369
+			if(dns_flags==0) {
370
+				if (str2sint(&pit->body, &dns_flags) < 0) {
371
+					LM_ERR("invalid flags: %.*s\n", pit->body.len, pit->body.s);
372
+					return -1;
373
+				}
374
+			}
375
+		}
376
+	}
377
+
378
+	if (dns_cache_add_record(dns_type,
379
+				&dns_name,
380
+				dns_ttl,
381
+				&dns_addr,
382
+				0 /* priority */,
383
+				0 /* weight */,
384
+				0 /* port */,
385
+				dns_flags) == 0) {
386
+		return 0;
387
+	}
307 388
 
389
+error:
390
+	return -1;
308 391
 }
309 392
 
393
+
310 394
 typedef struct _msg_iflag_name {
311 395
 	str name;
312 396
 	int value;