forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_cache.c
4731 lines (4342 loc) · 129 KB
/
dns_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* resolver related functions
*
* Copyright (C) 2006 iptelorg GmbH
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*!
* \file
* \brief Kamailio core :: DNS cache handling
* \ingroup core
* Module: \ref core
*/
#ifdef USE_DNS_CACHE
#ifdef DNS_SRV_LB
#include <stdlib.h> /* FIXME: rand() */
#endif
#include <string.h>
#include "globals.h"
#include "cfg_core.h"
#include "dns_cache.h"
#include "dns_wrappers.h"
#include "compiler_opt.h"
#include "mem/shm_mem.h"
#include "hashes.h"
#include "clist.h"
#include "locking.h"
#include "atomic_ops.h"
#include "ut.h"
#include "timer.h"
#include "timer_ticks.h"
#include "error.h"
#include "rpc.h"
#include "rand/fastrand.h"
#ifdef USE_DNS_CACHE_STATS
#include "pt.h"
#endif
#define DNS_CACHE_DEBUG /* extra sanity checks and debugging */
#ifndef MAX
#define MAX(a,b) ( ((a)>(b))?(a):(b))
#endif
#define MAX_DNS_RECORDS 255 /* maximum dns records number received in a
dns answer*/
#define DNS_HASH_SIZE 1024 /* must be <= 65535 */
#define DEFAULT_DNS_TIMER_INTERVAL 120 /* 2 min. */
#define DNS_HE_MAX_ADDR 10 /* maxium addresses returne in a hostent struct */
#define MAX_CNAME_CHAIN 10
#define SPACE_FORMAT " " /* format of view output */
#define DNS_SRV_ZERO_W_CHANCE 1000 /* one in a 1000*weight_sum chance for
selecting a 0-weight record */
int dns_cache_init=1; /* if 0, the DNS cache is not initialized at startup */
static gen_lock_t* dns_hash_lock=0;
static volatile unsigned int *dns_cache_mem_used=0; /* current mem. use */
unsigned int dns_timer_interval=DEFAULT_DNS_TIMER_INTERVAL; /* in s */
int dns_flags=0; /* default flags used for the dns_*resolvehost
(compatibility wrappers) */
#ifdef USE_DNS_CACHE_STATS
struct t_dns_cache_stats* dns_cache_stats=0;
#endif
#define LOCK_DNS_HASH() lock_get(dns_hash_lock)
#define UNLOCK_DNS_HASH() lock_release(dns_hash_lock)
#define FIX_TTL(t) \
(((t)<cfg_get(core, core_cfg, dns_cache_min_ttl))? \
cfg_get(core, core_cfg, dns_cache_min_ttl): \
(((t)>cfg_get(core, core_cfg, dns_cache_max_ttl))? \
cfg_get(core, core_cfg, dns_cache_max_ttl): \
(t)))
struct dns_hash_head{
struct dns_hash_entry* next;
struct dns_hash_entry* prev;
};
#ifdef DNS_LU_LST
struct dns_lu_lst* dns_last_used_lst=0;
#endif
static struct dns_hash_head* dns_hash=0;
static struct timer_ln* dns_timer_h=0;
#ifdef DNS_WATCHDOG_SUPPORT
static atomic_t *dns_servers_up = NULL;
#endif
static const char* dns_str_errors[]={
"no error",
"no more records", /* not an error, but and end condition */
"unknown error",
"internal error",
"bad SRV entry",
"unresolvable SRV request",
"bad A or AAAA entry",
"unresolvable A or AAAA request",
"invalid ip in A or AAAA record",
"blacklisted ip",
"name too long ", /* try again with a shorter name */
"ip AF mismatch", /* address family mismatch */
"unresolvable NAPTR request",
"bug - critical error"
};
/* param: err (negative error number) */
const char* dns_strerror(int err)
{
err=-err;
if ((err>=0) && (err<sizeof(dns_str_errors)/sizeof(char*)))
return dns_str_errors[err];
return "bug -- bad error number";
}
/* "internal" only, don't use unless you really know waht you're doing */
inline static void dns_destroy_entry(struct dns_hash_entry* e)
{
#ifdef DNS_CACHE_DEBUG
memset(e, 0, e->total_size);
#endif
shm_free(e); /* nice having it in one block isn't it? :-) */
}
/* "internal" only, same as above, asumes shm_lock() held (tm optimization) */
inline static void dns_destroy_entry_shm_unsafe(struct dns_hash_entry* e)
{
#ifdef DNS_CACHE_DEBUG
memset(e, 0, e->total_size);
#endif
shm_free_unsafe(e); /* nice having it in one block isn't it? :-) */
}
/* dec. the internal refcnt and if 0 deletes the entry */
void dns_hash_put(struct dns_hash_entry* e)
{
if(e && atomic_dec_and_test(&e->refcnt)){
/* atomic_sub_long(dns_cache_total_used, e->total_size); */
dns_destroy_entry(e);
}
}
/* same as above but uses dns_destroy_unsafe (assumes shm_lock held -- tm
* optimization) */
void dns_hash_put_shm_unsafe(struct dns_hash_entry* e)
{
if(e && atomic_dec_and_test(&e->refcnt)){
/* atomic_sub_long(dns_cache_total_used, e->total_size); */
dns_destroy_entry_shm_unsafe(e);
}
}
inline static int dns_cache_clean(unsigned int no, int expired_only);
inline static int dns_cache_free_mem(unsigned int target, int expired_only);
static ticks_t dns_timer(ticks_t ticks, struct timer_ln* tl, void* data)
{
#ifdef DNS_WATCHDOG_SUPPORT
/* do not clean the hash table if the servers are down */
if (atomic_get(dns_servers_up) == 0)
return (ticks_t)(-1);
#endif
if (*dns_cache_mem_used>12*(cfg_get(core, core_cfg, dns_cache_max_mem)/16)){ /* ~ 75% used */
dns_cache_free_mem(cfg_get(core, core_cfg, dns_cache_max_mem)/2, 1);
}else{
dns_cache_clean(-1, 1); /* all the table, only expired entries */
/* TODO: better strategy? */
}
return (ticks_t)(-1);
}
void destroy_dns_cache()
{
if (dns_timer_h){
timer_del(dns_timer_h);
timer_free(dns_timer_h);
dns_timer_h=0;
}
#ifdef DNS_WATCHDOG_SUPPORT
if (dns_servers_up){
shm_free(dns_servers_up);
dns_servers_up=0;
}
#endif
if (dns_hash_lock){
lock_destroy(dns_hash_lock);
lock_dealloc(dns_hash_lock);
dns_hash_lock=0;
}
if (dns_hash){
shm_free(dns_hash);
dns_hash=0;
}
#ifdef DNS_LU_LST
if (dns_last_used_lst){
shm_free(dns_last_used_lst);
dns_last_used_lst=0;
}
#endif
#ifdef USE_DNS_CACHE_STATS
if (dns_cache_stats)
shm_free(dns_cache_stats);
#endif
if (dns_cache_mem_used){
shm_free((void*)dns_cache_mem_used);
dns_cache_mem_used=0;
}
}
/* set the value of dns_flags */
void fix_dns_flags(str *gname, str *name)
{
/* restore the original value of dns_cache_flags first
* (DNS_IPV4_ONLY may have been set only because dns_try_ipv6
* was disabled, and the flag must be cleared when
* dns_try_ipv6 is enabled) (Miklos)
*/
dns_flags = cfg_get(core, core_cfg, dns_cache_flags) & 7;
if (cfg_get(core, core_cfg, dns_try_ipv6)==0){
dns_flags|=DNS_IPV4_ONLY;
}
if (dns_flags & DNS_IPV4_ONLY){
dns_flags&=~(DNS_IPV6_ONLY|DNS_IPV6_FIRST);
}
if (cfg_get(core, core_cfg, dns_srv_lb)){
#ifdef DNS_SRV_LB
dns_flags|=DNS_SRV_RR_LB;
#else
LM_WARN("SRV loadbalaning is set, but"
" support for it is not compiled -- ignoring\n");
#endif
}
if (cfg_get(core, core_cfg, dns_try_naptr)) {
#ifndef USE_NAPTR
LM_WARN("NAPTR support is enabled, but"
" support for it is not compiled -- ignoring\n");
#endif
dns_flags|=DNS_TRY_NAPTR;
}
}
/* fixup function for use_dns_failover
* verifies that use_dns_cache is set to 1
*/
int use_dns_failover_fixup(void *handle, str *gname, str *name, void **val)
{
if ((int)(long)(*val) && !cfg_get(core, handle, use_dns_cache)) {
LM_ERR("DNS cache is turned off, failover cannot be enabled. "
"(set use_dns_cache to 1)\n");
return -1;
}
return 0;
}
/* fixup function for use_dns_cache
* verifies that dns_cache_init is set to 1
*/
int use_dns_cache_fixup(void *handle, str *gname, str *name, void **val)
{
if ((int)(long)(*val) && !dns_cache_init) {
LM_ERR("DNS cache is turned off by dns_cache_init=0, "
"it cannot be enabled runtime.\n");
return -1;
}
if (((int)(long)(*val)==0) && cfg_get(core, handle, use_dns_failover)) {
LM_ERR("DNS failover depends on use_dns_cache, set use_dns_failover "
"to 0 before disabling the DNS cache\n");
return -1;
}
return 0;
}
/* KByte to Byte conversion */
int dns_cache_max_mem_fixup(void *handle, str *gname, str *name, void **val)
{
unsigned int u;
u = ((unsigned int)(long)(*val))<<10;
(*val) = (void *)(long)u;
return 0;
}
int init_dns_cache()
{
int r;
int ret;
if (dns_cache_init==0) {
/* the DNS cache is turned off */
default_core_cfg.use_dns_cache=0;
default_core_cfg.use_dns_failover=0;
return 0;
}
ret=0;
/* sanity check */
if (E_DNS_CRITICAL>=sizeof(dns_str_errors)/sizeof(char*)){
LM_CRIT("bad dns error table\n");
ret=E_BUG;
goto error;
}
dns_cache_mem_used=shm_malloc(sizeof(*dns_cache_mem_used));
if (dns_cache_mem_used==0){
ret=E_OUT_OF_MEM;
goto error;
}
#ifdef DNS_LU_LST
dns_last_used_lst=shm_malloc(sizeof(*dns_last_used_lst));
if (dns_last_used_lst==0){
ret=E_OUT_OF_MEM;
goto error;
}
clist_init(dns_last_used_lst, next, prev);
#endif
dns_hash=shm_malloc(sizeof(struct dns_hash_head)*DNS_HASH_SIZE);
if (dns_hash==0){
ret=E_OUT_OF_MEM;
goto error;
}
for (r=0; r<DNS_HASH_SIZE; r++)
clist_init(&dns_hash[r], next, prev);
dns_hash_lock=lock_alloc();
if (dns_hash_lock==0){
ret=E_OUT_OF_MEM;
goto error;
}
if (lock_init(dns_hash_lock)==0){
lock_dealloc(dns_hash_lock);
dns_hash_lock=0;
ret=-1;
goto error;
}
#ifdef DNS_WATCHDOG_SUPPORT
dns_servers_up=shm_malloc(sizeof(atomic_t));
if (dns_servers_up==0){
ret=E_OUT_OF_MEM;
goto error;
}
atomic_set(dns_servers_up, 1);
#endif
/* fix options */
default_core_cfg.dns_cache_max_mem<<=10; /* Kb */ /* TODO: test with 0 */
if (default_core_cfg.use_dns_cache==0)
default_core_cfg.use_dns_failover=0; /* cannot work w/o dns_cache support */
/* fix flags */
fix_dns_flags(NULL, NULL);
dns_timer_h=timer_alloc();
if (dns_timer_h==0){
ret=E_OUT_OF_MEM;
goto error;
}
if (dns_timer_interval){
timer_init(dns_timer_h, dns_timer, 0, 0); /* "slow" timer */
if (timer_add(dns_timer_h, S_TO_TICKS(dns_timer_interval))<0){
LM_CRIT("failed to add the timer\n");
timer_free(dns_timer_h);
dns_timer_h=0;
goto error;
}
}
return 0;
error:
destroy_dns_cache();
return ret;
}
#ifdef USE_DNS_CACHE_STATS
int init_dns_cache_stats(int iproc_num)
{
/* do not initialize the stats array if the DNS cache will not be used */
if (dns_cache_init==0) return 0;
/* if it is already initialized */
if (dns_cache_stats)
shm_free(dns_cache_stats);
dns_cache_stats=shm_malloc(sizeof(*dns_cache_stats) * iproc_num);
if (dns_cache_stats==0){
return E_OUT_OF_MEM;
}
memset(dns_cache_stats, 0, sizeof(*dns_cache_stats) * iproc_num);
return 0;
}
#endif
/* hash function, type is not used (obsolete)
* params: char* s, int len, int type
* returns the hash value
*/
#define dns_hash_no(s, len, type) \
(get_hash1_case_raw((s),(len)) % DNS_HASH_SIZE)
#ifdef DNS_CACHE_DEBUG
#define DEBUG_LU_LST
#ifdef DEBUG_LU_LST
#include <stdlib.h> /* abort() */
#define check_lu_lst(l) ((((l)->next==(l)) || ((l)->prev==(l))) && \
((l)!=dns_last_used_lst))
#define dbg_lu_lst(txt, l) \
LM_CRIT("%s: crt(%p, %p, %p)," \
" prev(%p, %p, %p), next(%p, %p, %p)\n", txt, \
(l), (l)->next, (l)->prev, \
(l)->prev, (l)->prev->next, (l)->prev->prev, \
(l)->next, (l)->next->next, (l)->next->prev \
)
#define debug_lu_lst( txt, l) \
do{ \
if (check_lu_lst((l))){ \
dbg_lu_lst(txt " crt:", (l)); \
abort(); \
} \
if (check_lu_lst((l)->next)){ \
dbg_lu_lst(txt " next:", (l)); \
abort(); \
} \
if (check_lu_lst((l)->prev)){ \
dbg_lu_lst(txt " prev:", (l)); \
abort(); \
} \
}while(0)
#endif
#endif /* DNS_CACHE_DEBUG */
/* must be called with the DNS_LOCK hold
* remove and entry from the hash, dec. its refcnt and if not referenced
* anymore deletes it */
inline static void _dns_hash_remove(struct dns_hash_entry* e)
{
clist_rm(e, next, prev);
#ifdef DNS_CACHE_DEBUG
e->next=e->prev=0;
#endif
#ifdef DNS_LU_LST
#ifdef DEBUG_LU_LST
debug_lu_lst("_dns_hash_remove: pre rm:", &e->last_used_lst);
#endif
clist_rm(&e->last_used_lst, next, prev);
#ifdef DEBUG_LU_LST
debug_lu_lst("_dns_hash_remove: post rm:", &e->last_used_lst);
#endif
#ifdef DNS_CACHE_DEBUG
e->last_used_lst.next=e->last_used_lst.prev=0;
#endif
#endif
*dns_cache_mem_used-=e->total_size;
dns_hash_put(e);
}
/* non locking version (the dns hash must _be_ locked externally)
* returns 0 when not found, or the entry on success (an entry with a
* similar name but with a CNAME type will always match).
* it doesn't increase the internal refcnt
* returns the entry when found, 0 when not found and sets *err to !=0
* on error (e.g. recursive cnames)
* WARNING: - internal use only
* - always check if the returned entry type is CNAME */
inline static struct dns_hash_entry* _dns_hash_find(str* name, int type,
int* h, int* err)
{
struct dns_hash_entry* e;
struct dns_hash_entry* tmp;
struct dns_hash_entry* ret;
ticks_t now;
int cname_chain;
str cname;
#ifdef DNS_WATCHDOG_SUPPORT
int servers_up;
servers_up = atomic_get(dns_servers_up);
#endif
cname_chain=0;
ret=0;
now=get_ticks_raw();
*err=0;
again:
*h=dns_hash_no(name->s, name->len, type);
#ifdef DNS_CACHE_DEBUG
LM_DBG("(%.*s(%d), %d), h=%d\n", name->len, name->s, name->len, type, *h);
#endif
clist_foreach_safe(&dns_hash[*h], e, tmp, next){
if (
#ifdef DNS_WATCHDOG_SUPPORT
/* remove expired elements only when the dns servers are up */
servers_up &&
#endif
/* automatically remove expired elements */
((e->ent_flags & DNS_FLAG_PERMANENT) == 0) &&
((s_ticks_t)(now-e->expire)>=0)
) {
_dns_hash_remove(e);
}else if ((e->type==type) && (e->name_len==name->len) &&
(strncasecmp(e->name, name->s, e->name_len)==0)){
e->last_used=now;
#ifdef DNS_LU_LST
/* add it at the end */
#ifdef DEBUG_LU_LST
debug_lu_lst("_dns_hash_find: pre rm:", &e->last_used_lst);
#endif
clist_rm(&e->last_used_lst, next, prev);
clist_append(dns_last_used_lst, &e->last_used_lst, next, prev);
#ifdef DEBUG_LU_LST
debug_lu_lst("_dns_hash_find: post append:", &e->last_used_lst);
#endif
#endif
return e;
}else if ((e->type==T_CNAME) &&
!((e->rr_lst==0) || (e->ent_flags & DNS_FLAG_BAD_NAME)) &&
(e->name_len==name->len) &&
(strncasecmp(e->name, name->s, e->name_len)==0)){
/*if CNAME matches and CNAME is entry is not a neg. cache entry
(could be produced by a specific CNAME lookup)*/
e->last_used=now;
#ifdef DNS_LU_LST
/* add it at the end */
#ifdef DEBUG_LU_LST
debug_lu_lst("_dns_hash_find: cname: pre rm:", &e->last_used_lst);
#endif
clist_rm(&e->last_used_lst, next, prev);
clist_append(dns_last_used_lst, &e->last_used_lst, next, prev);
#ifdef DEBUG_LU_LST
debug_lu_lst("_dns_hash_find: cname: post append:",
&e->last_used_lst);
#endif
#endif
ret=e; /* if this is an unfinished cname chain, we try to
return the last cname */
/* this is a cname => retry using its value */
if (cname_chain> MAX_CNAME_CHAIN){
LM_ERR("cname chain too long or recursive (\"%.*s\")\n",
name->len, name->s);
ret=0; /* error*/
*err=-1;
break;
}
cname_chain++;
cname.s=((struct cname_rdata*)e->rr_lst->rdata)->name;
cname.len= ((struct cname_rdata*)e->rr_lst->rdata)->name_len;
name=&cname;
goto again;
}
}
return ret;
}
/* frees cache entries, if expired_only=0 only expired entries will be
* removed, else all of them
* it will process maximum no entries (to process all of them use -1)
* returns the number of deleted entries
* This should be called from a timer process*/
inline static int dns_cache_clean(unsigned int no, int expired_only)
{
struct dns_hash_entry* e;
ticks_t now;
unsigned int n;
unsigned int deleted;
#ifdef DNS_LU_LST
struct dns_lu_lst* l;
struct dns_lu_lst* tmp;
#else
struct dns_hash_entry* t;
unsigned int h;
static unsigned int start=0;
#endif
n=0;
deleted=0;
now=get_ticks_raw();
LOCK_DNS_HASH();
#ifdef DNS_LU_LST
clist_foreach_safe(dns_last_used_lst, l, tmp, next){
e=(struct dns_hash_entry*)(((char*)l)-
(char*)&((struct dns_hash_entry*)(0))->last_used_lst);
if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
&& (!expired_only || ((s_ticks_t)(now-e->expire)>=0))
) {
_dns_hash_remove(e);
deleted++;
}
n++;
if (n>=no) break;
}
#else
for(h=start; h!=(start+DNS_HASH_SIZE); h++){
clist_foreach_safe(&dns_hash[h%DNS_HASH_SIZE], e, t, next){
if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
&& ((s_ticks_t)(now-e->expire)>=0)
) {
_dns_hash_remove(e);
deleted++;
}
n++;
if (n>=no) goto skip;
}
}
/* not fair, but faster then random() */
if (!expired_only){
for(h=start; h!=(start+DNS_HASH_SIZE); h++){
clist_foreach_safe(&dns_hash[h%DNS_HASH_SIZE], e, t, next){
if ((e->ent_flags & DNS_FLAG_PERMANENT) == 0) {
_dns_hash_remove(e);
deleted++;
}
n++;
if (n>=no) goto skip;
}
}
}
skip:
start=h;
#endif
UNLOCK_DNS_HASH();
return deleted;
}
/* frees cache entries, if expired_only=0 only expired entries will be
* removed, else all of them
* it will stop when the dns cache used memory reaches target (to process all
* of them use 0)
* returns the number of deleted entries */
inline static int dns_cache_free_mem(unsigned int target, int expired_only)
{
struct dns_hash_entry* e;
ticks_t now;
unsigned int deleted;
#ifdef DNS_LU_LST
struct dns_lu_lst* l;
struct dns_lu_lst* tmp;
#else
struct dns_hash_entry* t;
unsigned int h;
static unsigned int start=0;
#endif
deleted=0;
now=get_ticks_raw();
LOCK_DNS_HASH();
#ifdef DNS_LU_LST
clist_foreach_safe(dns_last_used_lst, l, tmp, next){
if (*dns_cache_mem_used<=target) break;
e=(struct dns_hash_entry*)(((char*)l)-
(char*)&((struct dns_hash_entry*)(0))->last_used_lst);
if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
&& (!expired_only || ((s_ticks_t)(now-e->expire)>=0))
) {
_dns_hash_remove(e);
deleted++;
}
}
#else
for(h=start; h!=(start+DNS_HASH_SIZE); h++){
clist_foreach_safe(&dns_hash[h%DNS_HASH_SIZE], e, t, next){
if (*dns_cache_mem_used<=target)
goto skip;
if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
&& ((s_ticks_t)(now-e->expire)>=0)
) {
_dns_hash_remove(e);
deleted++;
}
}
}
/* not fair, but faster then random() */
if (!expired_only){
for(h=start; h!=(start+DNS_HASH_SIZE); h++){
clist_foreach_safe(&dns_hash[h%DNS_HASH_SIZE], e, t, next){
if (*dns_cache_mem_used<=target)
goto skip;
if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
&& ((s_ticks_t)(now-e->expire)>=0)
) {
_dns_hash_remove(e);
deleted++;
}
}
}
}
skip:
start=h;
#endif
UNLOCK_DNS_HASH();
return deleted;
}
/* locking version (the dns hash must _not_be locked externally)
* returns 0 when not found, the searched entry on success (with CNAMEs
* followed) or the last CNAME entry from an unfinished CNAME chain,
* if the search matches a CNAME. On error sets *err (e.g. recursive CNAMEs).
* it increases the internal refcnt => when finished dns_hash_put() must
* be called on the returned entry
* WARNING: - the return might be a CNAME even if type!=CNAME, see above */
inline static struct dns_hash_entry* dns_hash_get(str* name, int type, int* h,
int* err)
{
struct dns_hash_entry* e;
LOCK_DNS_HASH();
e=_dns_hash_find(name, type, h, err);
if (e){
atomic_inc(&e->refcnt);
}
UNLOCK_DNS_HASH();
return e;
}
/* adds a fully created and init. entry (see dns_cache_mk_entry()) to the hash
* table
* returns 0 on success, -1 on error */
inline static int dns_cache_add(struct dns_hash_entry* e)
{
int h;
/* check space */
/* atomic_add_long(dns_cache_total_used, e->size); */
if ((*dns_cache_mem_used+e->total_size)>=cfg_get(core, core_cfg, dns_cache_max_mem)){
#ifdef USE_DNS_CACHE_STATS
dns_cache_stats[process_no].dc_lru_cnt++;
#endif
LM_WARN("cache full, trying to free...\n");
/* free ~ 12% of the cache */
dns_cache_free_mem(*dns_cache_mem_used/16*14,
!cfg_get(core, core_cfg, dns_cache_del_nonexp));
if ((*dns_cache_mem_used+e->total_size)>=cfg_get(core, core_cfg, dns_cache_max_mem)){
LM_ERR("max. cache mem size exceeded\n");
return -1;
}
}
atomic_inc(&e->refcnt);
h=dns_hash_no(e->name, e->name_len, e->type);
#ifdef DNS_CACHE_DEBUG
LM_DBG("adding %.*s(%d) %d (flags=%0x) at %d\n",
e->name_len, e->name, e->name_len, e->type, e->ent_flags, h);
#endif
LOCK_DNS_HASH();
*dns_cache_mem_used+=e->total_size; /* no need for atomic ops, written
only from within a lock */
clist_append(&dns_hash[h], e, next, prev);
#ifdef DNS_LU_LST
clist_append(dns_last_used_lst, &e->last_used_lst, next, prev);
#endif
UNLOCK_DNS_HASH();
return 0;
}
/* same as above, but it must be called with the dns hash lock held
* returns 0 on success, -1 on error */
inline static int dns_cache_add_unsafe(struct dns_hash_entry* e)
{
int h;
/* check space */
/* atomic_add_long(dns_cache_total_used, e->size); */
if ((*dns_cache_mem_used+e->total_size)>=cfg_get(core, core_cfg, dns_cache_max_mem)){
#ifdef USE_DNS_CACHE_STATS
dns_cache_stats[process_no].dc_lru_cnt++;
#endif
LM_WARN("cache full, trying to free...\n");
/* free ~ 12% of the cache */
UNLOCK_DNS_HASH();
dns_cache_free_mem(*dns_cache_mem_used/16*14,
!cfg_get(core, core_cfg, dns_cache_del_nonexp));
LOCK_DNS_HASH();
if ((*dns_cache_mem_used+e->total_size)>=cfg_get(core, core_cfg, dns_cache_max_mem)){
LM_ERR("max. cache mem size exceeded\n");
return -1;
}
}
atomic_inc(&e->refcnt);
h=dns_hash_no(e->name, e->name_len, e->type);
#ifdef DNS_CACHE_DEBUG
LM_DBG("adding %.*s(%d) %d (flags=%0x) at %d\n",
e->name_len, e->name, e->name_len, e->type, e->ent_flags, h);
#endif
*dns_cache_mem_used+=e->total_size; /* no need for atomic ops, written
only from within a lock */
clist_append(&dns_hash[h], e, next, prev);
#ifdef DNS_LU_LST
clist_append(dns_last_used_lst, &e->last_used_lst, next, prev);
#endif
return 0;
}
/* creates a "negative" entry which will be valid for ttl seconds */
inline static struct dns_hash_entry* dns_cache_mk_bad_entry(str* name,
int type,
int ttl,
int flags)
{
struct dns_hash_entry* e;
int size;
ticks_t now;
#ifdef DNS_CACHE_DEBUG
LM_DBG("(%.*s, %d, %d, %d)\n", name->len, name->s, type, ttl, flags);
#endif
size=sizeof(struct dns_hash_entry)+name->len-1+1;
e=shm_malloc(size);
if (e==0){
LM_ERR("out of memory\n");
return 0;
}
memset(e, 0, size); /* init with 0*/
e->total_size=size;
e->name_len=name->len;
e->type=type;
now=get_ticks_raw();
e->last_used=now;
e->expire=now+S_TO_TICKS(ttl);
memcpy(e->name, name->s, name->len);
e->ent_flags=flags;
return e;
}
/* create a a/aaaa hash entry from a name and ip address
* returns 0 on error */
inline static struct dns_hash_entry* dns_cache_mk_ip_entry(str* name,
struct ip_addr* ip)
{
struct dns_hash_entry* e;
int size;
ticks_t now;
/* everything is allocated in one block: dns_hash_entry + name +
* + dns_rr + rdata; dns_rr must start at an aligned adress,
* hence we need to round dns_hash_entry+name size to a sizeof(long)
* multiple.
* Memory image:
* struct dns_hash_entry
* name (name_len+1 bytes)
* padding to multiple of sizeof(long)
* dns_rr
* rdata (no padding needed, since for ip is just an array of chars)
*/
size=ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1)+
sizeof(struct dns_rr)+ ip->len;
e=shm_malloc(size);
if (e==0){
LM_ERR("out of memory\n");
return 0;
}
memset(e, 0, size); /* init with 0*/
e->total_size=size;
e->name_len=name->len;
e->type=(ip->af==AF_INET)?T_A:T_AAAA;
now=get_ticks_raw();
e->last_used=now;
e->expire=now-1; /* maximum expire */
memcpy(e->name, name->s, name->len); /* memset makes sure is 0-term. */
e->rr_lst=(void*)((char*)e+
ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1));
e->rr_lst->rdata=(void*)((char*)e->rr_lst+sizeof(struct dns_rr));
e->rr_lst->expire=now-1; /* maximum expire */
/* no need to align rr_lst->rdata for a or aaaa records */
memcpy(e->rr_lst->rdata, ip->u.addr, ip->len);
return e;
}
/* creates an srv hash entry from the given parameters
* returns 0 on error */
static struct dns_hash_entry* dns_cache_mk_srv_entry(str* name,
unsigned short priority,
unsigned short weight,
unsigned short port,
str* rr_name,
int ttl)
{
struct dns_hash_entry* e;
int size;
ticks_t now;
/* everything is allocated in one block: dns_hash_entry + name +
* + dns_rr + rdata; dns_rr must start at an aligned adress,
* hence we need to round dns_hash_entry+name size to a sizeof(long),
* and similarly, dns_rr must be rounded to sizeof(short).
* multiple.
* Memory image:
* struct dns_hash_entry
* name (name_len+1 bytes)
* padding to multiple of sizeof(long)
* dns_rr
* padding to multiple of sizeof(short)
* rdata
*/
size=ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1) +
ROUND_SHORT(sizeof(struct dns_rr)) +
sizeof(struct srv_rdata)-1 +
rr_name->len+1;
e=shm_malloc(size);
if (e==0){
LM_ERR("out of memory\n");
return 0;
}
memset(e, 0, size); /* init with 0*/
e->total_size=size;
e->name_len=name->len;
e->type=T_SRV;
now=get_ticks_raw();
e->last_used=now;
e->expire=now+S_TO_TICKS(ttl);
memcpy(e->name, name->s, name->len); /* memset makes sure is 0-term. */
e->rr_lst=(void*)((char*)e+
ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1));
e->rr_lst->rdata=(void*)((char*)e->rr_lst+ROUND_SHORT(sizeof(struct dns_rr)));
e->rr_lst->expire=e->expire;
((struct srv_rdata*)e->rr_lst->rdata)->priority = priority;
((struct srv_rdata*)e->rr_lst->rdata)->weight = weight;
((struct srv_rdata*)e->rr_lst->rdata)->port = port;
((struct srv_rdata*)e->rr_lst->rdata)->name_len = rr_name->len;
memcpy(((struct srv_rdata*)e->rr_lst->rdata)->name, rr_name->s, rr_name->len);
return e;
}
/* create a dns hash entry from a name and a rdata list (pkg_malloc'ed)
* (it will use only the type records with the name "name" from the
* rdata list with one exception: if a matching CNAME with the same
* name is found, the search will stop and this will be the record used)
* returns 0 on error and removes the used elements from the rdata list*/
inline static struct dns_hash_entry* dns_cache_mk_rd_entry(str* name, int type,
struct rdata** rd_lst)
{
struct dns_hash_entry* e;
struct dns_rr* rr;
struct dns_rr** tail_rr;
struct rdata** p;
struct rdata* tmp_lst;
struct rdata** tail;