addr.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @file addr.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. * @section DESCRIPTION
  30. *
  31. * AddrProto, a protocol for encoding network addresses.
  32. *
  33. * AddrProto is built with BProto, the protocol and code generator for building
  34. * custom message protocols. The BProto specification file is addr.bproto.
  35. */
  36. #ifndef BADVPN_PROTOCOL_ADDR_H
  37. #define BADVPN_PROTOCOL_ADDR_H
  38. #include <stdint.h>
  39. #include <string.h>
  40. #include <misc/debug.h>
  41. #include <system/BAddr.h>
  42. #include <base/BLog.h>
  43. #include <generated/bproto_addr.h>
  44. #include <generated/blog_channel_addr.h>
  45. #define ADDR_TYPE_IPV4 1
  46. #define ADDR_TYPE_IPV6 2
  47. #define ADDR_SIZE_IPV4 (addr_SIZEtype + addr_SIZEip_port + addr_SIZEipv4_addr)
  48. #define ADDR_SIZE_IPV6 (addr_SIZEtype + addr_SIZEip_port + addr_SIZEipv6_addr)
  49. /**
  50. * Determines if the given address is supported by AddrProto.
  51. * Depends only on the type of the address.
  52. *
  53. * @param addr address to check. Must be recognized according to {@link BAddr_IsRecognized}.
  54. * @return 1 if supported, 0 if not
  55. */
  56. static int addr_supported (BAddr addr);
  57. /**
  58. * Determines the size of the given address when encoded by AddrProto.
  59. * Depends only on the type of the address.
  60. *
  61. * @param addr address to check. Must be supported according to {@link addr_supported}.
  62. * @return encoded size
  63. */
  64. static int addr_size (BAddr addr);
  65. /**
  66. * Encodes an address according to AddrProto.
  67. *
  68. * @param out output buffer. Must have at least addr_size(addr) space.
  69. * @param addr address to encode. Must be supported according to {@link addr_supported}.
  70. */
  71. static void addr_write (uint8_t *out, BAddr addr);
  72. /**
  73. * Decodes an address according to AddrProto.
  74. *
  75. * @param data input buffer containing the address to decode
  76. * @param data_len size of input. Must be >=0.
  77. * @param out_addr the decoded address will be stored here on success
  78. * @return 1 on success, 0 on failure
  79. */
  80. static int addr_read (uint8_t *data, int data_len, BAddr *out_addr) WARN_UNUSED;
  81. int addr_supported (BAddr addr)
  82. {
  83. BAddr_Assert(&addr);
  84. switch (addr.type) {
  85. case BADDR_TYPE_IPV4:
  86. case BADDR_TYPE_IPV6:
  87. return 1;
  88. default:
  89. return 0;
  90. }
  91. }
  92. int addr_size (BAddr addr)
  93. {
  94. ASSERT(addr_supported(addr))
  95. switch (addr.type) {
  96. case BADDR_TYPE_IPV4:
  97. return ADDR_SIZE_IPV4;
  98. case BADDR_TYPE_IPV6:
  99. return ADDR_SIZE_IPV6;
  100. default:
  101. ASSERT(0)
  102. return 0;
  103. }
  104. }
  105. void addr_write (uint8_t *out, BAddr addr)
  106. {
  107. ASSERT(addr_supported(addr))
  108. addrWriter writer;
  109. addrWriter_Init(&writer, out);
  110. switch (addr.type) {
  111. case BADDR_TYPE_IPV4: {
  112. addrWriter_Addtype(&writer, ADDR_TYPE_IPV4);
  113. uint8_t *out_port = addrWriter_Addip_port(&writer);
  114. memcpy(out_port, &addr.ipv4.port, sizeof(addr.ipv4.port));
  115. uint8_t *out_addr = addrWriter_Addipv4_addr(&writer);
  116. memcpy(out_addr, &addr.ipv4.ip, sizeof(addr.ipv4.ip));
  117. } break;
  118. case BADDR_TYPE_IPV6: {
  119. addrWriter_Addtype(&writer, ADDR_TYPE_IPV6);
  120. uint8_t *out_port = addrWriter_Addip_port(&writer);
  121. memcpy(out_port, &addr.ipv6.port, sizeof(addr.ipv6.port));
  122. uint8_t *out_addr = addrWriter_Addipv6_addr(&writer);
  123. memcpy(out_addr, addr.ipv6.ip, sizeof(addr.ipv6.ip));
  124. } break;
  125. default:
  126. ASSERT(0);
  127. }
  128. int len = addrWriter_Finish(&writer);
  129. B_USE(len)
  130. ASSERT(len == addr_size(addr))
  131. }
  132. int addr_read (uint8_t *data, int data_len, BAddr *out_addr)
  133. {
  134. ASSERT(data_len >= 0)
  135. addrParser parser;
  136. if (!addrParser_Init(&parser, data, data_len)) {
  137. BLog(BLOG_ERROR, "failed to parse addr");
  138. return 0;
  139. }
  140. uint8_t type = 0; // to remove warning
  141. addrParser_Gettype(&parser, &type);
  142. switch (type) {
  143. case ADDR_TYPE_IPV4: {
  144. uint8_t *port_data;
  145. if (!addrParser_Getip_port(&parser, &port_data)) {
  146. BLog(BLOG_ERROR, "port missing for IPv4 address");
  147. return 0;
  148. }
  149. uint8_t *addr_data;
  150. if (!addrParser_Getipv4_addr(&parser, &addr_data)) {
  151. BLog(BLOG_ERROR, "address missing for IPv4 address");
  152. return 0;
  153. }
  154. uint16_t port;
  155. uint32_t addr;
  156. memcpy(&port, port_data, sizeof(port));
  157. memcpy(&addr, addr_data, sizeof(addr));
  158. BAddr_InitIPv4(out_addr, addr, port);
  159. } break;
  160. case ADDR_TYPE_IPV6: {
  161. uint8_t *port_data;
  162. if (!addrParser_Getip_port(&parser, &port_data)) {
  163. BLog(BLOG_ERROR, "port missing for IPv6 address");
  164. return 0;
  165. }
  166. uint8_t *addr_data;
  167. if (!addrParser_Getipv6_addr(&parser, &addr_data)) {
  168. BLog(BLOG_ERROR, "address missing for IPv6 address");
  169. return 0;
  170. }
  171. uint16_t port;
  172. memcpy(&port, port_data, sizeof(port));
  173. BAddr_InitIPv6(out_addr, addr_data, port);
  174. } break;
  175. default:
  176. BLog(BLOG_ERROR, "unknown address type %d", (int)type);
  177. return 0;
  178. }
  179. return 1;
  180. }
  181. #endif