/contrib/bind9/lib/dns/portlist.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 266 lines · 217 code · 31 blank · 18 comment · 74 complexity · 8e9b0e6fb00e1669a00da25913577ca1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 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: portlist.c,v 1.13 2007/06/19 23:47:16 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <isc/magic.h>
  22. #include <isc/mem.h>
  23. #include <isc/mutex.h>
  24. #include <isc/net.h>
  25. #include <isc/refcount.h>
  26. #include <isc/result.h>
  27. #include <isc/string.h>
  28. #include <isc/types.h>
  29. #include <isc/util.h>
  30. #include <dns/types.h>
  31. #include <dns/portlist.h>
  32. #define DNS_PORTLIST_MAGIC ISC_MAGIC('P','L','S','T')
  33. #define DNS_VALID_PORTLIST(p) ISC_MAGIC_VALID(p, DNS_PORTLIST_MAGIC)
  34. typedef struct dns_element {
  35. in_port_t port;
  36. isc_uint16_t flags;
  37. } dns_element_t;
  38. struct dns_portlist {
  39. unsigned int magic;
  40. isc_mem_t *mctx;
  41. isc_refcount_t refcount;
  42. isc_mutex_t lock;
  43. dns_element_t *list;
  44. unsigned int allocated;
  45. unsigned int active;
  46. };
  47. #define DNS_PL_INET 0x0001
  48. #define DNS_PL_INET6 0x0002
  49. #define DNS_PL_ALLOCATE 16
  50. static int
  51. compare(const void *arg1, const void *arg2) {
  52. const dns_element_t *e1 = (const dns_element_t *)arg1;
  53. const dns_element_t *e2 = (const dns_element_t *)arg2;
  54. if (e1->port < e2->port)
  55. return (-1);
  56. if (e1->port > e2->port)
  57. return (1);
  58. return (0);
  59. }
  60. isc_result_t
  61. dns_portlist_create(isc_mem_t *mctx, dns_portlist_t **portlistp) {
  62. dns_portlist_t *portlist;
  63. isc_result_t result;
  64. REQUIRE(portlistp != NULL && *portlistp == NULL);
  65. portlist = isc_mem_get(mctx, sizeof(*portlist));
  66. if (portlist == NULL)
  67. return (ISC_R_NOMEMORY);
  68. result = isc_mutex_init(&portlist->lock);
  69. if (result != ISC_R_SUCCESS) {
  70. isc_mem_put(mctx, portlist, sizeof(*portlist));
  71. return (result);
  72. }
  73. result = isc_refcount_init(&portlist->refcount, 1);
  74. if (result != ISC_R_SUCCESS) {
  75. DESTROYLOCK(&portlist->lock);
  76. isc_mem_put(mctx, portlist, sizeof(*portlist));
  77. return (result);
  78. }
  79. portlist->list = NULL;
  80. portlist->allocated = 0;
  81. portlist->active = 0;
  82. portlist->mctx = NULL;
  83. isc_mem_attach(mctx, &portlist->mctx);
  84. portlist->magic = DNS_PORTLIST_MAGIC;
  85. *portlistp = portlist;
  86. return (ISC_R_SUCCESS);
  87. }
  88. static dns_element_t *
  89. find_port(dns_element_t *list, unsigned int len, in_port_t port) {
  90. unsigned int xtry = len / 2;
  91. unsigned int min = 0;
  92. unsigned int max = len - 1;
  93. unsigned int last = len;
  94. for (;;) {
  95. if (list[xtry].port == port)
  96. return (&list[xtry]);
  97. if (port > list[xtry].port) {
  98. if (xtry == max)
  99. break;
  100. min = xtry;
  101. xtry = xtry + (max - xtry + 1) / 2;
  102. INSIST(xtry <= max);
  103. if (xtry == last)
  104. break;
  105. last = min;
  106. } else {
  107. if (xtry == min)
  108. break;
  109. max = xtry;
  110. xtry = xtry - (xtry - min + 1) / 2;
  111. INSIST(xtry >= min);
  112. if (xtry == last)
  113. break;
  114. last = max;
  115. }
  116. }
  117. return (NULL);
  118. }
  119. isc_result_t
  120. dns_portlist_add(dns_portlist_t *portlist, int af, in_port_t port) {
  121. dns_element_t *el;
  122. isc_result_t result;
  123. REQUIRE(DNS_VALID_PORTLIST(portlist));
  124. REQUIRE(af == AF_INET || af == AF_INET6);
  125. LOCK(&portlist->lock);
  126. if (portlist->active != 0) {
  127. el = find_port(portlist->list, portlist->active, port);
  128. if (el != NULL) {
  129. if (af == AF_INET)
  130. el->flags |= DNS_PL_INET;
  131. else
  132. el->flags |= DNS_PL_INET6;
  133. result = ISC_R_SUCCESS;
  134. goto unlock;
  135. }
  136. }
  137. if (portlist->allocated <= portlist->active) {
  138. unsigned int allocated;
  139. allocated = portlist->allocated + DNS_PL_ALLOCATE;
  140. el = isc_mem_get(portlist->mctx, sizeof(*el) * allocated);
  141. if (el == NULL) {
  142. result = ISC_R_NOMEMORY;
  143. goto unlock;
  144. }
  145. if (portlist->list != NULL) {
  146. memcpy(el, portlist->list,
  147. portlist->allocated * sizeof(*el));
  148. isc_mem_put(portlist->mctx, portlist->list,
  149. portlist->allocated * sizeof(*el));
  150. }
  151. portlist->list = el;
  152. portlist->allocated = allocated;
  153. }
  154. portlist->list[portlist->active].port = port;
  155. if (af == AF_INET)
  156. portlist->list[portlist->active].flags = DNS_PL_INET;
  157. else
  158. portlist->list[portlist->active].flags = DNS_PL_INET6;
  159. portlist->active++;
  160. qsort(portlist->list, portlist->active, sizeof(*el), compare);
  161. result = ISC_R_SUCCESS;
  162. unlock:
  163. UNLOCK(&portlist->lock);
  164. return (result);
  165. }
  166. void
  167. dns_portlist_remove(dns_portlist_t *portlist, int af, in_port_t port) {
  168. dns_element_t *el;
  169. REQUIRE(DNS_VALID_PORTLIST(portlist));
  170. REQUIRE(af == AF_INET || af == AF_INET6);
  171. LOCK(&portlist->lock);
  172. if (portlist->active != 0) {
  173. el = find_port(portlist->list, portlist->active, port);
  174. if (el != NULL) {
  175. if (af == AF_INET)
  176. el->flags &= ~DNS_PL_INET;
  177. else
  178. el->flags &= ~DNS_PL_INET6;
  179. if (el->flags == 0) {
  180. *el = portlist->list[portlist->active];
  181. portlist->active--;
  182. qsort(portlist->list, portlist->active,
  183. sizeof(*el), compare);
  184. }
  185. }
  186. }
  187. UNLOCK(&portlist->lock);
  188. }
  189. isc_boolean_t
  190. dns_portlist_match(dns_portlist_t *portlist, int af, in_port_t port) {
  191. dns_element_t *el;
  192. isc_boolean_t result = ISC_FALSE;
  193. REQUIRE(DNS_VALID_PORTLIST(portlist));
  194. REQUIRE(af == AF_INET || af == AF_INET6);
  195. LOCK(&portlist->lock);
  196. if (portlist->active != 0) {
  197. el = find_port(portlist->list, portlist->active, port);
  198. if (el != NULL) {
  199. if (af == AF_INET && (el->flags & DNS_PL_INET) != 0)
  200. result = ISC_TRUE;
  201. if (af == AF_INET6 && (el->flags & DNS_PL_INET6) != 0)
  202. result = ISC_TRUE;
  203. }
  204. }
  205. UNLOCK(&portlist->lock);
  206. return (result);
  207. }
  208. void
  209. dns_portlist_attach(dns_portlist_t *portlist, dns_portlist_t **portlistp) {
  210. REQUIRE(DNS_VALID_PORTLIST(portlist));
  211. REQUIRE(portlistp != NULL && *portlistp == NULL);
  212. isc_refcount_increment(&portlist->refcount, NULL);
  213. *portlistp = portlist;
  214. }
  215. void
  216. dns_portlist_detach(dns_portlist_t **portlistp) {
  217. dns_portlist_t *portlist;
  218. unsigned int count;
  219. REQUIRE(portlistp != NULL);
  220. portlist = *portlistp;
  221. REQUIRE(DNS_VALID_PORTLIST(portlist));
  222. *portlistp = NULL;
  223. isc_refcount_decrement(&portlist->refcount, &count);
  224. if (count == 0) {
  225. portlist->magic = 0;
  226. isc_refcount_destroy(&portlist->refcount);
  227. if (portlist->list != NULL)
  228. isc_mem_put(portlist->mctx, portlist->list,
  229. portlist->allocated *
  230. sizeof(*portlist->list));
  231. DESTROYLOCK(&portlist->lock);
  232. isc_mem_putanddetach(&portlist->mctx, portlist,
  233. sizeof(*portlist));
  234. }
  235. }