/src/main/jni/badvpn/client/client.c
C | 2997 lines | 2173 code | 480 blank | 344 comment | 446 complexity | 0f9a0d4b763132203a898d1a14789a57 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- /**
- * @file client.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include <protocol/msgproto.h>
- #include <protocol/addr.h>
- #include <protocol/dataproto.h>
- #include <misc/version.h>
- #include <misc/debug.h>
- #include <misc/offset.h>
- #include <misc/byteorder.h>
- #include <misc/nsskey.h>
- #include <misc/loglevel.h>
- #include <misc/loggers_string.h>
- #include <misc/string_begins_with.h>
- #include <misc/open_standard_streams.h>
- #include <structure/LinkedList1.h>
- #include <base/DebugObject.h>
- #include <base/BLog.h>
- #include <security/BSecurity.h>
- #include <security/BRandom.h>
- #include <system/BSignal.h>
- #include <system/BTime.h>
- #include <system/BNetwork.h>
- #include <nspr_support/DummyPRFileDesc.h>
- #include <nspr_support/BSSLConnection.h>
- #include <server_connection/ServerConnection.h>
- #include <tuntap/BTap.h>
- #include <threadwork/BThreadWork.h>
- #ifndef BADVPN_USE_WINAPI
- #include <base/BLog_syslog.h>
- #endif
- #include <client/client.h>
- #include <generated/blog_channel_client.h>
- #define TRANSPORT_MODE_UDP 0
- #define TRANSPORT_MODE_TCP 1
- #define LOGGER_STDOUT 1
- #define LOGGER_SYSLOG 2
- // command-line options
- struct ext_addr_option {
- char *addr;
- char *scope;
- };
- struct bind_addr_option {
- char *addr;
- int num_ports;
- int num_ext_addrs;
- struct ext_addr_option ext_addrs[MAX_EXT_ADDRS];
- };
- struct {
- int help;
- int version;
- int logger;
- #ifndef BADVPN_USE_WINAPI
- char *logger_syslog_facility;
- char *logger_syslog_ident;
- #endif
- int loglevel;
- int loglevels[BLOG_NUM_CHANNELS];
- int threads;
- int use_threads_for_ssl_handshake;
- int use_threads_for_ssl_data;
- int ssl;
- char *nssdb;
- char *client_cert_name;
- char *server_name;
- char *server_addr;
- char *tapdev;
- int num_scopes;
- char *scopes[MAX_SCOPES];
- int num_bind_addrs;
- struct bind_addr_option bind_addrs[MAX_BIND_ADDRS];
- int transport_mode;
- int encryption_mode;
- int hash_mode;
- int otp_mode;
- int otp_num;
- int otp_num_warn;
- int fragmentation_latency;
- int peer_ssl;
- int peer_tcp_socket_sndbuf;
- int send_buffer_size;
- int send_buffer_relay_size;
- int max_macs;
- int max_groups;
- int igmp_group_membership_interval;
- int igmp_last_member_query_time;
- int allow_peer_talk_without_ssl;
- int max_peers;
- } options;
- // bind addresses
- struct ext_addr {
- int server_reported_port;
- BAddr addr; // if server_reported_port>=0, defined only after hello received
- char scope[64];
- };
- struct bind_addr {
- BAddr addr;
- int num_ports;
- int num_ext_addrs;
- struct ext_addr ext_addrs[MAX_EXT_ADDRS];
- };
- int num_bind_addrs;
- struct bind_addr bind_addrs[MAX_BIND_ADDRS];
- // TCP listeners
- PasswordListener listeners[MAX_BIND_ADDRS];
- // SPProto parameters (UDP only)
- struct spproto_security_params sp_params;
- // server address we connect to
- BAddr server_addr;
- // server name to use for SSL
- char server_name[256];
- // reactor
- BReactor ss;
- // thread work dispatcher
- BThreadWorkDispatcher twd;
- // client certificate if using SSL
- CERTCertificate *client_cert;
- // client private key if using SSL
- SECKEYPrivateKey *client_key;
- // device
- BTap device;
- int device_mtu;
- // DataProtoSource for device input (reading)
- DataProtoSource device_dpsource;
- // DPReceiveDevice for device output (writing)
- DPReceiveDevice device_output_dprd;
- // data communication MTU
- int data_mtu;
- // peers list
- LinkedList1 peers;
- int num_peers;
- // frame decider
- FrameDecider frame_decider;
- // peers that can be user as relays
- LinkedList1 relays;
- // peers than need a relay
- LinkedList1 waiting_relay_peers;
- // server connection
- ServerConnection server;
- // my ID, defined only after server_ready
- peerid_t my_id;
- // fair queue for sending peer messages to the server
- PacketPassFairQueue server_queue;
- // whether server is ready
- int server_ready;
- // dying server flow
- struct server_flow *dying_server_flow;
- // stops event processing, causing the program to exit
- static void terminate (void);
- // prints program name and version to standard output
- static void print_help (const char *name);
- // prints program name and version to standard output
- static void print_version (void);
- // parses the command line
- static int parse_arguments (int argc, char *argv[]);
- // processes certain command line options
- static int process_arguments (void);
- static int ssl_flags (void);
- // handler for program termination request
- static void signal_handler (void *unused);
- // adds a new peer
- static void peer_add (peerid_t id, int flags, const uint8_t *cert, int cert_len);
- // removes a peer
- static void peer_remove (struct peer_data *peer, int exiting);
- // appends the peer log prefix to the logger
- static void peer_logfunc (struct peer_data *peer);
- // passes a message to the logger, prepending it info about the peer
- static void peer_log (struct peer_data *peer, int level, const char *fmt, ...);
- // see if we are the master relative to this peer
- static int peer_am_master (struct peer_data *peer);
- // frees PeerChat, disconnecting it from the server flow
- static void peer_free_chat (struct peer_data *peer);
- // initializes the link
- static int peer_init_link (struct peer_data *peer);
- // frees link resources
- static void peer_free_link (struct peer_data *peer);
- // frees link, relaying, waiting relaying
- static void peer_cleanup_connections (struct peer_data *peer);
- // registers the peer as a relay provider
- static void peer_enable_relay_provider (struct peer_data *peer);
- // unregisters the peer as a relay provider
- static void peer_disable_relay_provider (struct peer_data *peer);
- // install relaying for a peer
- static void peer_install_relaying (struct peer_data *peer, struct peer_data *relay);
- // uninstall relaying for a peer
- static void peer_free_relaying (struct peer_data *peer);
- // handle a peer that needs a relay
- static void peer_need_relay (struct peer_data *peer);
- // inserts the peer into the need relay list
- static void peer_register_need_relay (struct peer_data *peer);
- // removes the peer from the need relay list
- static void peer_unregister_need_relay (struct peer_data *peer);
- // handle a link setup failure
- static void peer_reset (struct peer_data *peer);
- // fees chat and sends resetpeer
- static void peer_resetpeer (struct peer_data *peer);
- // chat handlers
- static void peer_chat_handler_error (struct peer_data *peer);
- static void peer_chat_handler_message (struct peer_data *peer, uint8_t *data, int data_len);
- // handlers for different message types
- static void peer_msg_youconnect (struct peer_data *peer, uint8_t *data, int data_len);
- static void peer_msg_cannotconnect (struct peer_data *peer, uint8_t *data, int data_len);
- static void peer_msg_cannotbind (struct peer_data *peer, uint8_t *data, int data_len);
- static void peer_msg_seed (struct peer_data *peer, uint8_t *data, int data_len);
- static void peer_msg_confirmseed (struct peer_data *peer, uint8_t *data, int data_len);
- static void peer_msg_youretry (struct peer_data *peer, uint8_t *data, int data_len);
- // handler from DatagramPeerIO when we should generate a new OTP send seed
- static void peer_udp_pio_handler_seed_warning (struct peer_data *peer);
- // handler from DatagramPeerIO when a new OTP seed can be recognized once it was provided to it
- static void peer_udp_pio_handler_seed_ready (struct peer_data *peer);
- // handler from DatagramPeerIO when an error occurs on the connection
- static void peer_udp_pio_handler_error (struct peer_data *peer);
- // handler from StreamPeerIO when an error occurs on the connection
- static void peer_tcp_pio_handler_error (struct peer_data *peer);
- // peer retry timer handler. The timer is used only on the master side,
- // wither when we detect an error, or the peer reports an error.
- static void peer_reset_timer_handler (struct peer_data *peer);
- // start binding, according to the protocol
- static void peer_start_binding (struct peer_data *peer);
- // tries binding on one address, according to the protocol
- static void peer_bind (struct peer_data *peer);
- static void peer_bind_one_address (struct peer_data *peer, int addr_index, int *cont);
- static void peer_connect (struct peer_data *peer, BAddr addr, uint8_t *encryption_key, uint64_t password);
- static int peer_start_msg (struct peer_data *peer, void **data, int type, int len);
- static void peer_end_msg (struct peer_data *peer);
- // sends a message with no payload to the peer
- static void peer_send_simple (struct peer_data *peer, int msgid);
- static void peer_send_conectinfo (struct peer_data *peer, int addr_index, int port_adjust, uint8_t *enckey, uint64_t pass);
- static void peer_send_confirmseed (struct peer_data *peer, uint16_t seed_id);
- // handler for peer DataProto up state changes
- static void peer_dataproto_handler (struct peer_data *peer, int up);
- // looks for a peer with the given ID
- static struct peer_data * find_peer_by_id (peerid_t id);
- // device error handler
- static void device_error_handler (void *unused);
- // DataProtoSource handler for packets from the device
- static void device_dpsource_handler (void *unused, const uint8_t *frame, int frame_len);
- // assign relays to clients waiting for them
- static void assign_relays (void);
- // checks if the given address scope is known (i.e. we can connect to an address in it)
- static char * address_scope_known (uint8_t *name, int name_len);
- // handlers for server messages
- static void server_handler_error (void *user);
- static void server_handler_ready (void *user, peerid_t param_my_id, uint32_t ext_ip);
- static void server_handler_newclient (void *user, peerid_t peer_id, int flags, const uint8_t *cert, int cert_len);
- static void server_handler_endclient (void *user, peerid_t peer_id);
- static void server_handler_message (void *user, peerid_t peer_id, uint8_t *data, int data_len);
- // jobs
- static void peer_job_send_seed (struct peer_data *peer);
- static void peer_job_init (struct peer_data *peer);
- // server flows
- static struct server_flow * server_flow_init (void);
- static void server_flow_free (struct server_flow *flow);
- static void server_flow_die (struct server_flow *flow);
- static void server_flow_qflow_handler_busy (struct server_flow *flow);
- static void server_flow_connect (struct server_flow *flow, PacketRecvInterface *input);
- static void server_flow_disconnect (struct server_flow *flow);
- int main (int argc, char *argv[])
- {
- if (argc <= 0) {
- return 1;
- }
-
- // open standard streams
- open_standard_streams();
-
- // parse command-line arguments
- if (!parse_arguments(argc, argv)) {
- fprintf(stderr, "Failed to parse arguments\n");
- print_help(argv[0]);
- goto fail0;
- }
-
- // handle --help and --version
- if (options.help) {
- print_version();
- print_help(argv[0]);
- return 0;
- }
- if (options.version) {
- print_version();
- return 0;
- }
-
- // initialize logger
- switch (options.logger) {
- case LOGGER_STDOUT:
- BLog_InitStdout();
- break;
- #ifndef BADVPN_USE_WINAPI
- case LOGGER_SYSLOG:
- if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
- fprintf(stderr, "Failed to initialize syslog logger\n");
- goto fail0;
- }
- break;
- #endif
- default:
- ASSERT(0);
- }
-
- // configure logger channels
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- if (options.loglevels[i] >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevels[i]);
- }
- else if (options.loglevel >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevel);
- }
- }
-
- BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
-
- if (options.ssl) {
- // init NSPR
- PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
-
- // register local NSPR file types
- if (!DummyPRFileDesc_GlobalInit()) {
- BLog(BLOG_ERROR, "DummyPRFileDesc_GlobalInit failed");
- goto fail01;
- }
- if (!BSSLConnection_GlobalInit()) {
- BLog(BLOG_ERROR, "BSSLConnection_GlobalInit failed");
- goto fail01;
- }
-
- // init NSS
- if (NSS_Init(options.nssdb) != SECSuccess) {
- BLog(BLOG_ERROR, "NSS_Init failed (%d)", (int)PR_GetError());
- goto fail01;
- }
-
- // set cipher policy
- if (NSS_SetDomesticPolicy() != SECSuccess) {
- BLog(BLOG_ERROR, "NSS_SetDomesticPolicy failed (%d)", (int)PR_GetError());
- goto fail02;
- }
-
- // init server cache
- if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
- BLog(BLOG_ERROR, "SSL_ConfigServerSessionIDCache failed (%d)", (int)PR_GetError());
- goto fail02;
- }
-
- // open server certificate and private key
- if (!open_nss_cert_and_key(options.client_cert_name, &client_cert, &client_key)) {
- BLog(BLOG_ERROR, "Cannot open certificate and key");
- goto fail03;
- }
- }
-
- // initialize network
- if (!BNetwork_GlobalInit()) {
- BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
- goto fail1;
- }
-
- // init time
- BTime_Init();
-
- // process arguments
- if (!process_arguments()) {
- BLog(BLOG_ERROR, "Failed to process arguments");
- goto fail1;
- }
-
- // init reactor
- if (!BReactor_Init(&ss)) {
- BLog(BLOG_ERROR, "BReactor_Init failed");
- goto fail1;
- }
-
- // setup signal handler
- if (!BSignal_Init(&ss, signal_handler, NULL)) {
- BLog(BLOG_ERROR, "BSignal_Init failed");
- goto fail2;
- }
-
- // init thread work dispatcher
- if (!BThreadWorkDispatcher_Init(&twd, &ss, options.threads)) {
- BLog(BLOG_ERROR, "BThreadWorkDispatcher_Init failed");
- goto fail3;
- }
-
- // init BSecurity
- if (BThreadWorkDispatcher_UsingThreads(&twd)) {
- if (!BSecurity_GlobalInitThreadSafe()) {
- BLog(BLOG_ERROR, "BSecurity_GlobalInitThreadSafe failed");
- goto fail4;
- }
- }
-
- // init listeners
- int num_listeners = 0;
- if (options.transport_mode == TRANSPORT_MODE_TCP) {
- while (num_listeners < num_bind_addrs) {
- struct bind_addr *addr = &bind_addrs[num_listeners];
- if (!PasswordListener_Init(&listeners[num_listeners], &ss, &twd, addr->addr, TCP_MAX_PASSWORD_LISTENER_CLIENTS, options.peer_ssl, ssl_flags(), client_cert, client_key)) {
- BLog(BLOG_ERROR, "PasswordListener_Init failed");
- goto fail8;
- }
- num_listeners++;
- }
- }
-
- // init device
- if (!BTap_Init(&device, &ss, options.tapdev, device_error_handler, NULL, 0)) {
- BLog(BLOG_ERROR, "BTap_Init failed");
- goto fail8;
- }
-
- // remember device MTU
- device_mtu = BTap_GetMTU(&device);
-
- BLog(BLOG_INFO, "device MTU is %d", device_mtu);
-
- // calculate data MTU
- if (device_mtu > INT_MAX - DATAPROTO_MAX_OVERHEAD) {
- BLog(BLOG_ERROR, "Device MTU is too large");
- goto fail9;
- }
- data_mtu = DATAPROTO_MAX_OVERHEAD + device_mtu;
-
- // init device input
- if (!DataProtoSource_Init(&device_dpsource, BTap_GetOutput(&device), device_dpsource_handler, NULL, &ss)) {
- BLog(BLOG_ERROR, "DataProtoSource_Init failed");
- goto fail9;
- }
-
- // init device output
- if (!DPReceiveDevice_Init(&device_output_dprd, device_mtu, (DPReceiveDevice_output_func)BTap_Send, &device, &ss, options.send_buffer_relay_size, PEER_RELAY_FLOW_INACTIVITY_TIME)) {
- BLog(BLOG_ERROR, "DPReceiveDevice_Init failed");
- goto fail10;
- }
-
- // init peers list
- LinkedList1_Init(&peers);
- num_peers = 0;
-
- // init frame decider
- FrameDecider_Init(&frame_decider, options.max_macs, options.max_groups, options.igmp_group_membership_interval, options.igmp_last_member_query_time, &ss);
-
- // init relays list
- LinkedList1_Init(&relays);
-
- // init need relay list
- LinkedList1_Init(&waiting_relay_peers);
-
- // start connecting to server
- if (!ServerConnection_Init(&server, &ss, &twd, server_addr, SC_KEEPALIVE_INTERVAL, SERVER_BUFFER_MIN_PACKETS, options.ssl, ssl_flags(), client_cert, client_key, server_name, NULL,
- server_handler_error, server_handler_ready, server_handler_newclient, server_handler_endclient, server_handler_message
- )) {
- BLog(BLOG_ERROR, "ServerConnection_Init failed");
- goto fail11;
- }
-
- // set server not ready
- server_ready = 0;
-
- // set no dying flow
- dying_server_flow = NULL;
-
- // enter event loop
- BLog(BLOG_NOTICE, "entering event loop");
- BReactor_Exec(&ss);
-
- if (server_ready) {
- // allow freeing server queue flows
- PacketPassFairQueue_PrepareFree(&server_queue);
-
- // make ServerConnection stop using buffers from peers before they are freed
- ServerConnection_ReleaseBuffers(&server);
- }
-
- // free peers
- LinkedList1Node *node;
- while (node = LinkedList1_GetFirst(&peers)) {
- struct peer_data *peer = UPPER_OBJECT(node, struct peer_data, list_node);
- peer_remove(peer, 1);
- }
-
- // free dying server flow
- if (dying_server_flow) {
- server_flow_free(dying_server_flow);
- }
-
- if (server_ready) {
- PacketPassFairQueue_Free(&server_queue);
- }
- ServerConnection_Free(&server);
- fail11:
- FrameDecider_Free(&frame_decider);
- DPReceiveDevice_Free(&device_output_dprd);
- fail10:
- DataProtoSource_Free(&device_dpsource);
- fail9:
- BTap_Free(&device);
- fail8:
- if (options.transport_mode == TRANSPORT_MODE_TCP) {
- while (num_listeners-- > 0) {
- PasswordListener_Free(&listeners[num_listeners]);
- }
- }
- if (BThreadWorkDispatcher_UsingThreads(&twd)) {
- BSecurity_GlobalFreeThreadSafe();
- }
- fail4:
- // NOTE: BThreadWorkDispatcher must be freed before NSPR and stuff
- BThreadWorkDispatcher_Free(&twd);
- fail3:
- BSignal_Finish();
- fail2:
- BReactor_Free(&ss);
- fail1:
- if (options.ssl) {
- CERT_DestroyCertificate(client_cert);
- SECKEY_DestroyPrivateKey(client_key);
- fail03:
- ASSERT_FORCE(SSL_ShutdownServerSessionIDCache() == SECSuccess)
- fail02:
- SSL_ClearSessionCache();
- ASSERT_FORCE(NSS_Shutdown() == SECSuccess)
- fail01:
- ASSERT_FORCE(PR_Cleanup() == PR_SUCCESS)
- PL_ArenaFinish();
- }
- BLog(BLOG_NOTICE, "exiting");
- BLog_Free();
- fail0:
- // finish objects
- DebugObjectGlobal_Finish();
- return 1;
- }
- void terminate (void)
- {
- BLog(BLOG_NOTICE, "tearing down");
-
- // exit event loop
- BReactor_Quit(&ss, 0);
- }
- void print_help (const char *name)
- {
- printf(
- "Usage:\n"
- " %s\n"
- " [--help]\n"
- " [--version]\n"
- " [--logger <"LOGGERS_STRING">]\n"
- #ifndef BADVPN_USE_WINAPI
- " (logger=syslog?\n"
- " [--syslog-facility <string>]\n"
- " [--syslog-ident <string>]\n"
- " )\n"
- #endif
- " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
- " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
- " [--threads <integer>]\n"
- " [--use-threads-for-ssl-handshake]\n"
- " [--use-threads-for-ssl-data]\n"
- " [--ssl --nssdb <string> --client-cert-name <string>]\n"
- " [--server-name <string>]\n"
- " --server-addr <addr>\n"
- " [--tapdev <name>]\n"
- " [--scope <scope_name>] ...\n"
- " [\n"
- " --bind-addr <addr>\n"
- " (transport-mode=udp? --num-ports <num>)\n"
- " [--ext-addr <addr / {server_reported}:port> <scope_name>] ...\n"
- " ] ...\n"
- " --transport-mode <udp/tcp>\n"
- " (transport-mode=udp?\n"
- " --encryption-mode <blowfish/aes/none>\n"
- " --hash-mode <md5/sha1/none>\n"
- " [--otp <blowfish/aes> <num> <num-warn>]\n"
- " [--fragmentation-latency <milliseconds>]\n"
- " )\n"
- " (transport-mode=tcp?\n"
- " (ssl? [--peer-ssl])\n"
- " [--peer-tcp-socket-sndbuf <bytes / 0>]\n"
- " )\n"
- " [--send-buffer-size <num-packets>]\n"
- " [--send-buffer-relay-size <num-packets>]\n"
- " [--max-macs <num>]\n"
- " [--max-groups <num>]\n"
- " [--igmp-group-membership-interval <ms>]\n"
- " [--igmp-last-member-query-time <ms>]\n"
- " [--allow-peer-talk-without-ssl]\n"
- " [--max-peers <number>]\n"
- "Address format is a.b.c.d:port (IPv4) or [addr]:port (IPv6).\n",
- name
- );
- }
- void print_version (void)
- {
- printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
- }
- int parse_arguments (int argc, char *argv[])
- {
- if (argc <= 0) {
- return 0;
- }
-
- options.help = 0;
- options.version = 0;
- options.logger = LOGGER_STDOUT;
- #ifndef BADVPN_USE_WINAPI
- options.logger_syslog_facility = "daemon";
- options.logger_syslog_ident = argv[0];
- #endif
- options.loglevel = -1;
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- options.loglevels[i] = -1;
- }
- options.threads = 0;
- options.use_threads_for_ssl_handshake = 0;
- options.use_threads_for_ssl_data = 0;
- options.ssl = 0;
- options.nssdb = NULL;
- options.client_cert_name = NULL;
- options.server_name = NULL;
- options.server_addr = NULL;
- options.tapdev = NULL;
- options.num_scopes = 0;
- options.num_bind_addrs = 0;
- options.transport_mode = -1;
- options.encryption_mode = -1;
- options.hash_mode = -1;
- options.otp_mode = SPPROTO_OTP_MODE_NONE;
- options.fragmentation_latency = PEER_DEFAULT_UDP_FRAGMENTATION_LATENCY;
- options.peer_ssl = 0;
- options.peer_tcp_socket_sndbuf = -1;
- options.send_buffer_size = PEER_DEFAULT_SEND_BUFFER_SIZE;
- options.send_buffer_relay_size = PEER_DEFAULT_SEND_BUFFER_RELAY_SIZE;
- options.max_macs = PEER_DEFAULT_MAX_MACS;
- options.max_groups = PEER_DEFAULT_MAX_GROUPS;
- options.igmp_group_membership_interval = DEFAULT_IGMP_GROUP_MEMBERSHIP_INTERVAL;
- options.igmp_last_member_query_time = DEFAULT_IGMP_LAST_MEMBER_QUERY_TIME;
- options.allow_peer_talk_without_ssl = 0;
- options.max_peers = DEFAULT_MAX_PEERS;
-
- int have_fragmentation_latency = 0;
-
- int i;
- for (i = 1; i < argc; i++) {
- char *arg = argv[i];
- if (!strcmp(arg, "--help")) {
- options.help = 1;
- }
- else if (!strcmp(arg, "--version")) {
- options.version = 1;
- }
- else if (!strcmp(arg, "--logger")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- char *arg2 = argv[i + 1];
- if (!strcmp(arg2, "stdout")) {
- options.logger = LOGGER_STDOUT;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg2, "syslog")) {
- options.logger = LOGGER_SYSLOG;
- }
- #endif
- else {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg, "--syslog-facility")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_facility = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--syslog-ident")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_ident = argv[i + 1];
- i++;
- }
- #endif
- else if (!strcmp(arg, "--loglevel")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--channel-loglevel")) {
- if (2 >= argc - i) {
- fprintf(stderr, "%s: requires two arguments\n", arg);
- return 0;
- }
- int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
- if (channel < 0) {
- fprintf(stderr, "%s: wrong channel argument\n", arg);
- return 0;
- }
- int loglevel = parse_loglevel(argv[i + 2]);
- if (loglevel < 0) {
- fprintf(stderr, "%s: wrong loglevel argument\n", arg);
- return 0;
- }
- options.loglevels[channel] = loglevel;
- i += 2;
- }
- else if (!strcmp(arg, "--threads")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.threads = atoi(argv[i + 1]);
- i++;
- }
- else if (!strcmp(arg, "--use-threads-for-ssl-handshake")) {
- options.use_threads_for_ssl_handshake = 1;
- }
- else if (!strcmp(arg, "--use-threads-for-ssl-data")) {
- options.use_threads_for_ssl_data = 1;
- }
- else if (!strcmp(arg, "--ssl")) {
- options.ssl = 1;
- }
- else if (!strcmp(arg, "--nssdb")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.nssdb = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--client-cert-name")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.client_cert_name = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--server-name")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.server_name = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--server-addr")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.server_addr = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--tapdev")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.tapdev = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--scope")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if (options.num_scopes == MAX_SCOPES) {
- fprintf(stderr, "%s: too many\n", arg);
- return 0;
- }
- options.scopes[options.num_scopes] = argv[i + 1];
- options.num_scopes++;
- i++;
- }
- else if (!strcmp(arg, "--bind-addr")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if (options.num_bind_addrs == MAX_BIND_ADDRS) {
- fprintf(stderr, "%s: too many\n", arg);
- return 0;
- }
- struct bind_addr_option *addr = &options.bind_addrs[options.num_bind_addrs];
- addr->addr = argv[i + 1];
- addr->num_ports = -1;
- addr->num_ext_addrs = 0;
- options.num_bind_addrs++;
- i++;
- }
- else if (!strcmp(arg, "--num-ports")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if (options.num_bind_addrs == 0) {
- fprintf(stderr, "%s: must folow --bind-addr\n", arg);
- return 0;
- }
- struct bind_addr_option *addr = &options.bind_addrs[options.num_bind_addrs - 1];
- if ((addr->num_ports = atoi(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--ext-addr")) {
- if (2 >= argc - i) {
- fprintf(stderr, "%s: requires two arguments\n", arg);
- return 0;
- }
- if (options.num_bind_addrs == 0) {
- fprintf(stderr, "%s: must folow --bind-addr\n", arg);
- return 0;
- }
- struct bind_addr_option *addr = &options.bind_addrs[options.num_bind_addrs - 1];
- if (addr->num_ext_addrs == MAX_EXT_ADDRS) {
- fprintf(stderr, "%s: too many\n", arg);
- return 0;
- }
- struct ext_addr_option *eaddr = &addr->ext_addrs[addr->num_ext_addrs];
- eaddr->addr = argv[i + 1];
- eaddr->scope = argv[i + 2];
- addr->num_ext_addrs++;
- i += 2;
- }
- else if (!strcmp(arg, "--transport-mode")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- char *arg2 = argv[i + 1];
- if (!strcmp(arg2, "udp")) {
- options.transport_mode = TRANSPORT_MODE_UDP;
- }
- else if (!strcmp(arg2, "tcp")) {
- options.transport_mode = TRANSPORT_MODE_TCP;
- }
- else {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--encryption-mode")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- char *arg2 = argv[i + 1];
- if (!strcmp(arg2, "none")) {
- options.encryption_mode = SPPROTO_ENCRYPTION_MODE_NONE;
- }
- else if (!strcmp(arg2, "blowfish")) {
- options.encryption_mode = BENCRYPTION_CIPHER_BLOWFISH;
- }
- else if (!strcmp(arg2, "aes")) {
- options.encryption_mode = BENCRYPTION_CIPHER_AES;
- }
- else {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--hash-mode")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- char *arg2 = argv[i + 1];
- if (!strcmp(arg2, "none")) {
- options.hash_mode = SPPROTO_HASH_MODE_NONE;
- }
- else if (!strcmp(arg2, "md5")) {
- options.hash_mode = BHASH_TYPE_MD5;
- }
- else if (!strcmp(arg2, "sha1")) {
- options.hash_mode = BHASH_TYPE_SHA1;
- }
- else {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--otp")) {
- if (3 >= argc - i) {
- fprintf(stderr, "%s: requires three arguments\n", arg);
- return 0;
- }
- char *otp_mode = argv[i + 1];
- char *otp_num = argv[i + 2];
- char *otp_num_warn = argv[i + 3];
- if (!strcmp(otp_mode, "blowfish")) {
- options.otp_mode = BENCRYPTION_CIPHER_BLOWFISH;
- }
- else if (!strcmp(otp_mode, "aes")) {
- options.otp_mode = BENCRYPTION_CIPHER_AES;
- }
- else {
- fprintf(stderr, "%s: wrong mode\n", arg);
- return 0;
- }
- if ((options.otp_num = atoi(otp_num)) <= 0) {
- fprintf(stderr, "%s: wrong num\n", arg);
- return 0;
- }
- options.otp_num_warn = atoi(otp_num_warn);
- if (options.otp_num_warn <= 0 || options.otp_num_warn > options.otp_num) {
- fprintf(stderr, "%s: wrong num warn\n", arg);
- return 0;
- }
- i += 3;
- }
- else if (!strcmp(arg, "--fragmentation-latency")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.fragmentation_latency = atoi(argv[i + 1]);
- have_fragmentation_latency = 1;
- i++;
- }
- else if (!strcmp(arg, "--peer-ssl")) {
- options.peer_ssl = 1;
- }
- else if (!strcmp(arg, "--peer-tcp-socket-sndbuf")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.peer_tcp_socket_sndbuf = atoi(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--send-buffer-size")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.send_buffer_size = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--send-buffer-relay-size")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.send_buffer_relay_size = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--max-macs")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.max_macs = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--max-groups")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.max_groups = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--igmp-group-membership-interval")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.igmp_group_membership_interval = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--igmp-last-member-query-time")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.igmp_last_member_query_time = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--max-peers")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.max_peers = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--allow-peer-talk-without-ssl")) {
- options.allow_peer_talk_without_ssl = 1;
- }
- else {
- fprintf(stderr, "unknown option: %s\n", arg);
- return 0;
- }
- }
-
- if (options.help || options.version) {
- return 1;
- }
-
- if (options.ssl != !!options.nssdb) {
- fprintf(stderr, "False: --ssl <=> --nssdb\n");
- return 0;
- }
-
- if (options.ssl != !!options.client_cert_name) {
- fprintf(stderr, "False: --ssl <=> --client-cert-name\n");
- return 0;
- }
-
- if (!options.server_addr) {
- fprintf(stderr, "False: --server-addr\n");
- return 0;
- }
-
- if (options.transport_mode < 0) {
- fprintf(stderr, "False: --transport-mode\n");
- return 0;
- }
-
- if ((options.transport_mode == TRANSPORT_MODE_UDP) != (options.encryption_mode >= 0)) {
- fprintf(stderr, "False: UDP <=> --encryption-mode\n");
- return 0;
- }
-
- if ((options.transport_mode == TRANSPORT_MODE_UDP) != (options.hash_mode >= 0)) {
- fprintf(stderr, "False: UDP <=> --hash-mode\n");
- return 0;
- }
-
- if (!(!(options.otp_mode != SPPROTO_OTP_MODE_NONE) || (options.transport_mode == TRANSPORT_MODE_UDP))) {
- fprintf(stderr, "False: --otp => UDP\n");
- return 0;
- }
-
- if (!(!have_fragmentation_latency || (options.transport_mode == TRANSPORT_MODE_UDP))) {
- fprintf(stderr, "False: --fragmentation-latency => UDP\n");
- return 0;
- }
-
- if (!(!options.peer_ssl || (options.ssl && options.transport_mode == TRANSPORT_MODE_TCP))) {
- fprintf(stderr, "False: --peer-ssl => (--ssl && TCP)\n");
- return 0;
- }
-
- if (!(!(options.peer_tcp_socket_sndbuf >= 0) || options.transport_mode == TRANSPORT_MODE_TCP)) {
- fprintf(stderr, "False: --peer-tcp-socket-sndbuf => TCP\n");
- return 0;
- }
-
- return 1;
- }
- int process_arguments (void)
- {
- // resolve server address
- ASSERT(options.server_addr)
- if (!BAddr_Parse(&server_addr, options.server_addr, server_name, sizeof(server_name))) {
- BLog(BLOG_ERROR, "server addr: BAddr_Parse failed");
- return 0;
- }
-
- // override server name if requested
- if (options.server_name) {
- if (strlen(options.server_name) >= sizeof(server_name)) {
- BLog(BLOG_ERROR, "server name: too long");
- return 0;
- }
- strcpy(server_name, options.server_name);
- }
-
- // resolve bind addresses and external addresses
- num_bind_addrs = 0;
- for (int i = 0; i < options.num_bind_addrs; i++) {
- struct bind_addr_option *addr = &options.bind_addrs[i];
- struct bind_addr *out = &bind_addrs[num_bind_addrs];
-
- // read addr
- if (!BAddr_Parse(&out->addr, addr->addr, NULL, 0)) {
- BLog(BLOG_ERROR, "bind addr: BAddr_Parse failed");
- return 0;
- }
-
- // read num ports
- if (options.transport_mode == TRANSPORT_MODE_UDP) {
- if (addr->num_ports < 0) {
- BLog(BLOG_ERROR, "bind addr: num ports missing");
- return 0;
- }
- out->num_ports = addr->num_ports;
- }
- else if (addr->num_ports >= 0) {
- BLog(BLOG_ERROR, "bind addr: num ports given, but not using UDP");
- return 0;
- }
-
- // read ext addrs
- out->num_ext_addrs = 0;
- for (int j = 0; j < addr->num_ext_addrs; j++) {
- struct ext_addr_option *eaddr = &addr->ext_addrs[j];
- struct ext_addr *eout = &out->ext_addrs[out->num_ext_addrs];
-
- // read addr
- if (string_begins_with(eaddr->addr, "{server_reported}:")) {
- char *colon = strstr(eaddr->addr, ":");
- if ((eout->server_reported_port = atoi(colon + 1)) < 0) {
- BLog(BLOG_ERROR, "ext addr: wrong port");
- return 0;
- }
- } else {
- eout->server_reported_port = -1;
- if (!BAddr_Parse(&eout->addr, eaddr->addr, NULL, 0)) {
- BLog(BLOG_ERROR, "ext addr: BAddr_Parse failed");
- return 0;
- }
- if (!addr_supported(eout->addr)) {
- BLog(BLOG_ERROR, "ext addr: addr_supported failed");
- return 0;
- }
- }
-
- // read scope
- if (strlen(eaddr->scope) >= sizeof(eout->scope)) {
- BLog(BLOG_ERROR, "ext addr: too long");
- return 0;
- }
- strcpy(eout->scope, eaddr->scope);
-
- out->num_ext_addrs++;
- }
-
- num_bind_addrs++;
- }
-
- // initialize SPProto parameters
- if (options.transport_mode == TRANSPORT_MODE_UDP) {
- sp_params.encryption_mode = options.encryption_mode;
- sp_params.hash_mode = options.hash_mode;
- sp_params.otp_mode = options.otp_mode;
- if (options.otp_mode > 0) {
- sp_params.otp_num = options.otp_num;
- }
- }
-
- return 1;
- }
- int ssl_flags (void)
- {
- int flags = 0;
- if (options.use_threads_for_ssl_handshake) {
- flags |= BSSLCONNECTION_FLAG_THREADWORK_HANDSHAKE;
- }
- if (options.use_threads_for_ssl_data) {
- flags |= BSSLCONNECTION_FLAG_THREADWORK_IO;
- }
- return flags;
- }
- void signal_handler (void *unused)
- {
- BLog(BLOG_NOTICE, "termination requested");
-
- terminate();
- }
- void peer_add (peerid_t id, int flags, const uint8_t *cert, int cert_len)
- {
- ASSERT(server_ready)
- ASSERT(num_peers < options.max_peers)
- ASSERT(!find_peer_by_id(id))
- ASSERT(id != my_id)
- ASSERT(cert_len >= 0)
- ASSERT(cert_len <= SCID_NEWCLIENT_MAX_CERT_LEN)
-
- // allocate structure
- struct peer_data *peer = (struct peer_data *)malloc(sizeof(*peer));
- if (!peer) {
- BLog(BLOG_ERROR, "peer %d: failed to allocate memory", (int)id);
- goto fail0;
- }
-
- // remember id
- peer->id = id;
-
- // remember flags
- peer->flags = flags;
-
- // set no common name
- peer->common_name = NULL;
-
- if (options.ssl) {
- // remember certificate
- memcpy(peer->cert, cert, cert_len);
- peer->cert_len = cert_len;
-
- // make sure that CERT_DecodeCertFromPackage will interpretet the input as raw DER and not base64,
- // in which case following workaroud wouldn't help
- if (!(cert_len > 0 && (cert[0] & 0x1f) == 0x10)) {
- peer_log(peer, BLOG_ERROR, "certificate does not look like DER");
- goto fail1;
- }
-
- // copy the certificate and append it a good load of zero bytes,
- // hopefully preventing the crappy CERT_DecodeCertFromPackage from crashing
- // by reading past the of its input
- uint8_t *certbuf = (uint8_t *)malloc(cert_len + 100);
- if (!certbuf) {
- peer_log(peer, BLOG_ERROR, "malloc failed");
- goto fail1;
- }
- memcpy(certbuf, cert, cert_len);
- memset(certbuf + cert_len, 0, 100);
-
- // decode certificate, so we can extract the common name
- CERTCertificate *nsscert = CERT_DecodeCertFromPackage((char *)certbuf, cert_len);
- if (!nsscert) {
- peer_log(peer, BLOG_ERROR, "CERT_DecodeCertFromPackage failed (%d)", PORT_GetError());
- free(certbuf);
- goto fail1;
- }
-
- free(certbuf);
-
- // remember common name
- if (!(peer->common_name = CERT_GetCommonName(&nsscert->subject))) {
- peer_log(peer, BLOG_ERROR, "CERT_GetCommonName failed");
- CERT_DestroyCertificate(nsscert);
- goto fail1;
- }
-
- CERT_DestroyCertificate(nsscert);
- }
-
- // init and set init job (must be before initing server flow so we can send)
- BPending_Init(&peer->job_init, BReactor_PendingGroup(&ss), (BPending_handler)peer_job_init, peer);
- BPending_Set(&peer->job_init);
-
- // init server flow
- if (!(peer->server_flow = server_flow_init())) {
- peer_log(peer, BLOG_ERROR, "server_flow_init failed");
- goto fail2;
- }
-
- if ((peer->flags & SCID_NEWCLIENT_FLAG_SSL) && !options.ssl) {
- peer_log(peer, BLOG_ERROR, "peer requires talking with SSL, but we're not using SSL!?");
- goto fail3;
- }
-
- if (options.ssl && !(peer->flags & SCID_NEWCLIENT_FLAG_SSL) && !options.allow_peer_talk_without_ssl) {
- peer_log(peer, BLOG_ERROR, "peer requires talking without SSL, but we don't allow that");
- goto fail3;
- }
-
- // choose chat SSL mode
- int chat_ssl_mode = PEERCHAT_SSL_NONE;
- if ((peer->flags & SCID_NEWCLIENT_FLAG_SSL)) {
- chat_ssl_mode = (peer_am_master(peer) ? PEERCHAT_SSL_SERVER : PEERCHAT_SSL_CLIENT);
- }
-
- // init chat
- if (!PeerChat_Init(&peer->chat, peer->id, chat_ssl_mode, ssl_flags(), client_cert, client_key, peer->cert, peer->cert_len, BReactor_PendingGroup(&ss), &twd, peer,
- (BLog_logfunc)peer_logfunc,
- (PeerChat_handler_error)peer_chat_handler_error,
- (PeerChat_handler_message)peer_chat_handler_message
- )) {
- peer_log(peer, BLOG_ERROR, "PeerChat_Init failed");
- goto fail3;
- }
-
- // set no message
- peer->chat_send_msg_len = -1;
-
- // connect server flow to chat
- server_flow_connect(peer->server_flow, PeerChat_GetSendOutput(&peer->chat));
-
- // set have chat
- peer->have_chat = 1;
-
- // set have no resetpeer
- peer->have_resetpeer = 0;
-
- // init local flow
- if (!DataProtoFlow_Init(&peer->local_dpflow, &device_dpsource, my_id, peer->id, options.send_buffer_size, -1, NULL, NULL)) {
- peer_log(peer, BLOG_ERROR, "DataProtoFlow_Init failed");
- goto fail4;
- }
-
- // init frame decider peer
- if (!FrameDeciderPeer_Init(&peer->decider_peer, &frame_decider, peer, (BLog_logfunc)peer_logfunc)) {
- peer_log(peer, BLOG_ERROR, "FrameDeciderPeer_Init failed");
- goto fail5;
- }
-
- // init receive peer
- DPReceivePeer_Init(&peer->receive_peer, &device_output_dprd, peer->id, &peer->decider_peer, !!(peer->flags & SCID_NEWCLIENT_FLAG_RELAY_CLIENT));
-
- // have no link
- peer->have_link = 0;
-
- // have no relaying
- peer->relaying_peer = NULL;
-
- // not waiting for relay
- peer->waiting_relay = 0;
-
- // init reset timer
- BTimer_Init(&peer->reset_timer, PEER_RETRY_TIME, (BTimer_handler)peer_reset_timer_handler, peer);
-
- // is not relay server
- peer->is_relay = 0;
-
- // init binding
- peer->binding = 0;
-
- // add to peers list
- LinkedList1_Append(&peers, &peer->list_node);
- num_peers++;
-
- switch (chat_ssl_mode) {
- case PEERCHAT_SSL_NONE:
- peer_log(peer, BLOG_INFO, "initialized; talking to peer in plaintext mode");
- break;
- case PEERCHAT_SSL_CLIENT:
- peer_log(peer, BLOG_INFO, "initialized; talking to peer in SSL client mode");
- break;
- case PEERCHAT_SSL_SERVER:
- peer_log(peer, BLOG_INFO, "initialized; talking to peer in SSL server mode");
- break;
- }
-
- return;
-
- fail5:
- DataProtoFlow_Free(&peer->local_dpflow);
- fail4:
- server_flow_disconnect(peer->server_flow);
- PeerChat_Free(&peer->chat);
- fail3:
- server_flow_free(peer->server_flow);
- fail2:
- BPending_Free(&peer->job_init);
- if (peer->common_name) {
- PORT_Free(peer->common_name);
- }
- fail1:
- free(peer);
- fail0:
- return;
- }
- void peer_remove (struct peer_data *peer, int exiting)
- {
- peer_log(peer, BLOG_INFO, "removing");
- …
Large files files are truncated, but you can click here to view the full file