ServerConnection.c 20 KB

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