SCOutmsgEncoder.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 *o, uint8_t *data)
  29. {
  30. DebugObject_Access(&o->d_obj);
  31. ASSERT(!o->output_packet)
  32. ASSERT(data)
  33. // schedule receive
  34. o->output_packet = data;
  35. PacketRecvInterface_Receiver_Recv(o->input, o->output_packet + SCOUTMSG_OVERHEAD);
  36. }
  37. static void input_handler_done (SCOutmsgEncoder *o, int in_len)
  38. {
  39. DebugObject_Access(&o->d_obj);
  40. ASSERT(o->output_packet)
  41. // write SC header
  42. struct sc_header *header = (struct sc_header *)o->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(o->peer_id);
  47. // finish output packet
  48. o->output_packet = NULL;
  49. PacketRecvInterface_Done(&o->output, SCOUTMSG_OVERHEAD + in_len);
  50. }
  51. void SCOutmsgEncoder_Init (SCOutmsgEncoder *o, peerid_t peer_id, PacketRecvInterface *input, BPendingGroup *pg)
  52. {
  53. ASSERT(PacketRecvInterface_GetMTU(input) <= INT_MAX - SCOUTMSG_OVERHEAD)
  54. // init arguments
  55. o->peer_id = peer_id;
  56. o->input = input;
  57. // init input
  58. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  59. // init output
  60. PacketRecvInterface_Init(&o->output, SCOUTMSG_OVERHEAD + PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  61. // set no output packet
  62. o->output_packet = NULL;
  63. DebugObject_Init(&o->d_obj);
  64. }
  65. void SCOutmsgEncoder_Free (SCOutmsgEncoder *o)
  66. {
  67. DebugObject_Free(&o->d_obj);
  68. // free input
  69. PacketRecvInterface_Free(&o->output);
  70. }
  71. PacketRecvInterface * SCOutmsgEncoder_GetOutput (SCOutmsgEncoder *o)
  72. {
  73. DebugObject_Access(&o->d_obj);
  74. return &o->output;
  75. }