iPXE
dns.h
Go to the documentation of this file.
00001 #ifndef _IPXE_DNS_H
00002 #define _IPXE_DNS_H
00003 
00004 /** @file
00005  *
00006  * DNS protocol
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <ipxe/in.h>
00014 
00015 /** DNS server port */
00016 #define DNS_PORT 53
00017 
00018 /** An RFC1035-encoded DNS name */
00019 struct dns_name {
00020         /** Start of data */
00021         void *data;
00022         /** Offset of name within data */
00023         size_t offset;
00024         /** Total length of data */
00025         size_t len;
00026 };
00027 
00028 /**
00029  * Test for a DNS compression pointer
00030  *
00031  * @v byte              Initial byte
00032  * @ret is_compressed   Is a compression pointer
00033  */
00034 #define DNS_IS_COMPRESSED( byte ) ( (byte) & 0xc0 )
00035 
00036 /**
00037  * Extract DNS compression pointer
00038  *
00039  * @v word              Initial word
00040  * @ret offset          Offset
00041  */
00042 #define DNS_COMPRESSED_OFFSET( word ) ( (word) & ~0xc000 )
00043 
00044 /**
00045  * Extract DNS label length
00046  *
00047  * @v byte              Initial byte
00048  * @ret len             Label length
00049  */
00050 #define DNS_LABEL_LEN( byte ) ( (byte) & ~0xc0 )
00051 
00052 /** Maximum length of a single DNS label */
00053 #define DNS_MAX_LABEL_LEN 0x3f
00054 
00055 /** Maximum length of a DNS name (mandated by RFC1035 section 2.3.4) */
00056 #define DNS_MAX_NAME_LEN 255
00057 
00058 /** Maximum depth of CNAME recursion
00059  *
00060  * This is a policy decision.
00061  */
00062 #define DNS_MAX_CNAME_RECURSION 32
00063 
00064 /** A DNS packet header */
00065 struct dns_header {
00066         /** Query identifier */
00067         uint16_t id;
00068         /** Flags */
00069         uint16_t flags;
00070         /** Number of question records */
00071         uint16_t qdcount;
00072         /** Number of answer records */
00073         uint16_t ancount;
00074         /** Number of name server records */
00075         uint16_t nscount;
00076         /** Number of additional records */
00077         uint16_t arcount;
00078 } __attribute__ (( packed ));
00079 
00080 /** Recursion desired flag */
00081 #define DNS_FLAG_RD 0x0100
00082 
00083 /** A DNS question */
00084 struct dns_question {
00085         /** Query type */
00086         uint16_t qtype;
00087         /** Query class */
00088         uint16_t qclass;
00089 } __attribute__ (( packed ));
00090 
00091 /** DNS class "IN" */
00092 #define DNS_CLASS_IN 1
00093 
00094 /** A DNS resource record */
00095 struct dns_rr_common {
00096         /** Type */
00097         uint16_t type;
00098         /** Class */
00099         uint16_t class;
00100         /** Time to live */
00101         uint32_t ttl;
00102         /** Resource data length */
00103         uint16_t rdlength;
00104 } __attribute__ (( packed ));
00105 
00106 /** Type of a DNS "A" record */
00107 #define DNS_TYPE_A 1
00108 
00109 /** A DNS "A" record */
00110 struct dns_rr_a {
00111         /** Common fields */
00112         struct dns_rr_common common;
00113         /** IPv4 address */
00114         struct in_addr in_addr;
00115 } __attribute__ (( packed ));
00116 
00117 /** Type of a DNS "AAAA" record */
00118 #define DNS_TYPE_AAAA 28
00119 
00120 /** A DNS "AAAA" record */
00121 struct dns_rr_aaaa {
00122         /** Common fields */
00123         struct dns_rr_common common;
00124         /** IPv6 address */
00125         struct in6_addr in6_addr;
00126 } __attribute__ (( packed ));
00127 
00128 /** Type of a DNS "NAME" record */
00129 #define DNS_TYPE_CNAME 5
00130 
00131 /** A DNS "CNAME" record */
00132 struct dns_rr_cname {
00133         /** Common fields */
00134         struct dns_rr_common common;
00135 } __attribute__ (( packed ));
00136 
00137 /** A DNS resource record */
00138 union dns_rr {
00139         /** Common fields */
00140         struct dns_rr_common common;
00141         /** "A" record */
00142         struct dns_rr_a a;
00143         /** "AAAA" record */
00144         struct dns_rr_aaaa aaaa;
00145         /** "CNAME" record */
00146         struct dns_rr_cname cname;
00147 };
00148 
00149 extern int dns_encode ( const char *string, struct dns_name *name );
00150 extern int dns_decode ( struct dns_name *name, char *data, size_t len );
00151 extern int dns_compare ( struct dns_name *first, struct dns_name *second );
00152 extern int dns_copy ( struct dns_name *src, struct dns_name *dst );
00153 extern int dns_skip ( struct dns_name *name );
00154 
00155 #endif /* _IPXE_DNS_H */