StreamPeerIO.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /**
  2. * @file StreamPeerIO.c
  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 <stdlib.h>
  23. #include <ssl.h>
  24. #include <sslerr.h>
  25. #include <misc/offset.h>
  26. #include <misc/debug.h>
  27. #include <misc/byteorder.h>
  28. #include <base/BLog.h>
  29. #include <client/StreamPeerIO.h>
  30. #include <generated/blog_channel_StreamPeerIO.h>
  31. #define MODE_NONE 0
  32. #define MODE_CONNECT 1
  33. #define MODE_LISTEN 2
  34. #define CONNECT_STATE_CONNECTING 0
  35. #define CONNECT_STATE_HANDSHAKE 1
  36. #define CONNECT_STATE_SENDING 2
  37. #define CONNECT_STATE_SENT 3
  38. #define CONNECT_STATE_FINISHED 4
  39. #define LISTEN_STATE_LISTENER 0
  40. #define LISTEN_STATE_GOTCLIENT 1
  41. #define LISTEN_STATE_FINISHED 2
  42. static void decoder_handler_error (StreamPeerIO *pio);
  43. static void connector_handler (StreamPeerIO *pio, int is_error);
  44. static void connection_handler (StreamPeerIO *pio, int event);
  45. static void connect_sslcon_handler (StreamPeerIO *pio, int event);
  46. static void pwsender_handler (StreamPeerIO *pio);
  47. static void listener_handler_client (StreamPeerIO *pio, sslsocket *sock);
  48. static int init_io (StreamPeerIO *pio, sslsocket *sock);
  49. static void free_io (StreamPeerIO *pio);
  50. static void sslcon_handler (StreamPeerIO *pio, int event);
  51. static SECStatus client_auth_certificate_callback (StreamPeerIO *pio, PRFileDesc *fd, PRBool checkSig, PRBool isServer);
  52. static SECStatus client_client_auth_data_callback (StreamPeerIO *pio, PRFileDesc *fd, CERTDistNames *caNames, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey);
  53. static int compare_certificate (StreamPeerIO *pio, CERTCertificate *cert);
  54. static void reset_state (StreamPeerIO *pio);
  55. static void reset_and_report_error (StreamPeerIO *pio);
  56. void decoder_handler_error (StreamPeerIO *pio)
  57. {
  58. DebugObject_Access(&pio->d_obj);
  59. BLog(BLOG_ERROR, "decoder error");
  60. reset_and_report_error(pio);
  61. return;
  62. }
  63. void connector_handler (StreamPeerIO *pio, int is_error)
  64. {
  65. DebugObject_Access(&pio->d_obj);
  66. ASSERT(pio->mode == MODE_CONNECT)
  67. ASSERT(pio->connect.state == CONNECT_STATE_CONNECTING)
  68. // check connection result
  69. if (is_error) {
  70. BLog(BLOG_NOTICE, "connection failed");
  71. goto fail0;
  72. }
  73. // init connection
  74. if (!BConnection_Init(&pio->connect.sock.con, BCONNECTION_SOURCE_CONNECTOR(&pio->connect.connector), pio->reactor, pio, (BConnection_handler)connection_handler)) {
  75. BLog(BLOG_ERROR, "BConnection_Init failed");
  76. goto fail0;
  77. }
  78. if (pio->ssl) {
  79. // init connection interfaces
  80. BConnection_SendAsync_Init(&pio->connect.sock.con);
  81. BConnection_RecvAsync_Init(&pio->connect.sock.con);
  82. // create bottom NSPR file descriptor
  83. if (!BSSLConnection_MakeBackend(&pio->connect.sock.bottom_prfd, BConnection_SendAsync_GetIf(&pio->connect.sock.con), BConnection_RecvAsync_GetIf(&pio->connect.sock.con))) {
  84. BLog(BLOG_ERROR, "BSSLConnection_MakeBackend failed");
  85. goto fail1;
  86. }
  87. // create SSL file descriptor from the bottom NSPR file descriptor
  88. if (!(pio->connect.sock.ssl_prfd = SSL_ImportFD(NULL, &pio->connect.sock.bottom_prfd))) {
  89. ASSERT_FORCE(PR_Close(&pio->connect.sock.bottom_prfd) == PR_SUCCESS)
  90. goto fail1;
  91. }
  92. // set client mode
  93. if (SSL_ResetHandshake(pio->connect.sock.ssl_prfd, PR_FALSE) != SECSuccess) {
  94. BLog(BLOG_ERROR, "SSL_ResetHandshake failed");
  95. goto fail2;
  96. }
  97. // set verify peer certificate hook
  98. if (SSL_AuthCertificateHook(pio->connect.sock.ssl_prfd, (SSLAuthCertificate)client_auth_certificate_callback, pio) != SECSuccess) {
  99. BLog(BLOG_ERROR, "SSL_AuthCertificateHook failed");
  100. goto fail2;
  101. }
  102. // set client certificate callback
  103. if (SSL_GetClientAuthDataHook(pio->connect.sock.ssl_prfd, (SSLGetClientAuthData)client_client_auth_data_callback, pio) != SECSuccess) {
  104. BLog(BLOG_ERROR, "SSL_GetClientAuthDataHook failed");
  105. goto fail2;
  106. }
  107. // init BSSLConnection
  108. BSSLConnection_Init(&pio->connect.sslcon, pio->connect.sock.ssl_prfd, 1, BReactor_PendingGroup(pio->reactor), pio, (BSSLConnection_handler)connect_sslcon_handler);
  109. // change state
  110. pio->connect.state = CONNECT_STATE_HANDSHAKE;
  111. } else {
  112. // init connection send interface
  113. BConnection_SendAsync_Init(&pio->connect.sock.con);
  114. // init password sender
  115. 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);
  116. // change state
  117. pio->connect.state = CONNECT_STATE_SENDING;
  118. }
  119. return;
  120. if (pio->ssl) {
  121. fail2:
  122. ASSERT_FORCE(PR_Close(pio->connect.sock.ssl_prfd) == PR_SUCCESS)
  123. fail1:
  124. BConnection_RecvAsync_Free(&pio->connect.sock.con);
  125. BConnection_SendAsync_Free(&pio->connect.sock.con);
  126. }
  127. BConnection_Free(&pio->connect.sock.con);
  128. fail0:
  129. reset_and_report_error(pio);
  130. return;
  131. }
  132. void connection_handler (StreamPeerIO *pio, int event)
  133. {
  134. DebugObject_Access(&pio->d_obj);
  135. ASSERT(pio->mode == MODE_CONNECT || pio->mode == MODE_LISTEN)
  136. ASSERT(!(pio->mode == MODE_CONNECT) || pio->connect.state >= CONNECT_STATE_HANDSHAKE)
  137. ASSERT(!(pio->mode == MODE_LISTEN) || pio->listen.state >= LISTEN_STATE_FINISHED)
  138. if (event == BCONNECTION_EVENT_RECVCLOSED) {
  139. BLog(BLOG_NOTICE, "connection closed");
  140. } else {
  141. BLog(BLOG_NOTICE, "connection error");
  142. }
  143. reset_and_report_error(pio);
  144. return;
  145. }
  146. void connect_sslcon_handler (StreamPeerIO *pio, int event)
  147. {
  148. DebugObject_Access(&pio->d_obj);
  149. ASSERT(pio->ssl)
  150. ASSERT(pio->mode == MODE_CONNECT)
  151. ASSERT(pio->connect.state == CONNECT_STATE_HANDSHAKE || pio->connect.state == CONNECT_STATE_SENDING)
  152. ASSERT(event == BSSLCONNECTION_EVENT_UP || event == BSSLCONNECTION_EVENT_ERROR)
  153. if (event == BSSLCONNECTION_EVENT_ERROR) {
  154. BLog(BLOG_NOTICE, "SSL error");
  155. reset_and_report_error(pio);
  156. return;
  157. }
  158. // handshake complete
  159. ASSERT(pio->connect.state == CONNECT_STATE_HANDSHAKE)
  160. // remove client certificate callback
  161. if (SSL_GetClientAuthDataHook(pio->connect.sock.ssl_prfd, NULL, NULL) != SECSuccess) {
  162. BLog(BLOG_ERROR, "SSL_GetClientAuthDataHook failed");
  163. goto fail0;
  164. }
  165. // remove verify peer certificate callback
  166. if (SSL_AuthCertificateHook(pio->connect.sock.ssl_prfd, NULL, NULL) != SECSuccess) {
  167. BLog(BLOG_ERROR, "SSL_AuthCertificateHook failed");
  168. goto fail0;
  169. }
  170. // init password sender
  171. 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);
  172. // change state
  173. pio->connect.state = CONNECT_STATE_SENDING;
  174. return;
  175. fail0:
  176. reset_and_report_error(pio);
  177. return;
  178. }
  179. void pwsender_handler (StreamPeerIO *pio)
  180. {
  181. DebugObject_Access(&pio->d_obj);
  182. ASSERT(pio->mode == MODE_CONNECT)
  183. ASSERT(pio->connect.state == CONNECT_STATE_SENDING)
  184. // free password sender
  185. SingleStreamSender_Free(&pio->connect.pwsender);
  186. if (pio->ssl) {
  187. // free BSSLConnection (we used the send interface)
  188. BSSLConnection_Free(&pio->connect.sslcon);
  189. } else {
  190. // init connection send interface
  191. BConnection_SendAsync_Free(&pio->connect.sock.con);
  192. }
  193. // change state
  194. pio->connect.state = CONNECT_STATE_SENT;
  195. // setup i/o
  196. if (!init_io(pio, &pio->connect.sock)) {
  197. goto fail0;
  198. }
  199. // change state
  200. pio->connect.state = CONNECT_STATE_FINISHED;
  201. return;
  202. fail0:
  203. reset_and_report_error(pio);
  204. return;
  205. }
  206. void listener_handler_client (StreamPeerIO *pio, sslsocket *sock)
  207. {
  208. DebugObject_Access(&pio->d_obj);
  209. ASSERT(pio->mode == MODE_LISTEN)
  210. ASSERT(pio->listen.state == LISTEN_STATE_LISTENER)
  211. // remember socket
  212. pio->listen.sock = sock;
  213. // set connection handler
  214. BConnection_SetHandlers(&pio->listen.sock->con, pio, (BConnection_handler)connection_handler);
  215. // change state
  216. pio->listen.state = LISTEN_STATE_GOTCLIENT;
  217. // check ceritficate
  218. if (pio->ssl) {
  219. CERTCertificate *peer_cert = SSL_PeerCertificate(pio->listen.sock->ssl_prfd);
  220. if (!peer_cert) {
  221. BLog(BLOG_ERROR, "SSL_PeerCertificate failed");
  222. goto fail0;
  223. }
  224. // compare certificate to the one provided by the server
  225. if (!compare_certificate(pio, peer_cert)) {
  226. CERT_DestroyCertificate(peer_cert);
  227. goto fail0;
  228. }
  229. CERT_DestroyCertificate(peer_cert);
  230. }
  231. // setup i/o
  232. if (!init_io(pio, pio->listen.sock)) {
  233. goto fail0;
  234. }
  235. // change state
  236. pio->listen.state = LISTEN_STATE_FINISHED;
  237. return;
  238. fail0:
  239. reset_and_report_error(pio);
  240. return;
  241. }
  242. int init_io (StreamPeerIO *pio, sslsocket *sock)
  243. {
  244. ASSERT(!pio->sock)
  245. // limit socket send buffer, else our scheduling is pointless
  246. if (pio->sock_sndbuf > 0) {
  247. if (!BConnection_SetSendBuffer(&sock->con, pio->sock_sndbuf)) {
  248. BLog(BLOG_WARNING, "BConnection_SetSendBuffer failed");
  249. }
  250. }
  251. if (pio->ssl) {
  252. // init BSSLConnection
  253. BSSLConnection_Init(&pio->sslcon, sock->ssl_prfd, 0, BReactor_PendingGroup(pio->reactor), pio, (BSSLConnection_handler)sslcon_handler);
  254. } else {
  255. // init connection interfaces
  256. BConnection_SendAsync_Init(&sock->con);
  257. BConnection_RecvAsync_Init(&sock->con);
  258. }
  259. StreamPassInterface *send_if = (pio->ssl ? BSSLConnection_GetSendIf(&pio->sslcon) : BConnection_SendAsync_GetIf(&sock->con));
  260. StreamRecvInterface *recv_if = (pio->ssl ? BSSLConnection_GetRecvIf(&pio->sslcon) : BConnection_RecvAsync_GetIf(&sock->con));
  261. // init receiving
  262. StreamRecvConnector_ConnectInput(&pio->input_connector, recv_if);
  263. // init sending
  264. PacketStreamSender_Init(&pio->output_pss, send_if, PACKETPROTO_ENCLEN(pio->payload_mtu), BReactor_PendingGroup(pio->reactor));
  265. PacketPassConnector_ConnectOutput(&pio->output_connector, PacketStreamSender_GetInput(&pio->output_pss));
  266. pio->sock = sock;
  267. return 1;
  268. }
  269. void free_io (StreamPeerIO *pio)
  270. {
  271. ASSERT(pio->sock)
  272. // reset decoder
  273. PacketProtoDecoder_Reset(&pio->input_decoder);
  274. // free sending
  275. PacketPassConnector_DisconnectOutput(&pio->output_connector);
  276. PacketStreamSender_Free(&pio->output_pss);
  277. // free receiving
  278. StreamRecvConnector_DisconnectInput(&pio->input_connector);
  279. if (pio->ssl) {
  280. // free BSSLConnection
  281. BSSLConnection_Free(&pio->sslcon);
  282. } else {
  283. // free connection interfaces
  284. BConnection_RecvAsync_Free(&pio->sock->con);
  285. BConnection_SendAsync_Free(&pio->sock->con);
  286. }
  287. pio->sock = NULL;
  288. }
  289. void sslcon_handler (StreamPeerIO *pio, int event)
  290. {
  291. DebugObject_Access(&pio->d_obj);
  292. ASSERT(pio->ssl)
  293. ASSERT(pio->mode == MODE_CONNECT || pio->mode == MODE_LISTEN)
  294. ASSERT(!(pio->mode == MODE_CONNECT) || pio->connect.state == CONNECT_STATE_FINISHED)
  295. ASSERT(!(pio->mode == MODE_LISTEN) || pio->listen.state == LISTEN_STATE_FINISHED)
  296. ASSERT(event == BSSLCONNECTION_EVENT_ERROR)
  297. BLog(BLOG_NOTICE, "SSL error");
  298. reset_and_report_error(pio);
  299. return;
  300. }
  301. SECStatus client_auth_certificate_callback (StreamPeerIO *pio, PRFileDesc *fd, PRBool checkSig, PRBool isServer)
  302. {
  303. ASSERT(pio->ssl)
  304. ASSERT(pio->mode == MODE_CONNECT)
  305. ASSERT(pio->connect.state == CONNECT_STATE_HANDSHAKE)
  306. DebugObject_Access(&pio->d_obj);
  307. // This callback is used to bypass checking the server's domain name, as peers
  308. // don't have domain names. We byte-compare the certificate to the one reported
  309. // by the server anyway.
  310. SECStatus ret = SECFailure;
  311. CERTCertificate *server_cert = SSL_PeerCertificate(pio->connect.sock.ssl_prfd);
  312. if (!server_cert) {
  313. BLog(BLOG_ERROR, "SSL_PeerCertificate failed");
  314. PORT_SetError(SSL_ERROR_BAD_CERTIFICATE);
  315. goto fail1;
  316. }
  317. if (CERT_VerifyCertNow(CERT_GetDefaultCertDB(), server_cert, PR_TRUE, certUsageSSLServer, SSL_RevealPinArg(pio->connect.sock.ssl_prfd)) != SECSuccess) {
  318. goto fail2;
  319. }
  320. // compare to certificate provided by the server
  321. if (!compare_certificate(pio, server_cert)) {
  322. PORT_SetError(SSL_ERROR_BAD_CERTIFICATE);
  323. goto fail2;
  324. }
  325. ret = SECSuccess;
  326. fail2:
  327. CERT_DestroyCertificate(server_cert);
  328. fail1:
  329. return ret;
  330. }
  331. SECStatus client_client_auth_data_callback (StreamPeerIO *pio, PRFileDesc *fd, CERTDistNames *caNames, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey)
  332. {
  333. ASSERT(pio->ssl)
  334. ASSERT(pio->mode == MODE_CONNECT)
  335. ASSERT(pio->connect.state == CONNECT_STATE_HANDSHAKE)
  336. DebugObject_Access(&pio->d_obj);
  337. CERTCertificate *cert = CERT_DupCertificate(pio->connect.ssl_cert);
  338. if (!cert) {
  339. BLog(BLOG_ERROR, "CERT_DupCertificate failed");
  340. goto fail0;
  341. }
  342. SECKEYPrivateKey *key = SECKEY_CopyPrivateKey(pio->connect.ssl_key);
  343. if (!key) {
  344. BLog(BLOG_ERROR, "SECKEY_CopyPrivateKey failed");
  345. goto fail1;
  346. }
  347. *pRetCert = cert;
  348. *pRetKey = key;
  349. return SECSuccess;
  350. fail1:
  351. CERT_DestroyCertificate(cert);
  352. fail0:
  353. return SECFailure;
  354. }
  355. int compare_certificate (StreamPeerIO *pio, CERTCertificate *cert)
  356. {
  357. ASSERT(pio->ssl)
  358. SECItem der = cert->derCert;
  359. if (der.len != pio->ssl_peer_cert_len || memcmp(der.data, pio->ssl_peer_cert, der.len)) {
  360. BLog(BLOG_NOTICE, "Client certificate doesn't match");
  361. return 0;
  362. }
  363. return 1;
  364. }
  365. void reset_state (StreamPeerIO *pio)
  366. {
  367. // free resources
  368. switch (pio->mode) {
  369. case MODE_NONE:
  370. break;
  371. case MODE_LISTEN:
  372. switch (pio->listen.state) {
  373. case LISTEN_STATE_FINISHED:
  374. free_io(pio);
  375. case LISTEN_STATE_GOTCLIENT:
  376. if (pio->ssl) {
  377. ASSERT_FORCE(PR_Close(pio->listen.sock->ssl_prfd) == PR_SUCCESS)
  378. BConnection_RecvAsync_Free(&pio->listen.sock->con);
  379. BConnection_SendAsync_Free(&pio->listen.sock->con);
  380. }
  381. BConnection_Free(&pio->listen.sock->con);
  382. free(pio->listen.sock);
  383. case LISTEN_STATE_LISTENER:
  384. if (pio->listen.state == LISTEN_STATE_LISTENER) {
  385. PasswordListener_RemoveEntry(pio->listen.listener, &pio->listen.pwentry);
  386. }
  387. break;
  388. default:
  389. ASSERT(0);
  390. }
  391. break;
  392. case MODE_CONNECT:
  393. switch (pio->connect.state) {
  394. case CONNECT_STATE_FINISHED:
  395. free_io(pio);
  396. case CONNECT_STATE_SENT:
  397. case CONNECT_STATE_SENDING:
  398. if (pio->connect.state == CONNECT_STATE_SENDING) {
  399. SingleStreamSender_Free(&pio->connect.pwsender);
  400. if (!pio->ssl) {
  401. BConnection_SendAsync_Free(&pio->connect.sock.con);
  402. }
  403. }
  404. case CONNECT_STATE_HANDSHAKE:
  405. if (pio->ssl) {
  406. if (pio->connect.state == CONNECT_STATE_HANDSHAKE || pio->connect.state == CONNECT_STATE_SENDING) {
  407. BSSLConnection_Free(&pio->connect.sslcon);
  408. }
  409. ASSERT_FORCE(PR_Close(pio->connect.sock.ssl_prfd) == PR_SUCCESS)
  410. BConnection_RecvAsync_Free(&pio->connect.sock.con);
  411. BConnection_SendAsync_Free(&pio->connect.sock.con);
  412. }
  413. BConnection_Free(&pio->connect.sock.con);
  414. case CONNECT_STATE_CONNECTING:
  415. BConnector_Free(&pio->connect.connector);
  416. break;
  417. default:
  418. ASSERT(0);
  419. }
  420. break;
  421. default:
  422. ASSERT(0);
  423. }
  424. // set mode none
  425. pio->mode = MODE_NONE;
  426. ASSERT(!pio->sock)
  427. }
  428. void reset_and_report_error (StreamPeerIO *pio)
  429. {
  430. reset_state(pio);
  431. pio->handler_error(pio->user);
  432. return;
  433. }
  434. int StreamPeerIO_Init (
  435. StreamPeerIO *pio,
  436. BReactor *reactor,
  437. int ssl,
  438. uint8_t *ssl_peer_cert,
  439. int ssl_peer_cert_len,
  440. int payload_mtu,
  441. int sock_sndbuf,
  442. PacketPassInterface *user_recv_if,
  443. StreamPeerIO_handler_error handler_error,
  444. void *user
  445. )
  446. {
  447. ASSERT(ssl == 0 || ssl == 1)
  448. ASSERT(payload_mtu >= 0)
  449. ASSERT(PacketPassInterface_GetMTU(user_recv_if) >= payload_mtu)
  450. ASSERT(handler_error)
  451. // init arguments
  452. pio->reactor = reactor;
  453. pio->ssl = ssl;
  454. if (pio->ssl) {
  455. pio->ssl_peer_cert = ssl_peer_cert;
  456. pio->ssl_peer_cert_len = ssl_peer_cert_len;
  457. }
  458. pio->payload_mtu = payload_mtu;
  459. pio->sock_sndbuf = sock_sndbuf;
  460. pio->handler_error = handler_error;
  461. pio->user = user;
  462. // check payload MTU
  463. if (pio->payload_mtu > PACKETPROTO_MAXPAYLOAD) {
  464. BLog(BLOG_ERROR, "payload MTU is too large");
  465. goto fail0;
  466. }
  467. // init receiveing objects
  468. StreamRecvConnector_Init(&pio->input_connector, BReactor_PendingGroup(pio->reactor));
  469. if (!PacketProtoDecoder_Init(&pio->input_decoder, StreamRecvConnector_GetOutput(&pio->input_connector), user_recv_if, BReactor_PendingGroup(pio->reactor), pio,
  470. (PacketProtoDecoder_handler_error)decoder_handler_error
  471. )) {
  472. BLog(BLOG_ERROR, "FlowErrorDomain_Init failed");
  473. goto fail1;
  474. }
  475. // init sending objects
  476. PacketCopier_Init(&pio->output_user_copier, pio->payload_mtu, BReactor_PendingGroup(pio->reactor));
  477. PacketProtoEncoder_Init(&pio->output_user_ppe, PacketCopier_GetOutput(&pio->output_user_copier), BReactor_PendingGroup(pio->reactor));
  478. PacketPassConnector_Init(&pio->output_connector, PACKETPROTO_ENCLEN(pio->payload_mtu), BReactor_PendingGroup(pio->reactor));
  479. if (!SinglePacketBuffer_Init(&pio->output_user_spb, PacketProtoEncoder_GetOutput(&pio->output_user_ppe), PacketPassConnector_GetInput(&pio->output_connector), BReactor_PendingGroup(pio->reactor))) {
  480. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  481. goto fail2;
  482. }
  483. // set mode none
  484. pio->mode = MODE_NONE;
  485. // set no socket
  486. pio->sock = NULL;
  487. DebugObject_Init(&pio->d_obj);
  488. return 1;
  489. fail2:
  490. PacketPassConnector_Free(&pio->output_connector);
  491. PacketProtoEncoder_Free(&pio->output_user_ppe);
  492. PacketCopier_Free(&pio->output_user_copier);
  493. PacketProtoDecoder_Free(&pio->input_decoder);
  494. fail1:
  495. StreamRecvConnector_Free(&pio->input_connector);
  496. fail0:
  497. return 0;
  498. }
  499. void StreamPeerIO_Free (StreamPeerIO *pio)
  500. {
  501. DebugObject_Free(&pio->d_obj);
  502. // reset state
  503. reset_state(pio);
  504. // free sending objects
  505. SinglePacketBuffer_Free(&pio->output_user_spb);
  506. PacketPassConnector_Free(&pio->output_connector);
  507. PacketProtoEncoder_Free(&pio->output_user_ppe);
  508. PacketCopier_Free(&pio->output_user_copier);
  509. // free receiveing objects
  510. PacketProtoDecoder_Free(&pio->input_decoder);
  511. StreamRecvConnector_Free(&pio->input_connector);
  512. }
  513. PacketPassInterface * StreamPeerIO_GetSendInput (StreamPeerIO *pio)
  514. {
  515. DebugObject_Access(&pio->d_obj);
  516. return PacketCopier_GetInput(&pio->output_user_copier);
  517. }
  518. int StreamPeerIO_Connect (StreamPeerIO *pio, BAddr addr, uint64_t password, CERTCertificate *ssl_cert, SECKEYPrivateKey *ssl_key)
  519. {
  520. DebugObject_Access(&pio->d_obj);
  521. ASSERT(BConnection_AddressSupported(addr))
  522. // reset state
  523. reset_state(pio);
  524. // init connector
  525. if (!BConnector_Init(&pio->connect.connector, addr, pio->reactor, pio, (BConnector_handler)connector_handler)) {
  526. BLog(BLOG_ERROR, "BConnector_Init failed");
  527. goto fail0;
  528. }
  529. // remember data
  530. if (pio->ssl) {
  531. pio->connect.ssl_cert = ssl_cert;
  532. pio->connect.ssl_key = ssl_key;
  533. }
  534. pio->connect.password = htol64(password);
  535. // set state
  536. pio->mode = MODE_CONNECT;
  537. pio->connect.state = CONNECT_STATE_CONNECTING;
  538. return 1;
  539. fail0:
  540. return 0;
  541. }
  542. void StreamPeerIO_Listen (StreamPeerIO *pio, PasswordListener *listener, uint64_t *password)
  543. {
  544. DebugObject_Access(&pio->d_obj);
  545. ASSERT(listener->ssl == pio->ssl)
  546. // reset state
  547. reset_state(pio);
  548. // add PasswordListener entry
  549. uint64_t newpass = PasswordListener_AddEntry(listener, &pio->listen.pwentry, (PasswordListener_handler_client)listener_handler_client, pio);
  550. // remember data
  551. pio->listen.listener = listener;
  552. // set state
  553. pio->mode = MODE_LISTEN;
  554. pio->listen.state = LISTEN_STATE_LISTENER;
  555. *password = newpass;
  556. }