address_utils.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @file address_utils.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef NCD_ADDRESS_UTILS_H
  30. #define NCD_ADDRESS_UTILS_H
  31. #include <string.h>
  32. #include <limits.h>
  33. #include <misc/debug.h>
  34. #include <misc/ipaddr.h>
  35. #include <misc/ipaddr6.h>
  36. #include <misc/byteorder.h>
  37. #include <system/BAddr.h>
  38. #include <system/BConnectionGeneric.h>
  39. #include <ncd/NCDVal.h>
  40. #include <ncd/extra/value_utils.h>
  41. typedef int (*ncd_read_bconnection_addr_CustomHandler) (void *user, NCDValRef protocol, NCDValRef data);
  42. static int ncd_read_baddr (NCDValRef val, BAddr *out) WARN_UNUSED;
  43. static NCDValRef ncd_make_baddr (BAddr addr, NCDValMem *mem);
  44. static int ncd_read_bconnection_addr (NCDValRef val, struct BConnection_addr *out_addr) WARN_UNUSED;
  45. static int ncd_read_bconnection_addr_ext (NCDValRef val, ncd_read_bconnection_addr_CustomHandler custom_handler, void *user, struct BConnection_addr *out_addr) WARN_UNUSED;
  46. static int ncd_read_baddr (NCDValRef val, BAddr *out)
  47. {
  48. ASSERT(!NCDVal_IsInvalid(val))
  49. ASSERT(out)
  50. if (!NCDVal_IsList(val)) {
  51. goto fail;
  52. }
  53. NCDValRef type_val;
  54. if (!NCDVal_ListReadHead(val, 1, &type_val)) {
  55. goto fail;
  56. }
  57. if (!NCDVal_IsString(type_val)) {
  58. goto fail;
  59. }
  60. BAddr addr;
  61. if (NCDVal_StringEquals(type_val, "none")) {
  62. if (!NCDVal_ListRead(val, 1, &type_val)) {
  63. goto fail;
  64. }
  65. addr.type = BADDR_TYPE_NONE;
  66. }
  67. else if (NCDVal_StringEquals(type_val, "ipv4")) {
  68. NCDValRef ipaddr_val;
  69. NCDValRef port_val;
  70. if (!NCDVal_ListRead(val, 3, &type_val, &ipaddr_val, &port_val)) {
  71. goto fail;
  72. }
  73. if (!NCDVal_IsString(ipaddr_val)) {
  74. goto fail;
  75. }
  76. addr.type = BADDR_TYPE_IPV4;
  77. if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(ipaddr_val), &addr.ipv4.ip)) {
  78. goto fail;
  79. }
  80. uintmax_t port;
  81. if (!ncd_read_uintmax(port_val, &port) || port > UINT16_MAX) {
  82. goto fail;
  83. }
  84. addr.ipv4.port = hton16(port);
  85. }
  86. else if (NCDVal_StringEquals(type_val, "ipv6")) {
  87. NCDValRef ipaddr_val;
  88. NCDValRef port_val;
  89. if (!NCDVal_ListRead(val, 3, &type_val, &ipaddr_val, &port_val)) {
  90. goto fail;
  91. }
  92. if (!NCDVal_IsString(ipaddr_val)) {
  93. goto fail;
  94. }
  95. addr.type = BADDR_TYPE_IPV6;
  96. struct ipv6_addr i6addr;
  97. if (!ipaddr6_parse_ipv6_addr(NCDVal_StringMemRef(ipaddr_val), &i6addr)) {
  98. goto fail;
  99. }
  100. memcpy(addr.ipv6.ip, i6addr.bytes, 16);
  101. uintmax_t port;
  102. if (!ncd_read_uintmax(port_val, &port) || port > UINT16_MAX) {
  103. goto fail;
  104. }
  105. addr.ipv6.port = hton16(port);
  106. }
  107. else {
  108. goto fail;
  109. }
  110. *out = addr;
  111. return 1;
  112. fail:
  113. return 0;
  114. }
  115. static NCDValRef ncd_make_baddr (BAddr addr, NCDValMem *mem)
  116. {
  117. BAddr_Assert(&addr);
  118. ASSERT(mem)
  119. NCDValRef val;
  120. switch (addr.type) {
  121. default:
  122. case BADDR_TYPE_NONE: {
  123. val = NCDVal_NewList(mem, 1);
  124. if (NCDVal_IsInvalid(val)) {
  125. goto fail;
  126. }
  127. const char *str = (addr.type == BADDR_TYPE_NONE ? "none" : "unknown");
  128. NCDValRef type_val = NCDVal_NewString(mem, str);
  129. if (NCDVal_IsInvalid(type_val)) {
  130. goto fail;
  131. }
  132. if (!NCDVal_ListAppend(val, type_val)) {
  133. goto fail;
  134. }
  135. } break;
  136. case BADDR_TYPE_IPV4: {
  137. val = NCDVal_NewList(mem, 3);
  138. if (NCDVal_IsInvalid(val)) {
  139. goto fail;
  140. }
  141. NCDValRef type_val = NCDVal_NewString(mem, "ipv4");
  142. if (NCDVal_IsInvalid(type_val)) {
  143. goto fail;
  144. }
  145. char ipaddr_buf[IPADDR_PRINT_MAX];
  146. ipaddr_print_addr(addr.ipv4.ip, ipaddr_buf);
  147. NCDValRef ipaddr_val = NCDVal_NewString(mem, ipaddr_buf);
  148. if (NCDVal_IsInvalid(ipaddr_val)) {
  149. goto fail;
  150. }
  151. NCDValRef port_val = ncd_make_uintmax(mem, ntoh16(addr.ipv4.port));
  152. if (NCDVal_IsInvalid(port_val)) {
  153. goto fail;
  154. }
  155. if (!NCDVal_ListAppend(val, type_val)) {
  156. goto fail;
  157. }
  158. if (!NCDVal_ListAppend(val, ipaddr_val)) {
  159. goto fail;
  160. }
  161. if (!NCDVal_ListAppend(val, port_val)) {
  162. goto fail;
  163. }
  164. } break;
  165. case BADDR_TYPE_IPV6: {
  166. val = NCDVal_NewList(mem, 3);
  167. if (NCDVal_IsInvalid(val)) {
  168. goto fail;
  169. }
  170. NCDValRef type_val = NCDVal_NewString(mem, "ipv6");
  171. if (NCDVal_IsInvalid(type_val)) {
  172. goto fail;
  173. }
  174. char ipaddr_buf[IPADDR6_PRINT_MAX];
  175. struct ipv6_addr i6addr;
  176. memcpy(i6addr.bytes, addr.ipv6.ip, 16);
  177. ipaddr6_print_addr(i6addr, ipaddr_buf);
  178. NCDValRef ipaddr_val = NCDVal_NewString(mem, ipaddr_buf);
  179. if (NCDVal_IsInvalid(ipaddr_val)) {
  180. goto fail;
  181. }
  182. NCDValRef port_val = ncd_make_uintmax(mem, ntoh16(addr.ipv6.port));
  183. if (NCDVal_IsInvalid(port_val)) {
  184. goto fail;
  185. }
  186. if (!NCDVal_ListAppend(val, type_val)) {
  187. goto fail;
  188. }
  189. if (!NCDVal_ListAppend(val, ipaddr_val)) {
  190. goto fail;
  191. }
  192. if (!NCDVal_ListAppend(val, port_val)) {
  193. goto fail;
  194. }
  195. } break;
  196. }
  197. return val;
  198. fail:
  199. return NCDVal_NewInvalid();
  200. }
  201. static int ncd_read_bconnection_addr (NCDValRef val, struct BConnection_addr *out_addr)
  202. {
  203. return ncd_read_bconnection_addr_ext(val, NULL, NULL, out_addr);
  204. }
  205. static int ncd_read_bconnection_addr_ext (NCDValRef val, ncd_read_bconnection_addr_CustomHandler custom_handler, void *user, struct BConnection_addr *out_addr)
  206. {
  207. ASSERT(!NCDVal_IsInvalid(val))
  208. if (!NCDVal_IsList(val)) {
  209. goto fail;
  210. }
  211. NCDValRef protocol_arg;
  212. NCDValRef data_arg;
  213. if (!NCDVal_ListRead(val, 2, &protocol_arg, &data_arg)) {
  214. goto fail;
  215. }
  216. if (!NCDVal_IsString(protocol_arg)) {
  217. goto fail;
  218. }
  219. if (NCDVal_StringEquals(protocol_arg, "unix")) {
  220. if (!NCDVal_IsStringNoNulls(data_arg)) {
  221. goto fail;
  222. }
  223. *out_addr = BConnection_addr_unix(NCDVal_StringMemRef(data_arg));
  224. }
  225. else if (NCDVal_StringEquals(protocol_arg, "tcp")) {
  226. BAddr baddr;
  227. if (!ncd_read_baddr(data_arg, &baddr)) {
  228. goto fail;
  229. }
  230. *out_addr = BConnection_addr_baddr(baddr);
  231. }
  232. else {
  233. if (!custom_handler || !custom_handler(user, protocol_arg, data_arg)) {
  234. goto fail;
  235. }
  236. out_addr->type = -1;
  237. }
  238. return 1;
  239. fail:
  240. return 0;
  241. }
  242. #endif