PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/resolv/res_mkquery.c

https://gitlab.com/gbenson/glibc
C | 303 lines | 159 code | 26 blank | 118 comment | 26 complexity | 7a84e59686762fcf0938ef5257fc5052 MD5 | raw file
  1. /* Creation of DNS query packets.
  2. Copyright (C) 1995-2018 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /*
  16. * Copyright (c) 1985, 1993
  17. * The Regents of the University of California. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 4. Neither the name of the University nor the names of its contributors
  28. * may be used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  32. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  35. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41. * SUCH DAMAGE.
  42. */
  43. /*
  44. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  45. *
  46. * Permission to use, copy, modify, and distribute this software for any
  47. * purpose with or without fee is hereby granted, provided that the above
  48. * copyright notice and this permission notice appear in all copies, and that
  49. * the name of Digital Equipment Corporation not be used in advertising or
  50. * publicity pertaining to distribution of the document or software without
  51. * specific, written prior permission.
  52. *
  53. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  54. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  55. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  56. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  57. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  58. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  59. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  60. * SOFTWARE.
  61. */
  62. /*
  63. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  64. *
  65. * Permission to use, copy, modify, and distribute this software for any
  66. * purpose with or without fee is hereby granted, provided that the above
  67. * copyright notice and this permission notice appear in all copies.
  68. *
  69. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  70. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  71. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  72. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  73. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  74. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  75. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  76. * SOFTWARE.
  77. */
  78. #include <sys/types.h>
  79. #include <sys/param.h>
  80. #include <netinet/in.h>
  81. #include <arpa/nameser.h>
  82. #include <netdb.h>
  83. #include <resolv/resolv-internal.h>
  84. #include <resolv/resolv_context.h>
  85. #include <string.h>
  86. #include <sys/time.h>
  87. #include <shlib-compat.h>
  88. #include <hp-timing.h>
  89. #include <stdint.h>
  90. #if HP_TIMING_AVAIL
  91. # define RANDOM_BITS(Var) { uint64_t v64; HP_TIMING_NOW (v64); Var = v64; }
  92. #endif
  93. int
  94. __res_context_mkquery (struct resolv_context *ctx, int op, const char *dname,
  95. int class, int type, const unsigned char *data,
  96. unsigned char *buf, int buflen)
  97. {
  98. HEADER *hp;
  99. unsigned char *cp;
  100. int n;
  101. unsigned char *dnptrs[20], **dpp, **lastdnptr;
  102. if (class < 0 || class > 65535 || type < 0 || type > 65535)
  103. return -1;
  104. /* Initialize header fields. */
  105. if ((buf == NULL) || (buflen < HFIXEDSZ))
  106. return -1;
  107. memset (buf, 0, HFIXEDSZ);
  108. hp = (HEADER *) buf;
  109. /* We randomize the IDs every time. The old code just incremented
  110. by one after the initial randomization which still predictable if
  111. the application does multiple requests. */
  112. int randombits;
  113. #ifdef RANDOM_BITS
  114. RANDOM_BITS (randombits);
  115. #else
  116. struct timeval tv;
  117. __gettimeofday (&tv, NULL);
  118. randombits = (tv.tv_sec << 8) ^ tv.tv_usec;
  119. #endif
  120. hp->id = randombits;
  121. hp->opcode = op;
  122. hp->rd = (ctx->resp->options & RES_RECURSE) != 0;
  123. hp->rcode = NOERROR;
  124. cp = buf + HFIXEDSZ;
  125. buflen -= HFIXEDSZ;
  126. dpp = dnptrs;
  127. *dpp++ = buf;
  128. *dpp++ = NULL;
  129. lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
  130. /* Perform opcode specific processing. */
  131. switch (op)
  132. {
  133. case NS_NOTIFY_OP:
  134. if ((buflen -= QFIXEDSZ + (data == NULL ? 0 : RRFIXEDSZ)) < 0)
  135. return -1;
  136. goto compose;
  137. case QUERY:
  138. if ((buflen -= QFIXEDSZ) < 0)
  139. return -1;
  140. compose:
  141. n = ns_name_compress (dname, cp, buflen,
  142. (const unsigned char **) dnptrs,
  143. (const unsigned char **) lastdnptr);
  144. if (n < 0)
  145. return -1;
  146. cp += n;
  147. buflen -= n;
  148. NS_PUT16 (type, cp);
  149. NS_PUT16 (class, cp);
  150. hp->qdcount = htons (1);
  151. if (op == QUERY || data == NULL)
  152. break;
  153. /* Make an additional record for completion domain. */
  154. n = ns_name_compress ((char *)data, cp, buflen,
  155. (const unsigned char **) dnptrs,
  156. (const unsigned char **) lastdnptr);
  157. if (__glibc_unlikely (n < 0))
  158. return -1;
  159. cp += n;
  160. buflen -= n;
  161. NS_PUT16 (T_NULL, cp);
  162. NS_PUT16 (class, cp);
  163. NS_PUT32 (0, cp);
  164. NS_PUT16 (0, cp);
  165. hp->arcount = htons (1);
  166. break;
  167. default:
  168. return -1;
  169. }
  170. return cp - buf;
  171. }
  172. /* Common part of res_nmkquery and res_mkquery. */
  173. static int
  174. context_mkquery_common (struct resolv_context *ctx,
  175. int op, const char *dname, int class, int type,
  176. const unsigned char *data,
  177. unsigned char *buf, int buflen)
  178. {
  179. if (ctx == NULL)
  180. return -1;
  181. int result = __res_context_mkquery
  182. (ctx, op, dname, class, type, data, buf, buflen);
  183. if (result >= 2)
  184. memcpy (&ctx->resp->id, buf, 2);
  185. __resolv_context_put (ctx);
  186. return result;
  187. }
  188. /* Form all types of queries. Returns the size of the result or -1 on
  189. error.
  190. STATP points to an initialized resolver state. OP is the opcode of
  191. the query. DNAME is the domain. CLASS and TYPE are the DNS query
  192. class and type. DATA can be NULL; otherwise, it is a pointer to a
  193. domain name which is included in the generated packet (if op ==
  194. NS_NOTIFY_OP). BUF must point to the out buffer of BUFLEN bytes.
  195. DATALEN and NEWRR_IN are currently ignored. */
  196. int
  197. res_nmkquery (res_state statp, int op, const char *dname,
  198. int class, int type,
  199. const unsigned char *data, int datalen,
  200. const unsigned char *newrr_in,
  201. unsigned char *buf, int buflen)
  202. {
  203. return context_mkquery_common
  204. (__resolv_context_get_override (statp),
  205. op, dname, class, type, data, buf, buflen);
  206. }
  207. int
  208. res_mkquery (int op, const char *dname, int class, int type,
  209. const unsigned char *data, int datalen,
  210. const unsigned char *newrr_in,
  211. unsigned char *buf, int buflen)
  212. {
  213. return context_mkquery_common
  214. (__resolv_context_get_preinit (),
  215. op, dname, class, type, data, buf, buflen);
  216. }
  217. /* Create an OPT resource record. Return the length of the final
  218. packet, or -1 on error.
  219. STATP must be an initialized resolver state. N0 is the current
  220. number of bytes of the packet (already written to BUF by the
  221. aller). BUF is the packet being constructed. The array it
  222. pointers to must be BUFLEN bytes long. ANSLEN is the advertised
  223. EDNS buffer size (to be included in the OPT resource record). */
  224. int
  225. __res_nopt (struct resolv_context *ctx,
  226. int n0, unsigned char *buf, int buflen, int anslen)
  227. {
  228. uint16_t flags = 0;
  229. HEADER *hp = (HEADER *) buf;
  230. unsigned char *cp = buf + n0;
  231. unsigned char *ep = buf + buflen;
  232. if ((ep - cp) < 1 + RRFIXEDSZ)
  233. return -1;
  234. /* Add the root label. */
  235. *cp++ = 0;
  236. NS_PUT16 (T_OPT, cp); /* Record type. */
  237. /* Lowering the advertised buffer size based on the actual
  238. answer buffer size is desirable because the server will
  239. minimize the reply to fit into the UDP packet (and A
  240. non-minimal response might not fit the buffer).
  241. The RESOLV_EDNS_BUFFER_SIZE limit could still result in TCP
  242. fallback and a non-minimal response which has to be
  243. hard-truncated in the stub resolver, but this is price to
  244. pay for avoiding fragmentation. (This issue does not
  245. affect the nss_dns functions because they use the stub
  246. resolver in such a way that it allocates a properly sized
  247. response buffer.) */
  248. {
  249. uint16_t buffer_size;
  250. if (anslen < 512)
  251. buffer_size = 512;
  252. else if (anslen > RESOLV_EDNS_BUFFER_SIZE)
  253. buffer_size = RESOLV_EDNS_BUFFER_SIZE;
  254. else
  255. buffer_size = anslen;
  256. NS_PUT16 (buffer_size, cp);
  257. }
  258. *cp++ = NOERROR; /* Extended RCODE. */
  259. *cp++ = 0; /* EDNS version. */
  260. if (ctx->resp->options & RES_USE_DNSSEC)
  261. flags |= NS_OPT_DNSSEC_OK;
  262. NS_PUT16 (flags, cp);
  263. NS_PUT16 (0, cp); /* RDATA length (no options are preent). */
  264. hp->arcount = htons (ntohs (hp->arcount) + 1);
  265. return cp - buf;
  266. }
  267. #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_2)
  268. # undef res_mkquery
  269. weak_alias (__res_mkquery, res_mkquery);
  270. #endif