addr.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @file addr.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * AddrProto, a protocol for encoding network addresses.
  25. *
  26. * AddrProto is built with BProto, the protocol and code generator for building
  27. * custom message protocols. The BProto specification file is addr.bproto.
  28. */
  29. #ifndef BADVPN_PROTOCOL_ADDR_H
  30. #define BADVPN_PROTOCOL_ADDR_H
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <misc/debug.h>
  34. #include <system/BAddr.h>
  35. #include <system/BLog.h>
  36. #include <generated/bproto_addr.h>
  37. #include <generated/blog_channel_addr.h>
  38. #define ADDR_TYPE_IPV4 1
  39. #define ADDR_TYPE_IPV6 2
  40. #define ADDR_SIZE_IPV4 (addr_SIZEtype + addr_SIZEip_port + addr_SIZEipv4_addr)
  41. #define ADDR_SIZE_IPV6 (addr_SIZEtype + addr_SIZEip_port + addr_SIZEipv6_addr)
  42. /**
  43. * Determines if the given address is supported by AddrProto.
  44. * Depends only on the type of the address.
  45. *
  46. * @param addr address to check. Must be recognized according to {@link BAddr_IsRecognized}.
  47. * @return 1 if supported, 0 if not
  48. */
  49. static int addr_supported (BAddr addr);
  50. /**
  51. * Determines the size of the given address when encoded by AddrProto.
  52. * Depends only on the type of the address.
  53. *
  54. * @param addr address to check. Must be supported according to {@link addr_supported}.
  55. * @return encoded size
  56. */
  57. static int addr_size (BAddr addr);
  58. /**
  59. * Encodes an address according to AddrProto.
  60. *
  61. * @param out output buffer. Must have at least addr_size(addr) space.
  62. * @param addr address to encode. Must be supported according to {@link addr_supported}.
  63. */
  64. static void addr_write (uint8_t *out, BAddr addr);
  65. /**
  66. * Decodes an address according to AddrProto.
  67. *
  68. * @param data input buffer containing the address to decode
  69. * @param data_len size of input. Must be >=0.
  70. * @param out_addr the decoded address will be stored here on success
  71. * @return 1 on success, 0 on failure
  72. */
  73. static int addr_read (uint8_t *data, int data_len, BAddr *out_addr) WARN_UNUSED;
  74. int addr_supported (BAddr addr)
  75. {
  76. BAddr_Assert(&addr);
  77. switch (addr.type) {
  78. case BADDR_TYPE_IPV4:
  79. case BADDR_TYPE_IPV6:
  80. return 1;
  81. default:
  82. return 0;
  83. }
  84. }
  85. int addr_size (BAddr addr)
  86. {
  87. ASSERT(addr_supported(addr))
  88. switch (addr.type) {
  89. case BADDR_TYPE_IPV4:
  90. return ADDR_SIZE_IPV4;
  91. case BADDR_TYPE_IPV6:
  92. return ADDR_SIZE_IPV6;
  93. default:
  94. ASSERT(0)
  95. return 0;
  96. }
  97. }
  98. void addr_write (uint8_t *out, BAddr addr)
  99. {
  100. ASSERT(addr_supported(addr))
  101. addrWriter writer;
  102. addrWriter_Init(&writer, out);
  103. switch (addr.type) {
  104. case BADDR_TYPE_IPV4: {
  105. addrWriter_Addtype(&writer, ADDR_TYPE_IPV4);
  106. uint8_t *out_port = addrWriter_Addip_port(&writer);
  107. memcpy(out_port, &addr.ipv4.port, sizeof(addr.ipv4.port));
  108. uint8_t *out_addr = addrWriter_Addipv4_addr(&writer);
  109. memcpy(out_addr, &addr.ipv4.ip, sizeof(addr.ipv4.ip));
  110. } break;
  111. case BADDR_TYPE_IPV6: {
  112. addrWriter_Addtype(&writer, ADDR_TYPE_IPV6);
  113. uint8_t *out_port = addrWriter_Addip_port(&writer);
  114. memcpy(out_port, &addr.ipv6.port, sizeof(addr.ipv6.port));
  115. uint8_t *out_addr = addrWriter_Addipv6_addr(&writer);
  116. memcpy(out_addr, addr.ipv6.ip, sizeof(addr.ipv6.ip));
  117. } break;
  118. default:
  119. ASSERT(0);
  120. }
  121. int len = addrWriter_Finish(&writer);
  122. ASSERT(len == addr_size(addr))
  123. }
  124. int addr_read (uint8_t *data, int data_len, BAddr *out_addr)
  125. {
  126. ASSERT(data_len >= 0)
  127. addrParser parser;
  128. if (!addrParser_Init(&parser, data, data_len)) {
  129. BLog(BLOG_ERROR, "failed to parse addr");
  130. return 0;
  131. }
  132. uint8_t type;
  133. addrParser_Gettype(&parser, &type);
  134. switch (type) {
  135. case ADDR_TYPE_IPV4: {
  136. uint8_t *port_data;
  137. if (!addrParser_Getip_port(&parser, &port_data)) {
  138. BLog(BLOG_ERROR, "port missing for IPv4 address");
  139. return 0;
  140. }
  141. uint8_t *addr_data;
  142. if (!addrParser_Getipv4_addr(&parser, &addr_data)) {
  143. BLog(BLOG_ERROR, "address missing for IPv4 address");
  144. return 0;
  145. }
  146. uint16_t port;
  147. uint32_t addr;
  148. memcpy(&port, port_data, sizeof(port));
  149. memcpy(&addr, addr_data, sizeof(addr));
  150. BAddr_InitIPv4(out_addr, addr, port);
  151. } break;
  152. case ADDR_TYPE_IPV6: {
  153. uint8_t *port_data;
  154. if (!addrParser_Getip_port(&parser, &port_data)) {
  155. BLog(BLOG_ERROR, "port missing for IPv6 address");
  156. return 0;
  157. }
  158. uint8_t *addr_data;
  159. if (!addrParser_Getipv6_addr(&parser, &addr_data)) {
  160. BLog(BLOG_ERROR, "address missing for IPv6 address");
  161. return 0;
  162. }
  163. uint16_t port;
  164. memcpy(&port, port_data, sizeof(port));
  165. BAddr_InitIPv6(out_addr, addr_data, port);
  166. } break;
  167. default:
  168. BLog(BLOG_ERROR, "unknown address type %d", (int)type);
  169. return 0;
  170. }
  171. return 1;
  172. }
  173. #endif