/contrib/bind9/lib/dns/byaddr.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 317 lines · 230 code · 53 blank · 34 comment · 42 complexity · b429fbe86ef78054405946d583cabf01 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: byaddr.c,v 1.41 2009/09/02 23:48:02 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <isc/mem.h>
  21. #include <isc/netaddr.h>
  22. #include <isc/print.h>
  23. #include <isc/string.h> /* Required for HP/UX (and others?) */
  24. #include <isc/task.h>
  25. #include <isc/util.h>
  26. #include <dns/byaddr.h>
  27. #include <dns/db.h>
  28. #include <dns/events.h>
  29. #include <dns/lookup.h>
  30. #include <dns/rdata.h>
  31. #include <dns/rdataset.h>
  32. #include <dns/rdatastruct.h>
  33. #include <dns/resolver.h>
  34. #include <dns/result.h>
  35. #include <dns/view.h>
  36. /*
  37. * XXXRTH We could use a static event...
  38. */
  39. static char hex_digits[] = {
  40. '0', '1', '2', '3', '4', '5', '6', '7',
  41. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  42. };
  43. isc_result_t
  44. dns_byaddr_createptrname(isc_netaddr_t *address, isc_boolean_t nibble,
  45. dns_name_t *name)
  46. {
  47. /*
  48. * We dropped bitstring labels, so all lookups will use nibbles.
  49. */
  50. UNUSED(nibble);
  51. return (dns_byaddr_createptrname2(address,
  52. DNS_BYADDROPT_IPV6INT, name));
  53. }
  54. isc_result_t
  55. dns_byaddr_createptrname2(isc_netaddr_t *address, unsigned int options,
  56. dns_name_t *name)
  57. {
  58. char textname[128];
  59. unsigned char *bytes;
  60. int i;
  61. char *cp;
  62. isc_buffer_t buffer;
  63. unsigned int len;
  64. REQUIRE(address != NULL);
  65. /*
  66. * We create the text representation and then convert to a
  67. * dns_name_t. This is not maximally efficient, but it keeps all
  68. * of the knowledge of wire format in the dns_name_ routines.
  69. */
  70. bytes = (unsigned char *)(&address->type);
  71. if (address->family == AF_INET) {
  72. (void)snprintf(textname, sizeof(textname),
  73. "%u.%u.%u.%u.in-addr.arpa.",
  74. (bytes[3] & 0xff),
  75. (bytes[2] & 0xff),
  76. (bytes[1] & 0xff),
  77. (bytes[0] & 0xff));
  78. } else if (address->family == AF_INET6) {
  79. cp = textname;
  80. for (i = 15; i >= 0; i--) {
  81. *cp++ = hex_digits[bytes[i] & 0x0f];
  82. *cp++ = '.';
  83. *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
  84. *cp++ = '.';
  85. }
  86. if ((options & DNS_BYADDROPT_IPV6INT) != 0)
  87. strcpy(cp, "ip6.int.");
  88. else
  89. strcpy(cp, "ip6.arpa.");
  90. } else
  91. return (ISC_R_NOTIMPLEMENTED);
  92. len = (unsigned int)strlen(textname);
  93. isc_buffer_init(&buffer, textname, len);
  94. isc_buffer_add(&buffer, len);
  95. return (dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL));
  96. }
  97. #ifdef BIND9
  98. struct dns_byaddr {
  99. /* Unlocked. */
  100. unsigned int magic;
  101. isc_mem_t * mctx;
  102. isc_mutex_t lock;
  103. dns_fixedname_t name;
  104. /* Locked by lock. */
  105. unsigned int options;
  106. dns_lookup_t * lookup;
  107. isc_task_t * task;
  108. dns_byaddrevent_t * event;
  109. isc_boolean_t canceled;
  110. };
  111. #define BYADDR_MAGIC ISC_MAGIC('B', 'y', 'A', 'd')
  112. #define VALID_BYADDR(b) ISC_MAGIC_VALID(b, BYADDR_MAGIC)
  113. #define MAX_RESTARTS 16
  114. static inline isc_result_t
  115. copy_ptr_targets(dns_byaddr_t *byaddr, dns_rdataset_t *rdataset) {
  116. isc_result_t result;
  117. dns_name_t *name;
  118. dns_rdata_t rdata = DNS_RDATA_INIT;
  119. /*
  120. * The caller must be holding the byaddr's lock.
  121. */
  122. result = dns_rdataset_first(rdataset);
  123. while (result == ISC_R_SUCCESS) {
  124. dns_rdata_ptr_t ptr;
  125. dns_rdataset_current(rdataset, &rdata);
  126. result = dns_rdata_tostruct(&rdata, &ptr, NULL);
  127. if (result != ISC_R_SUCCESS)
  128. return (result);
  129. name = isc_mem_get(byaddr->mctx, sizeof(*name));
  130. if (name == NULL) {
  131. dns_rdata_freestruct(&ptr);
  132. return (ISC_R_NOMEMORY);
  133. }
  134. dns_name_init(name, NULL);
  135. result = dns_name_dup(&ptr.ptr, byaddr->mctx, name);
  136. dns_rdata_freestruct(&ptr);
  137. if (result != ISC_R_SUCCESS) {
  138. isc_mem_put(byaddr->mctx, name, sizeof(*name));
  139. return (ISC_R_NOMEMORY);
  140. }
  141. ISC_LIST_APPEND(byaddr->event->names, name, link);
  142. dns_rdata_reset(&rdata);
  143. result = dns_rdataset_next(rdataset);
  144. }
  145. if (result == ISC_R_NOMORE)
  146. result = ISC_R_SUCCESS;
  147. return (result);
  148. }
  149. static void
  150. lookup_done(isc_task_t *task, isc_event_t *event) {
  151. dns_byaddr_t *byaddr = event->ev_arg;
  152. dns_lookupevent_t *levent;
  153. isc_result_t result;
  154. REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
  155. REQUIRE(VALID_BYADDR(byaddr));
  156. REQUIRE(byaddr->task == task);
  157. UNUSED(task);
  158. levent = (dns_lookupevent_t *)event;
  159. if (levent->result == ISC_R_SUCCESS) {
  160. result = copy_ptr_targets(byaddr, levent->rdataset);
  161. byaddr->event->result = result;
  162. } else
  163. byaddr->event->result = levent->result;
  164. isc_event_free(&event);
  165. isc_task_sendanddetach(&byaddr->task, (isc_event_t **)&byaddr->event);
  166. }
  167. static void
  168. bevent_destroy(isc_event_t *event) {
  169. dns_byaddrevent_t *bevent;
  170. dns_name_t *name, *next_name;
  171. isc_mem_t *mctx;
  172. REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
  173. mctx = event->ev_destroy_arg;
  174. bevent = (dns_byaddrevent_t *)event;
  175. for (name = ISC_LIST_HEAD(bevent->names);
  176. name != NULL;
  177. name = next_name) {
  178. next_name = ISC_LIST_NEXT(name, link);
  179. ISC_LIST_UNLINK(bevent->names, name, link);
  180. dns_name_free(name, mctx);
  181. isc_mem_put(mctx, name, sizeof(*name));
  182. }
  183. isc_mem_put(mctx, event, event->ev_size);
  184. }
  185. isc_result_t
  186. dns_byaddr_create(isc_mem_t *mctx, isc_netaddr_t *address, dns_view_t *view,
  187. unsigned int options, isc_task_t *task,
  188. isc_taskaction_t action, void *arg, dns_byaddr_t **byaddrp)
  189. {
  190. isc_result_t result;
  191. dns_byaddr_t *byaddr;
  192. isc_event_t *ievent;
  193. byaddr = isc_mem_get(mctx, sizeof(*byaddr));
  194. if (byaddr == NULL)
  195. return (ISC_R_NOMEMORY);
  196. byaddr->mctx = mctx;
  197. byaddr->options = options;
  198. byaddr->event = isc_mem_get(mctx, sizeof(*byaddr->event));
  199. if (byaddr->event == NULL) {
  200. result = ISC_R_NOMEMORY;
  201. goto cleanup_byaddr;
  202. }
  203. ISC_EVENT_INIT(byaddr->event, sizeof(*byaddr->event), 0, NULL,
  204. DNS_EVENT_BYADDRDONE, action, arg, byaddr,
  205. bevent_destroy, mctx);
  206. byaddr->event->result = ISC_R_FAILURE;
  207. ISC_LIST_INIT(byaddr->event->names);
  208. byaddr->task = NULL;
  209. isc_task_attach(task, &byaddr->task);
  210. result = isc_mutex_init(&byaddr->lock);
  211. if (result != ISC_R_SUCCESS)
  212. goto cleanup_event;
  213. dns_fixedname_init(&byaddr->name);
  214. result = dns_byaddr_createptrname2(address, options,
  215. dns_fixedname_name(&byaddr->name));
  216. if (result != ISC_R_SUCCESS)
  217. goto cleanup_lock;
  218. byaddr->lookup = NULL;
  219. result = dns_lookup_create(mctx, dns_fixedname_name(&byaddr->name),
  220. dns_rdatatype_ptr, view, 0, task,
  221. lookup_done, byaddr, &byaddr->lookup);
  222. if (result != ISC_R_SUCCESS)
  223. goto cleanup_lock;
  224. byaddr->canceled = ISC_FALSE;
  225. byaddr->magic = BYADDR_MAGIC;
  226. *byaddrp = byaddr;
  227. return (ISC_R_SUCCESS);
  228. cleanup_lock:
  229. DESTROYLOCK(&byaddr->lock);
  230. cleanup_event:
  231. ievent = (isc_event_t *)byaddr->event;
  232. isc_event_free(&ievent);
  233. byaddr->event = NULL;
  234. isc_task_detach(&byaddr->task);
  235. cleanup_byaddr:
  236. isc_mem_put(mctx, byaddr, sizeof(*byaddr));
  237. return (result);
  238. }
  239. void
  240. dns_byaddr_cancel(dns_byaddr_t *byaddr) {
  241. REQUIRE(VALID_BYADDR(byaddr));
  242. LOCK(&byaddr->lock);
  243. if (!byaddr->canceled) {
  244. byaddr->canceled = ISC_TRUE;
  245. if (byaddr->lookup != NULL)
  246. dns_lookup_cancel(byaddr->lookup);
  247. }
  248. UNLOCK(&byaddr->lock);
  249. }
  250. void
  251. dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
  252. dns_byaddr_t *byaddr;
  253. REQUIRE(byaddrp != NULL);
  254. byaddr = *byaddrp;
  255. REQUIRE(VALID_BYADDR(byaddr));
  256. REQUIRE(byaddr->event == NULL);
  257. REQUIRE(byaddr->task == NULL);
  258. dns_lookup_destroy(&byaddr->lookup);
  259. DESTROYLOCK(&byaddr->lock);
  260. byaddr->magic = 0;
  261. isc_mem_put(byaddr->mctx, byaddr, sizeof(*byaddr));
  262. *byaddrp = NULL;
  263. }
  264. #endif /* BIND9 */