ServerConnection.c 19 KB

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