res_debug.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * Copyright (c) 1985
  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. * Copyright (c) 1995 IBM Corporation
  50. *
  51. * Permission is hereby granted, free of charge, to any person obtaining
  52. * a copy of this software and associated documentation files (the
  53. * 'Software'), to deal in the Software without restriction, including
  54. * without limitation the rights to use, copy, modify, merge, publish,
  55. * distribute, sublicense, and/or sell copies of the Software, and to
  56. * permit persons to whom the Software is furnished to do so, subject to
  57. * the following conditions:
  58. *
  59. * The above copyright notice and this permission notice shall be
  60. * included in all copies or substantial portions of the Software.
  61. *
  62. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  63. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  64. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  65. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  66. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  67. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  68. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  69. */
  70. /*
  71. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  72. *
  73. * Permission to use, copy, modify, and distribute this software for any
  74. * purpose with or without fee is hereby granted, provided that the above
  75. * copyright notice and this permission notice appear in all copies.
  76. *
  77. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  78. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  79. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  80. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  81. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  82. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  83. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  84. * SOFTWARE.
  85. */
  86. #include <sys/types.h>
  87. #include <sys/param.h>
  88. #include <sys/socket.h>
  89. #include <netinet/in.h>
  90. #include <arpa/inet.h>
  91. #include <arpa/nameser.h>
  92. #include <ctype.h>
  93. #include <errno.h>
  94. #include <math.h>
  95. #include <netdb.h>
  96. #include <resolv/resolv-internal.h>
  97. #include <stdio.h>
  98. #include <stdlib.h>
  99. #include <string.h>
  100. #include <time.h>
  101. #include <shlib-compat.h>
  102. #include <libc-diag.h>
  103. #ifdef SPRINTF_CHAR
  104. # define SPRINTF(x) strlen(sprintf/**/x)
  105. #else
  106. # define SPRINTF(x) sprintf x
  107. #endif
  108. extern const char *_res_sectioncodes[] attribute_hidden;
  109. /* _res_opcodes was exported by accident as a variable. */
  110. #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_26)
  111. static const char *res_opcodes[] =
  112. #else
  113. static const char res_opcodes[][9] =
  114. #endif
  115. {
  116. "QUERY",
  117. "IQUERY",
  118. "CQUERYM",
  119. "CQUERYU", /* experimental */
  120. "NOTIFY", /* experimental */
  121. "UPDATE",
  122. "6",
  123. "7",
  124. "8",
  125. "9",
  126. "10",
  127. "11",
  128. "12",
  129. "13",
  130. "ZONEINIT",
  131. "ZONEREF",
  132. };
  133. #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_26)
  134. strong_alias (res_opcodes, _res_opcodes)
  135. compat_symbol (libresolv, _res_opcodes, _res_opcodes, GLIBC_2_0);
  136. #endif
  137. static const char *p_section(int section, int opcode);
  138. /*
  139. * Print the current options.
  140. */
  141. void
  142. fp_resstat(const res_state statp, FILE *file) {
  143. u_long mask;
  144. fprintf(file, ";; res options:");
  145. for (mask = 1; mask != 0; mask <<= 1)
  146. if (statp->options & mask)
  147. fprintf(file, " %s", p_option(mask));
  148. putc('\n', file);
  149. }
  150. static void
  151. do_section (int pfcode, ns_msg *handle, ns_sect section, int pflag, FILE *file)
  152. {
  153. int n, sflag, rrnum;
  154. static int buflen = 2048;
  155. char *buf;
  156. ns_opcode opcode;
  157. ns_rr rr;
  158. /*
  159. * Print answer records.
  160. */
  161. sflag = (pfcode & pflag);
  162. if (pfcode && !sflag)
  163. return;
  164. buf = malloc(buflen);
  165. if (buf == NULL) {
  166. fprintf(file, ";; memory allocation failure\n");
  167. return;
  168. }
  169. opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
  170. rrnum = 0;
  171. for (;;) {
  172. if (ns_parserr(handle, section, rrnum, &rr)) {
  173. if (errno != ENODEV)
  174. fprintf(file, ";; ns_parserr: %s\n",
  175. strerror(errno));
  176. else if (rrnum > 0 && sflag != 0 &&
  177. (pfcode & RES_PRF_HEAD1))
  178. putc('\n', file);
  179. goto cleanup;
  180. }
  181. if (rrnum == 0 && sflag != 0 && (pfcode & RES_PRF_HEAD1))
  182. fprintf(file, ";; %s SECTION:\n",
  183. p_section(section, opcode));
  184. if (section == ns_s_qd)
  185. fprintf(file, ";;\t%s, type = %s, class = %s\n",
  186. ns_rr_name(rr),
  187. p_type(ns_rr_type(rr)),
  188. p_class(ns_rr_class(rr)));
  189. else {
  190. n = ns_sprintrr(handle, &rr, NULL, NULL,
  191. buf, buflen);
  192. if (n < 0) {
  193. if (errno == ENOSPC) {
  194. free(buf);
  195. buf = NULL;
  196. if (buflen < 131072)
  197. buf = malloc(buflen += 1024);
  198. if (buf == NULL) {
  199. fprintf(file,
  200. ";; memory allocation failure\n");
  201. return;
  202. }
  203. continue;
  204. }
  205. fprintf(file, ";; ns_sprintrr: %s\n",
  206. strerror(errno));
  207. goto cleanup;
  208. }
  209. fputs(buf, file);
  210. fputc('\n', file);
  211. }
  212. rrnum++;
  213. }
  214. cleanup:
  215. free(buf);
  216. }
  217. /*
  218. * Print the contents of a query.
  219. * This is intended to be primarily a debugging routine.
  220. */
  221. void
  222. fp_nquery (const unsigned char *msg, int len, FILE *file)
  223. {
  224. ns_msg handle;
  225. int qdcount, ancount, nscount, arcount;
  226. u_int opcode, rcode, id;
  227. /* There is no need to initialize _res: If _res is not yet
  228. initialized, _res.pfcode is zero. But initialization will
  229. leave it at zero, too. _res.pfcode is an unsigned long,
  230. but the code here assumes that the flags fit into an int,
  231. so use that. */
  232. int pfcode = _res.pfcode;
  233. if (ns_initparse(msg, len, &handle) < 0) {
  234. fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
  235. return;
  236. }
  237. opcode = ns_msg_getflag(handle, ns_f_opcode);
  238. rcode = ns_msg_getflag(handle, ns_f_rcode);
  239. id = ns_msg_id(handle);
  240. qdcount = ns_msg_count(handle, ns_s_qd);
  241. ancount = ns_msg_count(handle, ns_s_an);
  242. nscount = ns_msg_count(handle, ns_s_ns);
  243. arcount = ns_msg_count(handle, ns_s_ar);
  244. /*
  245. * Print header fields.
  246. */
  247. if ((!pfcode) || (pfcode & RES_PRF_HEADX) || rcode)
  248. fprintf(file,
  249. ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
  250. res_opcodes[opcode], p_rcode(rcode), id);
  251. if ((!pfcode) || (pfcode & RES_PRF_HEADX))
  252. putc(';', file);
  253. if ((!pfcode) || (pfcode & RES_PRF_HEAD2)) {
  254. fprintf(file, "; flags:");
  255. if (ns_msg_getflag(handle, ns_f_qr))
  256. fprintf(file, " qr");
  257. if (ns_msg_getflag(handle, ns_f_aa))
  258. fprintf(file, " aa");
  259. if (ns_msg_getflag(handle, ns_f_tc))
  260. fprintf(file, " tc");
  261. if (ns_msg_getflag(handle, ns_f_rd))
  262. fprintf(file, " rd");
  263. if (ns_msg_getflag(handle, ns_f_ra))
  264. fprintf(file, " ra");
  265. if (ns_msg_getflag(handle, ns_f_z))
  266. fprintf(file, " ??");
  267. if (ns_msg_getflag(handle, ns_f_ad))
  268. fprintf(file, " ad");
  269. if (ns_msg_getflag(handle, ns_f_cd))
  270. fprintf(file, " cd");
  271. }
  272. if ((!pfcode) || (pfcode & RES_PRF_HEAD1)) {
  273. fprintf(file, "; %s: %d",
  274. p_section(ns_s_qd, opcode), qdcount);
  275. fprintf(file, ", %s: %d",
  276. p_section(ns_s_an, opcode), ancount);
  277. fprintf(file, ", %s: %d",
  278. p_section(ns_s_ns, opcode), nscount);
  279. fprintf(file, ", %s: %d",
  280. p_section(ns_s_ar, opcode), arcount);
  281. }
  282. if ((!pfcode) || (pfcode &
  283. (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
  284. putc('\n',file);
  285. }
  286. /*
  287. * Print the various sections.
  288. */
  289. do_section (pfcode, &handle, ns_s_qd, RES_PRF_QUES, file);
  290. do_section (pfcode, &handle, ns_s_an, RES_PRF_ANS, file);
  291. do_section (pfcode, &handle, ns_s_ns, RES_PRF_AUTH, file);
  292. do_section (pfcode, &handle, ns_s_ar, RES_PRF_ADD, file);
  293. if (qdcount == 0 && ancount == 0 &&
  294. nscount == 0 && arcount == 0)
  295. putc('\n', file);
  296. }
  297. libresolv_hidden_def (fp_nquery)
  298. void
  299. fp_query (const unsigned char *msg, FILE *file)
  300. {
  301. fp_nquery (msg, PACKETSZ, file);
  302. }
  303. libresolv_hidden_def (fp_query)
  304. void
  305. p_query (const unsigned char *msg)
  306. {
  307. fp_query (msg, stdout);
  308. }
  309. const u_char *
  310. p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
  311. char name[MAXDNAME];
  312. int n;
  313. if ((n = __libc_dn_expand (msg, msg + len, cp, name, sizeof name)) < 0)
  314. return (NULL);
  315. if (name[0] == '\0')
  316. putc('.', file);
  317. else
  318. fputs(name, file);
  319. return (cp + n);
  320. }
  321. libresolv_hidden_def (p_cdnname)
  322. const u_char *
  323. p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
  324. return (p_cdnname(cp, msg, PACKETSZ, file));
  325. }
  326. /* Return a fully-qualified domain name from a compressed name (with
  327. length supplied). */
  328. const u_char *
  329. p_fqnname (const u_char *cp, const u_char *msg, int msglen, char *name,
  330. int namelen)
  331. {
  332. int n, newlen;
  333. if ((n = __libc_dn_expand (msg, cp + msglen, cp, name, namelen)) < 0)
  334. return (NULL);
  335. newlen = strlen(name);
  336. if (newlen == 0 || name[newlen - 1] != '.') {
  337. if (newlen + 1 >= namelen) /* Lack space for final dot */
  338. return (NULL);
  339. else
  340. strcpy(name + newlen, ".");
  341. }
  342. return (cp + n);
  343. }
  344. libresolv_hidden_def (p_fqnname)
  345. /* XXX: the rest of these functions need to become length-limited, too. */
  346. const u_char *
  347. p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
  348. char name[MAXDNAME];
  349. const u_char *n;
  350. n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
  351. if (n == NULL)
  352. return (NULL);
  353. fputs(name, file);
  354. return (n);
  355. }
  356. /*
  357. * Names of RR classes and qclasses. Classes and qclasses are the same, except
  358. * that C_ANY is a qclass but not a class. (You can ask for records of class
  359. * C_ANY, but you can't have any records of that class in the database.)
  360. */
  361. extern const struct res_sym __p_class_syms[];
  362. libresolv_hidden_proto (__p_class_syms)
  363. const struct res_sym __p_class_syms[] = {
  364. {C_IN, (char *) "IN"},
  365. {C_CHAOS, (char *) "CHAOS"},
  366. {C_HS, (char *) "HS"},
  367. {C_HS, (char *) "HESIOD"},
  368. {C_ANY, (char *) "ANY"},
  369. {C_NONE, (char *) "NONE"},
  370. {C_IN, NULL, NULL}
  371. };
  372. libresolv_hidden_data_def (__p_class_syms)
  373. /*
  374. * Names of message sections.
  375. */
  376. const struct res_sym __p_default_section_syms[] attribute_hidden = {
  377. {ns_s_qd, (char *) "QUERY"},
  378. {ns_s_an, (char *) "ANSWER"},
  379. {ns_s_ns, (char *) "AUTHORITY"},
  380. {ns_s_ar, (char *) "ADDITIONAL"},
  381. {0, NULL, NULL}
  382. };
  383. const struct res_sym __p_update_section_syms[] attribute_hidden = {
  384. {S_ZONE, (char *) "ZONE"},
  385. {S_PREREQ, (char *) "PREREQUISITE"},
  386. {S_UPDATE, (char *) "UPDATE"},
  387. {S_ADDT, (char *) "ADDITIONAL"},
  388. {0, NULL, NULL}
  389. };
  390. /*
  391. * Names of RR types and qtypes. The list is incomplete because its
  392. * size is part of the ABI.
  393. */
  394. extern const struct res_sym __p_type_syms[];
  395. libresolv_hidden_proto (__p_type_syms)
  396. const struct res_sym __p_type_syms[] = {
  397. {ns_t_a, (char *) "A", (char *) "address"},
  398. {ns_t_ns, (char *) "NS", (char *) "name server"},
  399. {ns_t_md, (char *) "MD", (char *) "mail destination (deprecated)"},
  400. {ns_t_mf, (char *) "MF", (char *) "mail forwarder (deprecated)"},
  401. {ns_t_cname, (char *) "CNAME", (char *) "canonical name"},
  402. {ns_t_soa, (char *) "SOA", (char *) "start of authority"},
  403. {ns_t_mb, (char *) "MB", (char *) "mailbox"},
  404. {ns_t_mg, (char *) "MG", (char *) "mail group member"},
  405. {ns_t_mr, (char *) "MR", (char *) "mail rename"},
  406. {ns_t_null, (char *) "NULL", (char *) "null"},
  407. {ns_t_wks, (char *) "WKS", (char *) "well-known service (deprecated)"},
  408. {ns_t_ptr, (char *) "PTR", (char *) "domain name pointer"},
  409. {ns_t_hinfo, (char *) "HINFO", (char *) "host information"},
  410. {ns_t_minfo, (char *) "MINFO", (char *) "mailbox information"},
  411. {ns_t_mx, (char *) "MX", (char *) "mail exchanger"},
  412. {ns_t_txt, (char *) "TXT", (char *) "text"},
  413. {ns_t_rp, (char *) "RP", (char *) "responsible person"},
  414. {ns_t_afsdb, (char *) "AFSDB", (char *) "DCE or AFS server"},
  415. {ns_t_x25, (char *) "X25", (char *) "X25 address"},
  416. {ns_t_isdn, (char *) "ISDN", (char *) "ISDN address"},
  417. {ns_t_rt, (char *) "RT", (char *) "router"},
  418. {ns_t_nsap, (char *) "NSAP", (char *) "nsap address"},
  419. {ns_t_nsap_ptr, (char *) "NSAP_PTR", (char *) "domain name pointer"},
  420. {ns_t_sig, (char *) "SIG", (char *) "signature"},
  421. {ns_t_key, (char *) "KEY", (char *) "key"},
  422. {ns_t_px, (char *) "PX", (char *) "mapping information"},
  423. {ns_t_gpos, (char *) "GPOS",
  424. (char *) "geographical position (withdrawn)"},
  425. {ns_t_aaaa, (char *) "AAAA", (char *) "IPv6 address"},
  426. {ns_t_loc, (char *) "LOC", (char *) "location"},
  427. {ns_t_nxt, (char *) "NXT", (char *) "next valid name (unimplemented)"},
  428. {ns_t_eid, (char *) "EID", (char *) "endpoint identifier (unimplemented)"},
  429. {ns_t_nimloc, (char *) "NIMLOC", (char *) "NIMROD locator (unimplemented)"},
  430. {ns_t_srv, (char *) "SRV", (char *) "server selection"},
  431. {ns_t_atma, (char *) "ATMA", (char *) "ATM address (unimplemented)"},
  432. {ns_t_dname, (char *) "DNAME", (char *) "Non-terminal DNAME (for IPv6)"},
  433. {ns_t_tsig, (char *) "TSIG", (char *) "transaction signature"},
  434. {ns_t_ixfr, (char *) "IXFR", (char *) "incremental zone transfer"},
  435. {ns_t_axfr, (char *) "AXFR", (char *) "zone transfer"},
  436. {ns_t_mailb, (char *) "MAILB", (char *) "mailbox-related data (deprecated)"},
  437. {ns_t_maila, (char *) "MAILA", (char *) "mail agent (deprecated)"},
  438. {ns_t_naptr, (char *) "NAPTR", (char *) "URN Naming Authority"},
  439. {ns_t_kx, (char *) "KX", (char *) "Key Exchange"},
  440. {ns_t_cert, (char *) "CERT", (char *) "Certificate"},
  441. {ns_t_any, (char *) "ANY", (char *) "\"any\""},
  442. {0, NULL, NULL}, /* Padding to preserve ABI. */
  443. {0, NULL, NULL}
  444. };
  445. libresolv_hidden_data_def (__p_type_syms)
  446. /*
  447. * Names of DNS rcodes.
  448. */
  449. const struct res_sym __p_rcode_syms[] attribute_hidden = {
  450. {ns_r_noerror, (char *) "NOERROR", (char *) "no error"},
  451. {ns_r_formerr, (char *) "FORMERR", (char *) "format error"},
  452. {ns_r_servfail, (char *) "SERVFAIL", (char *) "server failed"},
  453. {ns_r_nxdomain, (char *) "NXDOMAIN", (char *) "no such domain name"},
  454. {ns_r_notimpl, (char *) "NOTIMP", (char *) "not implemented"},
  455. {ns_r_refused, (char *) "REFUSED", (char *) "refused"},
  456. {ns_r_yxdomain, (char *) "YXDOMAIN", (char *) "domain name exists"},
  457. {ns_r_yxrrset, (char *) "YXRRSET", (char *) "rrset exists"},
  458. {ns_r_nxrrset, (char *) "NXRRSET", (char *) "rrset doesn't exist"},
  459. {ns_r_notauth, (char *) "NOTAUTH", (char *) "not authoritative"},
  460. {ns_r_notzone, (char *) "NOTZONE", (char *) "Not in zone"},
  461. {ns_r_max, (char *) "", (char *) ""},
  462. {ns_r_badsig, (char *) "BADSIG", (char *) "bad signature"},
  463. {ns_r_badkey, (char *) "BADKEY", (char *) "bad key"},
  464. {ns_r_badtime, (char *) "BADTIME", (char *) "bad time"},
  465. {0, NULL, NULL}
  466. };
  467. int
  468. sym_ston(const struct res_sym *syms, const char *name, int *success) {
  469. for ((void)NULL; syms->name != NULL; syms++) {
  470. if (strcasecmp (name, syms->name) == 0) {
  471. if (success)
  472. *success = 1;
  473. return (syms->number);
  474. }
  475. }
  476. if (success)
  477. *success = 0;
  478. return (syms->number); /* The default value. */
  479. }
  480. const char *
  481. sym_ntos(const struct res_sym *syms, int number, int *success) {
  482. static char unname[20];
  483. for ((void)NULL; syms->name != NULL; syms++) {
  484. if (number == syms->number) {
  485. if (success)
  486. *success = 1;
  487. return (syms->name);
  488. }
  489. }
  490. sprintf(unname, "%d", number); /* XXX nonreentrant */
  491. if (success)
  492. *success = 0;
  493. return (unname);
  494. }
  495. libresolv_hidden_def (sym_ntos)
  496. const char *
  497. sym_ntop(const struct res_sym *syms, int number, int *success) {
  498. static char unname[20];
  499. for ((void)NULL; syms->name != NULL; syms++) {
  500. if (number == syms->number) {
  501. if (success)
  502. *success = 1;
  503. return (syms->humanname);
  504. }
  505. }
  506. sprintf(unname, "%d", number); /* XXX nonreentrant */
  507. if (success)
  508. *success = 0;
  509. return (unname);
  510. }
  511. /*
  512. * Return a string for the type.
  513. */
  514. const char *
  515. p_type(int type) {
  516. return (sym_ntos(__p_type_syms, type, (int *)0));
  517. }
  518. libresolv_hidden_def (p_type)
  519. /*
  520. * Return a string for the type.
  521. */
  522. static const char *
  523. p_section(int section, int opcode) {
  524. const struct res_sym *symbols;
  525. switch (opcode) {
  526. case ns_o_update:
  527. symbols = __p_update_section_syms;
  528. break;
  529. default:
  530. symbols = __p_default_section_syms;
  531. break;
  532. }
  533. return (sym_ntos(symbols, section, (int *)0));
  534. }
  535. /*
  536. * Return a mnemonic for class.
  537. */
  538. const char *
  539. p_class(int class) {
  540. return (sym_ntos(__p_class_syms, class, (int *)0));
  541. }
  542. libresolv_hidden_def (p_class)
  543. /*
  544. * Return a mnemonic for an option
  545. */
  546. const char *
  547. p_option(u_long option) {
  548. static char nbuf[40];
  549. switch (option) {
  550. case RES_INIT: return "init";
  551. case RES_DEBUG: return "debug";
  552. case RES_USEVC: return "use-vc";
  553. case RES_IGNTC: return "igntc";
  554. case RES_RECURSE: return "recurs";
  555. case RES_DEFNAMES: return "defnam";
  556. case RES_STAYOPEN: return "styopn";
  557. case RES_DNSRCH: return "dnsrch";
  558. case RES_NOALIASES: return "noaliases";
  559. case RES_ROTATE: return "rotate";
  560. case RES_USE_EDNS0: return "edns0";
  561. case RES_SNGLKUP: return "single-request";
  562. case RES_SNGLKUPREOP: return "single-request-reopen";
  563. case RES_USE_DNSSEC: return "dnssec";
  564. case RES_NOTLDQUERY: return "no-tld-query";
  565. case RES_NORELOAD: return "no-reload";
  566. case RES_TRUSTAD: return "trust-ad";
  567. case RES_NOAAAA: return "no-aaaa";
  568. /* XXX nonreentrant */
  569. default: sprintf(nbuf, "?0x%lx?", (u_long)option);
  570. return (nbuf);
  571. }
  572. }
  573. libresolv_hidden_def (p_option)
  574. /*
  575. * Return a mnemonic for a time to live.
  576. */
  577. const char *
  578. p_time(uint32_t value) {
  579. static char nbuf[40]; /* XXX nonreentrant */
  580. if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)
  581. sprintf(nbuf, "%u", value);
  582. return (nbuf);
  583. }
  584. /*
  585. * Return a string for the rcode.
  586. */
  587. const char *
  588. p_rcode(int rcode) {
  589. return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
  590. }
  591. libresolv_hidden_def (p_rcode)
  592. /*
  593. * routines to convert between on-the-wire RR format and zone file format.
  594. * Does not contain conversion to/from decimal degrees; divide or multiply
  595. * by 60*60*1000 for that.
  596. */
  597. static const unsigned int poweroften[10]=
  598. { 1, 10, 100, 1000, 10000, 100000,
  599. 1000000,10000000,100000000,1000000000};
  600. /* takes an XeY precision/size value, returns a string representation. */
  601. static const char *
  602. precsize_ntoa (uint8_t prec)
  603. {
  604. static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
  605. unsigned long val;
  606. int mantissa, exponent;
  607. mantissa = (int)((prec >> 4) & 0x0f) % 10;
  608. exponent = (int)((prec >> 0) & 0x0f) % 10;
  609. val = mantissa * poweroften[exponent];
  610. (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
  611. return (retbuf);
  612. }
  613. /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
  614. static uint8_t
  615. precsize_aton (const char **strptr)
  616. {
  617. unsigned int mval = 0, cmval = 0;
  618. uint8_t retval = 0;
  619. const char *cp;
  620. int exponent;
  621. int mantissa;
  622. cp = *strptr;
  623. while (isdigit(*cp))
  624. mval = mval * 10 + (*cp++ - '0');
  625. if (*cp == '.') { /* centimeters */
  626. cp++;
  627. if (isdigit(*cp)) {
  628. cmval = (*cp++ - '0') * 10;
  629. if (isdigit(*cp)) {
  630. cmval += (*cp++ - '0');
  631. }
  632. }
  633. }
  634. cmval = (mval * 100) + cmval;
  635. for (exponent = 0; exponent < 9; exponent++)
  636. if (cmval < poweroften[exponent+1])
  637. break;
  638. mantissa = cmval / poweroften[exponent];
  639. if (mantissa > 9)
  640. mantissa = 9;
  641. retval = (mantissa << 4) | exponent;
  642. *strptr = cp;
  643. return (retval);
  644. }
  645. /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
  646. static uint32_t
  647. latlon2ul (const char **latlonstrptr, int *which)
  648. {
  649. const char *cp;
  650. uint32_t retval;
  651. int deg = 0, min = 0, secs = 0, secsfrac = 0;
  652. cp = *latlonstrptr;
  653. while (isdigit(*cp))
  654. deg = deg * 10 + (*cp++ - '0');
  655. while (isspace(*cp))
  656. cp++;
  657. if (!(isdigit(*cp)))
  658. goto fndhemi;
  659. while (isdigit(*cp))
  660. min = min * 10 + (*cp++ - '0');
  661. while (isspace(*cp))
  662. cp++;
  663. if (!(isdigit(*cp)))
  664. goto fndhemi;
  665. while (isdigit(*cp))
  666. secs = secs * 10 + (*cp++ - '0');
  667. if (*cp == '.') { /* decimal seconds */
  668. cp++;
  669. if (isdigit(*cp)) {
  670. secsfrac = (*cp++ - '0') * 100;
  671. if (isdigit(*cp)) {
  672. secsfrac += (*cp++ - '0') * 10;
  673. if (isdigit(*cp)) {
  674. secsfrac += (*cp++ - '0');
  675. }
  676. }
  677. }
  678. }
  679. while (!isspace(*cp)) /* if any trailing garbage */
  680. cp++;
  681. while (isspace(*cp))
  682. cp++;
  683. fndhemi:
  684. switch (*cp) {
  685. case 'N': case 'n':
  686. case 'E': case 'e':
  687. retval = ((unsigned)1<<31)
  688. + (((((deg * 60) + min) * 60) + secs) * 1000)
  689. + secsfrac;
  690. break;
  691. case 'S': case 's':
  692. case 'W': case 'w':
  693. retval = ((unsigned)1<<31)
  694. - (((((deg * 60) + min) * 60) + secs) * 1000)
  695. - secsfrac;
  696. break;
  697. default:
  698. retval = 0; /* invalid value -- indicates error */
  699. break;
  700. }
  701. switch (*cp) {
  702. case 'N': case 'n':
  703. case 'S': case 's':
  704. *which = 1; /* latitude */
  705. break;
  706. case 'E': case 'e':
  707. case 'W': case 'w':
  708. *which = 2; /* longitude */
  709. break;
  710. default:
  711. *which = 0; /* error */
  712. break;
  713. }
  714. cp++; /* skip the hemisphere */
  715. while (!isspace(*cp)) /* if any trailing garbage */
  716. cp++;
  717. while (isspace(*cp)) /* move to next field */
  718. cp++;
  719. *latlonstrptr = cp;
  720. return (retval);
  721. }
  722. /* converts a zone file representation in a string to an RDATA on-the-wire
  723. * representation. */
  724. int
  725. loc_aton (const char *ascii, u_char *binary)
  726. {
  727. const char *cp, *maxcp;
  728. u_char *bcp;
  729. uint32_t latit = 0, longit = 0, alt = 0;
  730. uint32_t lltemp1 = 0, lltemp2 = 0;
  731. int altmeters = 0, altfrac = 0, altsign = 1;
  732. uint8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
  733. uint8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
  734. uint8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
  735. int which1 = 0, which2 = 0;
  736. cp = ascii;
  737. maxcp = cp + strlen(ascii);
  738. lltemp1 = latlon2ul(&cp, &which1);
  739. lltemp2 = latlon2ul(&cp, &which2);
  740. switch (which1 + which2) {
  741. case 3: /* 1 + 2, the only valid combination */
  742. if ((which1 == 1) && (which2 == 2)) { /* normal case */
  743. latit = lltemp1;
  744. longit = lltemp2;
  745. } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
  746. longit = lltemp1;
  747. latit = lltemp2;
  748. } else { /* some kind of brokenness */
  749. return (0);
  750. }
  751. break;
  752. default: /* we didn't get one of each */
  753. return (0);
  754. }
  755. /* altitude */
  756. if (*cp == '-') {
  757. altsign = -1;
  758. cp++;
  759. }
  760. if (*cp == '+')
  761. cp++;
  762. while (isdigit(*cp))
  763. altmeters = altmeters * 10 + (*cp++ - '0');
  764. if (*cp == '.') { /* decimal meters */
  765. cp++;
  766. if (isdigit(*cp)) {
  767. altfrac = (*cp++ - '0') * 10;
  768. if (isdigit(*cp)) {
  769. altfrac += (*cp++ - '0');
  770. }
  771. }
  772. }
  773. alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
  774. while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
  775. cp++;
  776. while (isspace(*cp) && (cp < maxcp))
  777. cp++;
  778. if (cp >= maxcp)
  779. goto defaults;
  780. siz = precsize_aton(&cp);
  781. while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
  782. cp++;
  783. while (isspace(*cp) && (cp < maxcp))
  784. cp++;
  785. if (cp >= maxcp)
  786. goto defaults;
  787. hp = precsize_aton(&cp);
  788. while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
  789. cp++;
  790. while (isspace(*cp) && (cp < maxcp))
  791. cp++;
  792. if (cp >= maxcp)
  793. goto defaults;
  794. vp = precsize_aton(&cp);
  795. defaults:
  796. bcp = binary;
  797. *bcp++ = (uint8_t) 0; /* version byte */
  798. *bcp++ = siz;
  799. *bcp++ = hp;
  800. *bcp++ = vp;
  801. PUTLONG(latit,bcp);
  802. PUTLONG(longit,bcp);
  803. PUTLONG(alt,bcp);
  804. return (16); /* size of RR in octets */
  805. }
  806. /* takes an on-the-wire LOC RR and formats it in a human readable format. */
  807. const char *
  808. loc_ntoa (const u_char *binary, char *ascii)
  809. {
  810. static const char error[] = "?";
  811. static char tmpbuf[sizeof
  812. "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
  813. const u_char *cp = binary;
  814. int latdeg, latmin, latsec, latsecfrac;
  815. int longdeg, longmin, longsec, longsecfrac;
  816. char northsouth, eastwest;
  817. int altmeters, altfrac, altsign;
  818. const uint32_t referencealt = 100000 * 100;
  819. int32_t latval, longval, altval;
  820. uint32_t templ;
  821. uint8_t sizeval, hpval, vpval, versionval;
  822. char *sizestr, *hpstr, *vpstr;
  823. versionval = *cp++;
  824. if (ascii == NULL)
  825. ascii = tmpbuf;
  826. if (versionval) {
  827. (void) sprintf(ascii, "; error: unknown LOC RR version");
  828. return (ascii);
  829. }
  830. sizeval = *cp++;
  831. hpval = *cp++;
  832. vpval = *cp++;
  833. GETLONG(templ, cp);
  834. latval = (templ - ((unsigned)1<<31));
  835. GETLONG(templ, cp);
  836. longval = (templ - ((unsigned)1<<31));
  837. GETLONG(templ, cp);
  838. if (templ < referencealt) { /* below WGS 84 spheroid */
  839. altval = referencealt - templ;
  840. altsign = -1;
  841. } else {
  842. altval = templ - referencealt;
  843. altsign = 1;
  844. }
  845. if (latval < 0) {
  846. northsouth = 'S';
  847. latval = -latval;
  848. } else
  849. northsouth = 'N';
  850. latsecfrac = latval % 1000;
  851. latval = latval / 1000;
  852. latsec = latval % 60;
  853. latval = latval / 60;
  854. latmin = latval % 60;
  855. latval = latval / 60;
  856. latdeg = latval;
  857. if (longval < 0) {
  858. eastwest = 'W';
  859. longval = -longval;
  860. } else
  861. eastwest = 'E';
  862. longsecfrac = longval % 1000;
  863. longval = longval / 1000;
  864. longsec = longval % 60;
  865. longval = longval / 60;
  866. longmin = longval % 60;
  867. longval = longval / 60;
  868. longdeg = longval;
  869. altfrac = altval % 100;
  870. altmeters = (altval / 100) * altsign;
  871. if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
  872. sizestr = (char *) error;
  873. if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
  874. hpstr = (char *) error;
  875. if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
  876. vpstr = (char *) error;
  877. sprintf(ascii,
  878. "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
  879. latdeg, latmin, latsec, latsecfrac, northsouth,
  880. longdeg, longmin, longsec, longsecfrac, eastwest,
  881. altmeters, altfrac, sizestr, hpstr, vpstr);
  882. if (sizestr != (char *) error)
  883. free(sizestr);
  884. if (hpstr != (char *) error)
  885. free(hpstr);
  886. if (vpstr != (char *) error)
  887. free(vpstr);
  888. return (ascii);
  889. }
  890. libresolv_hidden_def (loc_ntoa)
  891. /* Return the number of DNS hierarchy levels in the name. */
  892. int
  893. dn_count_labels(const char *name) {
  894. int i, len, count;
  895. len = strlen(name);
  896. for (i = 0, count = 0; i < len; i++) {
  897. /* XXX need to check for \. or use named's nlabels(). */
  898. if (name[i] == '.')
  899. count++;
  900. }
  901. /* don't count initial wildcard */
  902. if (name[0] == '*')
  903. if (count)
  904. count--;
  905. /* don't count the null label for root. */
  906. /* if terminating '.' not found, must adjust */
  907. /* count to include last label */
  908. if (len > 0 && name[len-1] != '.')
  909. count++;
  910. return (count);
  911. }
  912. libresolv_hidden_def (__dn_count_labels)
  913. #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_27)
  914. /*
  915. * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
  916. * SIG records are required to be printed like this, by the Secure DNS RFC.
  917. * This is an obsolescent function and does not handle dates outside the
  918. * signed 32-bit range.
  919. */
  920. char *
  921. __p_secstodate (u_long secs) {
  922. /* XXX nonreentrant */
  923. static char output[15]; /* YYYYMMDDHHMMSS and null */
  924. time_t clock = secs;
  925. struct tm *time;
  926. struct tm timebuf;
  927. /* The call to __gmtime_r can never produce a year overflowing
  928. the range of int, given the check on SECS, but check for a
  929. NULL return anyway to avoid a null pointer dereference in
  930. case there are any other unspecified errors. */
  931. if (secs > 0x7fffffff
  932. || (time = __gmtime_r (&clock, &timebuf)) == NULL) {
  933. strcpy (output, "<overflow>");
  934. __set_errno (EOVERFLOW);
  935. return output;
  936. }
  937. time->tm_year += 1900;
  938. time->tm_mon += 1;
  939. /* The struct tm fields, given the above range check,
  940. must have values that mean this sprintf exactly fills the
  941. buffer. But as of GCC 8 of 2017-11-21, GCC cannot tell
  942. that, even given range checks on all fields with
  943. __builtin_unreachable called for out-of-range values. */
  944. DIAG_PUSH_NEEDS_COMMENT;
  945. # if __GNUC_PREREQ (7, 0)
  946. DIAG_IGNORE_NEEDS_COMMENT (8, "-Wformat-overflow=");
  947. # endif
  948. sprintf(output, "%04d%02d%02d%02d%02d%02d",
  949. time->tm_year, time->tm_mon, time->tm_mday,
  950. time->tm_hour, time->tm_min, time->tm_sec);
  951. DIAG_POP_NEEDS_COMMENT;
  952. return (output);
  953. }
  954. compat_symbol (libresolv, __p_secstodate, __p_secstodate, GLIBC_2_0);
  955. #endif