Ignore:
Timestamp:
Oct 8, 2006 12:39:34 PM (18 years ago)
Author:
bennylp
Message:

Major addition to support DNS SRV resolution in PJSIP:

  • added DNS asynchronous/caching resolver engine in PJLIB-UTIL (resolver.[hc])
  • modified SIP resolver (sip_resolve.c) to properly perform DNS SRV/A resolution when DNS resolution is enabled.
  • added dns_test.c in PJSIP-TEST for testing the SIP resolver.
  • added nameserver configuration in PJSUA-LIB
  • added "--nameserver" option in PJSUA.
  • updated project/Makefiles and doxygen documentation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib-util/include/pjlib-util/dns.h

    r745 r753  
    2323/** 
    2424 * @file dns.h 
    25  * @brief Asynchronous DNS Name Resolution/resolver 
     25 * @brief Low level DNS message parsing and packetization. 
    2626 */ 
    2727#include <pj/types.h> 
     
    3232 
    3333/** 
    34  * @defgroup PJ_DNS_RESOLVER Asynchronous DNS Name Resolution/resolver 
     34 * @defgroup PJ_DNS Low-level DNS message parsing and packetization 
    3535 * @ingroup PJ 
    3636 * @{ 
    3737 * 
    38  * This module provides services to performing asynchronous DNS resolution. 
    39  */ 
     38 * This module provides low-level services to parse and packetize DNS queries 
     39 * and responses. The functions support building a DNS query packet and parse 
     40 * the data in the DNS response. 
     41 * 
     42 * To create a DNS query packet, application should call #pj_dns_make_query() 
     43 * function, specifying the desired DNS query type, the name to be resolved, 
     44 * and the buffer where the DNS packet will be built into.  
     45 * 
     46 * When incoming DNS query or response packet arrives, application can use 
     47 * #pj_dns_parse_packet() to parse the TCP/UDP payload into parsed DNS packet 
     48 * structure. 
     49 * 
     50 * This module does not provide any networking functionalities to send or 
     51 * receive DNS packets. This functionality should be provided by higher layer 
     52 * modules such as @ref PJ_DNS_RESOLVER. 
     53 */ 
     54 
     55enum 
     56{ 
     57    PJ_DNS_CLASS_IN     = 1     /**< DNS class IN.                          */ 
     58}; 
    4059 
    4160/** 
    4261 * This enumeration describes standard DNS record types as described by 
    43  * RFC 1035. 
     62 * RFC 1035, RFC 2782, and others. 
    4463 */ 
    4564typedef enum pj_dns_type 
     
    159178 
    160179 
    161  
    162 /** 
    163  * This constants describes record types in the DNS packet. 
    164  */ 
    165 typedef enum pj_dns_rec_type 
    166 { 
    167     DNS_QUERY_REC,  /**< The record is a query record       */ 
    168     DNS_RR_REC,     /**< The record is resource record      */ 
    169     DNS_NS_REC,     /**< The record is name server record   */ 
    170     DNS_REC_AR,     /**< The record is additional RR.       */ 
    171 } pj_dns_rec_type; 
    172  
    173  
    174 /** 
    175  * This structure describes a Resource Record parsed from the DNS response. 
     180/**  
     181 * These constants describe DNS RCODEs. Application can fold these constants 
     182 * into PJLIB pj_status_t namespace by calling #PJ_STATUS_FROM_DNS_RCODE() 
     183 * macro. 
     184 */ 
     185typedef enum pj_dns_rcode 
     186{ 
     187    PJ_DNS_RCODE_FORMERR    = 1,    /**< Format error.                      */ 
     188    PJ_DNS_RCODE_SERVFAIL   = 2,    /**< Server failure.                    */ 
     189    PJ_DNS_RCODE_NXDOMAIN   = 3,    /**< Name Error.                        */ 
     190    PJ_DNS_RCODE_NOTIMPL    = 4,    /**< Not Implemented.                   */ 
     191    PJ_DNS_RCODE_REFUSED    = 5,    /**< Refused.                           */ 
     192    PJ_DNS_RCODE_YXDOMAIN   = 6,    /**< The name exists.                   */ 
     193    PJ_DNS_RCODE_YXRRSET    = 7,    /**< The RRset (name, type) exists.     */ 
     194    PJ_DNS_RCODE_NXRRSET    = 8,    /**< The RRset (name, type) doesn't exist*/ 
     195    PJ_DNS_RCODE_NOTAUTH    = 9,    /**< Not authorized.                    */ 
     196    PJ_DNS_RCODE_NOTZONE    = 10    /**< The zone specified is not a zone.  */ 
     197 
     198} pj_dns_rcode; 
     199 
     200 
     201/** 
     202 * This constant specifies the maximum names to keep in the temporary name 
     203 * table when performing name compression scheme when duplicating DNS packet 
     204 * (the #pj_dns_packet_dup() function). 
     205 * 
     206 * Generally name compression is desired, since it saves some memory (see 
     207 * PJ_DNS_RESOLVER_RES_BUF_SIZE setting). However it comes at the expense of  
     208 * a little processing overhead to perform name scanning and also a little 
     209 * bit more stack usage (8 bytes per entry on 32bit platform). 
     210 * 
     211 * Default: 16 
     212 */ 
     213#ifndef PJ_DNS_MAX_NAMES_IN_NAMETABLE 
     214#   define PJ_DNS_MAX_NAMES_IN_NAMETABLE        16 
     215#endif 
     216 
     217 
     218/** 
     219 * This structure describes a DNS query record. 
     220 */ 
     221typedef struct pj_dns_parsed_query 
     222{ 
     223    pj_str_t    name;       /**< The domain in the query.                   */ 
     224    pj_uint16_t type;       /**< Type of the query (pj_dns_type)            */ 
     225    pj_uint16_t dnsclass;   /**< Network class (PJ_DNS_CLASS_IN=1)          */ 
     226} pj_dns_parsed_query; 
     227 
     228 
     229/** 
     230 * This structure describes a Resource Record parsed from the DNS packet. 
    176231 * All integral values are in host byte order. 
    177232 */ 
     
    180235    pj_str_t     name;      /**< The domain name which this rec pertains.   */ 
    181236    pj_uint16_t  type;      /**< RR type code.                              */ 
    182     pj_uint16_t  class_;    /**< Class of data (normally 1, for IN).        */ 
     237    pj_uint16_t  dnsclass;  /**< Class of data (PJ_DNS_CLASS_IN=1).         */ 
    183238    pj_uint32_t  ttl;       /**< Time to live.                              */ 
    184239    pj_uint16_t  rdlength;  /**< Resource data length.                      */ 
    185     void        *data;      /**< Pointer to the raw resource data.          */ 
     240    void        *data;      /**< Pointer to the raw resource data, only 
     241                                 when the type is not known. If it is known, 
     242                                 the data will be put in rdata below.       */ 
    186243     
    187244    /** For resource types that are recognized/supported by this library, 
     
    224281 
    225282/** 
    226  * This structure describes the response parsed from the raw DNS response. 
    227  * Note that all integral values in the parsed response are represented in 
     283 * This structure describes the parsed repersentation of the raw DNS packet. 
     284 * Note that all integral values in the parsed packet are represented in 
    228285 * host byte order. 
    229286 */ 
    230 typedef struct pj_dns_parsed_response 
    231 { 
    232     pj_dns_hdr        hdr;  /**< Pointer to DNS hdr, in host byte order */ 
    233     pj_dns_parsed_rr *ans;  /**< Array of DNS RR answer.                */ 
    234     pj_dns_parsed_rr *ns;   /**< Array of NS record in the answer.      */ 
    235     pj_dns_parsed_rr *arr;  /**< Array of additional RR answer.         */ 
    236 } pj_dns_parsed_response; 
    237  
    238  
    239 /** 
    240  * Create DNS query packet to resolve the specified names. 
     287typedef struct pj_dns_parsed_packet 
     288{ 
     289    pj_dns_hdr           hdr;   /**< Pointer to DNS hdr, in host byte order */ 
     290    pj_dns_parsed_query *q;     /**< Array of DNS queries.                  */ 
     291    pj_dns_parsed_rr    *ans;   /**< Array of DNS RR answer.                */ 
     292    pj_dns_parsed_rr    *ns;    /**< Array of NS record in the answer.      */ 
     293    pj_dns_parsed_rr    *arr;   /**< Array of additional RR answer.         */ 
     294} pj_dns_parsed_packet; 
     295 
     296 
     297/** 
     298 * Create DNS query packet to resolve the specified names. This function 
     299 * can be used to build any types of DNS query, such as A record or DNS SRV 
     300 * record. 
     301 * 
     302 * Application specifies the type of record and the name to be queried, 
     303 * and the function will build the DNS query packet into the buffer 
     304 * specified. Once the packet is successfully built, application can send 
     305 * the packet via TCP or UDP connection. 
    241306 * 
    242307 * @param packet        The buffer to put the DNS query packet. 
     
    246311 * @param id            DNS query ID to associate DNS response with the 
    247312 *                      query. 
    248  * @param qtype         DNS type of record to be queried. 
     313 * @param qtype         DNS type of record to be queried (see #pj_dns_type). 
    249314 * @param name          Name to be queried from the DNS server. 
    250315 * 
     
    254319                                       unsigned *size, 
    255320                                       pj_uint16_t id, 
    256                                        pj_dns_type qtype, 
     321                                       int qtype, 
    257322                                       const pj_str_t *name); 
    258323 
    259324/** 
    260  * Parse raw DNS response packet into DNS response structure. 
    261  * 
    262  * @param pool          Pool to allocate memory for the parsed response. 
    263  * @param packet 
    264  * @param size 
    265  * @param p_res 
     325 * Parse raw DNS packet into parsed DNS packet structure. This function is 
     326 * able to parse few DNS resource records such as A record, PTR record, 
     327 * CNAME record, NS record, and SRV record. 
     328 * 
     329 * @param pool          Pool to allocate memory for the parsed packet. 
     330 * @param packet        Pointer to the DNS packet (the TCP/UDP payload of  
     331 *                      the raw packet). 
     332 * @param size          The size of the DNS packet. 
     333 * @param p_res         Pointer to store the resulting parsed packet. 
    266334 * 
    267335 * @return              PJ_SUCCESS on success, or the appropriate error code. 
    268336 */ 
    269 PJ_DECL(pj_status_t) pj_dns_parse_response(pj_pool_t *pool, 
    270                                            const void *packet, 
    271                                            unsigned size, 
    272                                            pj_dns_parsed_response **p_res); 
    273  
    274 /** 
    275  * Dump DNS response to standard log. 
    276  * 
    277  * @param res           The DNS response. 
    278  */ 
    279 PJ_DECL(void) pj_dns_dump_response(const pj_dns_parsed_response *res); 
     337PJ_DECL(pj_status_t) pj_dns_parse_packet(pj_pool_t *pool, 
     338                                         const void *packet, 
     339                                         unsigned size, 
     340                                         pj_dns_parsed_packet **p_res); 
     341 
     342/** 
     343 * Duplicate DNS packet. 
     344 * 
     345 * @param pool          The pool to allocate memory for the duplicated packet. 
     346 * @param p             The DNS packet to be cloned. 
     347 * @p_dst               Pointer to store the cloned DNS packet. 
     348 */ 
     349PJ_DECL(void) pj_dns_packet_dup(pj_pool_t *pool, 
     350                                const pj_dns_parsed_packet*p, 
     351                                pj_dns_parsed_packet **p_dst); 
     352 
     353 
     354/** 
     355 * Utility function to get the type name string of the specified DNS type. 
     356 * 
     357 * @param type          DNS type (see #pj_dns_type). 
     358 * 
     359 * @return              String name of the type (e.g. "A", "SRV", etc.). 
     360 */ 
     361PJ_DECL(const char *) pj_dns_get_type_name(int type); 
     362 
     363 
     364/** 
     365 * Dump DNS packet to standard log. 
     366 * 
     367 * @param res           The DNS packet. 
     368 */ 
     369PJ_DECL(void) pj_dns_dump_packet(const pj_dns_parsed_packet *res); 
    280370 
    281371 
Note: See TracChangeset for help on using the changeset viewer.