client.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @file client.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. #include <stdio.h>
  23. #include <stdint.h>
  24. #include <protocol/scproto.h>
  25. #include <structure/LinkedList2.h>
  26. #include <structure/HashTable.h>
  27. #include <structure/BAVL.h>
  28. #include <flow/SinglePacketBuffer.h>
  29. #include <tuntap/BTap.h>
  30. #include <client/DatagramPeerIO.h>
  31. #include <client/StreamPeerIO.h>
  32. #include <client/DataProto.h>
  33. // NOTE: all time values are in milliseconds
  34. // name of the program
  35. #define PROGRAM_NAME "client"
  36. // server output buffer size
  37. #define SERVER_BUFFER_MIN_PACKETS 200
  38. // maximum UDP payload size
  39. #define CLIENT_UDP_MTU 1472
  40. // maximum number of peers
  41. #define MAX_PEERS 29
  42. // maximum number of peer's MAC addresses to remember
  43. #define PEER_MAX_MACS 16
  44. // maximum number of multicast addresses per peer
  45. #define PEER_MAX_GROUPS 16
  46. // how long we wait for a packet to reach full size before sending it (see FragmentProtoDisassembler latency argument)
  47. #define PEER_DEFAULT_FRAGMENTATION_LATENCY 0
  48. // keep-alive packet interval for p2p communication
  49. #define PEER_KEEPALIVE_INTERVAL 10000
  50. // keep-alive receive timer for p2p communication (after how long to consider the link down)
  51. #define PEER_KEEPALIVE_RECEIVE_TIMER 22000
  52. // size of frame send buffer, in number of frames
  53. #define PEER_DEFAULT_SEND_BUFFER_SIZE 32
  54. // size of frame send buffer for relayed packets, in number of frames
  55. #define PEER_DEFAULT_SEND_BUFFER_RELAY_SIZE 32
  56. // retry time
  57. #define PEER_RETRY_TIME 5000
  58. // number of MAC seeds to keep for checking received packets
  59. #define MACPOOL_NUM_RECV_SEEDS 2
  60. // for how long a peer can send no Membership Reports for a group
  61. // before the peer and group are disassociated
  62. #define IGMP_DEFAULT_GROUP_MEMBERSHIP_INTERVAL 260000
  63. // how long to wait for joins after a Group Specific query has been
  64. // forwarded to a peer before assuming there are no listeners at the peer
  65. #define IGMP_LAST_MEMBER_QUERY_TIME 2000
  66. // maximum bind addresses
  67. #define MAX_BIND_ADDRS 8
  68. // maximum external addresses per bind address
  69. #define MAX_EXT_ADDRS 8
  70. // maximum scopes
  71. #define MAX_SCOPES 8
  72. struct device_data {
  73. BTap btap;
  74. int mtu;
  75. SinglePacketBuffer input_buffer;
  76. PacketPassInterface input_interface;
  77. PacketPassInterface *output_interface;
  78. uint8_t *framebuf;
  79. int framelen;
  80. };
  81. struct peer_data;
  82. // entry in global MAC hash table
  83. struct mac_table_entry {
  84. struct peer_data *peer;
  85. LinkedList2Node list_node; // node in macs_used or macs_free
  86. // defined when used:
  87. uint8_t mac[6]; // MAC address
  88. HashTableNode table_node; // node in global MAC address table
  89. };
  90. // entry in global multicast hash table
  91. struct multicast_table_entry {
  92. // defined when free:
  93. LinkedList2Node free_list_node; // node in free entries list
  94. // defined when used:
  95. uint32_t sig; // last 23 bits of group address
  96. HashTableNode table_node; // node in global multicast hash table
  97. LinkedList2 group_entries; // list of peers' group entries that match this multicast entry
  98. };
  99. // multicast group entry in peers
  100. struct peer_group_entry {
  101. struct peer_data *peer;
  102. LinkedList2Node list_node; // node in peer's free or used groups list
  103. BTimer timer; // timer for removing the group, running when group entry is used
  104. // defined when used:
  105. // basic group data
  106. uint32_t group; // group address
  107. HashTableNode table_node; // node in peer's groups hash table
  108. btime_t timer_endtime;
  109. // multicast table entry data
  110. LinkedList2Node multicast_list_node; // node in list of multicast MACs that may mean this group
  111. struct multicast_table_entry *multicast_entry; // pointer to entry in multicast hash table
  112. };
  113. struct peer_data {
  114. // peer identifier
  115. peerid_t id;
  116. // flags provided by the server
  117. int flags;
  118. // certificate reported by the server, defined only if using SSL
  119. uint8_t cert[SCID_NEWCLIENT_MAX_CERT_LEN];
  120. int cert_len;
  121. // local flow
  122. DataProtoLocalSource local_dpflow;
  123. // relay source
  124. DataProtoRelaySource relay_source;
  125. // flag if link objects are initialized
  126. int have_link;
  127. // link sending
  128. DataProtoDest send_dp;
  129. // link receive interface
  130. PacketPassInterface recv_ppi;
  131. // transport-specific link objects
  132. union {
  133. struct {
  134. DatagramPeerIO pio;
  135. uint16_t sendseed_nextid;
  136. int sendseed_sent;
  137. uint16_t sendseed_sent_id;
  138. uint8_t *sendseed_sent_key;
  139. uint8_t *sendseed_sent_iv;
  140. } udp;
  141. struct {
  142. StreamPeerIO pio;
  143. } tcp;
  144. } pio;
  145. // flag if relaying is installed
  146. int have_relaying;
  147. // relaying objects
  148. struct peer_data *relaying_peer; // peer through which we are relaying
  149. LinkedList2Node relaying_list_node; // node in relay peer's relay_users
  150. // waiting for relay data
  151. int waiting_relay;
  152. LinkedList2Node waiting_relay_list_node;
  153. // retry timer
  154. BTimer reset_timer;
  155. // MAC address entries
  156. struct mac_table_entry macs_data[PEER_MAX_MACS];
  157. // used entries, in global mac table
  158. LinkedList2 macs_used;
  159. // free entries
  160. LinkedList2 macs_free;
  161. // IPv4 multicast groups the peer is a destination for
  162. struct peer_group_entry groups_data[PEER_MAX_GROUPS];
  163. LinkedList2 groups_used;
  164. LinkedList2 groups_free;
  165. HashTable groups_hashtable;
  166. // peers linked list node
  167. LinkedList2Node list_node;
  168. // peers-by-ID hash table node
  169. HashTableNode table_node;
  170. // relay server specific
  171. int is_relay;
  172. LinkedList2Node relay_list_node;
  173. LinkedList2 relay_users;
  174. // binding state
  175. int binding;
  176. int binding_addrpos;
  177. };