SCOutmsgEncoder.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @file SCOutmsgEncoder.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 <stddef.h>
  23. #include <limits.h>
  24. #include <misc/balign.h>
  25. #include <misc/debug.h>
  26. #include <misc/byteorder.h>
  27. #include "SCOutmsgEncoder.h"
  28. static void output_handler_recv (SCOutmsgEncoder *enc, uint8_t *data)
  29. {
  30. ASSERT(!enc->output_packet)
  31. ASSERT(data)
  32. DebugObject_Access(&enc->d_obj);
  33. // schedule receive
  34. enc->output_packet = data;
  35. PacketRecvInterface_Receiver_Recv(enc->input, enc->output_packet + SCOUTMSG_OVERHEAD);
  36. }
  37. static void input_handler_done (SCOutmsgEncoder *enc, int in_len)
  38. {
  39. ASSERT(enc->output_packet)
  40. DebugObject_Access(&enc->d_obj);
  41. // write SC header
  42. struct sc_header *header = (struct sc_header *)enc->output_packet;
  43. header->type = htol8(SCID_OUTMSG);
  44. // write outmsg
  45. struct sc_client_outmsg *outmsg = (struct sc_client_outmsg *)(header + 1);
  46. outmsg->clientid = htol16(enc->peer_id);
  47. // finish output packet
  48. enc->output_packet = NULL;
  49. PacketRecvInterface_Done(&enc->output, SCOUTMSG_OVERHEAD + in_len);
  50. }
  51. void SCOutmsgEncoder_Init (SCOutmsgEncoder *enc, peerid_t peer_id, PacketRecvInterface *input, BPendingGroup *pg)
  52. {
  53. ASSERT(PacketRecvInterface_GetMTU(input) <= INT_MAX - SCOUTMSG_OVERHEAD)
  54. // init arguments
  55. enc->peer_id = peer_id;
  56. enc->input = input;
  57. // init input
  58. PacketRecvInterface_Receiver_Init(enc->input, (PacketRecvInterface_handler_done)input_handler_done, enc);
  59. // init output
  60. PacketRecvInterface_Init(
  61. &enc->output, SCOUTMSG_OVERHEAD + PacketRecvInterface_GetMTU(enc->input),
  62. (PacketRecvInterface_handler_recv)output_handler_recv, enc, pg
  63. );
  64. // set no output packet
  65. enc->output_packet = NULL;
  66. DebugObject_Init(&enc->d_obj);
  67. }
  68. void SCOutmsgEncoder_Free (SCOutmsgEncoder *enc)
  69. {
  70. DebugObject_Free(&enc->d_obj);
  71. // free input
  72. PacketRecvInterface_Free(&enc->output);
  73. }
  74. PacketRecvInterface * SCOutmsgEncoder_GetOutput (SCOutmsgEncoder *enc)
  75. {
  76. DebugObject_Access(&enc->d_obj);
  77. return &enc->output;
  78. }