server.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <misc/dead.h>
  25. #include <structure/LinkedList2.h>
  26. #include <structure/HashTable.h>
  27. #include <structure/BAVL.h>
  28. #include <system/BSocket.h>
  29. #include <flow/StreamSocketSource.h>
  30. #include <flow/PacketProtoDecoder.h>
  31. #include <flow/PacketStreamSender.h>
  32. #include <flow/StreamSocketSink.h>
  33. #include <flow/PacketPassPriorityQueue.h>
  34. #include <flow/PacketPassFairQueue.h>
  35. #include <flow/PacketProtoFlow.h>
  36. #include <nspr_support/BPRFileDesc.h>
  37. #include <nspr_support/PRStreamSource.h>
  38. #include <nspr_support/PRStreamSink.h>
  39. // name of the program
  40. #define PROGRAM_NAME "server"
  41. // maxiumum number of connected clients. Must be <=2^16.
  42. #define MAX_CLIENTS 30
  43. // client output control flow buffer size in packets
  44. // it must hold: initdata, newclient's, endclient's (if other peers die when informing them)
  45. // make it big enough to hold the initial packet burst (initdata, newclient's),
  46. #define CLIENT_CONTROL_BUFFER_MIN_PACKETS (1 + 2*(MAX_CLIENTS - 1))
  47. // size of client-to-client buffers in packets
  48. #define CLIENT_PEER_FLOW_BUFFER_MIN_PACKETS 10
  49. // after how long of not hearing anything from the client we disconnect it
  50. #define CLIENT_NO_DATA_TIME_LIMIT 30000
  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. // dead variable
  63. dead_t dead;
  64. // source client
  65. struct client_data *src_client;
  66. // destination client
  67. struct client_data *dest_client;
  68. peerid_t dest_client_id;
  69. // node in source client hash table (by destination), only when src_client != NULL
  70. BAVLNode src_tree_node;
  71. // node in source client list, only when src_client != NULL
  72. LinkedList2Node src_list_node;
  73. // node in destination client list
  74. LinkedList2Node dest_list_node;
  75. // output chain
  76. PacketPassFairQueueFlow qflow;
  77. PacketProtoFlow oflow;
  78. BestEffortPacketWriteInterface *bepwi;
  79. int packet_len;
  80. uint8_t *packet;
  81. };
  82. struct client_data {
  83. // dead variable
  84. dead_t dead;
  85. // socket
  86. BSocket sock;
  87. BAddr addr;
  88. // SSL file descriptor
  89. PRFileDesc bottom_prfd;
  90. PRFileDesc *ssl_prfd;
  91. BPRFileDesc ssl_bprfd;
  92. // initialization state
  93. int initstatus;
  94. // client data if using SSL
  95. uint8_t cert[SCID_NEWCLIENT_MAX_CERT_LEN];
  96. int cert_len;
  97. char *common_name;
  98. // no data timer
  99. BTimer disconnect_timer;
  100. // client ID
  101. peerid_t id;
  102. // node in clients linked list
  103. LinkedList2Node list_node;
  104. // node in clients-by-id hash table
  105. HashTableNode table_node_id;
  106. // flows from us
  107. LinkedList2 peer_out_flows_list;
  108. BAVL peer_out_flows_tree;
  109. // whether it's being removed
  110. int dying;
  111. // error domain
  112. FlowErrorDomain domain;
  113. // input
  114. union {
  115. StreamSocketSource plain;
  116. PRStreamSource ssl;
  117. } input_source;
  118. PacketProtoDecoder input_decoder;
  119. PacketPassInterface input_interface;
  120. // output common
  121. union {
  122. StreamSocketSink plain;
  123. PRStreamSink ssl;
  124. } output_sink;
  125. PacketStreamSender output_sender;
  126. PacketPassPriorityQueue output_priorityqueue;
  127. // output control flow
  128. PacketPassPriorityQueueFlow output_control_qflow;
  129. PacketProtoFlow output_control_oflow;
  130. BestEffortPacketWriteInterface *output_control_input;
  131. int output_control_packet_len;
  132. uint8_t *output_control_packet;
  133. // output peers flow
  134. PacketPassPriorityQueueFlow output_peers_qflow;
  135. PacketPassFairQueue output_peers_fairqueue;
  136. LinkedList2 output_peers_flows;
  137. };