isns.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2014 Alexander Motin <mav@FreeBSD.org>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. */
  29. #include <sys/types.h>
  30. #include <sys/time.h>
  31. #include <sys/socket.h>
  32. #include <sys/wait.h>
  33. #include <sys/endian.h>
  34. #include <netinet/in.h>
  35. #include <arpa/inet.h>
  36. #include <netdb.h>
  37. #include <stdbool.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include "ctld.h"
  42. #include "isns.h"
  43. struct isns_req *
  44. isns_req_alloc(void)
  45. {
  46. struct isns_req *req;
  47. req = calloc(1, sizeof(struct isns_req));
  48. if (req == NULL) {
  49. log_err(1, "calloc");
  50. return (NULL);
  51. }
  52. req->ir_buflen = sizeof(struct isns_hdr);
  53. req->ir_usedlen = 0;
  54. req->ir_buf = calloc(1, req->ir_buflen);
  55. if (req->ir_buf == NULL) {
  56. free(req);
  57. log_err(1, "calloc");
  58. return (NULL);
  59. }
  60. return (req);
  61. }
  62. struct isns_req *
  63. isns_req_create(uint16_t func, uint16_t flags)
  64. {
  65. struct isns_req *req;
  66. struct isns_hdr *hdr;
  67. req = isns_req_alloc();
  68. req->ir_usedlen = sizeof(struct isns_hdr);
  69. hdr = (struct isns_hdr *)req->ir_buf;
  70. be16enc(hdr->ih_version, ISNS_VERSION);
  71. be16enc(hdr->ih_function, func);
  72. be16enc(hdr->ih_flags, flags);
  73. return (req);
  74. }
  75. void
  76. isns_req_free(struct isns_req *req)
  77. {
  78. free(req->ir_buf);
  79. free(req);
  80. }
  81. static int
  82. isns_req_getspace(struct isns_req *req, uint32_t len)
  83. {
  84. void *newbuf;
  85. int newlen;
  86. if (req->ir_usedlen + len <= req->ir_buflen)
  87. return (0);
  88. newlen = 1 << flsl(req->ir_usedlen + len);
  89. newbuf = realloc(req->ir_buf, newlen);
  90. if (newbuf == NULL) {
  91. log_err(1, "realloc");
  92. return (1);
  93. }
  94. req->ir_buf = newbuf;
  95. req->ir_buflen = newlen;
  96. return (0);
  97. }
  98. void
  99. isns_req_add(struct isns_req *req, uint32_t tag, uint32_t len,
  100. const void *value)
  101. {
  102. struct isns_tlv *tlv;
  103. uint32_t vlen;
  104. vlen = len + ((len & 3) ? (4 - (len & 3)) : 0);
  105. isns_req_getspace(req, sizeof(*tlv) + vlen);
  106. tlv = (struct isns_tlv *)&req->ir_buf[req->ir_usedlen];
  107. be32enc(tlv->it_tag, tag);
  108. be32enc(tlv->it_length, vlen);
  109. memcpy(tlv->it_value, value, len);
  110. if (vlen != len)
  111. memset(&tlv->it_value[len], 0, vlen - len);
  112. req->ir_usedlen += sizeof(*tlv) + vlen;
  113. }
  114. void
  115. isns_req_add_delim(struct isns_req *req)
  116. {
  117. isns_req_add(req, 0, 0, NULL);
  118. }
  119. void
  120. isns_req_add_str(struct isns_req *req, uint32_t tag, const char *value)
  121. {
  122. isns_req_add(req, tag, strlen(value) + 1, value);
  123. }
  124. void
  125. isns_req_add_32(struct isns_req *req, uint32_t tag, uint32_t value)
  126. {
  127. uint32_t beval;
  128. be32enc(&beval, value);
  129. isns_req_add(req, tag, sizeof(value), &beval);
  130. }
  131. void
  132. isns_req_add_addr(struct isns_req *req, uint32_t tag, struct addrinfo *ai)
  133. {
  134. struct sockaddr_in *in4;
  135. struct sockaddr_in6 *in6;
  136. uint8_t buf[16];
  137. switch (ai->ai_addr->sa_family) {
  138. case AF_INET:
  139. in4 = (struct sockaddr_in *)(void *)ai->ai_addr;
  140. memset(buf, 0, 10);
  141. buf[10] = 0xff;
  142. buf[11] = 0xff;
  143. memcpy(&buf[12], &in4->sin_addr, sizeof(in4->sin_addr));
  144. isns_req_add(req, tag, sizeof(buf), buf);
  145. break;
  146. case AF_INET6:
  147. in6 = (struct sockaddr_in6 *)(void *)ai->ai_addr;
  148. isns_req_add(req, tag, sizeof(in6->sin6_addr), &in6->sin6_addr);
  149. break;
  150. default:
  151. log_errx(1, "Unsupported address family %d",
  152. ai->ai_addr->sa_family);
  153. }
  154. }
  155. void
  156. isns_req_add_port(struct isns_req *req, uint32_t tag, struct addrinfo *ai)
  157. {
  158. struct sockaddr_in *in4;
  159. struct sockaddr_in6 *in6;
  160. uint32_t buf;
  161. switch (ai->ai_addr->sa_family) {
  162. case AF_INET:
  163. in4 = (struct sockaddr_in *)(void *)ai->ai_addr;
  164. be32enc(&buf, ntohs(in4->sin_port));
  165. isns_req_add(req, tag, sizeof(buf), &buf);
  166. break;
  167. case AF_INET6:
  168. in6 = (struct sockaddr_in6 *)(void *)ai->ai_addr;
  169. be32enc(&buf, ntohs(in6->sin6_port));
  170. isns_req_add(req, tag, sizeof(buf), &buf);
  171. break;
  172. default:
  173. log_errx(1, "Unsupported address family %d",
  174. ai->ai_addr->sa_family);
  175. }
  176. }
  177. int
  178. isns_req_send(int s, struct isns_req *req)
  179. {
  180. struct isns_hdr *hdr;
  181. int res;
  182. hdr = (struct isns_hdr *)req->ir_buf;
  183. be16enc(hdr->ih_length, req->ir_usedlen - sizeof(*hdr));
  184. be16enc(hdr->ih_flags, be16dec(hdr->ih_flags) |
  185. ISNS_FLAG_LAST | ISNS_FLAG_FIRST);
  186. be16enc(hdr->ih_transaction, 0);
  187. be16enc(hdr->ih_sequence, 0);
  188. res = write(s, req->ir_buf, req->ir_usedlen);
  189. return ((res < 0) ? -1 : 0);
  190. }
  191. int
  192. isns_req_receive(int s, struct isns_req *req)
  193. {
  194. struct isns_hdr *hdr;
  195. ssize_t res, len;
  196. req->ir_usedlen = 0;
  197. isns_req_getspace(req, sizeof(*hdr));
  198. res = read(s, req->ir_buf, sizeof(*hdr));
  199. if (res < (ssize_t)sizeof(*hdr))
  200. return (-1);
  201. req->ir_usedlen = sizeof(*hdr);
  202. hdr = (struct isns_hdr *)req->ir_buf;
  203. if (be16dec(hdr->ih_version) != ISNS_VERSION)
  204. return (-1);
  205. if ((be16dec(hdr->ih_flags) & (ISNS_FLAG_LAST | ISNS_FLAG_FIRST)) !=
  206. (ISNS_FLAG_LAST | ISNS_FLAG_FIRST))
  207. return (-1);
  208. len = be16dec(hdr->ih_length);
  209. isns_req_getspace(req, len);
  210. res = read(s, &req->ir_buf[req->ir_usedlen], len);
  211. if (res < len)
  212. return (-1);
  213. req->ir_usedlen += len;
  214. return (0);
  215. }
  216. uint32_t
  217. isns_req_get_status(struct isns_req *req)
  218. {
  219. if (req->ir_usedlen < sizeof(struct isns_hdr) + 4)
  220. return (-1);
  221. return (be32dec(&req->ir_buf[sizeof(struct isns_hdr)]));
  222. }