ServerConnection.c 20 KB

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