| 1 | #ifndef _ARPA_NAMESER_H_ |
| 2 | |
| 3 | #include <resolv/arpa/nameser.h> |
| 4 | |
| 5 | # ifndef _ISOMAC |
| 6 | |
| 7 | /* If the machine allows unaligned access we can do better than using |
| 8 | the NS_GET16, NS_GET32, NS_PUT16, and NS_PUT32 macros from the |
| 9 | installed header. */ |
| 10 | #include <string.h> |
| 11 | #include <stdint.h> |
| 12 | #include <netinet/in.h> |
| 13 | |
| 14 | extern const struct _ns_flagdata _ns_flagdata[] attribute_hidden; |
| 15 | |
| 16 | #if _STRING_ARCH_unaligned |
| 17 | |
| 18 | # undef NS_GET16 |
| 19 | # define NS_GET16(s, cp) \ |
| 20 | do { \ |
| 21 | const uint16_t *t_cp = (const uint16_t *) (cp); \ |
| 22 | (s) = ntohs (*t_cp); \ |
| 23 | (cp) += NS_INT16SZ; \ |
| 24 | } while (0) |
| 25 | |
| 26 | # undef NS_GET32 |
| 27 | # define NS_GET32(l, cp) \ |
| 28 | do { \ |
| 29 | const uint32_t *t_cp = (const uint32_t *) (cp); \ |
| 30 | (l) = ntohl (*t_cp); \ |
| 31 | (cp) += NS_INT32SZ; \ |
| 32 | } while (0) |
| 33 | |
| 34 | # undef NS_PUT16 |
| 35 | # define NS_PUT16(s, cp) \ |
| 36 | do { \ |
| 37 | uint16_t *t_cp = (uint16_t *) (cp); \ |
| 38 | *t_cp = htons (s); \ |
| 39 | (cp) += NS_INT16SZ; \ |
| 40 | } while (0) |
| 41 | |
| 42 | # undef NS_PUT32 |
| 43 | # define NS_PUT32(l, cp) \ |
| 44 | do { \ |
| 45 | uint32_t *t_cp = (uint32_t *) (cp); \ |
| 46 | *t_cp = htonl (l); \ |
| 47 | (cp) += NS_INT32SZ; \ |
| 48 | } while (0) |
| 49 | |
| 50 | #endif |
| 51 | |
| 52 | extern unsigned int __ns_get16 (const unsigned char *) __THROW; |
| 53 | extern unsigned long __ns_get32 (const unsigned char *) __THROW; |
| 54 | int __ns_name_ntop (const unsigned char *, char *, size_t) __THROW; |
| 55 | int __ns_name_unpack (const unsigned char *, const unsigned char *, |
| 56 | const unsigned char *, unsigned char *, size_t) __THROW; |
| 57 | |
| 58 | /* Like ns_samename, but for uncompressed binary names. Return true |
| 59 | if the two arguments compare are equal as case-insensitive domain |
| 60 | names. */ |
| 61 | _Bool __ns_samebinaryname (const unsigned char *, const unsigned char *) |
| 62 | attribute_hidden; |
| 63 | |
| 64 | #define ns_msg_getflag(handle, flag) \ |
| 65 | (((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift) |
| 66 | |
| 67 | libresolv_hidden_proto (ns_get16) |
| 68 | libresolv_hidden_proto (ns_get32) |
| 69 | libresolv_hidden_proto (ns_put16) |
| 70 | libresolv_hidden_proto (ns_put32) |
| 71 | libresolv_hidden_proto (ns_initparse) |
| 72 | libresolv_hidden_proto (ns_skiprr) |
| 73 | libresolv_hidden_proto (ns_parserr) |
| 74 | libresolv_hidden_proto (ns_sprintrr) |
| 75 | libresolv_hidden_proto (ns_sprintrrf) |
| 76 | libresolv_hidden_proto (ns_samedomain) |
| 77 | libresolv_hidden_proto (ns_format_ttl) |
| 78 | |
| 79 | extern __typeof (ns_makecanon) __libc_ns_makecanon; |
| 80 | libc_hidden_proto (__libc_ns_makecanon) |
| 81 | extern __typeof (ns_name_compress) __ns_name_compress; |
| 82 | libc_hidden_proto (__ns_name_compress) |
| 83 | extern __typeof (ns_name_ntop) __ns_name_ntop; |
| 84 | libc_hidden_proto (__ns_name_ntop) |
| 85 | extern __typeof (ns_name_pack) __ns_name_pack; |
| 86 | libc_hidden_proto (__ns_name_pack) |
| 87 | extern __typeof (ns_name_pton) __ns_name_pton; |
| 88 | libc_hidden_proto (__ns_name_pton) |
| 89 | extern __typeof (ns_name_skip) __ns_name_skip; |
| 90 | libc_hidden_proto (__ns_name_skip) |
| 91 | extern __typeof (ns_name_uncompress) __ns_name_uncompress; |
| 92 | libc_hidden_proto (__ns_name_uncompress) |
| 93 | extern __typeof (ns_name_unpack) __ns_name_unpack; |
| 94 | libc_hidden_proto (__ns_name_unpack) |
| 95 | extern __typeof (ns_samename) __libc_ns_samename; |
| 96 | libc_hidden_proto (__libc_ns_samename) |
| 97 | |
| 98 | /* Packet parser helper functions. */ |
| 99 | |
| 100 | /* Verify that P points to an uncompressed domain name in wire format. |
| 101 | On success, return the length of the encoded name, including the |
| 102 | terminating null byte. On failure, return -1 and set errno. EOM |
| 103 | must point one past the last byte in the packet. */ |
| 104 | int __ns_name_length_uncompressed (const unsigned char *p, |
| 105 | const unsigned char *eom) attribute_hidden; |
| 106 | |
| 107 | /* Iterator over the resource records in a DNS packet. */ |
| 108 | struct ns_rr_cursor |
| 109 | { |
| 110 | /* These members are not changed after initialization. */ |
| 111 | const unsigned char *begin; /* First byte of packet. */ |
| 112 | const unsigned char *end; /* One past the last byte of the packet. */ |
| 113 | const unsigned char *first_rr; /* First resource record (or packet end). */ |
| 114 | |
| 115 | /* Advanced towards the end while reading the packet. */ |
| 116 | const unsigned char *current; |
| 117 | }; |
| 118 | |
| 119 | /* Returns the RCODE field from the DNS header. */ |
| 120 | static inline int |
| 121 | ns_rr_cursor_rcode (const struct ns_rr_cursor *c) |
| 122 | { |
| 123 | return c->begin[3] & 0x0f; /* Lower 4 bits at offset 3. */ |
| 124 | } |
| 125 | |
| 126 | /* Returns the length of the answer section according to the DNS header. */ |
| 127 | static inline int |
| 128 | ns_rr_cursor_ancount (const struct ns_rr_cursor *c) |
| 129 | { |
| 130 | return c->begin[6] * 256 + c->begin[7]; /* 16 bits at offset 6. */ |
| 131 | } |
| 132 | |
| 133 | /* Returns the length of the authority (name server) section according |
| 134 | to the DNS header. */ |
| 135 | static inline int |
| 136 | ns_rr_cursor_nscount (const struct ns_rr_cursor *c) |
| 137 | { |
| 138 | return c->begin[8] * 256 + c->begin[9]; /* 16 bits at offset 8. */ |
| 139 | } |
| 140 | |
| 141 | /* Returns the length of the additional data section according to the |
| 142 | DNS header. */ |
| 143 | static inline int |
| 144 | ns_rr_cursor_adcount (const struct ns_rr_cursor *c) |
| 145 | { |
| 146 | return c->begin[10] * 256 + c->begin[11]; /* 16 bits at offset 10. */ |
| 147 | } |
| 148 | |
| 149 | /* Returns a pointer to the uncompressed question name in wire |
| 150 | format. */ |
| 151 | static inline const unsigned char * |
| 152 | ns_rr_cursor_qname (const struct ns_rr_cursor *c) |
| 153 | { |
| 154 | return c->begin + 12; /* QNAME starts right after the header. */ |
| 155 | } |
| 156 | |
| 157 | /* Returns the question type of the first and only question. */ |
| 158 | static inline const int |
| 159 | ns_rr_cursor_qtype (const struct ns_rr_cursor *c) |
| 160 | { |
| 161 | /* 16 bits 4 bytes back from the first RR header start. */ |
| 162 | return c->first_rr[-4] * 256 + c->first_rr[-3]; |
| 163 | } |
| 164 | |
| 165 | /* Returns the clss of the first and only question (usally C_IN). */ |
| 166 | static inline const int |
| 167 | ns_rr_cursor_qclass (const struct ns_rr_cursor *c) |
| 168 | { |
| 169 | /* 16 bits 2 bytes back from the first RR header start. */ |
| 170 | return c->first_rr[-2] * 256 + c->first_rr[-1]; |
| 171 | } |
| 172 | |
| 173 | /* Initializes *C to cover the packet [BUF, BUF+LEN). Returns false |
| 174 | if LEN is less than sizeof (*HD), if the packet does not contain a |
| 175 | full (uncompressed) question, or if the question count is not 1. */ |
| 176 | _Bool __ns_rr_cursor_init (struct ns_rr_cursor *c, |
| 177 | const unsigned char *buf, size_t len) |
| 178 | attribute_hidden; |
| 179 | |
| 180 | /* Like ns_rr, but the record owner name is not decoded into text format. */ |
| 181 | struct ns_rr_wire |
| 182 | { |
| 183 | unsigned char rname[NS_MAXCDNAME]; /* Owner name of the record. */ |
| 184 | uint16_t rtype; /* Resource record type (T_*). */ |
| 185 | uint16_t rclass; /* Resource record class (C_*). */ |
| 186 | uint32_t ttl; /* Time-to-live field. */ |
| 187 | const unsigned char *rdata; /* Start of resource record data. */ |
| 188 | uint16_t rdlength; /* Length of the data at rdata, in bytes. */ |
| 189 | }; |
| 190 | |
| 191 | /* Attempts to parse the record at C into *RR. On success, return |
| 192 | true, and C is advanced past the record, and RR->rdata points to |
| 193 | the record data. On failure, errno is set to EMSGSIZE, and false |
| 194 | is returned. */ |
| 195 | _Bool __ns_rr_cursor_next (struct ns_rr_cursor *c, struct ns_rr_wire *rr) |
| 196 | attribute_hidden; |
| 197 | |
| 198 | # endif /* !_ISOMAC */ |
| 199 | #endif |
| 200 | |