scproto.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @file scproto.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. * Definitions for SCProto, the protocol that the clients communicate in
  25. * with the server.
  26. *
  27. * All multi-byte integers in structs are little-endian, unless stated otherwise.
  28. *
  29. * A SCProto packet consists of:
  30. * - a header (struct {@link sc_header}) which contains the type of the
  31. * packet
  32. * - the payload
  33. *
  34. * It goes roughly like that:
  35. *
  36. * When the client connects to the server, it sends a "clienthello" packet
  37. * to the server. The packet contains the protocol version the client is using.
  38. * When the server receives the "clienthello" packet, it checks the version.
  39. * If it doesn't match, it disconnects the client. Otherwise the server sends
  40. * the client a "serverhello" packet to the client. That packet contains
  41. * the ID of the client and possibly its IPv4 address as the server sees it
  42. * (zero if not applicable).
  43. * The server than proceeds to synchronize the peers' knowledge of each other.
  44. * It does that by sending a "newclient" messages to a client to inform it of
  45. * another peer, and "endclient" messages to inform it that a peer is gone.
  46. * The server forwards messages between synchronized peers to allow them to
  47. * communicate. A peer sends a message to another peer by sending the "outmsg"
  48. * packet to the server, and the server delivers a message to a peer by sending
  49. * it the "inmsg" packet.
  50. */
  51. #ifndef BADVPN_PROTOCOL_SCPROTO_H
  52. #define BADVPN_PROTOCOL_SCPROTO_H
  53. #include <stdint.h>
  54. #define SC_VERSION 27
  55. #define SC_OLDVERSION 26
  56. #define SC_KEEPALIVE_INTERVAL 10000
  57. /**
  58. * SCProto packet header.
  59. * Follows up to SC_MAX_PAYLOAD bytes of payload.
  60. */
  61. struct sc_header {
  62. /**
  63. * Message type.
  64. */
  65. uint8_t type;
  66. } __attribute__((packed));
  67. #define SC_MAX_PAYLOAD 2000
  68. #define SC_MAX_ENC (sizeof(struct sc_header) + SC_MAX_PAYLOAD)
  69. typedef uint16_t peerid_t;
  70. #define SCID_KEEPALIVE 0
  71. #define SCID_CLIENTHELLO 1
  72. #define SCID_SERVERHELLO 2
  73. #define SCID_NEWCLIENT 3
  74. #define SCID_ENDCLIENT 4
  75. #define SCID_OUTMSG 5
  76. #define SCID_INMSG 6
  77. /**
  78. * "clienthello" client packet payload.
  79. * Packet type is SCID_CLIENTHELLO.
  80. */
  81. struct sc_client_hello {
  82. /**
  83. * Protocol version the client is using.
  84. */
  85. uint16_t version;
  86. } __attribute__((packed));
  87. /**
  88. * "serverhello" server packet payload.
  89. * Packet type is SCID_SERVERHELLO.
  90. */
  91. struct sc_server_hello {
  92. /**
  93. * Flags. Not used yet.
  94. */
  95. uint16_t flags;
  96. /**
  97. * Peer ID of the client.
  98. */
  99. peerid_t id;
  100. /**
  101. * IPv4 address of the client as seen by the server
  102. * (network byte order). Zero if not applicable.
  103. */
  104. uint32_t clientAddr;
  105. } __attribute__((packed));
  106. /**
  107. * "newclient" server packet payload.
  108. * Packet type is SCID_NEWCLIENT.
  109. * If the server is using TLS, follows up to SCID_NEWCLIENT_MAX_CERT_LEN
  110. * bytes of the new client's certificate (encoded in DER).
  111. */
  112. struct sc_server_newclient {
  113. /**
  114. * ID of the new peer.
  115. */
  116. peerid_t id;
  117. /**
  118. * Flags. Possible flags:
  119. * - SCID_NEWCLIENT_FLAG_RELAY_SERVER
  120. * You can relay frames to other peers through this peer.
  121. * - SCID_NEWCLIENT_FLAG_RELAY_CLIENT
  122. * You must allow this peer to relay frames to other peers through you.
  123. */
  124. uint16_t flags;
  125. } __attribute__((packed));
  126. #define SCID_NEWCLIENT_FLAG_RELAY_SERVER 1
  127. #define SCID_NEWCLIENT_FLAG_RELAY_CLIENT 2
  128. #define SCID_NEWCLIENT_MAX_CERT_LEN (SC_MAX_PAYLOAD - sizeof(struct sc_server_newclient))
  129. /**
  130. * "endclient" server packet payload.
  131. * Packet type is SCID_ENDCLIENT.
  132. */
  133. struct sc_server_endclient {
  134. /**
  135. * ID of the removed peer.
  136. */
  137. peerid_t id;
  138. } __attribute__((packed));
  139. /**
  140. * "outmsg" client packet header.
  141. * Packet type is SCID_OUTMSG.
  142. * Follows up to SC_MAX_MSGLEN bytes of message payload.
  143. */
  144. struct sc_client_outmsg {
  145. /**
  146. * ID of the destionation peer.
  147. */
  148. peerid_t clientid;
  149. } __attribute__((packed));
  150. /**
  151. * "inmsg" server packet payload.
  152. * Packet type is SCID_INMSG.
  153. * Follows up to SC_MAX_MSGLEN bytes of message payload.
  154. */
  155. struct sc_server_inmsg {
  156. /**
  157. * ID of the source peer.
  158. */
  159. peerid_t clientid;
  160. } __attribute__((packed));
  161. #define _SC_MAX_OUTMSGLEN (SC_MAX_PAYLOAD - sizeof(struct sc_client_outmsg))
  162. #define _SC_MAX_INMSGLEN (SC_MAX_PAYLOAD - sizeof(struct sc_server_inmsg))
  163. #define SC_MAX_MSGLEN (_SC_MAX_OUTMSGLEN < _SC_MAX_INMSGLEN ? _SC_MAX_OUTMSGLEN : _SC_MAX_INMSGLEN)
  164. #endif