server.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @file server.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 <stdint.h>
  30. #include <protocol/scproto.h>
  31. #include <structure/LinkedList2.h>
  32. #include <structure/BAVL.h>
  33. #include <flow/PacketProtoDecoder.h>
  34. #include <flow/PacketStreamSender.h>
  35. #include <flow/PacketPassPriorityQueue.h>
  36. #include <flow/PacketPassFairQueue.h>
  37. #include <flow/PacketProtoFlow.h>
  38. #include <system/BReactor.h>
  39. #include <system/BConnection.h>
  40. #include <nspr_support/BSSLConnection.h>
  41. // name of the program
  42. #define PROGRAM_NAME "server"
  43. // maxiumum number of connected clients. Must be <=2^16.
  44. #define MAX_CLIENTS 30
  45. // client output control flow buffer size in packets
  46. // it must hold: initdata, newclient's, endclient's (if other peers die when informing them)
  47. // make it big enough to hold the initial packet burst (initdata, newclient's),
  48. #define CLIENT_CONTROL_BUFFER_MIN_PACKETS (1 + 2*(MAX_CLIENTS - 1))
  49. // size of client-to-client buffers in packets
  50. #define CLIENT_PEER_FLOW_BUFFER_MIN_PACKETS 10
  51. // after how long of not hearing anything from the client we disconnect it
  52. #define CLIENT_NO_DATA_TIME_LIMIT 30000
  53. // SO_SNDBFUF socket option for clients
  54. #define CLIENT_DEFAULT_SOCKET_SNDBUF 16384
  55. // reset time when a buffer runs out or when we get the resetpeer message
  56. #define CLIENT_RESET_TIME 30000
  57. // maxiumum listen addresses
  58. #define MAX_LISTEN_ADDRS 16
  59. //#define SIMULATE_OUT_OF_CONTROL_BUFFER 20
  60. //#define SIMULATE_OUT_OF_FLOW_BUFFER 100
  61. // performing SSL handshake
  62. #define INITSTATUS_HANDSHAKE 1
  63. // waiting for clienthello
  64. #define INITSTATUS_WAITHELLO 2
  65. // initialisation was complete
  66. #define INITSTATUS_COMPLETE 3
  67. #define INITSTATUS_HASLINK(status) ((status) == INITSTATUS_WAITHELLO || (status) == INITSTATUS_COMPLETE)
  68. struct client_data;
  69. struct peer_know;
  70. struct peer_flow {
  71. // source client
  72. struct client_data *src_client;
  73. // destination client
  74. struct client_data *dest_client;
  75. peerid_t dest_client_id;
  76. // node in source client hash table (by destination), only when src_client != NULL
  77. BAVLNode src_tree_node;
  78. // node in source client list, only when src_client != NULL
  79. LinkedList2Node src_list_node;
  80. // node in destination client list
  81. LinkedList2Node dest_list_node;
  82. // output chain
  83. int have_io;
  84. PacketPassFairQueueFlow qflow;
  85. PacketProtoFlow oflow;
  86. BufferWriter *input;
  87. int packet_len;
  88. uint8_t *packet;
  89. // reset timer
  90. BTimer reset_timer;
  91. // opposite flow
  92. struct peer_flow *opposite;
  93. // pair data
  94. struct peer_know *know;
  95. int accepted;
  96. int resetting;
  97. };
  98. struct peer_know {
  99. struct client_data *from;
  100. struct client_data *to;
  101. int relay_server;
  102. int relay_client;
  103. LinkedList2Node from_node;
  104. LinkedList2Node to_node;
  105. BPending inform_job;
  106. BPending uninform_job;
  107. };
  108. struct client_data {
  109. // socket
  110. BConnection con;
  111. BAddr addr;
  112. // SSL connection, if using SSL
  113. PRFileDesc bottom_prfd;
  114. PRFileDesc *ssl_prfd;
  115. BSSLConnection sslcon;
  116. // initialization state
  117. int initstatus;
  118. // client data if using SSL
  119. uint8_t cert[SCID_NEWCLIENT_MAX_CERT_LEN];
  120. int cert_len;
  121. uint8_t cert_old[SCID_NEWCLIENT_MAX_CERT_LEN];
  122. int cert_old_len;
  123. char *common_name;
  124. // client version
  125. int version;
  126. // no data timer
  127. BTimer disconnect_timer;
  128. // client ID
  129. peerid_t id;
  130. // node in clients linked list
  131. LinkedList2Node list_node;
  132. // node in clients tree (by ID)
  133. BAVLNode tree_node;
  134. // knowledge lists
  135. LinkedList2 know_out_list;
  136. LinkedList2 know_in_list;
  137. // flows from us
  138. LinkedList2 peer_out_flows_list;
  139. BAVL peer_out_flows_tree;
  140. // whether it's being removed
  141. int dying;
  142. BPending dying_job;
  143. // input
  144. PacketProtoDecoder input_decoder;
  145. PacketPassInterface input_interface;
  146. // output common
  147. PacketStreamSender output_sender;
  148. PacketPassPriorityQueue output_priorityqueue;
  149. // output control flow
  150. PacketPassPriorityQueueFlow output_control_qflow;
  151. PacketProtoFlow output_control_oflow;
  152. BufferWriter *output_control_input;
  153. int output_control_packet_len;
  154. uint8_t *output_control_packet;
  155. // output peers flow
  156. PacketPassPriorityQueueFlow output_peers_qflow;
  157. PacketPassFairQueue output_peers_fairqueue;
  158. LinkedList2 output_peers_flows;
  159. };