ServerConnection.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /**
  2. * @file ServerConnection.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 <stdio.h>
  30. #include <string.h>
  31. #include <stddef.h>
  32. #include <misc/debug.h>
  33. #include <misc/strdup.h>
  34. #include <base/BLog.h>
  35. #include <server_connection/ServerConnection.h>
  36. #include <generated/blog_channel_ServerConnection.h>
  37. #define STATE_CONNECTING 1
  38. #define STATE_WAITINIT 2
  39. #define STATE_COMPLETE 3
  40. static void report_error (ServerConnection *o);
  41. static void connector_handler (ServerConnection *o, int is_error);
  42. static void pending_handler (ServerConnection *o);
  43. static SECStatus client_auth_data_callback (ServerConnection *o, PRFileDesc *fd, CERTDistNames *caNames, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey);
  44. static void connection_handler (ServerConnection *o, int event);
  45. static void sslcon_handler (ServerConnection *o, int event);
  46. static void decoder_handler_error (ServerConnection *o);
  47. static void input_handler_send (ServerConnection *o, uint8_t *data, int data_len);
  48. static void packet_hello (ServerConnection *o, uint8_t *data, int data_len);
  49. static void packet_newclient (ServerConnection *o, uint8_t *data, int data_len);
  50. static void packet_endclient (ServerConnection *o, uint8_t *data, int data_len);
  51. static void packet_inmsg (ServerConnection *o, uint8_t *data, int data_len);
  52. static int start_packet (ServerConnection *o, void **data, int len);
  53. static void end_packet (ServerConnection *o, uint8_t type);
  54. static void newclient_job_handler (ServerConnection *o);
  55. void report_error (ServerConnection *o)
  56. {
  57. DEBUGERROR(&o->d_err, o->handler_error(o->user))
  58. }
  59. void connector_handler (ServerConnection *o, int is_error)
  60. {
  61. DebugObject_Access(&o->d_obj);
  62. ASSERT(o->state == STATE_CONNECTING)
  63. ASSERT(!o->buffers_released)
  64. // check connection attempt result
  65. if (is_error) {
  66. BLog(BLOG_ERROR, "connection failed");
  67. goto fail0;
  68. }
  69. BLog(BLOG_NOTICE, "connected");
  70. // init connection
  71. if (!BConnection_Init(&o->con, BConnection_source_connector(&o->connector), o->reactor, o, (BConnection_handler)connection_handler)) {
  72. BLog(BLOG_ERROR, "BConnection_Init failed");
  73. goto fail0;
  74. }
  75. // init connection interfaces
  76. BConnection_SendAsync_Init(&o->con);
  77. BConnection_RecvAsync_Init(&o->con);
  78. StreamPassInterface *send_iface = BConnection_SendAsync_GetIf(&o->con);
  79. StreamRecvInterface *recv_iface = BConnection_RecvAsync_GetIf(&o->con);
  80. if (o->have_ssl) {
  81. // create bottom NSPR file descriptor
  82. if (!BSSLConnection_MakeBackend(&o->bottom_prfd, send_iface, recv_iface, o->twd, o->ssl_flags)) {
  83. BLog(BLOG_ERROR, "BSSLConnection_MakeBackend failed");
  84. goto fail0a;
  85. }
  86. // create SSL file descriptor from the bottom NSPR file descriptor
  87. if (!(o->ssl_prfd = SSL_ImportFD(NULL, &o->bottom_prfd))) {
  88. BLog(BLOG_ERROR, "SSL_ImportFD failed");
  89. ASSERT_FORCE(PR_Close(&o->bottom_prfd) == PR_SUCCESS)
  90. goto fail0a;
  91. }
  92. // set client mode
  93. if (SSL_ResetHandshake(o->ssl_prfd, PR_FALSE) != SECSuccess) {
  94. BLog(BLOG_ERROR, "SSL_ResetHandshake failed");
  95. goto fail1;
  96. }
  97. // set server name
  98. if (SSL_SetURL(o->ssl_prfd, o->server_name) != SECSuccess) {
  99. BLog(BLOG_ERROR, "SSL_SetURL failed");
  100. goto fail1;
  101. }
  102. // set client certificate callback
  103. if (SSL_GetClientAuthDataHook(o->ssl_prfd, (SSLGetClientAuthData)client_auth_data_callback, o) != SECSuccess) {
  104. BLog(BLOG_ERROR, "SSL_GetClientAuthDataHook failed");
  105. goto fail1;
  106. }
  107. // init BSSLConnection
  108. BSSLConnection_Init(&o->sslcon, o->ssl_prfd, 0, BReactor_PendingGroup(o->reactor), o, (BSSLConnection_handler)sslcon_handler);
  109. send_iface = BSSLConnection_GetSendIf(&o->sslcon);
  110. recv_iface = BSSLConnection_GetRecvIf(&o->sslcon);
  111. }
  112. // init input chain
  113. PacketPassInterface_Init(&o->input_interface, SC_MAX_ENC, (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(o->reactor));
  114. if (!PacketProtoDecoder_Init(&o->input_decoder, recv_iface, &o->input_interface, BReactor_PendingGroup(o->reactor), o, (PacketProtoDecoder_handler_error)decoder_handler_error)) {
  115. BLog(BLOG_ERROR, "PacketProtoDecoder_Init failed");
  116. goto fail2;
  117. }
  118. // set job to send hello
  119. // this needs to be in here because hello sending must be done after sending started (so we can write into the send buffer),
  120. // but before receiving started (so we don't get into conflict with the user sending packets)
  121. BPending_Init(&o->start_job, BReactor_PendingGroup(o->reactor), (BPending_handler)pending_handler, o);
  122. BPending_Set(&o->start_job);
  123. // init keepalive output branch
  124. SCKeepaliveSource_Init(&o->output_ka_zero, BReactor_PendingGroup(o->reactor));
  125. PacketProtoEncoder_Init(&o->output_ka_encoder, SCKeepaliveSource_GetOutput(&o->output_ka_zero), BReactor_PendingGroup(o->reactor));
  126. // init output common
  127. // init sender
  128. PacketStreamSender_Init(&o->output_sender, send_iface, PACKETPROTO_ENCLEN(SC_MAX_ENC), BReactor_PendingGroup(o->reactor));
  129. // init keepalives
  130. if (!KeepaliveIO_Init(&o->output_keepaliveio, o->reactor, PacketStreamSender_GetInput(&o->output_sender), PacketProtoEncoder_GetOutput(&o->output_ka_encoder), o->keepalive_interval)) {
  131. BLog(BLOG_ERROR, "KeepaliveIO_Init failed");
  132. goto fail3;
  133. }
  134. // init queue
  135. PacketPassPriorityQueue_Init(&o->output_queue, KeepaliveIO_GetInput(&o->output_keepaliveio), BReactor_PendingGroup(o->reactor), 0);
  136. // init output local flow
  137. // init queue flow
  138. PacketPassPriorityQueueFlow_Init(&o->output_local_qflow, &o->output_queue, 0);
  139. // init PacketProtoFlow
  140. if (!PacketProtoFlow_Init(&o->output_local_oflow, SC_MAX_ENC, o->buffer_size, PacketPassPriorityQueueFlow_GetInput(&o->output_local_qflow), BReactor_PendingGroup(o->reactor))) {
  141. BLog(BLOG_ERROR, "PacketProtoFlow_Init failed");
  142. goto fail4;
  143. }
  144. o->output_local_if = PacketProtoFlow_GetInput(&o->output_local_oflow);
  145. // have no output packet
  146. o->output_local_packet_len = -1;
  147. // init output user flow
  148. PacketPassPriorityQueueFlow_Init(&o->output_user_qflow, &o->output_queue, 1);
  149. // update state
  150. o->state = STATE_WAITINIT;
  151. return;
  152. fail4:
  153. PacketPassPriorityQueueFlow_Free(&o->output_local_qflow);
  154. PacketPassPriorityQueue_Free(&o->output_queue);
  155. KeepaliveIO_Free(&o->output_keepaliveio);
  156. fail3:
  157. PacketStreamSender_Free(&o->output_sender);
  158. PacketProtoEncoder_Free(&o->output_ka_encoder);
  159. SCKeepaliveSource_Free(&o->output_ka_zero);
  160. BPending_Free(&o->start_job);
  161. PacketProtoDecoder_Free(&o->input_decoder);
  162. fail2:
  163. PacketPassInterface_Free(&o->input_interface);
  164. if (o->have_ssl) {
  165. BSSLConnection_Free(&o->sslcon);
  166. fail1:
  167. ASSERT_FORCE(PR_Close(o->ssl_prfd) == PR_SUCCESS)
  168. }
  169. fail0a:
  170. BConnection_RecvAsync_Free(&o->con);
  171. BConnection_SendAsync_Free(&o->con);
  172. BConnection_Free(&o->con);
  173. fail0:
  174. // report error
  175. report_error(o);
  176. }
  177. void pending_handler (ServerConnection *o)
  178. {
  179. ASSERT(o->state == STATE_WAITINIT)
  180. ASSERT(!o->buffers_released)
  181. DebugObject_Access(&o->d_obj);
  182. // send hello
  183. struct sc_client_hello omsg;
  184. void *packet;
  185. if (!start_packet(o, &packet, sizeof(omsg))) {
  186. BLog(BLOG_ERROR, "no buffer for hello");
  187. report_error(o);
  188. return;
  189. }
  190. omsg.version = htol16(SC_VERSION);
  191. memcpy(packet, &omsg, sizeof(omsg));
  192. end_packet(o, SCID_CLIENTHELLO);
  193. }
  194. SECStatus client_auth_data_callback (ServerConnection *o, PRFileDesc *fd, CERTDistNames *caNames, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey)
  195. {
  196. ASSERT(o->have_ssl)
  197. DebugObject_Access(&o->d_obj);
  198. CERTCertificate *newcert;
  199. if (!(newcert = CERT_DupCertificate(o->client_cert))) {
  200. return SECFailure;
  201. }
  202. SECKEYPrivateKey *newkey;
  203. if (!(newkey = SECKEY_CopyPrivateKey(o->client_key))) {
  204. CERT_DestroyCertificate(newcert);
  205. return SECFailure;
  206. }
  207. *pRetCert = newcert;
  208. *pRetKey = newkey;
  209. return SECSuccess;
  210. }
  211. void connection_handler (ServerConnection *o, int event)
  212. {
  213. DebugObject_Access(&o->d_obj);
  214. ASSERT(o->state >= STATE_WAITINIT)
  215. ASSERT(!o->buffers_released)
  216. if (event == BCONNECTION_EVENT_RECVCLOSED) {
  217. BLog(BLOG_INFO, "connection closed");
  218. } else {
  219. BLog(BLOG_INFO, "connection error");
  220. }
  221. report_error(o);
  222. return;
  223. }
  224. void sslcon_handler (ServerConnection *o, int event)
  225. {
  226. DebugObject_Access(&o->d_obj);
  227. ASSERT(o->have_ssl)
  228. ASSERT(o->state >= STATE_WAITINIT)
  229. ASSERT(!o->buffers_released)
  230. ASSERT(event == BSSLCONNECTION_EVENT_ERROR)
  231. BLog(BLOG_ERROR, "SSL error");
  232. report_error(o);
  233. return;
  234. }
  235. void decoder_handler_error (ServerConnection *o)
  236. {
  237. DebugObject_Access(&o->d_obj);
  238. ASSERT(o->state >= STATE_WAITINIT)
  239. ASSERT(!o->buffers_released)
  240. BLog(BLOG_ERROR, "decoder error");
  241. report_error(o);
  242. return;
  243. }
  244. void input_handler_send (ServerConnection *o, uint8_t *data, int data_len)
  245. {
  246. ASSERT(o->state >= STATE_WAITINIT)
  247. ASSERT(!o->buffers_released)
  248. ASSERT(data_len >= 0)
  249. ASSERT(data_len <= SC_MAX_ENC)
  250. DebugObject_Access(&o->d_obj);
  251. // accept packet
  252. PacketPassInterface_Done(&o->input_interface);
  253. // parse header
  254. if (data_len < sizeof(struct sc_header)) {
  255. BLog(BLOG_ERROR, "packet too short (no sc header)");
  256. report_error(o);
  257. return;
  258. }
  259. struct sc_header header;
  260. memcpy(&header, data, sizeof(header));
  261. data += sizeof(header);
  262. data_len -= sizeof(header);
  263. uint8_t type = ltoh8(header.type);
  264. // call appropriate handler based on packet type
  265. switch (type) {
  266. case SCID_SERVERHELLO:
  267. packet_hello(o, data, data_len);
  268. return;
  269. case SCID_NEWCLIENT:
  270. packet_newclient(o, data, data_len);
  271. return;
  272. case SCID_ENDCLIENT:
  273. packet_endclient(o, data, data_len);
  274. return;
  275. case SCID_INMSG:
  276. packet_inmsg(o, data, data_len);
  277. return;
  278. default:
  279. BLog(BLOG_ERROR, "unknown packet type %d", (int)type);
  280. report_error(o);
  281. return;
  282. }
  283. }
  284. void packet_hello (ServerConnection *o, uint8_t *data, int data_len)
  285. {
  286. if (o->state != STATE_WAITINIT) {
  287. BLog(BLOG_ERROR, "hello: not expected");
  288. report_error(o);
  289. return;
  290. }
  291. if (data_len != sizeof(struct sc_server_hello)) {
  292. BLog(BLOG_ERROR, "hello: invalid length");
  293. report_error(o);
  294. return;
  295. }
  296. struct sc_server_hello msg;
  297. memcpy(&msg, data, sizeof(msg));
  298. peerid_t id = ltoh16(msg.id);
  299. // change state
  300. o->state = STATE_COMPLETE;
  301. // report
  302. o->handler_ready(o->user, id, msg.clientAddr);
  303. return;
  304. }
  305. void packet_newclient (ServerConnection *o, uint8_t *data, int data_len)
  306. {
  307. if (o->state != STATE_COMPLETE) {
  308. BLog(BLOG_ERROR, "newclient: not expected");
  309. report_error(o);
  310. return;
  311. }
  312. if (data_len < sizeof(struct sc_server_newclient) || data_len > sizeof(struct sc_server_newclient) + SCID_NEWCLIENT_MAX_CERT_LEN) {
  313. BLog(BLOG_ERROR, "newclient: invalid length");
  314. report_error(o);
  315. return;
  316. }
  317. struct sc_server_newclient msg;
  318. memcpy(&msg, data, sizeof(msg));
  319. peerid_t id = ltoh16(msg.id);
  320. // schedule reporting new client
  321. o->newclient_data = data;
  322. o->newclient_data_len = data_len;
  323. BPending_Set(&o->newclient_job);
  324. // send acceptpeer
  325. struct sc_client_acceptpeer omsg;
  326. void *packet;
  327. if (!start_packet(o, &packet, sizeof(omsg))) {
  328. BLog(BLOG_ERROR, "newclient: out of buffer for acceptpeer");
  329. report_error(o);
  330. return;
  331. }
  332. omsg.clientid = htol16(id);
  333. memcpy(packet, &omsg, sizeof(omsg));
  334. end_packet(o, SCID_ACCEPTPEER);
  335. }
  336. void packet_endclient (ServerConnection *o, uint8_t *data, int data_len)
  337. {
  338. if (o->state != STATE_COMPLETE) {
  339. BLog(BLOG_ERROR, "endclient: not expected");
  340. report_error(o);
  341. return;
  342. }
  343. if (data_len != sizeof(struct sc_server_endclient)) {
  344. BLog(BLOG_ERROR, "endclient: invalid length");
  345. report_error(o);
  346. return;
  347. }
  348. struct sc_server_endclient msg;
  349. memcpy(&msg, data, sizeof(msg));
  350. peerid_t id = ltoh16(msg.id);
  351. // report
  352. o->handler_endclient(o->user, id);
  353. return;
  354. }
  355. void packet_inmsg (ServerConnection *o, uint8_t *data, int data_len)
  356. {
  357. if (o->state != STATE_COMPLETE) {
  358. BLog(BLOG_ERROR, "inmsg: not expected");
  359. report_error(o);
  360. return;
  361. }
  362. if (data_len < sizeof(struct sc_server_inmsg)) {
  363. BLog(BLOG_ERROR, "inmsg: missing header");
  364. report_error(o);
  365. return;
  366. }
  367. if (data_len > sizeof(struct sc_server_inmsg) + SC_MAX_MSGLEN) {
  368. BLog(BLOG_ERROR, "inmsg: too long");
  369. report_error(o);
  370. return;
  371. }
  372. struct sc_server_inmsg msg;
  373. memcpy(&msg, data, sizeof(msg));
  374. peerid_t peer_id = ltoh16(msg.clientid);
  375. uint8_t *payload = data + sizeof(struct sc_server_inmsg);
  376. int payload_len = data_len - sizeof(struct sc_server_inmsg);
  377. // report
  378. o->handler_message(o->user, peer_id, payload, payload_len);
  379. return;
  380. }
  381. int start_packet (ServerConnection *o, void **data, int len)
  382. {
  383. ASSERT(o->state >= STATE_WAITINIT)
  384. ASSERT(o->output_local_packet_len == -1)
  385. ASSERT(len >= 0)
  386. ASSERT(len <= SC_MAX_PAYLOAD)
  387. ASSERT(data || len == 0)
  388. // obtain memory location
  389. if (!BufferWriter_StartPacket(o->output_local_if, &o->output_local_packet)) {
  390. BLog(BLOG_ERROR, "out of buffer");
  391. return 0;
  392. }
  393. o->output_local_packet_len = len;
  394. if (data) {
  395. *data = o->output_local_packet + sizeof(struct sc_header);
  396. }
  397. return 1;
  398. }
  399. void end_packet (ServerConnection *o, uint8_t type)
  400. {
  401. ASSERT(o->state >= STATE_WAITINIT)
  402. ASSERT(o->output_local_packet_len >= 0)
  403. ASSERT(o->output_local_packet_len <= SC_MAX_PAYLOAD)
  404. // write header
  405. struct sc_header header;
  406. header.type = htol8(type);
  407. memcpy(o->output_local_packet, &header, sizeof(header));
  408. // finish writing packet
  409. BufferWriter_EndPacket(o->output_local_if, sizeof(struct sc_header) + o->output_local_packet_len);
  410. o->output_local_packet_len = -1;
  411. }
  412. int ServerConnection_Init (
  413. ServerConnection *o,
  414. BReactor *reactor,
  415. BThreadWorkDispatcher *twd,
  416. BAddr addr,
  417. int keepalive_interval,
  418. int buffer_size,
  419. int have_ssl,
  420. int ssl_flags,
  421. CERTCertificate *client_cert,
  422. SECKEYPrivateKey *client_key,
  423. const char *server_name,
  424. void *user,
  425. ServerConnection_handler_error handler_error,
  426. ServerConnection_handler_ready handler_ready,
  427. ServerConnection_handler_newclient handler_newclient,
  428. ServerConnection_handler_endclient handler_endclient,
  429. ServerConnection_handler_message handler_message
  430. )
  431. {
  432. ASSERT(keepalive_interval > 0)
  433. ASSERT(buffer_size > 0)
  434. ASSERT(have_ssl == 0 || have_ssl == 1)
  435. ASSERT(!have_ssl || server_name)
  436. // init arguments
  437. o->reactor = reactor;
  438. o->twd = twd;
  439. o->keepalive_interval = keepalive_interval;
  440. o->buffer_size = buffer_size;
  441. o->have_ssl = have_ssl;
  442. if (have_ssl) {
  443. o->ssl_flags = ssl_flags;
  444. o->client_cert = client_cert;
  445. o->client_key = client_key;
  446. }
  447. o->user = user;
  448. o->handler_error = handler_error;
  449. o->handler_ready = handler_ready;
  450. o->handler_newclient = handler_newclient;
  451. o->handler_endclient = handler_endclient;
  452. o->handler_message = handler_message;
  453. o->server_name = NULL;
  454. if (have_ssl && !(o->server_name = b_strdup(server_name))) {
  455. BLog(BLOG_ERROR, "malloc failed");
  456. goto fail0;
  457. }
  458. if (!BConnection_AddressSupported(addr)) {
  459. BLog(BLOG_ERROR, "BConnection_AddressSupported failed");
  460. goto fail1;
  461. }
  462. // init connector
  463. if (!BConnector_Init(&o->connector, addr, o->reactor, o, (BConnector_handler)connector_handler)) {
  464. BLog(BLOG_ERROR, "BConnector_Init failed");
  465. goto fail1;
  466. }
  467. // init newclient job
  468. BPending_Init(&o->newclient_job, BReactor_PendingGroup(o->reactor), (BPending_handler)newclient_job_handler, o);
  469. // set state
  470. o->state = STATE_CONNECTING;
  471. o->buffers_released = 0;
  472. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  473. DebugObject_Init(&o->d_obj);
  474. return 1;
  475. fail1:
  476. free(o->server_name);
  477. fail0:
  478. return 0;
  479. }
  480. void ServerConnection_Free (ServerConnection *o)
  481. {
  482. DebugObject_Free(&o->d_obj);
  483. DebugError_Free(&o->d_err);
  484. if (o->state > STATE_CONNECTING) {
  485. // allow freeing queue flows
  486. PacketPassPriorityQueue_PrepareFree(&o->output_queue);
  487. // stop using any buffers before they get freed
  488. if (o->have_ssl && !o->buffers_released) {
  489. BSSLConnection_ReleaseBuffers(&o->sslcon);
  490. }
  491. // free output user flow
  492. PacketPassPriorityQueueFlow_Free(&o->output_user_qflow);
  493. // free output local flow
  494. PacketProtoFlow_Free(&o->output_local_oflow);
  495. PacketPassPriorityQueueFlow_Free(&o->output_local_qflow);
  496. // free output common
  497. PacketPassPriorityQueue_Free(&o->output_queue);
  498. KeepaliveIO_Free(&o->output_keepaliveio);
  499. PacketStreamSender_Free(&o->output_sender);
  500. // free output keep-alive branch
  501. PacketProtoEncoder_Free(&o->output_ka_encoder);
  502. SCKeepaliveSource_Free(&o->output_ka_zero);
  503. // free job
  504. BPending_Free(&o->start_job);
  505. // free input chain
  506. PacketProtoDecoder_Free(&o->input_decoder);
  507. PacketPassInterface_Free(&o->input_interface);
  508. // free SSL
  509. if (o->have_ssl) {
  510. BSSLConnection_Free(&o->sslcon);
  511. ASSERT_FORCE(PR_Close(o->ssl_prfd) == PR_SUCCESS)
  512. }
  513. // free connection interfaces
  514. BConnection_RecvAsync_Free(&o->con);
  515. BConnection_SendAsync_Free(&o->con);
  516. // free connection
  517. BConnection_Free(&o->con);
  518. }
  519. // free newclient job
  520. BPending_Free(&o->newclient_job);
  521. // free connector
  522. BConnector_Free(&o->connector);
  523. // free server name
  524. free(o->server_name);
  525. }
  526. void ServerConnection_ReleaseBuffers (ServerConnection *o)
  527. {
  528. DebugObject_Access(&o->d_obj);
  529. ASSERT(!o->buffers_released)
  530. if (o->state > STATE_CONNECTING && o->have_ssl) {
  531. BSSLConnection_ReleaseBuffers(&o->sslcon);
  532. }
  533. o->buffers_released = 1;
  534. }
  535. PacketPassInterface * ServerConnection_GetSendInterface (ServerConnection *o)
  536. {
  537. ASSERT(o->state == STATE_COMPLETE)
  538. DebugError_AssertNoError(&o->d_err);
  539. DebugObject_Access(&o->d_obj);
  540. return PacketPassPriorityQueueFlow_GetInput(&o->output_user_qflow);
  541. }
  542. void newclient_job_handler (ServerConnection *o)
  543. {
  544. DebugObject_Access(&o->d_obj);
  545. ASSERT(o->state == STATE_COMPLETE)
  546. struct sc_server_newclient msg;
  547. memcpy(&msg, o->newclient_data, sizeof(msg));
  548. peerid_t id = ltoh16(msg.id);
  549. int flags = ltoh16(msg.flags);
  550. uint8_t *cert_data = o->newclient_data + sizeof(msg);
  551. int cert_len = o->newclient_data_len - sizeof(msg);
  552. // report new client
  553. o->handler_newclient(o->user, id, flags, cert_data, cert_len);
  554. return;
  555. }