StreamPeerIO.c 23 KB

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