resolve.c File Reference

DNS resolver for Kamailio. More...

#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"

Include dependency graph for resolve.c:

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_rdatadns_a_parser (unsigned char *rdata, unsigned char *end)
 Parses an A record rdata into an a_rdata structure.
struct aaaa_rdatadns_aaaa_parser (unsigned char *rdata, unsigned char *end)
 Parses an AAAA (ipv6) record rdata into an aaaa_rdata structure.
struct cname_rdatadns_cname_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata)
 Parses a CNAME record into a cname_rdata structure.
struct ebl_rdatadns_ebl_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata)
 parses a EBL record into a ebl_rdata structure
struct naptr_rdatadns_naptr_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata)
 parses the naptr record into a naptr_rdata structure
static struct dns_nodedns_node_copy (struct dns_node *s)
struct dns_nodedns_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_rdatadns_srv_parser (unsigned char *msg, unsigned char *end, unsigned char *rdata)
 parses the srv record into a srv_rdata structure
struct txt_rdatadns_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 rdataget_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_headfailover_bl = 0


Detailed Description

DNS resolver for Kamailio.

Definition in file resolve.c.


Define Documentation

#define DNS_BL_EXPIRE   4*60

Definition at line 77 of file resolve.c.

Referenced by get_next_su().

#define DNS_NODE_A   2

Definition at line 55 of file resolve.c.

Referenced by a2dns_node(), and get_next_he().

#define DNS_NODE_SRV   1

Definition at line 54 of file resolve.c.

Referenced by get_next_he(), and srv2dns_node().

#define DNS_REVOLVER_BL_ID   17

Definition at line 75 of file resolve.c.

Referenced by resolv_blacklist_init().

#define DNS_REVOLVER_BL_NAME   "dns"

Definition at line 76 of file resolve.c.

Referenced by resolv_blacklist_init().

#define local_free   pkg_free

#define local_malloc   pkg_malloc

#define naptr_prio ( _naptr   )     ((unsigned int)((((_naptr)->order) << 16) + ((_naptr)->pref)))

Definition at line 867 of file resolve.c.

Referenced by filter_and_sort_naptr().

#define rd2srv ( _rd   )     ((struct srv_rdata*)_rd->rdata)

Referenced by sort_srvs().


Function Documentation

static int a2dns_node ( struct rdata head,
struct dns_node **  dn 
) [inline, static]

struct a_rdata* dns_a_parser ( unsigned char *  rdata,
unsigned char *  end 
) [read]

Parses an A record rdata into an a_rdata structure.

Returns:
0 on error or a dyn. alloc'ed a_rdata struct

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.

Returns:
0 on error or a dyn. alloc'ed aaaa_rdata struct

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

Parameters:
msg - pointer to the dns message
end - pointer to the end of the message
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) | +----------------+

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().

static struct dns_node* dns_node_copy ( struct dns_node s  )  [static, read]

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().

struct dns_node* dns_res_copy ( struct dns_node s  )  [read]

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

Parameters:
msg - pointer to the dns message
end - pointer to the end of the message
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 ~ | | +----------------+

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.

Note:
RFC1035:
  • <character-string> is a single length octet followed by that number of characters.
  • TXT-DATA One or more <character-string>s.
We only take the first string here.

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.

Parameters:
name hostname
port port, will be set from this function
dn DNS node
Returns:
host IP address, or zero on error

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]

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  ) 

static int get_naptr_proto ( struct naptr_rdata n  )  [inline, static]

Detect the protocol from an NAPTR structure.

Parameters:
n NAPTR data
Returns:
the protocol, or PROTO_NONE on error

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]

int get_next_su ( struct proxy_l p,
union sockaddr_union su,
int  add_to_bl 
)

struct rdata* get_record ( char *  name,
int  type 
) [read]

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).

Returns:
0 on success, -1 on error

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.

Parameters:
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
Returns:
host IP address, or zero on errors

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().

static int srv2dns_node ( struct rdata head,
struct dns_node **  dn 
) [inline, static]


Variable Documentation

Definition at line 72 of file resolve.c.

Referenced by resolv_blacklist_init().

int dns_retr_no = -1

DNS resolver : Retry #

Definition at line 69 of file resolve.c.

Referenced by resolv_init().

int dns_retr_time = -1

DNS resolver: Retry time

Definition at line 68 of file resolve.c.

Referenced by resolv_init().

int dns_search_list = -1

DNS resolver: Search list

Definition at line 71 of file resolve.c.

Referenced by resolv_init().

int dns_servers_no = -1

DNS resolver: Server no

Definition at line 70 of file resolve.c.

Referenced by resolv_init().

int dns_try_ipv6 = 0

default off

Definition at line 66 of file resolve.c.

Referenced by resolvehost().

struct bl_head* failover_bl = 0 [static]

Definition at line 74 of file resolve.c.


Generated on Thu May 24 10:00:42 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6