client.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <flow/SinglePacketBuffer.h>
  28. #include <flow/PacketPassFairQueue.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. // input
  76. SinglePacketBuffer input_buffer;
  77. PacketPassInterface input_interface;
  78. // output
  79. PacketPassFairQueue output_queue;
  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. // local receive flow
  124. PacketPassInterface *local_recv_if;
  125. PacketPassFairQueueFlow local_recv_qflow;
  126. // relay source
  127. DataProtoRelaySource relay_source;
  128. // flag if link objects are initialized
  129. int have_link;
  130. // link sending
  131. DataProtoDest send_dp;
  132. // link receive interface
  133. PacketPassInterface recv_ppi;
  134. // transport-specific link objects
  135. union {
  136. struct {
  137. DatagramPeerIO pio;
  138. uint16_t sendseed_nextid;
  139. int sendseed_sent;
  140. uint16_t sendseed_sent_id;
  141. uint8_t *sendseed_sent_key;
  142. uint8_t *sendseed_sent_iv;
  143. } udp;
  144. struct {
  145. StreamPeerIO pio;
  146. } tcp;
  147. } pio;
  148. // flag if relaying is installed
  149. int have_relaying;
  150. // relaying objects
  151. struct peer_data *relaying_peer; // peer through which we are relaying
  152. LinkedList2Node relaying_list_node; // node in relay peer's relay_users
  153. // waiting for relay data
  154. int waiting_relay;
  155. LinkedList2Node waiting_relay_list_node;
  156. // retry timer
  157. BTimer reset_timer;
  158. // MAC address entries
  159. struct mac_table_entry macs_data[PEER_MAX_MACS];
  160. // used entries, in global mac table
  161. LinkedList2 macs_used;
  162. // free entries
  163. LinkedList2 macs_free;
  164. // IPv4 multicast groups the peer is a destination for
  165. struct peer_group_entry groups_data[PEER_MAX_GROUPS];
  166. LinkedList2 groups_used;
  167. LinkedList2 groups_free;
  168. HashTable groups_hashtable;
  169. // relay server specific
  170. int is_relay;
  171. LinkedList2Node relay_list_node;
  172. LinkedList2 relay_users;
  173. // binding state
  174. int binding;
  175. int binding_addrpos;
  176. // jobs
  177. BPending job_send_seed_after_binding;
  178. // peers linked list node
  179. LinkedList2Node list_node;
  180. // peers-by-ID hash table node
  181. HashTableNode table_node;
  182. };