server.h 4.8 KB

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