/
resolve.c
1705 lines (1574 loc) · 44.7 KB
/
resolve.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
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* 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 resolver
* \ingroup core
* Module: \ref core
*/
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <string.h>
#include "resolve.h"
#include "compiler_opt.h"
#include "dprint.h"
#include "mem/mem.h"
#include "ip_addr.h"
#include "error.h"
#include "globals.h" /* tcp_disable, tls_disable a.s.o */
#include "cfg_core.h"
#include "socket_info.h"
#ifdef USE_DNS_CACHE
#include "dns_cache.h"
#endif
/* counters framework */
struct dns_counters_h dns_cnts_h;
counter_def_t dns_cnt_defs[] = {
{&dns_cnts_h.failed_dns_req, "failed_dns_request", 0, 0, 0,
"incremented each time a DNS request has failed."},
{0, 0, 0, 0, 0, 0 }
};
/* mallocs for local stuff */
#define local_malloc pkg_malloc
#define local_free pkg_free
#ifdef USE_NAPTR
static int naptr_proto_pref[PROTO_LAST+1];
#endif
static int srv_proto_pref[PROTO_LAST+1];
#ifdef USE_NAPTR
static void init_naptr_proto_prefs()
{
int ignore_rfc, udp, tcp, tls, sctp;
if ((PROTO_UDP > PROTO_LAST) || (PROTO_TCP > PROTO_LAST) ||
(PROTO_TLS > PROTO_LAST) || (PROTO_SCTP > PROTO_LAST)){
BUG("init_naptr_proto_prefs: array too small \n");
return;
}
ignore_rfc = cfg_get(core, core_cfg, dns_naptr_ignore_rfc);
udp = cfg_get(core, core_cfg, dns_udp_pref);
tcp = cfg_get(core, core_cfg, dns_tcp_pref);
tls = cfg_get(core, core_cfg, dns_tls_pref);
sctp = cfg_get(core, core_cfg, dns_sctp_pref);
/* Old implementation ignored the Order field in the NAPTR RR and
* thus violated a MUST in RFC 2915. Currently still the default. */
if (ignore_rfc) {
naptr_proto_pref[PROTO_UDP] = udp;
naptr_proto_pref[PROTO_TCP] = tcp;
naptr_proto_pref[PROTO_TLS] = tls;
naptr_proto_pref[PROTO_SCTP] = sctp;
} else {
/* If value is less than 0, proto is disabled, otherwise
* ignored. */
naptr_proto_pref[PROTO_UDP] = udp < 0 ? udp : 1;
naptr_proto_pref[PROTO_TCP] = tcp < 0 ? tcp : 1;
naptr_proto_pref[PROTO_TLS] = tls < 0 ? tls : 1;
naptr_proto_pref[PROTO_SCTP] = sctp < 0 ? sctp : 1;
}
}
#endif /* USE_NAPTR */
static void init_srv_proto_prefs()
{
if ((PROTO_UDP > PROTO_LAST) || (PROTO_TCP > PROTO_LAST) ||
(PROTO_TLS > PROTO_LAST) || (PROTO_SCTP > PROTO_LAST)){
BUG("init_srv_proto_prefs: array too small \n");
return;
}
srv_proto_pref[PROTO_UDP] = cfg_get(core, core_cfg, dns_udp_pref);
srv_proto_pref[PROTO_TCP] = cfg_get(core, core_cfg, dns_tcp_pref);
srv_proto_pref[PROTO_TLS] = cfg_get(core, core_cfg, dns_tls_pref);
srv_proto_pref[PROTO_SCTP] = cfg_get(core, core_cfg, dns_sctp_pref);
}
#ifdef DNS_WATCHDOG_SUPPORT
static on_resolv_reinit on_resolv_reinit_cb = NULL;
/* register the callback function */
int register_resolv_reinit_cb(on_resolv_reinit cb)
{
if (on_resolv_reinit_cb) {
LM_ERR("callback function has been already registered\n");
return -1;
}
on_resolv_reinit_cb = cb;
return 0;
}
#endif
/* counter init function
must be called before fork
*/
static int stat_init(void)
{
if (counter_register_array("dns", dns_cnt_defs) < 0)
goto error;
return 0;
error:
return -1;
}
/** init. the resolver
* params: retr_time - time before retransmitting (must be >0)
* retr_no - retransmissions number
* servers_no - how many dns servers will be used
* (from the one listed in /etc/resolv.conf)
* search - if 0 the search list in /etc/resolv.conf will
* be ignored (HINT: even if you don't have a
* search list in resolv.conf, it's still better
* to set search to 0, because an empty seachlist
* means in fact search "" => it takes more time)
* If any of the parameters <0, the default (system specific) value
* will be used. See also resolv.conf(5).
* returns: 0 on success, -1 on error
*/
static int _resolv_init(void)
{
dns_func.sr_res_init();
#ifdef HAVE_RESOLV_RES
if (cfg_get(core, core_cfg, dns_retr_time)>0)
_res.retrans=cfg_get(core, core_cfg, dns_retr_time);
if (cfg_get(core, core_cfg, dns_retr_no)>0)
_res.retry=cfg_get(core, core_cfg, dns_retr_no);
if ((cfg_get(core, core_cfg, dns_servers_no)>=0)
&& (cfg_get(core, core_cfg, dns_servers_no)<_res.nscount))
_res.nscount=cfg_get(core, core_cfg, dns_servers_no);
if (cfg_get(core, core_cfg, dns_search_list)==0)
_res.options&=~(RES_DEFNAMES|RES_DNSRCH);
#else
#warning "no resolv timeout support"
LM_WARN("no resolv options support - resolv options will be ignored\n");
#endif
return 0;
}
/** wrapper function to initialize the resolver at startup */
int resolv_init(void)
{
int res = -1;
_resolv_init();
reinit_proto_prefs(NULL,NULL);
/* init counter API only at startup
* This function must be called before DNS cache init method (if available)
*/
res = stat_init();
return res;
}
/** wrapper function to reinitialize the resolver
* This function must be called by each child process whenever
* a resolver option changes
*/
void resolv_reinit(str *gname, str *name)
{
_resolv_init();
#ifdef DNS_WATCHDOG_SUPPORT
if (on_resolv_reinit_cb) on_resolv_reinit_cb(name);
#endif
LM_DBG("DNS resolver has been reinitialized\n");
}
/** fixup function for dns_reinit variable
* (resets the variable to 0)
*/
int dns_reinit_fixup(void *handle, str *gname, str *name, void **val)
{
*val = (void *)(long)0;
return 0;
}
/** wrapper function to recalculate the naptr and srv protocol preferences */
void reinit_proto_prefs(str *gname, str *name)
{
#ifdef USE_NAPTR
init_naptr_proto_prefs();
#endif
init_srv_proto_prefs();
}
/** fixup function for dns_try_ipv6
* verifies that Kamailio really listens on an ipv6 interface
*/
int dns_try_ipv6_fixup(void *handle, str *gname, str *name, void **val)
{
if ((int)(long)(*val) && !(socket_types & SOCKET_T_IPV6)) {
LM_ERR("SER does not listen on any ipv6 interface, "
"there is no point in resolving ipv6 addresses\n");
return -1;
}
return 0;
}
/** skips over a domain name in a dns message
* (it can be a sequence of labels ending in \0, a pointer or
* a sequence of labels ending in a pointer -- see rfc1035
* returns pointer after the domain name or null on error*/
unsigned char* dns_skipname(unsigned char* p, unsigned char* end)
{
while(p<end){
/* check if \0 (root label length) */
if (*p==0){
p+=1;
break;
}
/* check if we found a pointer */
if (((*p)&0xc0)==0xc0){
/* if pointer skip over it (2 bytes) & we found the end */
p+=2;
break;
}
/* normal label */
p+=*p+1;
}
return (p>end)?0:p;
}
/** parses the srv record into a srv_rdata structure
* msg - pointer to the dns message
* end - pointer to the end of the message
* eor - pointer to the end of the record/rdata
* rdata - pointer to the rdata part of the srv answer
* returns 0 on error, or a dyn. alloc'ed srv_rdata structure
*
* SRV rdata format:
* 111111
* 0123456789012345
* +----------------+
* | priority |
* |----------------|
* | weight |
* |----------------|
* | port number |
* |----------------|
* | |
* ~ name ~
* | |
* +----------------+
*/
struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
unsigned char* eor,
unsigned char* rdata)
{
struct srv_rdata* srv;
unsigned short priority;
unsigned short weight;
unsigned short port;
int len;
char name[MAX_DNS_NAME];
srv=0;
if ((rdata+6+1)>eor) goto error;
memcpy((void*)&priority, rdata, 2);
memcpy((void*)&weight, rdata+2, 2);
memcpy((void*)&port, rdata+4, 2);
rdata+=6;
if (dn_expand(msg, end, rdata, name, MAX_DNS_NAME-1)<0)
goto error;
len=strlen(name);
if (len>255)
goto error;
/* alloc enought space for the struct + null terminated name */
srv=local_malloc(sizeof(struct srv_rdata)-1+len+1);
if (srv==0){
LM_ERR("out of memory\n");
goto error;
}
srv->priority=ntohs(priority);
srv->weight=ntohs(weight);
srv->port=ntohs(port);
srv->name_len=len;
memcpy(srv->name, name, srv->name_len);
srv->name[srv->name_len]=0;
return srv;
error:
if (srv) local_free(srv);
return 0;
}
/** parses the naptr record into a naptr_rdata structure
* msg - pointer to the dns message
* end - pointer to the end of the message
* eor - pointer to the end of the record/rdata
* rdata - pointer to the rdata part of the naptr answer
* returns 0 on error, or a dyn. alloc'ed naptr_rdata structure */
/* NAPTR rdata format:
* 111111
* 0123456789012345
* +----------------+
* | order |
* |----------------|
* | preference |
* |----------------|
* ~ flags ~
* | (string) |
* |----------------|
* ~ services ~
* | (string) |
* |----------------|
* ~ regexp ~
* | (string) |
* |----------------|
* ~ replacement ~
| (name) |
* +----------------+
*/
struct naptr_rdata* dns_naptr_parser( unsigned char* msg, unsigned char* end,
unsigned char* eor,
unsigned char* rdata)
{
struct naptr_rdata* naptr;
unsigned char* flags;
unsigned char* services;
unsigned char* regexp;
unsigned short order;
unsigned short pref;
unsigned char flags_len;
unsigned char services_len;
unsigned char regexp_len;
int len;
char repl[MAX_DNS_NAME];
naptr = 0;
if ((rdata + 7 + 1)>eor) goto error;
memcpy((void*)&order, rdata, 2);
memcpy((void*)&pref, rdata + 2, 2);
flags_len = rdata[4];
if ((rdata + 7 + 1 + flags_len) > eor)
goto error;
flags=rdata+5;
services_len = rdata[5 + flags_len];
if ((rdata + 7 + 1 + flags_len + services_len) > eor)
goto error;
services=rdata + 6 + flags_len;
regexp_len = rdata[6 + flags_len + services_len];
if ((rdata + 7 +1 + flags_len + services_len + regexp_len) > eor)
goto error;
regexp=rdata + 7 + flags_len + services_len;
rdata = rdata + 7 + flags_len + services_len + regexp_len;
if (dn_expand(msg, end, rdata, repl, MAX_DNS_NAME-1) == -1)
goto error;
len=strlen(repl);
if (len>255)
goto error;
naptr=local_malloc(sizeof(struct naptr_rdata)+flags_len+services_len+
regexp_len+len+1-1);
if (naptr == 0){
LM_ERR("out of memory\n");
goto error;
}
naptr->order=ntohs(order);
naptr->pref=ntohs(pref);
naptr->flags=&naptr->str_table[0];
naptr->flags_len=flags_len;
memcpy(naptr->flags, flags, naptr->flags_len);
naptr->services=&naptr->str_table[flags_len];
naptr->services_len=services_len;
memcpy(naptr->services, services, naptr->services_len);
naptr->regexp=&naptr->str_table[flags_len+services_len];
naptr->regexp_len=regexp_len;
memcpy(naptr->regexp, regexp, naptr->regexp_len);
naptr->repl=&naptr->str_table[flags_len+services_len+regexp_len];
naptr->repl_len=len;
memcpy(naptr->repl, repl, len);
naptr->repl[len]=0; /* null term. */
return naptr;
error:
if (naptr) local_free(naptr);
return 0;
}
/** parses a CNAME record into a cname_rdata structure */
struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
unsigned char* rdata)
{
struct cname_rdata* cname;
int len;
char name[MAX_DNS_NAME];
cname=0;
if (dn_expand(msg, end, rdata, name, MAX_DNS_NAME-1)==-1)
goto error;
len=strlen(name);
if (len>255)
goto error;
/* alloc sizeof struct + space for the null terminated name */
cname=local_malloc(sizeof(struct cname_rdata)-1+len+1);
if(cname==0){
LM_ERR("out of memory\n");
goto error;
}
cname->name_len=len;
memcpy(cname->name, name, cname->name_len);
cname->name[cname->name_len]=0;
return cname;
error:
if (cname) local_free(cname);
return 0;
}
/** parses an A record rdata into an a_rdata structure
* returns 0 on error or a dyn. alloc'ed a_rdata struct
*/
struct a_rdata* dns_a_parser(unsigned char* rdata, unsigned char* eor)
{
struct a_rdata* a;
if (rdata+4>eor) goto error;
a=(struct a_rdata*)local_malloc(sizeof(struct a_rdata));
if (a==0){
LM_ERR("out of memory\n");
goto error;
}
memcpy(a->ip, rdata, 4);
return a;
error:
return 0;
}
/** parses an AAAA (ipv6) record rdata into an aaaa_rdata structure
* returns 0 on error or a dyn. alloc'ed aaaa_rdata struct */
struct aaaa_rdata* dns_aaaa_parser(unsigned char* rdata, unsigned char* eor)
{
struct aaaa_rdata* aaaa;
if (rdata+16>eor) goto error;
aaaa=(struct aaaa_rdata*)local_malloc(sizeof(struct aaaa_rdata));
if (aaaa==0){
LM_ERR("out of memory\n");
goto error;
}
memcpy(aaaa->ip6, rdata, 16);
return aaaa;
error:
return 0;
}
/** parses a TXT record into a txt_rdata structure.
* @param msg - pointer to the dns message
* @param end - pointer to the end of the record (rdata end)
* @param rdata - pointer to the rdata part of the txt answer
* returns 0 on error, or a dyn. alloc'ed txt_rdata structure */
/* TXT rdata format:
*
* one or several character strings:
* 01234567
* +--------------------+
* | len | string / ...
* |------------------+
*/
static struct txt_rdata* dns_txt_parser(unsigned char* msg, unsigned char* end,
unsigned char* rdata)
{
struct txt_rdata* txt;
int len, n, i;
int str_size;
unsigned char* p;
unsigned char* st;
txt=0;
if (unlikely((rdata+1)>end)) goto error;
n=0;
str_size=0;
/* count the number of strings */
p=rdata;
do{
len=*p;
p+=len+1;
str_size+=len+1; /* 1 for the term. 0 */
if (unlikely(p>end)) goto error;
n++;
}while(p<end);
/* alloc sizeof struct + space for the dns_cstr array + space for
the strings */
txt=local_malloc(sizeof(struct txt_rdata) +(n-1)*sizeof(struct dns_cstr)+
str_size);
if(unlikely(txt==0)){
LM_ERR("out of memory\n");
goto error;
}
/* string table */
st=(unsigned char*)txt+sizeof(struct txt_rdata) +
(n-1)*sizeof(struct dns_cstr);
txt->cstr_no=n;
txt->tslen=str_size;
/* fill the structure */
p=rdata;
for (i=0; i<n; i++){
len=*p;
memcpy(st, p+1, len);
st[len]=0;
txt->txt[i].cstr_len=len;
txt->txt[i].cstr=(char*)st;
st+=len+1;
p+=len+1;
}
return txt;
error:
if (txt) local_free(txt);
return 0;
}
/** parses an EBL record into a txt_rdata structure.
* @param msg - pointer to the dns message
* @param end - pointer to the end of the dns message
* @param eor - pointer to the end of the record (rdata end)
* @param rdata - pointer to the rdata part of the txt answer
* returns 0 on error, or a dyn. alloc'ed txt_rdata structure */
/* EBL rdata format:
* (see http://tools.ietf.org/html/draft-ietf-enum-branch-location-record-03)
* one or several character strings:
* 01234567
* +--------+
* | postion|
* +-----------+
* / separator /
* +-----------+
* / apex /
* +----------+
*
* where separator is a character string ( 8 bit len, followed by len chars)
* and apex is a domain-name.
*/
static struct ebl_rdata* dns_ebl_parser(unsigned char* msg, unsigned char* end,
unsigned char* eor,
unsigned char* rdata)
{
struct ebl_rdata* ebl;
int sep_len;
int apex_len;
char apex[MAX_DNS_NAME];
ebl=0;
/* check if len is at least 4 chars (minimum possible):
pos (1 byte) + sep. (min 1 byte) + apex (min. 2 bytes)
and also check if rdata+1 (pos) + 1 (sep. len) + sep_len + 1 is ok*/
if (unlikely(((rdata+4)>eor)||((rdata+1+1+rdata[1]+2)>eor))) goto error;
sep_len=rdata[1];
if (unlikely(dn_expand(msg, end, rdata+1+1+sep_len,
apex, MAX_DNS_NAME-1)==-1))
goto error;
apex_len=strlen(apex);
/* alloc sizeof struct + space for the 2 null-terminated strings */
ebl=local_malloc(sizeof(struct ebl_rdata)-1+sep_len+1+apex_len+1);
if (ebl==0){
LM_ERR("out of memory\n");
goto error;
}
ebl->position=rdata[0];
ebl->separator=&ebl->str_table[0];
ebl->apex=ebl->separator+sep_len+1;
ebl->separator_len=sep_len;
ebl->apex_len=apex_len;
memcpy(ebl->separator, rdata+2, sep_len);
ebl->separator[sep_len]=0;
memcpy(ebl->apex, apex, apex_len);
ebl->apex[apex_len]=0;
return ebl;
error:
if (ebl) local_free(ebl);
return 0;
}
/** parses a PTR record into a ptr_rdata structure */
struct ptr_rdata* dns_ptr_parser( unsigned char* msg, unsigned char* end,
unsigned char* rdata)
{
struct ptr_rdata* pname;
int len;
char name[MAX_DNS_NAME];
pname=0;
if (dn_expand(msg, end, rdata, name, MAX_DNS_NAME-1)==-1)
goto error;
len=strlen(name);
if (len>255)
goto error;
/* alloc sizeof struct + space for the null terminated name */
pname=local_malloc(sizeof(struct ptr_rdata)-1+len+1);
if(pname==0){
LM_ERR("out of memory\n");
goto error;
}
pname->ptrdname_len=len;
memcpy(pname->ptrdname, name, pname->ptrdname_len);
pname->ptrdname[pname->ptrdname_len]=0;
return pname;
error:
if (pname) local_free(pname);
return 0;
}
/** frees completely a struct rdata list */
void free_rdata_list(struct rdata* head)
{
struct rdata* l;
struct rdata* next_l;
l=head;
while (l != 0) {
next_l = l->next;
/* free the parsed rdata*/
if (l->rdata) local_free(l->rdata);
local_free(l);
l = next_l;
}
}
#ifdef HAVE_RESOLV_RES
/** checks whether supplied name exists in the resolver search list
* returns 1 if found
* 0 if not found
*/
int match_search_list(const struct __res_state* res, char* name) {
int i;
for (i=0; (i<MAXDNSRCH) && (res->dnsrch[i]); i++) {
if (strcasecmp(name, res->dnsrch[i])==0)
return 1;
}
return 0;
}
#endif
/** gets the DNS records for name:type
* returns a dyn. alloc'ed struct rdata linked list with the parsed responses
* or 0 on error
* see rfc1035 for the query/response format */
struct rdata* get_record(char* name, int type, int flags)
{
int size;
int skip;
int qno, answers_no;
int i, r;
static union dns_query buff;
unsigned char* p;
unsigned char* end;
unsigned char* rd_end;
static char rec_name[MAX_DNS_NAME]; /* placeholder for the record name */
int rec_name_len;
unsigned short rtype, class, rdlength;
unsigned int ttl;
struct rdata* head;
struct rdata** crt;
struct rdata** last;
struct rdata* rd;
struct srv_rdata* srv_rd;
struct srv_rdata* crt_srv;
int search_list_used;
int name_len;
struct rdata* fullname_rd;
char c;
name_len=strlen(name);
for (i = 0; i < name_len; i++) {
c = name[i];
if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) ||
((c >= '0') && (c <= '9')) || (name[i] == '.') ||
(name[i] == '-') || (name[i] == '_'))
continue;
LM_DBG("'%s' is not domain name\n", name);
return 0;
}
if (cfg_get(core, core_cfg, dns_search_list)==0) {
search_list_used=0;
name_len=0;
} else {
search_list_used=1;
}
fullname_rd=0;
size=dns_func.sr_res_search(name, C_IN, type, buff.buff, sizeof(buff));
if (unlikely(size<0)) {
LM_DBG("lookup(%s, %d) failed\n", name, type);
goto not_found;
}
else if (unlikely(size > sizeof(buff))) size=sizeof(buff);
head=rd=0;
last=crt=&head;
p=buff.buff+DNS_HDR_SIZE;
end=buff.buff+size;
if (unlikely(p>=end)) goto error_boundary;
qno=ntohs((unsigned short)buff.hdr.qdcount);
for (r=0; r<qno; r++){
/* skip the name of the question */
if (unlikely((p=dns_skipname(p, end))==0)) {
LM_ERR("skipname==0\n");
goto error;
}
p+=2+2; /* skip QCODE & QCLASS */
#if 0
for (;(p<end && (*p)); p++);
p+=1+2+2; /* skip the ending '\0, QCODE and QCLASS */
#endif
if (unlikely(p>end)) {
LM_ERR("p>=end\n");
goto error;
}
};
answers_no=ntohs((unsigned short)buff.hdr.ancount);
again:
for (r=0; (r<answers_no) && (p<end); r++){
#if 0
/* ignore it the default domain name */
if ((p=dns_skipname(p, end))==0) {
LM_ERR("get_record: skip_name=0 (#2)\n");
goto error;
}
#else
if (unlikely((skip=dn_expand(buff.buff, end, p, rec_name,
MAX_DNS_NAME-1))==-1)){
LM_ERR("dn_expand(rec_name) failed\n");
goto error;
}
#endif
p+=skip;
rec_name_len=strlen(rec_name);
if (unlikely(rec_name_len>255)){
LM_ERR("dn_expand(rec_name): name too long (%d)\n",
rec_name_len);
goto error;
}
/* check if enough space is left for type, class, ttl & size */
if (unlikely((p+2+2+4+2)>end)) goto error_boundary;
/* get type */
memcpy((void*) &rtype, (void*)p, 2);
rtype=ntohs(rtype);
p+=2;
/* get class */
memcpy((void*) &class, (void*)p, 2);
class=ntohs(class);
p+=2;
/* get ttl*/
memcpy((void*) &ttl, (void*)p, 4);
ttl=ntohl(ttl);
p+=4;
/* get size */
memcpy((void*)&rdlength, (void*)p, 2);
rdlength=ntohs(rdlength);
p+=2;
rd_end=p+rdlength;
if (unlikely((rd_end)>end)) goto error_boundary;
if ((flags & RES_ONLY_TYPE) && (rtype!=type)){
/* skip */
p=rd_end;
continue;
}
/* expand the "type" record (rdata)*/
rd=(struct rdata*) local_malloc(sizeof(struct rdata)+rec_name_len+
1-1);
if (rd==0){
LM_ERR("out of memory\n");
goto error;
}
rd->type=rtype;
rd->pclass=class;
rd->ttl=ttl;
rd->next=0;
memcpy(rd->name, rec_name, rec_name_len);
rd->name[rec_name_len]=0;
rd->name_len=rec_name_len;
/* check if full name matches */
if ((search_list_used==1)&&(fullname_rd==0)&&
(rec_name_len>=name_len)&&
(strncasecmp(rec_name, name, name_len)==0)) {
/* now we have record whose name is the same (up-to the
* name_len with the searched one):
* if the length is the same - we found full match, no fake
* cname needed, just clear the flag
* if the length of the name differs - it has matched using
* search list remember the rd, so we can create fake CNAME
* record when all answers are used and no better match found
*/
if (rec_name_len==name_len)
search_list_used=0;
/* this is safe.... here was rec_name_len > name_len */
else if (rec_name[name_len]=='.') {
#ifdef HAVE_RESOLV_RES
if ((cfg_get(core, core_cfg, dns_search_fmatch)==0) ||
(match_search_list(&_res, rec_name+name_len+1)!=0))
#endif
fullname_rd=rd;
}
}
switch(rtype){
case T_SRV:
srv_rd= dns_srv_parser(buff.buff, end, rd_end, p);
rd->rdata=(void*)srv_rd;
if (unlikely(srv_rd==0)) goto error_parse;
/* insert sorted into the list */
for (crt=&head; *crt; crt= &((*crt)->next)){
if ((*crt)->type!=T_SRV)
continue;
crt_srv=(struct srv_rdata*)(*crt)->rdata;
if ((srv_rd->priority < crt_srv->priority) ||
( (srv_rd->priority == crt_srv->priority) &&
(srv_rd->weight > crt_srv->weight) ) ){
/* insert here */
goto skip;
}
}
last=&(rd->next); /*end of for => this will be the last
element*/
skip:
/* insert here */
rd->next=*crt;
*crt=rd;
break;
case T_A:
rd->rdata=(void*) dns_a_parser(p, rd_end);
if (unlikely(rd->rdata==0)) goto error_parse;
*last=rd; /* last points to the last "next" or the list
head*/
last=&(rd->next);
break;
case T_AAAA:
rd->rdata=(void*) dns_aaaa_parser(p, rd_end);
if (unlikely(rd->rdata==0)) goto error_parse;
*last=rd;
last=&(rd->next);
break;
case T_CNAME:
rd->rdata=(void*) dns_cname_parser(buff.buff, end, p);
if(unlikely(rd->rdata==0)) goto error_parse;
*last=rd;
last=&(rd->next);
break;
case T_NAPTR:
rd->rdata=(void*)dns_naptr_parser(buff.buff, end, rd_end, p);
if(unlikely(rd->rdata==0)) goto error_parse;
*last=rd;
last=&(rd->next);
break;
case T_TXT:
rd->rdata= dns_txt_parser(buff.buff, rd_end, p);
if (rd->rdata==0) goto error_parse;
*last=rd;
last=&(rd->next);
break;
case T_EBL:
rd->rdata= dns_ebl_parser(buff.buff, end, rd_end, p);
if (rd->rdata==0) goto error_parse;
*last=rd;
last=&(rd->next);
break;
case T_PTR:
rd->rdata=(void*) dns_ptr_parser(buff.buff, end, p);
if(unlikely(rd->rdata==0)) goto error_parse;
*last=rd;
last=&(rd->next);
break;
default:
LM_ERR("unknown type %d\n", rtype);
rd->rdata=0;
*last=rd;
last=&(rd->next);
}
p+=rdlength;
}
if (flags & RES_AR){
flags&=~RES_AR;
answers_no=ntohs((unsigned short)buff.hdr.nscount);
#ifdef RESOLVE_DBG
LM_DBG("skipping %d NS (p=%p, end=%p)\n", answers_no, p, end);
#endif
for (r=0; (r<answers_no) && (p<end); r++){
/* skip over the ns records */
if ((p=dns_skipname(p, end))==0) {
LM_ERR("skip_name=0 (#3)\n");
goto error;
}
/* check if enough space is left for type, class, ttl & size */
if (unlikely((p+2+2+4+2)>end)) goto error_boundary;
memcpy((void*)&rdlength, (void*)p+2+2+4, 2);
p+=2+2+4+2+ntohs(rdlength);
}
answers_no=ntohs((unsigned short)buff.hdr.arcount);
#ifdef RESOLVE_DBG
LM_DBG("parsing %d ARs (p=%p, end=%p)\n", answers_no, p, end);
#endif
goto again; /* add also the additional records */
}
/* if the name was expanded using DNS search list
* create fake CNAME record to convert the short name
* (queried) to long name (answered)
*/
if ((search_list_used==1)&&(fullname_rd!=0)) {
rd=(struct rdata*) local_malloc(sizeof(struct rdata)+name_len+1-1);
if (unlikely(rd==0)){
LM_ERR("out of memory\n");
goto error;
}
rd->type=T_CNAME;
rd->pclass=fullname_rd->pclass;
rd->ttl=fullname_rd->ttl;
rd->next=head;
memcpy(rd->name, name, name_len);
rd->name[name_len]=0;
rd->name_len=name_len;
/* alloc sizeof struct + space for the null terminated name */
rd->rdata=(void*)local_malloc(sizeof(struct cname_rdata)-1+
head->name_len+1);
if(unlikely(rd->rdata==0)){
LM_ERR("out of memory\n");
goto error_rd;
}
((struct cname_rdata*)(rd->rdata))->name_len=fullname_rd->name_len;
memcpy(((struct cname_rdata*)(rd->rdata))->name, fullname_rd->name,
fullname_rd->name_len);
((struct cname_rdata*)(rd->rdata))->name[head->name_len]=0;
head=rd;
}
return head;
error_boundary:
LM_ERR("end of query buff reached\n");
if (head) free_rdata_list(head);
return 0;
error_parse:
LM_ERR("rdata parse error (%s, %d), %p-%p"
" rtype=%d, class=%d, ttl=%d, rdlength=%d\n",
name, type,
p, end, rtype, class, ttl, rdlength);
error_rd:
if (rd) local_free(rd); /* rd->rdata=0 & rd is not linked yet into
the list */