server.h 4.8 KB

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