Use libm_alias_double for dbl-64 modf.
[glibc.git] / resolv / nss_dns / dns-canon.c
blob7a5c39dc20f6ebb8fc6a661d041daf9327484bd3
1 /* Copyright (C) 2004-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <netdb.h>
21 #include <resolv.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <arpa/nameser.h>
25 #include <nsswitch.h>
26 #include <resolv/resolv_context.h>
27 #include <resolv/resolv-internal.h>
29 #if PACKETSZ > 65536
30 # define MAXPACKET PACKETSZ
31 #else
32 # define MAXPACKET 65536
33 #endif
36 /* We need this time later. */
37 typedef union querybuf
39 HEADER hdr;
40 unsigned char buf[MAXPACKET];
41 } querybuf;
44 static const short int qtypes[] = { ns_t_a, ns_t_aaaa };
45 #define nqtypes (sizeof (qtypes) / sizeof (qtypes[0]))
48 enum nss_status
49 _nss_dns_getcanonname_r (const char *name, char *buffer, size_t buflen,
50 char **result,int *errnop, int *h_errnop)
52 /* Just an alibi buffer, res_nquery will allocate a real buffer for
53 us. */
54 unsigned char buf[20];
55 union
57 querybuf *buf;
58 unsigned char *ptr;
59 } ansp = { .ptr = buf };
60 enum nss_status status = NSS_STATUS_UNAVAIL;
62 struct resolv_context *ctx = __resolv_context_get ();
63 if (ctx == NULL)
65 *errnop = errno;
66 *h_errnop = NETDB_INTERNAL;
67 return NSS_STATUS_UNAVAIL;
70 for (int i = 0; i < nqtypes; ++i)
72 int r = __res_context_query (ctx, name, ns_c_in, qtypes[i],
73 buf, sizeof (buf), &ansp.ptr, NULL, NULL,
74 NULL, NULL);
75 if (r > 0)
77 /* We need to decode the response. Just one question record.
78 And if we got no answers we bail out, too. */
79 if (ansp.buf->hdr.qdcount != htons (1))
80 continue;
82 /* Number of answers. */
83 unsigned int ancount = ntohs (ansp.buf->hdr.ancount);
85 /* Beginning and end of the buffer with query, answer, and the
86 rest. */
87 unsigned char *ptr = &ansp.buf->buf[sizeof (HEADER)];
88 unsigned char *endptr = ansp.ptr + r;
90 /* Skip over the query. This is the name, type, and class. */
91 int s = __dn_skipname (ptr, endptr);
92 if (s < 0)
94 unavail:
95 status = NSS_STATUS_UNAVAIL;
96 break;
99 /* Skip over the name and the two 16-bit values containing type
100 and class. */
101 ptr += s + 2 * sizeof (uint16_t);
103 while (ancount-- > 0)
105 /* Now the reply. First again the name from the query,
106 then type, class, TTL, and the length of the RDATA.
107 We remember the name start. */
108 unsigned char *namestart = ptr;
109 s = __dn_skipname (ptr, endptr);
110 if (s < 0)
111 goto unavail;
113 ptr += s;
115 /* Check that there are enough bytes for the RR
116 metadata. */
117 if (endptr - ptr < 10)
118 goto unavail;
120 /* Check whether type and class match. */
121 uint_fast16_t type;
122 NS_GET16 (type, ptr);
123 if (type == qtypes[i])
125 /* We found the record. */
126 s = __dn_expand (ansp.buf->buf, endptr, namestart,
127 buffer, buflen);
128 if (s < 0)
130 if (errno != EMSGSIZE)
131 goto unavail;
133 /* The buffer is too small. */
134 *errnop = ERANGE;
135 status = NSS_STATUS_TRYAGAIN;
136 h_errno = NETDB_INTERNAL;
138 else
140 /* Success. */
141 *result = buffer;
142 status = NSS_STATUS_SUCCESS;
145 goto out;
148 if (type != ns_t_cname)
149 goto unavail;
151 if (__ns_get16 (ptr) != ns_c_in)
152 goto unavail;
154 /* Also skip over class and TTL. */
155 ptr += sizeof (uint16_t) + sizeof (uint32_t);
157 /* Skip over RDATA length and RDATA itself. */
158 uint16_t rdatalen = __ns_get16 (ptr);
159 ptr += sizeof (uint16_t);
160 /* Not enough room for RDATA. */
161 if (endptr - ptr < rdatalen)
162 goto unavail;
163 ptr += rdatalen;
167 /* Restore original buffer before retry. */
168 if (ansp.ptr != buf)
170 free (ansp.ptr);
171 ansp.ptr = buf;
175 out:
176 *h_errnop = h_errno;
178 if (ansp.ptr != buf)
179 free (ansp.ptr);
180 __resolv_context_put (ctx);
181 return status;