client.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/BAVL.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. #include <client/DPRelay.h>
  34. #include <client/FrameDecider.h>
  35. // NOTE: all time values are in milliseconds
  36. // name of the program
  37. #define PROGRAM_NAME "client"
  38. // server output buffer size
  39. #define SERVER_BUFFER_MIN_PACKETS 200
  40. // maximum UDP payload size
  41. #define CLIENT_UDP_MTU 1472
  42. // maximum number of peers
  43. #define MAX_PEERS 256
  44. // maximum number of peer's MAC addresses to remember
  45. #define PEER_MAX_MACS 16
  46. // maximum number of multicast addresses per peer
  47. #define PEER_MAX_GROUPS 16
  48. // how long we wait for a packet to reach full size before sending it (see FragmentProtoDisassembler latency argument)
  49. #define PEER_UDP_DEFAULT_FRAGMENTATION_LATENCY 0
  50. // value related to how much out-of-order input we tolerate (see FragmentProtoAssembler num_frames argument)
  51. #define PEER_UDP_ASSEMBLER_NUM_FRAMES 4
  52. // keep-alive packet interval for p2p communication
  53. #define PEER_KEEPALIVE_INTERVAL 10000
  54. // keep-alive receive timer for p2p communication (after how long to consider the link down)
  55. #define PEER_KEEPALIVE_RECEIVE_TIMER 22000
  56. // size of frame send buffer, in number of frames
  57. #define PEER_DEFAULT_SEND_BUFFER_SIZE 32
  58. // size of frame send buffer for relayed packets, in number of frames
  59. #define PEER_DEFAULT_SEND_BUFFER_RELAY_SIZE 32
  60. // retry time
  61. #define PEER_RETRY_TIME 5000
  62. // for how long a peer can send no Membership Reports for a group
  63. // before the peer and group are disassociated
  64. #define IGMP_GROUP_MEMBERSHIP_INTERVAL 260000
  65. // how long to wait for joins after a Group Specific query has been
  66. // forwarded to a peer before assuming there are no listeners at the peer
  67. #define IGMP_LAST_MEMBER_QUERY_TIME 2000
  68. // maximum bind addresses
  69. #define MAX_BIND_ADDRS 8
  70. // maximum external addresses per bind address
  71. #define MAX_EXT_ADDRS 8
  72. // maximum scopes
  73. #define MAX_SCOPES 8
  74. struct device_data {
  75. BTap btap;
  76. int mtu;
  77. // input
  78. DataProtoDevice input_dpd;
  79. // output
  80. PacketPassFairQueue output_queue;
  81. };
  82. struct peer_data {
  83. // peer identifier
  84. peerid_t id;
  85. // flags provided by the server
  86. int flags;
  87. // certificate reported by the server, defined only if using SSL
  88. uint8_t cert[SCID_NEWCLIENT_MAX_CERT_LEN];
  89. int cert_len;
  90. // local flow
  91. DataProtoLocalSource local_dpflow;
  92. // local receive flow
  93. PacketPassInterface *local_recv_if;
  94. PacketPassFairQueueFlow local_recv_qflow;
  95. // relay source
  96. DPRelaySource relay_source;
  97. // relay sink
  98. DPRelaySink relay_sink;
  99. // flag if link objects are initialized
  100. int have_link;
  101. // link sending
  102. DataProtoDest send_dp;
  103. // link receive interface
  104. PacketPassInterface recv_ppi;
  105. // transport-specific link objects
  106. union {
  107. struct {
  108. DatagramPeerIO pio;
  109. uint16_t sendseed_nextid;
  110. int sendseed_sent;
  111. uint16_t sendseed_sent_id;
  112. uint8_t sendseed_sent_key[BENCRYPTION_MAX_KEY_SIZE];
  113. uint8_t sendseed_sent_iv[BENCRYPTION_MAX_BLOCK_SIZE];
  114. } udp;
  115. struct {
  116. StreamPeerIO pio;
  117. } tcp;
  118. } pio;
  119. // flag if relaying is installed
  120. int have_relaying;
  121. // relaying objects
  122. struct peer_data *relaying_peer; // peer through which we are relaying
  123. LinkedList2Node relaying_list_node; // node in relay peer's relay_users
  124. // waiting for relay data
  125. int waiting_relay;
  126. LinkedList2Node waiting_relay_list_node;
  127. // retry timer
  128. BTimer reset_timer;
  129. // frame decider peer
  130. FrameDeciderPeer decider_peer;
  131. // relay server specific
  132. int is_relay;
  133. LinkedList2Node relay_list_node;
  134. LinkedList2 relay_users;
  135. // binding state
  136. int binding;
  137. int binding_addrpos;
  138. // jobs
  139. BPending job_send_seed_after_binding;
  140. // peers linked list node
  141. LinkedList2Node list_node;
  142. // peers-by-ID hash table node
  143. BAVLNode tree_node;
  144. };