| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- /**
- * @file StreamPeerIO.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * This file is part of BadVPN.
- *
- * BadVPN is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * BadVPN is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include <stdlib.h>
- #include <ssl.h>
- #include <sslerr.h>
- #include <misc/offset.h>
- #include <misc/debug.h>
- #include <misc/byteorder.h>
- #include <base/BLog.h>
- #include <client/StreamPeerIO.h>
- #include <generated/blog_channel_StreamPeerIO.h>
- #define MODE_NONE 0
- #define MODE_CONNECT 1
- #define MODE_LISTEN 2
- #define CONNECT_STATE_CONNECTING 0
- #define CONNECT_STATE_HANDSHAKE 1
- #define CONNECT_STATE_SENDING 2
- #define CONNECT_STATE_SENT 3
- #define CONNECT_STATE_FINISHED 4
- #define LISTEN_STATE_LISTENER 0
- #define LISTEN_STATE_GOTCLIENT 1
- #define LISTEN_STATE_FINISHED 2
- static void decoder_handler_error (StreamPeerIO *pio);
- static void connector_handler (StreamPeerIO *pio, int is_error);
- static void connection_handler (StreamPeerIO *pio, int event);
- static void connect_sslcon_handler (StreamPeerIO *pio, int event);
- static void pwsender_handler (StreamPeerIO *pio);
- static void listener_handler_client (StreamPeerIO *pio, sslsocket *sock);
- static int init_io (StreamPeerIO *pio, sslsocket *sock);
- static void free_io (StreamPeerIO *pio);
- static void sslcon_handler (StreamPeerIO *pio, int event);
- static SECStatus client_auth_certificate_callback (StreamPeerIO *pio, PRFileDesc *fd, PRBool checkSig, PRBool isServer);
- static SECStatus client_client_auth_data_callback (StreamPeerIO *pio, PRFileDesc *fd, CERTDistNames *caNames, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey);
- static int compare_certificate (StreamPeerIO *pio, CERTCertificate *cert);
- static void reset_state (StreamPeerIO *pio);
- static void reset_and_report_error (StreamPeerIO *pio);
- void decoder_handler_error (StreamPeerIO *pio)
- {
- DebugObject_Access(&pio->d_obj);
-
- BLog(BLOG_ERROR, "decoder error");
-
- reset_and_report_error(pio);
- return;
- }
- void connector_handler (StreamPeerIO *pio, int is_error)
- {
- DebugObject_Access(&pio->d_obj);
- ASSERT(pio->mode == MODE_CONNECT)
- ASSERT(pio->connect.state == CONNECT_STATE_CONNECTING)
-
- // check connection result
- if (is_error) {
- BLog(BLOG_NOTICE, "connection failed");
- goto fail0;
- }
-
- // init connection
- if (!BConnection_Init(&pio->connect.sock.con, BCONNECTION_SOURCE_CONNECTOR(&pio->connect.connector), pio->reactor, pio, (BConnection_handler)connection_handler)) {
- BLog(BLOG_ERROR, "BConnection_Init failed");
- goto fail0;
- }
-
- if (pio->ssl) {
- // init connection interfaces
- BConnection_SendAsync_Init(&pio->connect.sock.con);
- BConnection_RecvAsync_Init(&pio->connect.sock.con);
-
- // create bottom NSPR file descriptor
- if (!BSSLConnection_MakeBackend(&pio->connect.sock.bottom_prfd, BConnection_SendAsync_GetIf(&pio->connect.sock.con), BConnection_RecvAsync_GetIf(&pio->connect.sock.con))) {
- BLog(BLOG_ERROR, "BSSLConnection_MakeBackend failed");
- goto fail1;
- }
-
- // create SSL file descriptor from the bottom NSPR file descriptor
- if (!(pio->connect.sock.ssl_prfd = SSL_ImportFD(NULL, &pio->connect.sock.bottom_prfd))) {
- ASSERT_FORCE(PR_Close(&pio->connect.sock.bottom_prfd) == PR_SUCCESS)
- goto fail1;
- }
-
- // set client mode
- if (SSL_ResetHandshake(pio->connect.sock.ssl_prfd, PR_FALSE) != SECSuccess) {
- BLog(BLOG_ERROR, "SSL_ResetHandshake failed");
- goto fail2;
- }
-
- // set verify peer certificate hook
- if (SSL_AuthCertificateHook(pio->connect.sock.ssl_prfd, (SSLAuthCertificate)client_auth_certificate_callback, pio) != SECSuccess) {
- BLog(BLOG_ERROR, "SSL_AuthCertificateHook failed");
- goto fail2;
- }
-
- // set client certificate callback
- if (SSL_GetClientAuthDataHook(pio->connect.sock.ssl_prfd, (SSLGetClientAuthData)client_client_auth_data_callback, pio) != SECSuccess) {
- BLog(BLOG_ERROR, "SSL_GetClientAuthDataHook failed");
- goto fail2;
- }
-
- // init BSSLConnection
- BSSLConnection_Init(&pio->connect.sslcon, pio->connect.sock.ssl_prfd, 1, pio->reactor, pio, (BSSLConnection_handler)connect_sslcon_handler);
-
- // change state
- pio->connect.state = CONNECT_STATE_HANDSHAKE;
- } else {
- // init connection send interface
- BConnection_SendAsync_Init(&pio->connect.sock.con);
-
- // init password sender
- SingleStreamSender_Init(&pio->connect.pwsender, (uint8_t *)&pio->connect.password, sizeof(pio->connect.password), BConnection_SendAsync_GetIf(&pio->connect.sock.con), BReactor_PendingGroup(pio->reactor), pio, (SingleStreamSender_handler)pwsender_handler);
-
- // change state
- pio->connect.state = CONNECT_STATE_SENDING;
- }
-
- return;
- if (pio->ssl) {
- fail2:
- ASSERT_FORCE(PR_Close(pio->connect.sock.ssl_prfd) == PR_SUCCESS)
- fail1:
- BConnection_RecvAsync_Free(&pio->connect.sock.con);
- BConnection_SendAsync_Free(&pio->connect.sock.con);
- }
- BConnection_Free(&pio->connect.sock.con);
- fail0:
- reset_and_report_error(pio);
- return;
- }
- void connection_handler (StreamPeerIO *pio, int event)
- {
- DebugObject_Access(&pio->d_obj);
- ASSERT(pio->mode == MODE_CONNECT || pio->mode == MODE_LISTEN)
- ASSERT(!(pio->mode == MODE_CONNECT) || pio->connect.state >= CONNECT_STATE_HANDSHAKE)
- ASSERT(!(pio->mode == MODE_LISTEN) || pio->listen.state >= LISTEN_STATE_FINISHED)
-
- if (event == BCONNECTION_EVENT_RECVCLOSED) {
- BLog(BLOG_NOTICE, "connection closed");
- } else {
- BLog(BLOG_NOTICE, "connection error");
- }
-
- reset_and_report_error(pio);
- return;
- }
- void connect_sslcon_handler (StreamPeerIO *pio, int event)
- {
- DebugObject_Access(&pio->d_obj);
- ASSERT(pio->ssl)
- ASSERT(pio->mode == MODE_CONNECT)
- ASSERT(pio->connect.state == CONNECT_STATE_HANDSHAKE || pio->connect.state == CONNECT_STATE_SENDING)
- ASSERT(event == BSSLCONNECTION_EVENT_UP || event == BSSLCONNECTION_EVENT_ERROR)
-
- if (event == BSSLCONNECTION_EVENT_ERROR) {
- BLog(BLOG_NOTICE, "SSL error");
-
- reset_and_report_error(pio);
- return;
- }
-
- // handshake complete
- ASSERT(pio->connect.state == CONNECT_STATE_HANDSHAKE)
-
- // remove client certificate callback
- if (SSL_GetClientAuthDataHook(pio->connect.sock.ssl_prfd, NULL, NULL) != SECSuccess) {
- BLog(BLOG_ERROR, "SSL_GetClientAuthDataHook failed");
- goto fail0;
- }
-
- // remove verify peer certificate callback
- if (SSL_AuthCertificateHook(pio->connect.sock.ssl_prfd, NULL, NULL) != SECSuccess) {
- BLog(BLOG_ERROR, "SSL_AuthCertificateHook failed");
- goto fail0;
- }
-
- // init password sender
- SingleStreamSender_Init(&pio->connect.pwsender, (uint8_t *)&pio->connect.password, sizeof(pio->connect.password), BSSLConnection_GetSendIf(&pio->connect.sslcon), BReactor_PendingGroup(pio->reactor), pio, (SingleStreamSender_handler)pwsender_handler);
-
- // change state
- pio->connect.state = CONNECT_STATE_SENDING;
-
- return;
-
- fail0:
- reset_and_report_error(pio);
- return;
- }
- void pwsender_handler (StreamPeerIO *pio)
- {
- DebugObject_Access(&pio->d_obj);
- ASSERT(pio->mode == MODE_CONNECT)
- ASSERT(pio->connect.state == CONNECT_STATE_SENDING)
-
- // free password sender
- SingleStreamSender_Free(&pio->connect.pwsender);
-
- if (pio->ssl) {
- // free BSSLConnection (we used the send interface)
- BSSLConnection_Free(&pio->connect.sslcon);
- } else {
- // init connection send interface
- BConnection_SendAsync_Free(&pio->connect.sock.con);
- }
-
- // change state
- pio->connect.state = CONNECT_STATE_SENT;
-
- // setup i/o
- if (!init_io(pio, &pio->connect.sock)) {
- goto fail0;
- }
-
- // change state
- pio->connect.state = CONNECT_STATE_FINISHED;
-
- return;
-
- fail0:
- reset_and_report_error(pio);
- return;
- }
- void listener_handler_client (StreamPeerIO *pio, sslsocket *sock)
- {
- DebugObject_Access(&pio->d_obj);
- ASSERT(pio->mode == MODE_LISTEN)
- ASSERT(pio->listen.state == LISTEN_STATE_LISTENER)
-
- // remember socket
- pio->listen.sock = sock;
-
- // set connection handler
- BConnection_SetHandlers(&pio->listen.sock->con, pio, (BConnection_handler)connection_handler);
-
- // change state
- pio->listen.state = LISTEN_STATE_GOTCLIENT;
-
- // check ceritficate
- if (pio->ssl) {
- CERTCertificate *peer_cert = SSL_PeerCertificate(pio->listen.sock->ssl_prfd);
- if (!peer_cert) {
- BLog(BLOG_ERROR, "SSL_PeerCertificate failed");
- goto fail0;
- }
-
- // compare certificate to the one provided by the server
- if (!compare_certificate(pio, peer_cert)) {
- CERT_DestroyCertificate(peer_cert);
- goto fail0;
- }
-
- CERT_DestroyCertificate(peer_cert);
- }
-
- // setup i/o
- if (!init_io(pio, pio->listen.sock)) {
- goto fail0;
- }
-
- // change state
- pio->listen.state = LISTEN_STATE_FINISHED;
-
- return;
-
- fail0:
- reset_and_report_error(pio);
- return;
- }
- int init_io (StreamPeerIO *pio, sslsocket *sock)
- {
- ASSERT(!pio->sock)
-
- // limit socket send buffer, else our scheduling is pointless
- if (pio->sock_sndbuf > 0) {
- if (!BConnection_SetSendBuffer(&sock->con, pio->sock_sndbuf)) {
- BLog(BLOG_WARNING, "BConnection_SetSendBuffer failed");
- }
- }
-
- if (pio->ssl) {
- // init BSSLConnection
- BSSLConnection_Init(&pio->sslcon, sock->ssl_prfd, 0, pio->reactor, pio, (BSSLConnection_handler)sslcon_handler);
- } else {
- // init connection interfaces
- BConnection_SendAsync_Init(&sock->con);
- BConnection_RecvAsync_Init(&sock->con);
- }
-
- StreamPassInterface *send_if = (pio->ssl ? BSSLConnection_GetSendIf(&pio->sslcon) : BConnection_SendAsync_GetIf(&sock->con));
- StreamRecvInterface *recv_if = (pio->ssl ? BSSLConnection_GetRecvIf(&pio->sslcon) : BConnection_RecvAsync_GetIf(&sock->con));
-
- // init receiving
- StreamRecvConnector_ConnectInput(&pio->input_connector, recv_if);
-
- // init sending
- PacketStreamSender_Init(&pio->output_pss, send_if, PACKETPROTO_ENCLEN(pio->payload_mtu), BReactor_PendingGroup(pio->reactor));
- PacketPassConnector_ConnectOutput(&pio->output_connector, PacketStreamSender_GetInput(&pio->output_pss));
-
- pio->sock = sock;
-
- return 1;
- }
- void free_io (StreamPeerIO *pio)
- {
- ASSERT(pio->sock)
-
- // reset decoder
- PacketProtoDecoder_Reset(&pio->input_decoder);
-
- // free sending
- PacketPassConnector_DisconnectOutput(&pio->output_connector);
- PacketStreamSender_Free(&pio->output_pss);
-
- // free receiving
- StreamRecvConnector_DisconnectInput(&pio->input_connector);
-
- if (pio->ssl) {
- // free BSSLConnection
- BSSLConnection_Free(&pio->sslcon);
- } else {
- // free connection interfaces
- BConnection_RecvAsync_Free(&pio->sock->con);
- BConnection_SendAsync_Free(&pio->sock->con);
- }
-
- pio->sock = NULL;
- }
- void sslcon_handler (StreamPeerIO *pio, int event)
- {
- DebugObject_Access(&pio->d_obj);
- ASSERT(pio->ssl)
- ASSERT(pio->mode == MODE_CONNECT || pio->mode == MODE_LISTEN)
- ASSERT(!(pio->mode == MODE_CONNECT) || pio->connect.state == CONNECT_STATE_FINISHED)
- ASSERT(!(pio->mode == MODE_LISTEN) || pio->listen.state == LISTEN_STATE_FINISHED)
- ASSERT(event == BSSLCONNECTION_EVENT_ERROR)
-
- BLog(BLOG_NOTICE, "SSL error");
-
- reset_and_report_error(pio);
- return;
- }
- SECStatus client_auth_certificate_callback (StreamPeerIO *pio, PRFileDesc *fd, PRBool checkSig, PRBool isServer)
- {
- ASSERT(pio->ssl)
- ASSERT(pio->mode == MODE_CONNECT)
- ASSERT(pio->connect.state == CONNECT_STATE_HANDSHAKE)
- DebugObject_Access(&pio->d_obj);
-
- // This callback is used to bypass checking the server's domain name, as peers
- // don't have domain names. We byte-compare the certificate to the one reported
- // by the server anyway.
-
- SECStatus ret = SECFailure;
-
- CERTCertificate *server_cert = SSL_PeerCertificate(pio->connect.sock.ssl_prfd);
- if (!server_cert) {
- BLog(BLOG_ERROR, "SSL_PeerCertificate failed");
- PORT_SetError(SSL_ERROR_BAD_CERTIFICATE);
- goto fail1;
- }
-
- if (CERT_VerifyCertNow(CERT_GetDefaultCertDB(), server_cert, PR_TRUE, certUsageSSLServer, SSL_RevealPinArg(pio->connect.sock.ssl_prfd)) != SECSuccess) {
- goto fail2;
- }
-
- // compare to certificate provided by the server
- if (!compare_certificate(pio, server_cert)) {
- PORT_SetError(SSL_ERROR_BAD_CERTIFICATE);
- goto fail2;
- }
-
- ret = SECSuccess;
-
- fail2:
- CERT_DestroyCertificate(server_cert);
- fail1:
- return ret;
- }
- SECStatus client_client_auth_data_callback (StreamPeerIO *pio, PRFileDesc *fd, CERTDistNames *caNames, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey)
- {
- ASSERT(pio->ssl)
- ASSERT(pio->mode == MODE_CONNECT)
- ASSERT(pio->connect.state == CONNECT_STATE_HANDSHAKE)
- DebugObject_Access(&pio->d_obj);
-
- CERTCertificate *cert = CERT_DupCertificate(pio->connect.ssl_cert);
- if (!cert) {
- BLog(BLOG_ERROR, "CERT_DupCertificate failed");
- goto fail0;
- }
-
- SECKEYPrivateKey *key = SECKEY_CopyPrivateKey(pio->connect.ssl_key);
- if (!key) {
- BLog(BLOG_ERROR, "SECKEY_CopyPrivateKey failed");
- goto fail1;
- }
-
- *pRetCert = cert;
- *pRetKey = key;
- return SECSuccess;
-
- fail1:
- CERT_DestroyCertificate(cert);
- fail0:
- return SECFailure;
- }
- int compare_certificate (StreamPeerIO *pio, CERTCertificate *cert)
- {
- ASSERT(pio->ssl)
-
- SECItem der = cert->derCert;
- if (der.len != pio->ssl_peer_cert_len || memcmp(der.data, pio->ssl_peer_cert, der.len)) {
- BLog(BLOG_NOTICE, "Client certificate doesn't match");
- return 0;
- }
-
- return 1;
- }
- void reset_state (StreamPeerIO *pio)
- {
- // free resources
- switch (pio->mode) {
- case MODE_NONE:
- break;
- case MODE_LISTEN:
- switch (pio->listen.state) {
- case LISTEN_STATE_FINISHED:
- free_io(pio);
- case LISTEN_STATE_GOTCLIENT:
- if (pio->ssl) {
- ASSERT_FORCE(PR_Close(pio->listen.sock->ssl_prfd) == PR_SUCCESS)
- BConnection_RecvAsync_Free(&pio->listen.sock->con);
- BConnection_SendAsync_Free(&pio->listen.sock->con);
- }
- BConnection_Free(&pio->listen.sock->con);
- free(pio->listen.sock);
- case LISTEN_STATE_LISTENER:
- if (pio->listen.state == LISTEN_STATE_LISTENER) {
- PasswordListener_RemoveEntry(pio->listen.listener, &pio->listen.pwentry);
- }
- break;
- default:
- ASSERT(0);
- }
- break;
- case MODE_CONNECT:
- switch (pio->connect.state) {
- case CONNECT_STATE_FINISHED:
- free_io(pio);
- case CONNECT_STATE_SENT:
- case CONNECT_STATE_SENDING:
- if (pio->connect.state == CONNECT_STATE_SENDING) {
- SingleStreamSender_Free(&pio->connect.pwsender);
- if (!pio->ssl) {
- BConnection_SendAsync_Free(&pio->connect.sock.con);
- }
- }
- case CONNECT_STATE_HANDSHAKE:
- if (pio->ssl) {
- if (pio->connect.state == CONNECT_STATE_HANDSHAKE || pio->connect.state == CONNECT_STATE_SENDING) {
- BSSLConnection_Free(&pio->connect.sslcon);
- }
- ASSERT_FORCE(PR_Close(pio->connect.sock.ssl_prfd) == PR_SUCCESS)
- BConnection_RecvAsync_Free(&pio->connect.sock.con);
- BConnection_SendAsync_Free(&pio->connect.sock.con);
- }
- BConnection_Free(&pio->connect.sock.con);
- case CONNECT_STATE_CONNECTING:
- BConnector_Free(&pio->connect.connector);
- break;
- default:
- ASSERT(0);
- }
- break;
- default:
- ASSERT(0);
- }
-
- // set mode none
- pio->mode = MODE_NONE;
-
- ASSERT(!pio->sock)
- }
- void reset_and_report_error (StreamPeerIO *pio)
- {
- reset_state(pio);
-
- pio->handler_error(pio->user);
- return;
- }
- int StreamPeerIO_Init (
- StreamPeerIO *pio,
- BReactor *reactor,
- int ssl,
- uint8_t *ssl_peer_cert,
- int ssl_peer_cert_len,
- int payload_mtu,
- int sock_sndbuf,
- PacketPassInterface *user_recv_if,
- StreamPeerIO_handler_error handler_error,
- void *user
- )
- {
- ASSERT(ssl == 0 || ssl == 1)
- ASSERT(payload_mtu >= 0)
- ASSERT(PacketPassInterface_GetMTU(user_recv_if) >= payload_mtu)
- ASSERT(handler_error)
-
- // init arguments
- pio->reactor = reactor;
- pio->ssl = ssl;
- if (pio->ssl) {
- pio->ssl_peer_cert = ssl_peer_cert;
- pio->ssl_peer_cert_len = ssl_peer_cert_len;
- }
- pio->payload_mtu = payload_mtu;
- pio->sock_sndbuf = sock_sndbuf;
- pio->handler_error = handler_error;
- pio->user = user;
-
- // check payload MTU
- if (pio->payload_mtu > PACKETPROTO_MAXPAYLOAD) {
- BLog(BLOG_ERROR, "payload MTU is too large");
- goto fail0;
- }
-
- // init receiveing objects
- StreamRecvConnector_Init(&pio->input_connector, BReactor_PendingGroup(pio->reactor));
- if (!PacketProtoDecoder_Init(&pio->input_decoder, StreamRecvConnector_GetOutput(&pio->input_connector), user_recv_if, BReactor_PendingGroup(pio->reactor), pio,
- (PacketProtoDecoder_handler_error)decoder_handler_error
- )) {
- BLog(BLOG_ERROR, "FlowErrorDomain_Init failed");
- goto fail1;
- }
-
- // init sending objects
- PacketCopier_Init(&pio->output_user_copier, pio->payload_mtu, BReactor_PendingGroup(pio->reactor));
- PacketProtoEncoder_Init(&pio->output_user_ppe, PacketCopier_GetOutput(&pio->output_user_copier), BReactor_PendingGroup(pio->reactor));
- PacketPassConnector_Init(&pio->output_connector, PACKETPROTO_ENCLEN(pio->payload_mtu), BReactor_PendingGroup(pio->reactor));
- if (!SinglePacketBuffer_Init(&pio->output_user_spb, PacketProtoEncoder_GetOutput(&pio->output_user_ppe), PacketPassConnector_GetInput(&pio->output_connector), BReactor_PendingGroup(pio->reactor))) {
- BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
- goto fail2;
- }
-
- // set mode none
- pio->mode = MODE_NONE;
-
- // set no socket
- pio->sock = NULL;
-
- DebugObject_Init(&pio->d_obj);
- return 1;
-
- fail2:
- PacketPassConnector_Free(&pio->output_connector);
- PacketProtoEncoder_Free(&pio->output_user_ppe);
- PacketCopier_Free(&pio->output_user_copier);
- PacketProtoDecoder_Free(&pio->input_decoder);
- fail1:
- StreamRecvConnector_Free(&pio->input_connector);
- fail0:
- return 0;
- }
- void StreamPeerIO_Free (StreamPeerIO *pio)
- {
- DebugObject_Free(&pio->d_obj);
- // reset state
- reset_state(pio);
-
- // free sending objects
- SinglePacketBuffer_Free(&pio->output_user_spb);
- PacketPassConnector_Free(&pio->output_connector);
- PacketProtoEncoder_Free(&pio->output_user_ppe);
- PacketCopier_Free(&pio->output_user_copier);
-
- // free receiveing objects
- PacketProtoDecoder_Free(&pio->input_decoder);
- StreamRecvConnector_Free(&pio->input_connector);
- }
- PacketPassInterface * StreamPeerIO_GetSendInput (StreamPeerIO *pio)
- {
- DebugObject_Access(&pio->d_obj);
-
- return PacketCopier_GetInput(&pio->output_user_copier);
- }
- int StreamPeerIO_Connect (StreamPeerIO *pio, BAddr addr, uint64_t password, CERTCertificate *ssl_cert, SECKEYPrivateKey *ssl_key)
- {
- DebugObject_Access(&pio->d_obj);
- ASSERT(BConnection_AddressSupported(addr))
-
- // reset state
- reset_state(pio);
-
- // init connector
- if (!BConnector_Init(&pio->connect.connector, addr, pio->reactor, pio, (BConnector_handler)connector_handler)) {
- BLog(BLOG_ERROR, "BConnector_Init failed");
- goto fail0;
- }
-
- // remember data
- if (pio->ssl) {
- pio->connect.ssl_cert = ssl_cert;
- pio->connect.ssl_key = ssl_key;
- }
- pio->connect.password = htol64(password);
-
- // set state
- pio->mode = MODE_CONNECT;
- pio->connect.state = CONNECT_STATE_CONNECTING;
-
- return 1;
-
- fail0:
- return 0;
- }
- void StreamPeerIO_Listen (StreamPeerIO *pio, PasswordListener *listener, uint64_t *password)
- {
- DebugObject_Access(&pio->d_obj);
- ASSERT(listener->ssl == pio->ssl)
-
- // reset state
- reset_state(pio);
-
- // add PasswordListener entry
- uint64_t newpass = PasswordListener_AddEntry(listener, &pio->listen.pwentry, (PasswordListener_handler_client)listener_handler_client, pio);
-
- // remember data
- pio->listen.listener = listener;
-
- // set state
- pio->mode = MODE_LISTEN;
- pio->listen.state = LISTEN_STATE_LISTENER;
-
- *password = newpass;
- }
|