/contrib/bind9/lib/dns/rdata/generic/opt_41.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 285 lines · 194 code · 59 blank · 32 comment · 44 complexity · 1cad7b7707fbe200f3b496fcf20ea9bc MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1998-2002 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$ */
  18. /* Reviewed: Thu Mar 16 14:06:44 PST 2000 by gson */
  19. /* RFC2671 */
  20. #ifndef RDATA_GENERIC_OPT_41_C
  21. #define RDATA_GENERIC_OPT_41_C
  22. #define RRTYPE_OPT_ATTRIBUTES (DNS_RDATATYPEATTR_SINGLETON | \
  23. DNS_RDATATYPEATTR_META | \
  24. DNS_RDATATYPEATTR_NOTQUESTION)
  25. static inline isc_result_t
  26. fromtext_opt(ARGS_FROMTEXT) {
  27. /*
  28. * OPT records do not have a text format.
  29. */
  30. REQUIRE(type == 41);
  31. UNUSED(type);
  32. UNUSED(rdclass);
  33. UNUSED(lexer);
  34. UNUSED(origin);
  35. UNUSED(options);
  36. UNUSED(target);
  37. UNUSED(callbacks);
  38. return (ISC_R_NOTIMPLEMENTED);
  39. }
  40. static inline isc_result_t
  41. totext_opt(ARGS_TOTEXT) {
  42. isc_region_t r;
  43. isc_region_t or;
  44. isc_uint16_t option;
  45. isc_uint16_t length;
  46. char buf[sizeof("64000 64000")];
  47. /*
  48. * OPT records do not have a text format.
  49. */
  50. REQUIRE(rdata->type == 41);
  51. dns_rdata_toregion(rdata, &r);
  52. while (r.length > 0) {
  53. option = uint16_fromregion(&r);
  54. isc_region_consume(&r, 2);
  55. length = uint16_fromregion(&r);
  56. isc_region_consume(&r, 2);
  57. sprintf(buf, "%u %u", option, length);
  58. RETERR(str_totext(buf, target));
  59. INSIST(r.length >= length);
  60. if (length > 0) {
  61. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  62. RETERR(str_totext(" (", target));
  63. RETERR(str_totext(tctx->linebreak, target));
  64. or = r;
  65. or.length = length;
  66. RETERR(isc_base64_totext(&or, tctx->width - 2,
  67. tctx->linebreak, target));
  68. isc_region_consume(&r, length);
  69. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  70. RETERR(str_totext(" )", target));
  71. }
  72. if (r.length > 0)
  73. RETERR(str_totext(" ", target));
  74. }
  75. return (ISC_R_SUCCESS);
  76. }
  77. static inline isc_result_t
  78. fromwire_opt(ARGS_FROMWIRE) {
  79. isc_region_t sregion;
  80. isc_region_t tregion;
  81. isc_uint16_t length;
  82. unsigned int total;
  83. REQUIRE(type == 41);
  84. UNUSED(type);
  85. UNUSED(rdclass);
  86. UNUSED(dctx);
  87. UNUSED(options);
  88. isc_buffer_activeregion(source, &sregion);
  89. total = 0;
  90. while (sregion.length != 0) {
  91. if (sregion.length < 4)
  92. return (ISC_R_UNEXPECTEDEND);
  93. /*
  94. * Eat the 16bit option code. There is nothing to
  95. * be done with it currently.
  96. */
  97. isc_region_consume(&sregion, 2);
  98. length = uint16_fromregion(&sregion);
  99. isc_region_consume(&sregion, 2);
  100. total += 4;
  101. if (sregion.length < length)
  102. return (ISC_R_UNEXPECTEDEND);
  103. isc_region_consume(&sregion, length);
  104. total += length;
  105. }
  106. isc_buffer_activeregion(source, &sregion);
  107. isc_buffer_availableregion(target, &tregion);
  108. if (tregion.length < total)
  109. return (ISC_R_NOSPACE);
  110. memcpy(tregion.base, sregion.base, total);
  111. isc_buffer_forward(source, total);
  112. isc_buffer_add(target, total);
  113. return (ISC_R_SUCCESS);
  114. }
  115. static inline isc_result_t
  116. towire_opt(ARGS_TOWIRE) {
  117. REQUIRE(rdata->type == 41);
  118. UNUSED(cctx);
  119. return (mem_tobuffer(target, rdata->data, rdata->length));
  120. }
  121. static inline int
  122. compare_opt(ARGS_COMPARE) {
  123. isc_region_t r1;
  124. isc_region_t r2;
  125. REQUIRE(rdata1->type == rdata2->type);
  126. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  127. REQUIRE(rdata1->type == 41);
  128. dns_rdata_toregion(rdata1, &r1);
  129. dns_rdata_toregion(rdata2, &r2);
  130. return (isc_region_compare(&r1, &r2));
  131. }
  132. static inline isc_result_t
  133. fromstruct_opt(ARGS_FROMSTRUCT) {
  134. dns_rdata_opt_t *opt = source;
  135. isc_region_t region;
  136. isc_uint16_t length;
  137. REQUIRE(type == 41);
  138. REQUIRE(source != NULL);
  139. REQUIRE(opt->common.rdtype == type);
  140. REQUIRE(opt->common.rdclass == rdclass);
  141. REQUIRE(opt->options != NULL || opt->length == 0);
  142. UNUSED(type);
  143. UNUSED(rdclass);
  144. region.base = opt->options;
  145. region.length = opt->length;
  146. while (region.length >= 4) {
  147. isc_region_consume(&region, 2); /* opt */
  148. length = uint16_fromregion(&region);
  149. isc_region_consume(&region, 2);
  150. if (region.length < length)
  151. return (ISC_R_UNEXPECTEDEND);
  152. isc_region_consume(&region, length);
  153. }
  154. if (region.length != 0)
  155. return (ISC_R_UNEXPECTEDEND);
  156. return (mem_tobuffer(target, opt->options, opt->length));
  157. }
  158. static inline isc_result_t
  159. tostruct_opt(ARGS_TOSTRUCT) {
  160. dns_rdata_opt_t *opt = target;
  161. isc_region_t r;
  162. REQUIRE(rdata->type == 41);
  163. REQUIRE(target != NULL);
  164. opt->common.rdclass = rdata->rdclass;
  165. opt->common.rdtype = rdata->type;
  166. ISC_LINK_INIT(&opt->common, link);
  167. dns_rdata_toregion(rdata, &r);
  168. opt->length = r.length;
  169. opt->options = mem_maybedup(mctx, r.base, r.length);
  170. if (opt->options == NULL)
  171. return (ISC_R_NOMEMORY);
  172. opt->offset = 0;
  173. opt->mctx = mctx;
  174. return (ISC_R_SUCCESS);
  175. }
  176. static inline void
  177. freestruct_opt(ARGS_FREESTRUCT) {
  178. dns_rdata_opt_t *opt = source;
  179. REQUIRE(source != NULL);
  180. REQUIRE(opt->common.rdtype == 41);
  181. if (opt->mctx == NULL)
  182. return;
  183. if (opt->options != NULL)
  184. isc_mem_free(opt->mctx, opt->options);
  185. opt->mctx = NULL;
  186. }
  187. static inline isc_result_t
  188. additionaldata_opt(ARGS_ADDLDATA) {
  189. REQUIRE(rdata->type == 41);
  190. UNUSED(rdata);
  191. UNUSED(add);
  192. UNUSED(arg);
  193. return (ISC_R_SUCCESS);
  194. }
  195. static inline isc_result_t
  196. digest_opt(ARGS_DIGEST) {
  197. /*
  198. * OPT records are not digested.
  199. */
  200. REQUIRE(rdata->type == 41);
  201. UNUSED(rdata);
  202. UNUSED(digest);
  203. UNUSED(arg);
  204. return (ISC_R_NOTIMPLEMENTED);
  205. }
  206. static inline isc_boolean_t
  207. checkowner_opt(ARGS_CHECKOWNER) {
  208. REQUIRE(type == 41);
  209. UNUSED(type);
  210. UNUSED(rdclass);
  211. UNUSED(wildcard);
  212. return (dns_name_equal(name, dns_rootname));
  213. }
  214. static inline isc_boolean_t
  215. checknames_opt(ARGS_CHECKNAMES) {
  216. REQUIRE(rdata->type == 41);
  217. UNUSED(rdata);
  218. UNUSED(owner);
  219. UNUSED(bad);
  220. return (ISC_TRUE);
  221. }
  222. static inline int
  223. casecompare_opt(ARGS_COMPARE) {
  224. return (compare_opt(rdata1, rdata2));
  225. }
  226. #endif /* RDATA_GENERIC_OPT_41_C */