You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.7 KiB

  1. /* udns_rr_a.c
  2. parse/query A/AAAA IN records
  3. Copyright (C) 2005 Michael Tokarev <mjt@corpit.ru>
  4. This file is part of UDNS library, an async DNS stub resolver.
  5. This 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.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library, in file named COPYING.LGPL; if not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place,
  16. Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <assert.h>
  21. #ifndef __MINGW32__
  22. # include <sys/types.h>
  23. # include <netinet/in.h>
  24. #endif
  25. #include "udns.h"
  26. /* here, we use common routine to parse both IPv4 and IPv6 addresses.
  27. */
  28. /* this structure should match dns_rr_a[46] */
  29. struct dns_rr_a {
  30. dns_rr_common(dnsa);
  31. unsigned char *dnsa_addr;
  32. };
  33. static int
  34. dns_parse_a(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
  35. void **result, unsigned dsize) {
  36. struct dns_rr_a *ret;
  37. struct dns_parse p;
  38. struct dns_rr rr;
  39. int r;
  40. /* first, validate and count number of addresses */
  41. dns_initparse(&p, qdn, pkt, cur, end);
  42. while((r = dns_nextrr(&p, &rr)) > 0)
  43. if (rr.dnsrr_dsz != dsize)
  44. return DNS_E_PROTOCOL;
  45. if (r < 0)
  46. return DNS_E_PROTOCOL;
  47. else if (!p.dnsp_nrr)
  48. return DNS_E_NODATA;
  49. ret = malloc(sizeof(*ret) + dsize * p.dnsp_nrr + dns_stdrr_size(&p));
  50. if (!ret)
  51. return DNS_E_NOMEM;
  52. ret->dnsa_nrr = p.dnsp_nrr;
  53. ret->dnsa_addr = (unsigned char*)(ret+1);
  54. /* copy the RRs */
  55. for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r)
  56. memcpy(ret->dnsa_addr + dsize * r, rr.dnsrr_dptr, dsize);
  57. dns_stdrr_finish((struct dns_rr_null *)ret,
  58. (char *)(ret->dnsa_addr + dsize * p.dnsp_nrr), &p);
  59. *result = ret;
  60. return 0;
  61. }
  62. int
  63. dns_parse_a4(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
  64. void **result) {
  65. #ifdef AF_INET
  66. assert(sizeof(struct in_addr) == 4);
  67. #endif
  68. assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_A);
  69. return dns_parse_a(qdn, pkt, cur, end, result, 4);
  70. }
  71. struct dns_query *
  72. dns_submit_a4(struct dns_ctx *ctx, const char *name, int flags,
  73. dns_query_a4_fn *cbck, void *data) {
  74. return
  75. dns_submit_p(ctx, name, DNS_C_IN, DNS_T_A, flags,
  76. dns_parse_a4, (dns_query_fn*)cbck, data);
  77. }
  78. struct dns_rr_a4 *
  79. dns_resolve_a4(struct dns_ctx *ctx, const char *name, int flags) {
  80. return (struct dns_rr_a4 *)
  81. dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_A, flags, dns_parse_a4);
  82. }
  83. int
  84. dns_parse_a6(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
  85. void **result) {
  86. #ifdef AF_INET6
  87. assert(sizeof(struct in6_addr) == 16);
  88. #endif
  89. assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_AAAA);
  90. return dns_parse_a(qdn, pkt, cur, end, result, 16);
  91. }
  92. struct dns_query *
  93. dns_submit_a6(struct dns_ctx *ctx, const char *name, int flags,
  94. dns_query_a6_fn *cbck, void *data) {
  95. return
  96. dns_submit_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags,
  97. dns_parse_a6, (dns_query_fn*)cbck, data);
  98. }
  99. struct dns_rr_a6 *
  100. dns_resolve_a6(struct dns_ctx *ctx, const char *name, int flags) {
  101. return (struct dns_rr_a6 *)
  102. dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags, dns_parse_a6);
  103. }