ServerConnection.c 20 KB

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