⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dns.h.svn-base

📁 域名解析器的实现
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
#ifndef __DNS_H__
#define __DNS_H__

#include <inc/ctypes.h>      

#include <l3/ap/dnsr/h/dnsr_if.h>

/*
 * Well-known port number for DNS service over TCP and UDP.
 */

#define DNS_NAMESERVER_PORT     53
#define MAX_STATIC_DNS_NAME     32

/* define declaration */
/*
 * RFC-1123 specifies minimum values for these timeouts:
 *
 * DNS_TIMEOUT_FLOOR must be at least 5 seconds.
 *
 * DNS_TIMEOUT_CEILING must be at least twice the Internet MSL, plus
 * an unspecified fudge factor to allow for processing on the remote
 * server.  For lack of a better plan, we use the floor value as the
 * fudge factor, on the theory that the floor represents the amount
 * of time the server would have if transmission were instantaneous.
 *
 * The Internet MSL is defined in RFC-1122, among other places.
 */
#define DNS_MSL                 (120000L)						  /* msec */
#define DNS_TIMEOUT_FLOOR       (2000L)							  /* msec */
#define DNS_TIMEOUT_CEILING     (2 * DNS_MSL + DNS_TIMEOUT_FLOOR) /* msec */

/*
 * Macros to get and set header fields.  These assume that the first
 * argument is a big-endian UI32_T.
 *
 * Implementation note: the expression (x & (1 + ~x)) returns the
 * least significant bit that is turned on in the unsigned value x.
 */

#define DNS_LDB(_word_,_mask_) \
  (((_word_) & (_mask_)) / ((_mask_) & (1 + ~(_mask_))))

#define DNS_FLD(_newval_,_mask_) \
  (((_newval_) * (((_mask_) & (1 + ~(_mask_))))) & (_mask_))

#define dns_decode_bits32(x) \
  ((((UI32_T)((x)[0])) << 24) | \
   (((UI32_T)((x)[1])) << 16) | \
   (((UI32_T)((x)[2])) <<  8) | \
   (((UI32_T)((x)[3])) <<  0))

#define dns_decode_bits16(x) \
  ((((UI16_T)((x)[0])) << 8) | \
   (((UI16_T)((x)[1])) << 0))

/*register UI32_T _y32_;*/
/*register UI16_T  _y16_;*/
#define dns_encode_bits32(x,y){ \
    (x)[0] = (unsigned char) (((y) >> 24) & 0xFF); \
    (x)[1] = (unsigned char) (((y) >> 16) & 0xFF); \
    (x)[2] = (unsigned char) (((y) >>  8) & 0xFF); \
    (x)[3] = (unsigned char) (((y) >>  0) & 0xFF); \
    }
  
#define dns_encode_bits16(x,y){ \
    (x)[0] = (unsigned char) (((y) >>  8) & 0xFF); \
    (x)[1] = (unsigned char) (((y) >>  0) & 0xFF); \
    }
    
    
/* End of Header Macros */       

/*-------------------------------------*/
/*
 * Known DNS classes.
 */
enum dns_c {
  DNS_C_IN = 1,                 /* Internet */
  /*DNS_C_CS = 2,*/                 /* CSNET (obsolete) */ 
  DNS_C_CH = 3,                 /* Chaosnet */
  DNS_C_HS = 4,                 /* Hesiod */
  DNS_C_ANY = 255               /* "*" wildcard */
};

/*
 * Known DNS types.
 */

enum dns_t {
  DNS_T_A = 1,                  /* Address */
  DNS_T_NS = 2,                 /* Name Server */
  /*DNS_T_MD = 3,*/               /* Mail Destination (obsolete, forbidden) */
  /*DNS_T_MF = 4,*/               /* Mail Forwarder (obsolete, forbidden) */
  DNS_T_CNAME = 5,              /* Canonnical Name */
  DNS_T_SOA = 6,                /* Start Of Authority */
  DNS_T_MB = 7,                 /* MailBox */
  DNS_T_MG = 8,                 /* MailGroup */
  DNS_T_MR = 9,                 /* MailRename */
  DNS_T_NULL = 10,              /* Null */
  DNS_T_WKS = 11,               /* Well-Known-Service */
  DNS_T_PTR = 12,               /* Pointer */
  DNS_T_HINFO = 13,             /* Host Information */
  DNS_T_MINFO = 14,             /* Mail Information */
  DNS_T_MX = 15,                /* Mail Exchange */
  DNS_T_TXT = 16,               /* Text */
  DNS_T_AXFR = 252,             /* Zone transfer */
  DNS_T_MAILB = 253,            /* Mailbox semi-wildcard */
  DNS_T_MAILA = 254,            /* Mailagent semi-wildcard */
  DNS_T_ANY = 255               /* "*" wildcard */
};

/*
 * Known DNS operation codes.
 */

enum dns_opcode {
  DNS_OPCODE_QUERY = 0,         /* Normal query */
  DNS_OPCODE_IQUERY = 1,        /* Inverse query */
  DNS_OPCODE_STATUS = 2         /* Server status request */
};

/*
 * Known DNS response codes.
 */

enum dns_rcode {
  DNS_RCODE_OK = 0,             /* No error */
  DNS_RCODE_FORMAT = 1,         /* Server didn't like our message format */
  DNS_RCODE_SERVER = 2,         /* Server claims to be buggy */
  DNS_RCODE_NAME = 3,           /* Name does not exist */
  DNS_RCODE_NIY = 4,            /* Server doesn't implement operation */
  DNS_RCODE_REFUSED = 5         /* Server refuses to perform operation */
};

/*
 * Masks for DNS header fields, big-endian word layout.
 */

#define DNS_HDR_ID      0xFFFF0000L /* Query ID */
#define DNS_HDR_RESP    0x00008000L /* Is reponse? */
#define DNS_HDR_OP      0x00007800L /* Query opcode */
#define DNS_HDR_AA      0x00000400L /* Responding server is authority? */
#define DNS_HDR_TC      0x00000200L /* Message truncated? */
#define DNS_HDR_RD      0x00000100L /* Recursion desired? */
#define DNS_HDR_RA      0x00000080L /* Recursion available? */
/*                      0x00000070L    unused bits, should be zero */
#define DNS_HDR_RCODE   0x0000000FL /* Response code */

/*
 * Positions of flag word and count halfwords, expressed as offsets
 * from start of header.  Flag word is 32 bits, counts are 16 bits each.
 */

#define DNS_HDR_FLAGS   0
#define DNS_HDR_QDCOUNT 4
#define DNS_HDR_ANCOUNT 6
#define DNS_HDR_NSCOUNT 8
#define DNS_HDR_ARCOUNT 10

/*
 * Length of packet header.  A valid packet must be at least this long.
 */

#define DNS_HDR_LENGTH  12
#define DNS_QS_QUERY_TYPE_LENGTH  2
#define DNS_QS_QUERY_CLASS_LENGTH 2

/*
 * Compressed DNS name magic numbers.
 */

#define DNS_COMPRESSION_MASK    0300
#define DNS_COMPRESSION_POINTER 0300

/*
 * Size limits on DNS names, from RFC-1035.
 */

#define DNS_MAX_DOMAIN_NAME     (255)
#define DNS_MAX_DOMAIN_LABEL    (63)
#define DNS_MAX_DOMAIN_LABELS   (DNS_MAX_DNAME/2)

/*
 * Size of auto arrays of RRs used in cache lookups and message decoding.
 * Eventually the code should be able to allocate bigger arrays from free
 * space when needed, but this will do for now.
 */

#define DNS_MAX_RRS_PER_MSG     30

/*
 * Maximum RR TTL we can handle.  TTLs are in seconds, Attache's clock
 * is in milliseconds.  With the current implementation, this implies
 * that we can't keep cached RRs for more than about 24 days.  If
 * someday we really need to keep RRs longer than that, we can do so
 * by dividing the range of possible clock values in some more
 * complicated way (e.g. 3/4 "future" and 1/4 "past").  This would
 * mean that we'd have to GC the cache once a week instead of once
 * every two weeks.  Whoopie.
 */

#define DNS_MAX_TTL     (0x7FFFFFFFL/1000)



/*
 * Error codes.  These must be negative numbers, since some of them are
 * returned by routines which normally return a non-negative count.
 * Routines which only return these codes should have a return type
 * of "enum dns_error", but routines which might also need to return
 * a positive number should have a return type of "int".
 */

enum dns_error {
  DNS_ERROR_OK = 0,
  DNS_ERROR_NAME_TOO_LONG = -1,
  DNS_ERROR_BAD_NAME = -2,
  DNS_ERROR_BAD_ARGS = -3,
  DNS_ERROR_LABEL_TOO_LONG = -4,
  DNS_ERROR_ALLOCATION_FAILURE = -5,
  DNS_ERROR_TIMEOUT = -6,
  DNS_ERROR_UNREACHABLE = -7,
  DNS_ERROR_FORMAT = -8,
  DNS_ERROR_SERVER_FAILURE = -9,
  DNS_ERROR_NONEXISTANT_NAME = -10,
  DNS_ERROR_NIY = -11,
  DNS_ERROR_REFUSED = -12,
  DNS_ERROR_IMPOSSIBLE = -13,
  DNS_ERROR_NO_RRS = -14,
  DNS_ERROR_ABORTED = -15,
  DNS_ERROR_BAD_PROTOCOL = -16,
  DNS_ERROR_TRUNCATED = -17,
  DNS_ERROR_NO_RECURSION = -18,
  DNS_ERROR_IRRELEVANT = -19,
  DNS_ERROR_NOT_IN_LOCAL_CACHE = -20,
  DNS_ERROR_NO_PORT = -21,  
  DNS_ERROR_QUERY_SELF = -22,
  DNS_ERROR_BINDTIMER_FAILURE = -23,
  DNS_ERROR_UNBINDTIMER_FAILURE = -24
};

/*
 * This should be the total number of error codes defined, ie,
 * one greater than the absolute value of the last error code.
 */
#define DNS_N_ERRORS    (22)

/*
 * Structures of query list
 */
struct query_table {
   UI16_T     query_id;   /* recursive server query id */
   UI32_T    claddr;     /* client ip address */   
   UI16_T     clid;       /* client query id */
   BOOLEAN_T valid;      /* indicate the entry whether is used or not */
   UI32_T    now;        /* record the current time */
   void*  ptr;           /* query pointer */      
};


/*
 * Structures to hold decoded RRs and DNS header.
 */

struct dns_rr {
  unsigned char *name;          /* RR name */
  UI16_T  class;                /* DNS class of RR */
  UI16_T  type;                 /* DNS type of RR */
  UI32_T ttl;                   /* RR's Time To Live */
  UI16_T  rdcount;              /* Length of RDATA portion of RR */
  unsigned char *rdata;         /* RDATA portion of RR */
  struct dns_rr *next;          /* Next RR in this bucket */
  UI32_T birthdate;             /* When this RR was received */
  UI32_T source;                /* Who sent us this RR */
  unsigned expired : 1;         /* RR has expired? */
  unsigned negative : 1;        /* Negative cache entry? */
  unsigned answer : 1;          /* From Answer section of RR? */
  unsigned authoritative : 1;   /* AA && answer && RNAME == QNAME? */
  enum dns_error errcode;       /* Negative cache error code */
};

struct dns_header {
  UI16_T  id;                  /* Query id */
  unsigned resp : 1;            /* Is response? */
  unsigned op : 4;              /* Kind of query */
  unsigned aa : 1;              /* Responding nameserver claims authority? */
  unsigned tc : 1;              /* Message was truncated? */
  unsigned rd : 1;              /* Recursion desired? */
  unsigned ra : 1;              /* Recursion available? */
  unsigned rcode : 4;           /* Response code */
  UI16_T  qdcount;             /* Question section count */
  UI16_T  ancount;             /* Answer section count */
  UI16_T  nscount;             /* Authority section count */
  UI16_T  arcount;             /* Additional section count */
};


/*
 * Defined in dns_recu.c
/*

/*------------------------------------------------------------------------
 * BOOLEAN_T  dns_static_host_init()
 * Purpose: intialize Static DNS Table
 *
 *          
 * Parameters:
 *    Input:
 *    Output:
 * returns :
 *------------------------------------------------------------------------
 */                    
void dns_static_host_init(void);

/*------------------------------------------------------------------------
 * BOOLEAN_T  dns_set_static_host()
 * Purpose: Set Static DNS Table
 *
 *          
 * Parameters:
 *    Input:
 *    Output:
 * returns :
 *------------------------------------------------------------------------
 */                    
BOOLEAN_T dns_set_static_host(UI32_T addr,UI8_T *name,UI32_T name_size,BOOLEAN_T status ,UI32_T index);

/*------------------------------------------------------------------------
 * BOOLEAN_T  dns_response_tx()
 * Purpose: direct to response DNS request packet without sending packet 
 *          to server
 *          
 * Parameters:
 *    Input:
 *    Output:
 * returns :
 *------------------------------------------------------------------------
 */                    
BOOLEAN_T dns_response_tx(PENDING_DNSR_MSG_PTR pMsg,   
                     int len, unsigned long addrs[],                     
                     unsigned char *name,
                     unsigned long ttls[]                     
                     );
                     
                     
                     
/*------------------------------------------------------------------------
 * void  dns_request_receive()
 * Purpose: parse DNS request packet
 *          
 * Parameters:
 *    Input:
 *    Output:
 * returns :
 *------------------------------------------------------------------------
 */
void dns_request_receive(PENDING_DNSR_MSG_PTR pMsg,   
                        struct dns_header	 *hdr);
                        
/*------------------------------------------------------------------------
 * int  dns_response_rx()
 * Purpose: Handle the reply packets from network
 *          
 * Parameters:
 *    Input:
 *    Output:
 * returns :
 *------------------------------------------------------------------------
 */
void dns_response_rx(PENDING_DNSR_MSG_PTR pMsg, struct dns_header *hdr);                    
/*                        
 * Defined in dns_ask.c.
 */
/*------------------------------------------------------------------------
 * I16_T  dnsr_init_query_db()
 * Purpose:   initialize query List
 *          
 * Parameters:
 *    Input:
 *    Output:
 * returns :
 *------------------------------------------------------------------------
 */                     
void dnsr_init_query_db(void);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -