client.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @file client.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #include <protocol/scproto.h>
  32. #include <structure/LinkedList1.h>
  33. #include <flow/PacketPassFairQueue.h>
  34. #include <flow/SinglePacketBuffer.h>
  35. #include <flow/PacketRecvConnector.h>
  36. #include <client/DatagramPeerIO.h>
  37. #include <client/StreamPeerIO.h>
  38. #include <client/DataProto.h>
  39. #include <client/DPReceive.h>
  40. #include <client/FrameDecider.h>
  41. #include <client/PeerChat.h>
  42. #include <client/SinglePacketSource.h>
  43. // NOTE: all time values are in milliseconds
  44. // name of the program
  45. #define PROGRAM_NAME "client"
  46. // server output buffer size
  47. #define SERVER_BUFFER_MIN_PACKETS 200
  48. // maximum UDP payload size
  49. #define CLIENT_UDP_MTU 1472
  50. // maximum number of pending TCP PasswordListener clients
  51. #define TCP_MAX_PASSWORD_LISTENER_CLIENTS 50
  52. // maximum number of peers
  53. #define MAX_PEERS 256
  54. // maximum number of peer's MAC addresses to remember
  55. #define PEER_DEFAULT_MAX_MACS 16
  56. // maximum number of multicast addresses per peer
  57. #define PEER_DEFAULT_MAX_GROUPS 16
  58. // how long we wait for a packet to reach full size before sending it (see FragmentProtoDisassembler latency argument)
  59. #define PEER_DEFAULT_UDP_FRAGMENTATION_LATENCY 0
  60. // value related to how much out-of-order input we tolerate (see FragmentProtoAssembler num_frames argument)
  61. #define PEER_UDP_ASSEMBLER_NUM_FRAMES 4
  62. // socket send buffer (SO_SNDBUF) for peer TCP connections, <=0 to not set
  63. #define PEER_DEFAULT_TCP_SOCKET_SNDBUF 1048576
  64. // keep-alive packet interval for p2p communication
  65. #define PEER_KEEPALIVE_INTERVAL 10000
  66. // keep-alive receive timer for p2p communication (after how long to consider the link down)
  67. #define PEER_KEEPALIVE_RECEIVE_TIMER 22000
  68. // size of frame send buffer, in number of frames
  69. #define PEER_DEFAULT_SEND_BUFFER_SIZE 32
  70. // size of frame send buffer for relayed packets, in number of frames
  71. #define PEER_DEFAULT_SEND_BUFFER_RELAY_SIZE 32
  72. // time after an unused relay flow is freed (-1 for never)
  73. #define PEER_RELAY_FLOW_INACTIVITY_TIME 10000
  74. // retry time
  75. #define PEER_RETRY_TIME 5000
  76. // for how long a peer can send no Membership Reports for a group
  77. // before the peer and group are disassociated
  78. #define DEFAULT_IGMP_GROUP_MEMBERSHIP_INTERVAL 260000
  79. // how long to wait for joins after a Group Specific query has been
  80. // forwarded to a peer before assuming there are no listeners at the peer
  81. #define DEFAULT_IGMP_LAST_MEMBER_QUERY_TIME 2000
  82. // maximum bind addresses
  83. #define MAX_BIND_ADDRS 8
  84. // maximum external addresses per bind address
  85. #define MAX_EXT_ADDRS 8
  86. // maximum scopes
  87. #define MAX_SCOPES 8
  88. //#define SIMULATE_PEER_OUT_OF_BUFFER 70
  89. struct server_flow {
  90. PacketPassFairQueueFlow qflow;
  91. SinglePacketBuffer encoder_buffer;
  92. PacketRecvConnector connector;
  93. int connected;
  94. };
  95. struct peer_data {
  96. // peer identifier
  97. peerid_t id;
  98. // flags provided by the server
  99. int flags;
  100. // certificate reported by the server, defined only if using SSL
  101. uint8_t cert[SCID_NEWCLIENT_MAX_CERT_LEN];
  102. int cert_len;
  103. char *common_name;
  104. // init job
  105. BPending job_init;
  106. // server flow
  107. struct server_flow *server_flow;
  108. // chat
  109. int have_chat;
  110. PeerChat chat;
  111. int chat_send_msg_len;
  112. // resetpeer source (when chat fails)
  113. int have_resetpeer;
  114. uint8_t resetpeer_packet[sizeof(struct packetproto_header) + sizeof(struct sc_header) + sizeof(struct sc_client_resetpeer)];
  115. SinglePacketSource resetpeer_source;
  116. // local flow
  117. DataProtoFlow local_dpflow;
  118. // frame decider peer
  119. FrameDeciderPeer decider_peer;
  120. // receive peer
  121. DPReceivePeer receive_peer;
  122. // flag if link objects are initialized
  123. int have_link;
  124. // receive receiver
  125. DPReceiveReceiver receive_receiver;
  126. // transport-specific link objects
  127. union {
  128. struct {
  129. DatagramPeerIO pio;
  130. uint16_t sendseed_nextid;
  131. int sendseed_sent;
  132. uint16_t sendseed_sent_id;
  133. uint8_t sendseed_sent_key[BENCRYPTION_MAX_KEY_SIZE];
  134. uint8_t sendseed_sent_iv[BENCRYPTION_MAX_BLOCK_SIZE];
  135. uint16_t pending_recvseed_id;
  136. BPending job_send_seed;
  137. } udp;
  138. struct {
  139. StreamPeerIO pio;
  140. } tcp;
  141. } pio;
  142. // link sending
  143. DataProtoSink send_dp;
  144. // relaying objects
  145. struct peer_data *relaying_peer; // peer through which we are relaying, or NULL
  146. LinkedList1Node relaying_list_node; // node in relay peer's relay_users
  147. // waiting for relay data
  148. int waiting_relay;
  149. LinkedList1Node waiting_relay_list_node;
  150. // retry timer
  151. BTimer reset_timer;
  152. // relay server specific
  153. int is_relay;
  154. LinkedList1Node relay_list_node;
  155. LinkedList1 relay_users;
  156. // binding state
  157. int binding;
  158. int binding_addrpos;
  159. // peers linked list node
  160. LinkedList1Node list_node;
  161. };