addr.h 5.7 KB

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