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.

155 lines
4.7 KiB

  1. /* udns_rr_srv.c
  2. parse/query SRV IN (rfc2782) 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. Copyright 2005 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
  18. 2005-09-11:
  19. Changed MX parser file into a SRV parser file
  20. */
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <assert.h>
  24. #include "udns.h"
  25. int
  26. dns_parse_srv(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
  27. void **result) {
  28. struct dns_rr_srv *ret;
  29. struct dns_parse p;
  30. struct dns_rr rr;
  31. int r, l;
  32. char *sp;
  33. dnsc_t srv[DNS_MAXDN];
  34. assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_SRV);
  35. /* first, validate the answer and count size of the result */
  36. l = 0;
  37. dns_initparse(&p, qdn, pkt, cur, end);
  38. while((r = dns_nextrr(&p, &rr)) > 0) {
  39. cur = rr.dnsrr_dptr + 6;
  40. r = dns_getdn(pkt, &cur, end, srv, sizeof(srv));
  41. if (r <= 0 || cur != rr.dnsrr_dend)
  42. return DNS_E_PROTOCOL;
  43. l += dns_dntop_size(srv);
  44. }
  45. if (r < 0)
  46. return DNS_E_PROTOCOL;
  47. if (!p.dnsp_nrr)
  48. return DNS_E_NODATA;
  49. /* next, allocate and set up result */
  50. l += dns_stdrr_size(&p);
  51. ret = malloc(sizeof(*ret) + sizeof(struct dns_srv) * p.dnsp_nrr + l);
  52. if (!ret)
  53. return DNS_E_NOMEM;
  54. ret->dnssrv_nrr = p.dnsp_nrr;
  55. ret->dnssrv_srv = (struct dns_srv *)(ret+1);
  56. /* and 3rd, fill in result, finally */
  57. sp = (char*)(ret->dnssrv_srv + p.dnsp_nrr);
  58. for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r) {
  59. ret->dnssrv_srv[r].name = sp;
  60. cur = rr.dnsrr_dptr;
  61. ret->dnssrv_srv[r].priority = dns_get16(cur);
  62. ret->dnssrv_srv[r].weight = dns_get16(cur+2);
  63. ret->dnssrv_srv[r].port = dns_get16(cur+4);
  64. cur += 6;
  65. dns_getdn(pkt, &cur, end, srv, sizeof(srv));
  66. sp += dns_dntop(srv, sp, DNS_MAXNAME);
  67. }
  68. dns_stdrr_finish((struct dns_rr_null *)ret, sp, &p);
  69. *result = ret;
  70. return 0;
  71. }
  72. /* Add a single service or proto name prepending an undescore (_),
  73. * according to rfc2782 rules.
  74. * Return 0 or the label length.
  75. * Routing assumes dn holds enouth space for a single DN label. */
  76. static int add_sname(dnsc_t *dn, const char *sn) {
  77. int l = dns_ptodn(sn, 0, dn + 1, DNS_MAXLABEL-1, NULL);
  78. if (l <= 1 || l - 2 != dn[1])
  79. /* Should we really check if sn is exactly one label? Do we care? */
  80. return 0;
  81. dn[0] = l - 1;
  82. dn[1] = '_';
  83. return l;
  84. }
  85. /* Construct a domain name for SRV query from the given name, service and proto.
  86. * The code allows any combinations of srv and proto (both are non-NULL,
  87. * both NULL, or either one is non-NULL). Whenever it makes any sense or not
  88. * is left as an exercise to programmer.
  89. * Return negative value on error (malformed query) or addition query flag(s).
  90. */
  91. static int
  92. build_srv_dn(dnsc_t *dn, const char *name, const char *srv, const char *proto)
  93. {
  94. int p = 0, l, isabs;
  95. if (srv) {
  96. l = add_sname(dn + p, srv);
  97. if (!l)
  98. return -1;
  99. p += l;
  100. }
  101. if (proto) {
  102. l = add_sname(dn + p, proto);
  103. if (!l)
  104. return -1;
  105. p += l;
  106. }
  107. l = dns_ptodn(name, 0, dn + p, DNS_MAXDN - p, &isabs);
  108. if (l < 0)
  109. return -1;
  110. return isabs ? DNS_NOSRCH : 0;
  111. }
  112. struct dns_query *
  113. dns_submit_srv(struct dns_ctx *ctx,
  114. const char *name, const char *srv, const char *proto,
  115. int flags, dns_query_srv_fn *cbck, void *data) {
  116. dnsc_t dn[DNS_MAXDN];
  117. int r = build_srv_dn(dn, name, srv, proto);
  118. if (r < 0) {
  119. dns_setstatus (ctx, DNS_E_BADQUERY);
  120. return NULL;
  121. }
  122. return
  123. dns_submit_dn(ctx, dn, DNS_C_IN, DNS_T_SRV, flags | r,
  124. dns_parse_srv, (dns_query_fn *)cbck, data);
  125. }
  126. struct dns_rr_srv *
  127. dns_resolve_srv(struct dns_ctx *ctx,
  128. const char *name, const char *srv, const char *proto, int flags)
  129. {
  130. dnsc_t dn[DNS_MAXDN];
  131. int r = build_srv_dn(dn, name, srv, proto);
  132. if (r < 0) {
  133. dns_setstatus(ctx, DNS_E_BADQUERY);
  134. return NULL;
  135. }
  136. return (struct dns_rr_srv *)
  137. dns_resolve_dn(ctx, dn, DNS_C_IN, DNS_T_SRV, flags | r, dns_parse_srv);
  138. }