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
14extern 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
52extern unsigned int __ns_get16 (const unsigned char *) __THROW;
53extern unsigned long __ns_get32 (const unsigned char *) __THROW;
54int __ns_name_ntop (const unsigned char *, char *, size_t) __THROW;
55int __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
67libresolv_hidden_proto (ns_get16)
68libresolv_hidden_proto (ns_get32)
69libresolv_hidden_proto (ns_put16)
70libresolv_hidden_proto (ns_put32)
71libresolv_hidden_proto (ns_initparse)
72libresolv_hidden_proto (ns_skiprr)
73libresolv_hidden_proto (ns_parserr)
74libresolv_hidden_proto (ns_sprintrr)
75libresolv_hidden_proto (ns_sprintrrf)
76libresolv_hidden_proto (ns_samedomain)
77libresolv_hidden_proto (ns_format_ttl)
78
79extern __typeof (ns_makecanon) __libc_ns_makecanon;
80libc_hidden_proto (__libc_ns_makecanon)
81extern __typeof (ns_name_compress) __ns_name_compress;
82libc_hidden_proto (__ns_name_compress)
83extern __typeof (ns_name_ntop) __ns_name_ntop;
84libc_hidden_proto (__ns_name_ntop)
85extern __typeof (ns_name_pack) __ns_name_pack;
86libc_hidden_proto (__ns_name_pack)
87extern __typeof (ns_name_pton) __ns_name_pton;
88libc_hidden_proto (__ns_name_pton)
89extern __typeof (ns_name_skip) __ns_name_skip;
90libc_hidden_proto (__ns_name_skip)
91extern __typeof (ns_name_uncompress) __ns_name_uncompress;
92libc_hidden_proto (__ns_name_uncompress)
93extern __typeof (ns_name_unpack) __ns_name_unpack;
94libc_hidden_proto (__ns_name_unpack)
95extern __typeof (ns_samename) __libc_ns_samename;
96libc_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. */
104int __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. */
108struct 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. */
120static inline int
121ns_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. */
127static inline int
128ns_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. */
135static inline int
136ns_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. */
143static inline int
144ns_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. */
151static inline const unsigned char *
152ns_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. */
158static inline const int
159ns_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). */
166static inline const int
167ns_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. */
181struct 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

source code of glibc/include/arpa/nameser.h