1 /* 2 * pkthdr.h - packet header from wire conversion routines 3 * 4 * a Net::DNS like library for C 5 * 6 * (c) NLnet Labs, 2005-2006 7 * 8 * See the file LICENSE for the license 9 */ 10 11 /** 12 * \file 13 * 14 * Contains functions that translate dns data from the wire format (as sent 15 * by servers and clients) to the internal structures for the packet header. 16 */ 17 18 #ifndef GLDNS_PKTHDR_H 19 #define GLDNS_PKTHDR_H 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 /* The length of the header */ 26 #define GLDNS_HEADER_SIZE 12 27 28 /* First octet of flags */ 29 #define GLDNS_RD_MASK 0x01U 30 #define GLDNS_RD_SHIFT 0 31 #define GLDNS_RD_WIRE(wirebuf) (*(wirebuf+2) & GLDNS_RD_MASK) 32 #define GLDNS_RD_SET(wirebuf) (*(wirebuf+2) |= GLDNS_RD_MASK) 33 #define GLDNS_RD_CLR(wirebuf) (*(wirebuf+2) &= ~GLDNS_RD_MASK) 34 35 #define GLDNS_TC_MASK 0x02U 36 #define GLDNS_TC_SHIFT 1 37 #define GLDNS_TC_WIRE(wirebuf) (*(wirebuf+2) & GLDNS_TC_MASK) 38 #define GLDNS_TC_SET(wirebuf) (*(wirebuf+2) |= GLDNS_TC_MASK) 39 #define GLDNS_TC_CLR(wirebuf) (*(wirebuf+2) &= ~GLDNS_TC_MASK) 40 41 #define GLDNS_AA_MASK 0x04U 42 #define GLDNS_AA_SHIFT 2 43 #define GLDNS_AA_WIRE(wirebuf) (*(wirebuf+2) & GLDNS_AA_MASK) 44 #define GLDNS_AA_SET(wirebuf) (*(wirebuf+2) |= GLDNS_AA_MASK) 45 #define GLDNS_AA_CLR(wirebuf) (*(wirebuf+2) &= ~GLDNS_AA_MASK) 46 47 #define GLDNS_OPCODE_MASK 0x78U 48 #define GLDNS_OPCODE_SHIFT 3 49 #define GLDNS_OPCODE_WIRE(wirebuf) ((*(wirebuf+2) & GLDNS_OPCODE_MASK) >> GLDNS_OPCODE_SHIFT) 50 #define GLDNS_OPCODE_SET(wirebuf, opcode) \ 51 (*(wirebuf+2) = ((*(wirebuf+2)) & ~GLDNS_OPCODE_MASK) | ((opcode) << GLDNS_OPCODE_SHIFT)) 52 53 #define GLDNS_QR_MASK 0x80U 54 #define GLDNS_QR_SHIFT 7 55 #define GLDNS_QR_WIRE(wirebuf) (*(wirebuf+2) & GLDNS_QR_MASK) 56 #define GLDNS_QR_SET(wirebuf) (*(wirebuf+2) |= GLDNS_QR_MASK) 57 #define GLDNS_QR_CLR(wirebuf) (*(wirebuf+2) &= ~GLDNS_QR_MASK) 58 59 /* Second octet of flags */ 60 #define GLDNS_RCODE_MASK 0x0fU 61 #define GLDNS_RCODE_SHIFT 0 62 #define GLDNS_RCODE_WIRE(wirebuf) (*(wirebuf+3) & GLDNS_RCODE_MASK) 63 #define GLDNS_RCODE_SET(wirebuf, rcode) \ 64 (*(wirebuf+3) = ((*(wirebuf+3)) & ~GLDNS_RCODE_MASK) | (rcode)) 65 66 #define GLDNS_CD_MASK 0x10U 67 #define GLDNS_CD_SHIFT 4 68 #define GLDNS_CD_WIRE(wirebuf) (*(wirebuf+3) & GLDNS_CD_MASK) 69 #define GLDNS_CD_SET(wirebuf) (*(wirebuf+3) |= GLDNS_CD_MASK) 70 #define GLDNS_CD_CLR(wirebuf) (*(wirebuf+3) &= ~GLDNS_CD_MASK) 71 72 #define GLDNS_AD_MASK 0x20U 73 #define GLDNS_AD_SHIFT 5 74 #define GLDNS_AD_WIRE(wirebuf) (*(wirebuf+3) & GLDNS_AD_MASK) 75 #define GLDNS_AD_SET(wirebuf) (*(wirebuf+3) |= GLDNS_AD_MASK) 76 #define GLDNS_AD_CLR(wirebuf) (*(wirebuf+3) &= ~GLDNS_AD_MASK) 77 78 #define GLDNS_Z_MASK 0x40U 79 #define GLDNS_Z_SHIFT 6 80 #define GLDNS_Z_WIRE(wirebuf) (*(wirebuf+3) & GLDNS_Z_MASK) 81 #define GLDNS_Z_SET(wirebuf) (*(wirebuf+3) |= GLDNS_Z_MASK) 82 #define GLDNS_Z_CLR(wirebuf) (*(wirebuf+3) &= ~GLDNS_Z_MASK) 83 84 #define GLDNS_RA_MASK 0x80U 85 #define GLDNS_RA_SHIFT 7 86 #define GLDNS_RA_WIRE(wirebuf) (*(wirebuf+3) & GLDNS_RA_MASK) 87 #define GLDNS_RA_SET(wirebuf) (*(wirebuf+3) |= GLDNS_RA_MASK) 88 #define GLDNS_RA_CLR(wirebuf) (*(wirebuf+3) &= ~GLDNS_RA_MASK) 89 90 /* Query ID */ 91 #define GLDNS_ID_WIRE(wirebuf) (gldns_read_uint16(wirebuf)) 92 #define GLDNS_ID_SET(wirebuf, id) (gldns_write_uint16(wirebuf, id)) 93 94 /* Counter of the question section */ 95 #define GLDNS_QDCOUNT_OFF 4 96 /* 97 #define QDCOUNT(wirebuf) (ntohs(*(uint16_t *)(wirebuf+QDCOUNT_OFF))) 98 */ 99 #define GLDNS_QDCOUNT(wirebuf) (gldns_read_uint16(wirebuf+GLDNS_QDCOUNT_OFF)) 100 101 /* Counter of the answer section */ 102 #define GLDNS_ANCOUNT_OFF 6 103 #define GLDNS_ANCOUNT(wirebuf) (gldns_read_uint16(wirebuf+GLDNS_ANCOUNT_OFF)) 104 105 /* Counter of the authority section */ 106 #define GLDNS_NSCOUNT_OFF 8 107 #define GLDNS_NSCOUNT(wirebuf) (gldns_read_uint16(wirebuf+GLDNS_NSCOUNT_OFF)) 108 109 /* Counter of the additional section */ 110 #define GLDNS_ARCOUNT_OFF 10 111 #define GLDNS_ARCOUNT(wirebuf) (gldns_read_uint16(wirebuf+GLDNS_ARCOUNT_OFF)) 112 113 /** 114 * The sections of a packet 115 */ 116 enum gldns_enum_pkt_section { 117 GLDNS_SECTION_QUESTION = 0, 118 GLDNS_SECTION_ANSWER = 1, 119 GLDNS_SECTION_AUTHORITY = 2, 120 GLDNS_SECTION_ADDITIONAL = 3, 121 /** bogus section, if not interested */ 122 GLDNS_SECTION_ANY = 4, 123 /** used to get all non-question rrs from a packet */ 124 GLDNS_SECTION_ANY_NOQUESTION = 5 125 }; 126 typedef enum gldns_enum_pkt_section gldns_pkt_section; 127 128 /* opcodes for pkt's */ 129 enum gldns_enum_pkt_opcode { 130 GLDNS_PACKET_QUERY = 0, 131 GLDNS_PACKET_IQUERY = 1, 132 GLDNS_PACKET_STATUS = 2, /* there is no 3?? DNS is weird */ 133 GLDNS_PACKET_NOTIFY = 4, 134 GLDNS_PACKET_UPDATE = 5 135 }; 136 typedef enum gldns_enum_pkt_opcode gldns_pkt_opcode; 137 138 /* rcodes for pkts */ 139 enum gldns_enum_pkt_rcode { 140 GLDNS_RCODE_NOERROR = 0, 141 GLDNS_RCODE_FORMERR = 1, 142 GLDNS_RCODE_SERVFAIL = 2, 143 GLDNS_RCODE_NXDOMAIN = 3, 144 GLDNS_RCODE_NOTIMPL = 4, 145 GLDNS_RCODE_REFUSED = 5, 146 GLDNS_RCODE_YXDOMAIN = 6, 147 GLDNS_RCODE_YXRRSET = 7, 148 GLDNS_RCODE_NXRRSET = 8, 149 GLDNS_RCODE_NOTAUTH = 9, 150 GLDNS_RCODE_NOTZONE = 10 151 }; 152 typedef enum gldns_enum_pkt_rcode gldns_pkt_rcode; 153 154 #ifdef __cplusplus 155 } 156 #endif 157 158 #endif /* GLDNS_PKTHDR_H */ 159