#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <string.h>
#include <stdlib.h>
#include "mem/mem.h"
#include "mem/shm_mem.h"
#include "resolve.h"
#include "dprint.h"
#include "ut.h"
#include "ip_addr.h"
#include "globals.h"
#include "blacklists.h"

Go to the source code of this file.
Data Structures | |
| struct | dns_val |
Defines | |
| #define | DNS_BL_EXPIRE 4*60 |
| #define | DNS_NODE_A 2 |
| #define | DNS_NODE_SRV 1 |
| #define | DNS_REVOLVER_BL_ID 17 |
| #define | DNS_REVOLVER_BL_NAME "dns" |
| #define | local_free pkg_free |
| #define | local_malloc pkg_malloc |
| #define | naptr_prio(_naptr) ((unsigned int)((((_naptr)->order) << 16) + ((_naptr)->pref))) |
| #define | rd2srv(_rd) ((struct srv_rdata*)_rd->rdata) |
Functions | |
| static int | a2dns_node (struct rdata *head, struct dns_node **dn) |
| struct a_rdata * | dns_a_parser (unsigned char *rdata, unsigned char *end) |
| Parses an A record rdata into an a_rdata structure. | |
| struct aaaa_rdata * | dns_aaaa_parser (unsigned char *rdata, unsigned char *end) |
| Parses an AAAA (ipv6) record rdata into an aaaa_rdata structure. | |
| struct cname_rdata * | dns_cname_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata) |
| Parses a CNAME record into a cname_rdata structure. | |
| struct ebl_rdata * | dns_ebl_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata) |
| parses a EBL record into a ebl_rdata structure | |
| struct naptr_rdata * | dns_naptr_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata) |
| parses the naptr record into a naptr_rdata structure | |
| static struct dns_node * | dns_node_copy (struct dns_node *s) |
| struct dns_node * | dns_res_copy (struct dns_node *s) |
| make a perfect copy of a resolver state machine | |
| unsigned char * | dns_skipname (unsigned char *p, unsigned char *end) |
| 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. | |
| struct srv_rdata * | dns_srv_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata) |
| parses the srv record into a srv_rdata structure | |
| struct txt_rdata * | dns_txt_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata) |
| Parses a TXT record into a txt_rdata structure. | |
| static struct hostent * | do_srv_lookup (char *name, unsigned short *port, struct dns_node **dn) |
| Get port for hostname from SRV record. | |
| static void | filter_and_sort_naptr (struct rdata **head_p, struct rdata **filtered_p, int is_sips) |
| void | free_dns_res (struct proxy_l *p) |
| free the DNS resolver state machine | |
| void | free_rdata_list (struct rdata *head) |
| frees completely a struct rdata list | |
| static int | get_naptr_proto (struct naptr_rdata *n) |
| Detect the protocol from an NAPTR structure. | |
| static struct hostent * | get_next_he (struct dns_node **node, unsigned short *proto, unsigned short *port) |
| int | get_next_su (struct proxy_l *p, union sockaddr_union *su, int add_to_bl) |
| taked the next destination from a resolver state machine | |
| struct rdata * | get_record (char *name, int type) |
| Hets the DNS records for name:type. | |
| int | resolv_blacklist_init (void) |
| Initialize blacklist. | |
| int | resolv_init (void) |
| Initialize the DNS resolver 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 searchlist 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). | |
| struct hostent * | sip_resolvehost (str *name, unsigned short *port, unsigned short *proto, int is_sips, struct dns_node **dn) |
| Resolve SIP hostname to ip address, protocol and port. | |
| static void | sort_srvs (struct rdata **head) |
| static int | srv2dns_node (struct rdata *head, struct dns_node **dn) |
Variables | |
| int | disable_dns_blacklist = 1 |
| int | dns_retr_no = -1 |
| int | dns_retr_time = -1 |
| int | dns_search_list = -1 |
| int | dns_servers_no = -1 |
| int | dns_try_ipv6 = 0 |
| static struct bl_head * | failover_bl = 0 |
Definition in file resolve.c.
| #define DNS_BL_EXPIRE 4*60 |
| #define DNS_NODE_A 2 |
| #define DNS_NODE_SRV 1 |
| #define DNS_REVOLVER_BL_ID 17 |
| #define DNS_REVOLVER_BL_NAME "dns" |
| #define local_free pkg_free |
Definition at line 64 of file resolve.c.
Referenced by dns_cname_parser(), dns_ebl_parser(), dns_naptr_parser(), dns_srv_parser(), dns_txt_parser(), free_rdata_list(), and get_record().
| #define local_malloc pkg_malloc |
Definition at line 63 of file resolve.c.
Referenced by dns_a_parser(), dns_aaaa_parser(), dns_cname_parser(), dns_ebl_parser(), dns_naptr_parser(), dns_srv_parser(), dns_txt_parser(), and get_record().
| #define naptr_prio | ( | _naptr | ) | ((unsigned int)((((_naptr)->order) << 16) + ((_naptr)->pref))) |
Referenced by sort_srvs().
Definition at line 714 of file resolve.c.
References DNS_NODE_A, get_srv, dns_node::idx, dns_val::ival, dns_node::kids, LM_DBG, LM_ERR, rdata::next, dns_node::no, NULL, shm_malloc(), dns_node::size, dns_val::sval, dns_node::type, and dns_node::vals.
Referenced by do_srv_lookup().
| struct a_rdata* dns_a_parser | ( | unsigned char * | rdata, | |
| unsigned char * | end | |||
| ) | [read] |
Parses an A record rdata into an a_rdata structure.
Definition at line 307 of file resolve.c.
References a_rdata::ip, LM_ERR, and local_malloc.
Referenced by get_record().
| struct aaaa_rdata* dns_aaaa_parser | ( | unsigned char * | rdata, | |
| unsigned char * | end | |||
| ) | [read] |
Parses an AAAA (ipv6) record rdata into an aaaa_rdata structure.
Definition at line 328 of file resolve.c.
References aaaa_rdata::ip6, LM_ERR, and local_malloc.
Referenced by get_record().
| struct cname_rdata* dns_cname_parser | ( | unsigned char * | msg, | |
| unsigned char * | end, | |||
| unsigned char * | rdata | |||
| ) | [read] |
Parses a CNAME record into a cname_rdata structure.
Definition at line 282 of file resolve.c.
References len, LM_ERR, local_free, local_malloc, MAX_DNS_NAME, and cname_rdata::name.
Referenced by get_record().
| struct ebl_rdata* dns_ebl_parser | ( | unsigned char * | msg, | |
| unsigned char * | end, | |||
| unsigned char * | rdata | |||
| ) | [read] |
parses a EBL record into a ebl_rdata structure
EBL Record
0 1 2 3 4 5 6 7 +--+--+--+--+--+--+--+--+ | POSITION | +--+--+--+--+--+--+--+--+ / SEPARATOR / +--+--+--+--+--+--+--+--+ / APEX / +--+--+--+--+--+--+--+--+
Definition at line 393 of file resolve.c.
References ebl_rdata::apex, ebl_rdata::apex_len, len, LM_ERR, local_free, local_malloc, MAX_DNS_NAME, ebl_rdata::position, ebl_rdata::separator, and ebl_rdata::separator_len.
Referenced by get_record().
| struct naptr_rdata* dns_naptr_parser | ( | unsigned char * | msg, | |
| unsigned char * | end, | |||
| unsigned char * | rdata | |||
| ) | [read] |
parses the naptr record into a naptr_rdata structure
| msg | - pointer to the dns message | |
| end | - pointer to the end of the message | |
| rdata | - pointer to the rdata part of the naptr answer |
Definition at line 237 of file resolve.c.
References naptr_rdata::flags, naptr_rdata::flags_len, LM_ERR, local_free, local_malloc, MAX_DNS_NAME, naptr_rdata::order, naptr_rdata::pref, naptr_rdata::regexp, naptr_rdata::regexp_len, naptr_rdata::repl, naptr_rdata::repl_len, naptr_rdata::services, and naptr_rdata::services_len.
Referenced by get_record().
Definition at line 1292 of file resolve.c.
References LM_ERR, dns_node::no, NULL, shm_malloc(), dns_node::size, dns_val::sval, and dns_node::vals.
Referenced by dns_res_copy().
make a perfect copy of a resolver state machine
Definition at line 1310 of file resolve.c.
References dns_node_copy(), dns_node::kids, NULL, and shm_free.
Referenced by shm_clone_proxy().
| unsigned char* dns_skipname | ( | unsigned char * | p, | |
| unsigned char * | end | |||
| ) |
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.
Definition at line 136 of file resolve.c.
Referenced by get_record().
| struct srv_rdata* dns_srv_parser | ( | unsigned char * | msg, | |
| unsigned char * | end, | |||
| unsigned char * | rdata | |||
| ) | [read] |
parses the srv record into a srv_rdata structure
| msg | - pointer to the dns message | |
| end | - pointer to the end of the message | |
| rdata | - pointer to the rdata part of the srv answer |
Definition at line 179 of file resolve.c.
References len, LM_ERR, local_free, local_malloc, MAX_DNS_NAME, srv_rdata::name, srv_rdata::port, srv_rdata::priority, srv, and srv_rdata::weight.
Referenced by get_record().
| struct txt_rdata* dns_txt_parser | ( | unsigned char * | msg, | |
| unsigned char * | end, | |||
| unsigned char * | rdata | |||
| ) | [read] |
Parses a TXT record into a txt_rdata structure.
Definition at line 351 of file resolve.c.
References len, LM_ERR, local_free, local_malloc, and txt_rdata::txt.
Referenced by get_record().
| static struct hostent* do_srv_lookup | ( | char * | name, | |
| unsigned short * | port, | |||
| struct dns_node ** | dn | |||
| ) | [static, read] |
Get port for hostname from SRV record.
| name | hostname | |
| port | port, will be set from this function | |
| dn | DNS node |
Definition at line 833 of file resolve.c.
References a2dns_node(), free_rdata_list(), get_record(), LM_CRIT, LM_DBG, srv_rdata::name, rdata::next, srv_rdata::port, rdata::rdata, resolvehost(), sort_srvs(), srv, and rdata::type.
Referenced by get_next_he(), and sip_resolvehost().
| static void filter_and_sort_naptr | ( | struct rdata ** | head_p, | |
| struct rdata ** | filtered_p, | |||
| int | is_sips | |||
| ) | [inline, static] |
Definition at line 870 of file resolve.c.
References naptr_rdata::flags, naptr_rdata::flags_len, get_naptr, LM_CRIT, LM_DBG, naptr_prio, rdata::next, rdata::rdata, naptr_rdata::regexp_len, naptr_rdata::repl, naptr_rdata::repl_len, naptr_rdata::services, naptr_rdata::services_len, and rdata::type.
Referenced by sip_resolvehost().
| void free_dns_res | ( | struct proxy_l * | p | ) |
free the DNS resolver state machine
Definition at line 1226 of file resolve.c.
References proxy_l::dn, dns_node::kids, NULL, and shm_free.
Referenced by free_proxy(), free_shm_proxy(), get_next_su(), mk_proxy(), and mk_shm_proxy().
| void free_rdata_list | ( | struct rdata * | head | ) |
frees completely a struct rdata list
Definition at line 439 of file resolve.c.
References local_free, rdata::next, and rdata::rdata.
Referenced by do_query(), do_srv_lookup(), enum_pv_query_3(), get_record(), i_enum_query_2(), is_from_user_enum_2(), sip_resolvehost(), and sort_srvs().
| static int get_naptr_proto | ( | struct naptr_rdata * | n | ) | [inline, static] |
Detect the protocol from an NAPTR structure.
| n | NAPTR data |
Definition at line 645 of file resolve.c.
References LM_CRIT, PROTO_NONE, PROTO_SCTP, PROTO_TCP, PROTO_TLS, PROTO_UDP, naptr_rdata::services, and naptr_rdata::services_len.
Referenced by sip_resolvehost(), and srv2dns_node().
| static struct hostent* get_next_he | ( | struct dns_node ** | node, | |
| unsigned short * | proto, | |||
| unsigned short * | port | |||
| ) | [static, read] |
Definition at line 1148 of file resolve.c.
References DNS_NODE_A, DNS_NODE_SRV, do_srv_lookup(), dns_node::idx, dns_val::ival, dns_node::kids, LM_CRIT, dns_node::no, NULL, resolvehost(), shm_free, dns_val::sval, dns_node::type, and dns_node::vals.
Referenced by get_next_su().
| int get_next_su | ( | struct proxy_l * | p, | |
| union sockaddr_union * | su, | |||
| int | add_to_bl | |||
| ) |
taked the next destination from a resolver state machine
Definition at line 1239 of file resolve.c.
References add_list_to_head(), add_rule_to_list(), proxy_l::addr_idx, ip_addr::af, proxy_l::dn, DNS_BL_EXPIRE, proxy_l::flags, free_dns_res(), free_hostent(), free_shm_hostent(), get_next_he(), proxy_l::host, hostent2ip_addr, hostent2su(), hostent_cpy(), hostent_shm_cpy(), net::ip, ip_addr::len, LM_ERR, net::mask, NULL, proxy_l::port, proxy_l::proto, PROXY_SHM_FLAG, and SIP_PORT.
Referenced by do_dns_failover(), forward_request(), forward_sl_request(), t_forward_nonack(), trace_send_duplicate(), and utils_forward().
| struct rdata* get_record | ( | char * | name, | |
| int | type | |||
| ) | [read] |
Hets the DNS records for name:type.
Definition at line 460 of file resolve.c.
References ANS_SIZE, dns_query::buff, rdata::class, dns_a_parser(), dns_aaaa_parser(), dns_cname_parser(), dns_ebl_parser(), DNS_HDR_SIZE, dns_naptr_parser(), dns_skipname(), dns_srv_parser(), dns_txt_parser(), free_rdata_list(), dns_query::hdr, LM_DBG, LM_ERR, local_free, local_malloc, rdata::next, srv_rdata::priority, rdata::rdata, T_EBL, rdata::ttl, rdata::type, and srv_rdata::weight.
Referenced by do_query(), do_srv_lookup(), dp_can_connect_str(), enum_pv_query_3(), i_enum_query_2(), is_from_user_enum_2(), and sip_resolvehost().
| int resolv_blacklist_init | ( | void | ) |
Initialize blacklist.
Definition at line 115 of file resolve.c.
References BL_BY_DEFAULT, BL_DO_EXPIRE, create_bl_head(), disable_dns_blacklist, DNS_REVOLVER_BL_ID, DNS_REVOLVER_BL_NAME, LM_ERR, bl_head::name, NULL, and str_init.
Referenced by main().
| int resolv_init | ( | void | ) |
Initialize the DNS resolver 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 searchlist 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).
Definition at line 93 of file resolve.c.
References dns_retr_no, dns_retr_time, dns_search_list, dns_servers_no, and LM_WARN.
Referenced by main().
| struct hostent* sip_resolvehost | ( | str * | name, | |
| unsigned short * | port, | |||
| unsigned short * | proto, | |||
| int | is_sips, | |||
| struct dns_node ** | dn | |||
| ) | [read] |
Resolve SIP hostname to ip address, protocol and port.
Resolve SIP hostname to ip address, protocol and port if necessary. If no port is given, it will be resolved with SRV, if additional no protocol is given, then it will be resolved with NAPTR. This function fallback from NAPTR to SRV and then to A record lookup, if no valid records could be found. If the NAPTR lookup fails, it will use the TLS (for SIPS) or UDP protocol, if the SRV lookup fails, it will use the default port for SIP or SIPS. If the hostname is already an IP address, it will also use the default port and protocol.
| name | hostname | |
| port | port, set to 0 if you want to resolve it | |
| proto | SIP protocol, set to PROTO_NONE if you want to resolve it | |
| is_sips | set to true for SIPS | |
| dn | DNS node |
Definition at line 978 of file resolve.c.
References do_srv_lookup(), filter_and_sort_naptr(), free_rdata_list(), get_naptr, get_naptr_proto(), get_record(), ip_addr2he(), _str::len, LM_DBG, LM_ERR, LM_WARN, MAX_DNS_NAME, rdata::next, NULL, PROTO_NONE, PROTO_SCTP, PROTO_TCP, PROTO_TLS, PROTO_UDP, resolvehost(), _str::s, shm_free, SIP_PORT, SIPS_PORT, srv2dns_node(), SRV_MAX_PREFIX_LEN, SRV_SCTP_PREFIX, SRV_SCTP_PREFIX_LEN, SRV_TCP_PREFIX, SRV_TCP_PREFIX_LEN, SRV_TLS_PREFIX, SRV_TLS_PREFIX_LEN, SRV_UDP_PREFIX, SRV_UDP_PREFIX_LEN, str2ip(), and str2ip6().
Referenced by check_via_address(), get_natping_socket(), is_from_user_enum_2(), mk_proxy(), mk_shm_proxy(), nh_timer(), and update_sock_struct_from_via().
| static void sort_srvs | ( | struct rdata ** | head | ) | [inline, static] |
Definition at line 756 of file resolve.c.
References free_rdata_list(), LM_CRIT, rdata::next, NULL, priority, rd2srv, and rdata::type.
Referenced by do_srv_lookup().
Definition at line 676 of file resolve.c.
References DNS_NODE_SRV, get_naptr, get_naptr_proto(), dns_node::idx, dns_val::ival, dns_node::kids, LM_ERR, rdata::next, dns_node::no, NULL, shm_malloc(), dns_node::size, dns_val::sval, dns_node::type, and dns_node::vals.
Referenced by sip_resolvehost().
| int disable_dns_blacklist = 1 |
| int dns_retr_no = -1 |
| int dns_retr_time = -1 |
| int dns_search_list = -1 |
| int dns_servers_no = -1 |
| int dns_try_ipv6 = 0 |
struct bl_head* failover_bl = 0 [static] |
1.5.6