PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/newlib/libc/sys/linux/net/res_mkquery.c

https://github.com/atgreen/moxiedev
C | 222 lines | 127 code | 10 blank | 85 comment | 19 complexity | cee7482f9ee1744b4a852c13f4446928 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause-No-Nuclear-License-2014, BSD-3-Clause, GPL-3.0, LGPL-2.0, CC-BY-SA-3.0
  1. /*
  2. * Copyright (c) 1985, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  31. *
  32. * Permission to use, copy, modify, and distribute this software for any
  33. * purpose with or without fee is hereby granted, provided that the above
  34. * copyright notice and this permission notice appear in all copies, and that
  35. * the name of Digital Equipment Corporation not be used in advertising or
  36. * publicity pertaining to distribution of the document or software without
  37. * specific, written prior permission.
  38. *
  39. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  40. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  41. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  42. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  43. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  44. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  45. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  46. * SOFTWARE.
  47. */
  48. /*
  49. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  50. *
  51. * Permission to use, copy, modify, and distribute this software for any
  52. * purpose with or without fee is hereby granted, provided that the above
  53. * copyright notice and this permission notice appear in all copies.
  54. *
  55. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  56. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  57. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  58. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  59. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  60. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  61. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  62. * SOFTWARE.
  63. */
  64. #if defined(LIBC_SCCS) && !defined(lint)
  65. static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
  66. static const char rcsid[] = "$BINDId: res_mkquery.c,v 8.12 1999/10/13 16:39:40 vixie Exp $";
  67. #endif /* LIBC_SCCS and not lint */
  68. #include <sys/types.h>
  69. #include <sys/param.h>
  70. #include <netinet/in.h>
  71. #include <arpa/nameser.h>
  72. #include <netdb.h>
  73. #include <resolv.h>
  74. #include <stdio.h>
  75. #include <string.h>
  76. #include <time.h>
  77. #include "local.h"
  78. /* Options. Leave them on. */
  79. /* #define DEBUG */
  80. #ifdef _LIBC
  81. # include <hp-timing.h>
  82. # if HP_TIMING_AVAIL
  83. # define RANDOM_BITS(Var) { uint64_t v64; HP_TIMING_NOW (v64); Var = v64; }
  84. # endif
  85. #endif
  86. /*
  87. * Form all types of queries.
  88. * Returns the size of the result or -1.
  89. */
  90. int
  91. res_nmkquery(res_state statp,
  92. int op, /* opcode of query */
  93. const char *dname, /* domain name */
  94. int class, int type, /* class and type of query */
  95. const u_char *data, /* resource record data */
  96. int datalen, /* length of data */
  97. const u_char *newrr_in, /* new rr for modify or append */
  98. u_char *buf, /* buffer to put query */
  99. int buflen) /* size of buffer */
  100. {
  101. register HEADER *hp;
  102. register u_char *cp;
  103. register int n;
  104. u_char *dnptrs[20], **dpp, **lastdnptr;
  105. #ifdef DEBUG
  106. if (statp->options & RES_DEBUG)
  107. printf(";; res_nmkquery(%s, %s, %s, %s)\n",
  108. _res_opcodes[op], dname, p_class(class), p_type(type));
  109. #endif
  110. /*
  111. * Initialize header fields.
  112. */
  113. if ((buf == NULL) || (buflen < HFIXEDSZ))
  114. return (-1);
  115. memset(buf, 0, HFIXEDSZ);
  116. hp = (HEADER *) buf;
  117. /* We randomize the IDs every time. The old code just
  118. incremented by one after the initial randomization which
  119. still predictable if the application does multiple
  120. requests. */
  121. #if 0
  122. hp->id = htons(++statp->id);
  123. #else
  124. hp->id = htons(statp->id);
  125. int randombits;
  126. do
  127. {
  128. #ifdef RANDOM_BITS
  129. RANDOM_BITS (randombits);
  130. #else
  131. struct timeval tv;
  132. gettimeofday (&tv, NULL);
  133. randombits = (tv.tv_sec << 8) ^ tv.tv_usec;
  134. #endif
  135. }
  136. while ((randombits & 0xffff) == 0);
  137. statp->id = (statp->id + randombits) & 0xffff;
  138. #endif
  139. hp->opcode = op;
  140. hp->rd = (statp->options & RES_RECURSE) != 0;
  141. hp->rcode = NOERROR;
  142. cp = buf + HFIXEDSZ;
  143. buflen -= HFIXEDSZ;
  144. dpp = dnptrs;
  145. *dpp++ = buf;
  146. *dpp++ = NULL;
  147. lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
  148. /*
  149. * perform opcode specific processing
  150. */
  151. switch (op) {
  152. case QUERY: /*FALLTHROUGH*/
  153. case NS_NOTIFY_OP:
  154. if ((buflen -= QFIXEDSZ) < 0)
  155. return (-1);
  156. if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
  157. return (-1);
  158. cp += n;
  159. buflen -= n;
  160. __putshort(type, cp);
  161. cp += INT16SZ;
  162. __putshort(class, cp);
  163. cp += INT16SZ;
  164. hp->qdcount = htons(1);
  165. if (op == QUERY || data == NULL)
  166. break;
  167. /*
  168. * Make an additional record for completion domain.
  169. */
  170. buflen -= RRFIXEDSZ;
  171. n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr);
  172. if (n < 0)
  173. return (-1);
  174. cp += n;
  175. buflen -= n;
  176. __putshort(T_NULL, cp);
  177. cp += INT16SZ;
  178. __putshort(class, cp);
  179. cp += INT16SZ;
  180. __putlong(0, cp);
  181. cp += INT32SZ;
  182. __putshort(0, cp);
  183. cp += INT16SZ;
  184. hp->arcount = htons(1);
  185. break;
  186. case IQUERY:
  187. /*
  188. * Initialize answer section
  189. */
  190. if (buflen < 1 + RRFIXEDSZ + datalen)
  191. return (-1);
  192. *cp++ = '\0'; /* no domain name */
  193. __putshort(type, cp);
  194. cp += INT16SZ;
  195. __putshort(class, cp);
  196. cp += INT16SZ;
  197. __putlong(0, cp);
  198. cp += INT32SZ;
  199. __putshort(datalen, cp);
  200. cp += INT16SZ;
  201. if (datalen) {
  202. memcpy(cp, data, datalen);
  203. cp += datalen;
  204. }
  205. hp->ancount = htons(1);
  206. break;
  207. default:
  208. return (-1);
  209. }
  210. return (cp - buf);
  211. }
  212. libresolv_hidden_def (res_nmkquery)