server.h 4.9 KB

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