address_utils.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/value_utils.h>
  41. static int ncd_read_baddr (NCDValRef val, BAddr *out) WARN_UNUSED;
  42. static NCDValRef ncd_make_baddr (BAddr addr, NCDValMem *mem);
  43. static int ncd_read_bconnection_addr (NCDValRef val, struct BConnection_addr *out_addr) WARN_UNUSED;
  44. static int ncd_read_baddr (NCDValRef val, BAddr *out)
  45. {
  46. ASSERT(!NCDVal_IsInvalid(val))
  47. ASSERT(out)
  48. if (!NCDVal_IsList(val)) {
  49. goto fail;
  50. }
  51. NCDValRef type_val;
  52. if (!NCDVal_ListReadHead(val, 1, &type_val)) {
  53. goto fail;
  54. }
  55. if (!NCDVal_IsString(type_val)) {
  56. goto fail;
  57. }
  58. BAddr addr;
  59. if (NCDVal_StringEquals(type_val, "none")) {
  60. if (!NCDVal_ListRead(val, 1, &type_val)) {
  61. goto fail;
  62. }
  63. addr.type = BADDR_TYPE_NONE;
  64. }
  65. else if (NCDVal_StringEquals(type_val, "ipv4")) {
  66. NCDValRef ipaddr_val;
  67. NCDValRef port_val;
  68. if (!NCDVal_ListRead(val, 3, &type_val, &ipaddr_val, &port_val)) {
  69. goto fail;
  70. }
  71. if (!NCDVal_IsString(ipaddr_val) || !NCDVal_IsString(port_val)) {
  72. goto fail;
  73. }
  74. addr.type = BADDR_TYPE_IPV4;
  75. if (!ipaddr_parse_ipv4_addr_bin(NCDVal_StringValue(ipaddr_val), NCDVal_StringLength(ipaddr_val), &addr.ipv4.ip)) {
  76. goto fail;
  77. }
  78. uintmax_t port;
  79. if (!ncd_read_uintmax(port_val, &port) || port > UINT16_MAX) {
  80. goto fail;
  81. }
  82. addr.ipv4.port = hton16(port);
  83. }
  84. else if (NCDVal_StringEquals(type_val, "ipv6")) {
  85. NCDValRef ipaddr_val;
  86. NCDValRef port_val;
  87. if (!NCDVal_ListRead(val, 3, &type_val, &ipaddr_val, &port_val)) {
  88. goto fail;
  89. }
  90. if (!NCDVal_IsString(ipaddr_val) || !NCDVal_IsString(port_val)) {
  91. goto fail;
  92. }
  93. addr.type = BADDR_TYPE_IPV6;
  94. struct ipv6_addr i6addr;
  95. if (!ipaddr6_parse_ipv6_addr_bin(NCDVal_StringValue(ipaddr_val), NCDVal_StringLength(ipaddr_val), &i6addr)) {
  96. goto fail;
  97. }
  98. memcpy(addr.ipv6.ip, i6addr.bytes, 16);
  99. uintmax_t port;
  100. if (!ncd_read_uintmax(port_val, &port) || port > UINT16_MAX) {
  101. goto fail;
  102. }
  103. addr.ipv6.port = hton16(port);
  104. }
  105. else {
  106. goto fail;
  107. }
  108. *out = addr;
  109. return 1;
  110. fail:
  111. return 0;
  112. }
  113. static NCDValRef ncd_make_baddr (BAddr addr, NCDValMem *mem)
  114. {
  115. BAddr_Assert(&addr);
  116. ASSERT(mem)
  117. NCDValRef val;
  118. switch (addr.type) {
  119. default:
  120. case BADDR_TYPE_NONE: {
  121. val = NCDVal_NewList(mem, 1);
  122. if (NCDVal_IsInvalid(val)) {
  123. goto fail;
  124. }
  125. const char *str = (addr.type == BADDR_TYPE_NONE ? "none" : "unknown");
  126. NCDValRef type_val = NCDVal_NewString(mem, str);
  127. if (NCDVal_IsInvalid(type_val)) {
  128. goto fail;
  129. }
  130. NCDVal_ListAppend(val, type_val);
  131. } break;
  132. case BADDR_TYPE_IPV4: {
  133. val = NCDVal_NewList(mem, 3);
  134. if (NCDVal_IsInvalid(val)) {
  135. goto fail;
  136. }
  137. NCDValRef type_val = NCDVal_NewString(mem, "ipv4");
  138. if (NCDVal_IsInvalid(type_val)) {
  139. goto fail;
  140. }
  141. char ipaddr_buf[IPADDR_PRINT_MAX];
  142. ipaddr_print_addr(addr.ipv4.ip, ipaddr_buf);
  143. NCDValRef ipaddr_val = NCDVal_NewString(mem, ipaddr_buf);
  144. if (NCDVal_IsInvalid(ipaddr_val)) {
  145. goto fail;
  146. }
  147. NCDValRef port_val = ncd_make_uintmax(mem, ntoh16(addr.ipv4.port));
  148. if (NCDVal_IsInvalid(port_val)) {
  149. goto fail;
  150. }
  151. NCDVal_ListAppend(val, type_val);
  152. NCDVal_ListAppend(val, ipaddr_val);
  153. NCDVal_ListAppend(val, port_val);
  154. } break;
  155. case BADDR_TYPE_IPV6: {
  156. val = NCDVal_NewList(mem, 3);
  157. if (NCDVal_IsInvalid(val)) {
  158. goto fail;
  159. }
  160. NCDValRef type_val = NCDVal_NewString(mem, "ipv6");
  161. if (NCDVal_IsInvalid(type_val)) {
  162. goto fail;
  163. }
  164. char ipaddr_buf[IPADDR6_PRINT_MAX];
  165. struct ipv6_addr i6addr;
  166. memcpy(i6addr.bytes, addr.ipv6.ip, 16);
  167. ipaddr6_print_addr(i6addr, ipaddr_buf);
  168. NCDValRef ipaddr_val = NCDVal_NewString(mem, ipaddr_buf);
  169. if (NCDVal_IsInvalid(ipaddr_val)) {
  170. goto fail;
  171. }
  172. NCDValRef port_val = ncd_make_uintmax(mem, ntoh16(addr.ipv6.port));
  173. if (NCDVal_IsInvalid(port_val)) {
  174. goto fail;
  175. }
  176. NCDVal_ListAppend(val, type_val);
  177. NCDVal_ListAppend(val, ipaddr_val);
  178. NCDVal_ListAppend(val, port_val);
  179. } break;
  180. }
  181. return val;
  182. fail:
  183. return NCDVal_NewInvalid();
  184. }
  185. static int ncd_read_bconnection_addr (NCDValRef val, struct BConnection_addr *out_addr)
  186. {
  187. ASSERT(!NCDVal_IsInvalid(val))
  188. if (!NCDVal_IsList(val)) {
  189. goto fail;
  190. }
  191. NCDValRef protocol_arg;
  192. NCDValRef data_arg;
  193. if (!NCDVal_ListRead(val, 2, &protocol_arg, &data_arg)) {
  194. goto fail;
  195. }
  196. if (!NCDVal_IsString(protocol_arg)) {
  197. goto fail;
  198. }
  199. if (NCDVal_StringEquals(protocol_arg, "unix")) {
  200. if (!NCDVal_IsStringNoNulls(data_arg)) {
  201. goto fail;
  202. }
  203. *out_addr = BConnection_addr_unix(NCDVal_StringValue(data_arg));
  204. }
  205. else if (NCDVal_StringEquals(protocol_arg, "tcp")) {
  206. BAddr baddr;
  207. if (!ncd_read_baddr(data_arg, &baddr)) {
  208. goto fail;
  209. }
  210. *out_addr = BConnection_addr_baddr(baddr);
  211. }
  212. else {
  213. goto fail;
  214. }
  215. return 1;
  216. fail:
  217. return 0;
  218. }
  219. #endif