RandomPacketSink.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @file RandomPacketSink.h
  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. #ifndef _RANDOMPACKETSINK_H
  23. #define _RANDOMPACKETSINK_H
  24. #include <stdio.h>
  25. #include <security/BRandom.h>
  26. #include <system/BReactor.h>
  27. #include <system/DebugObject.h>
  28. #include <flow/PacketPassInterface.h>
  29. typedef struct {
  30. BReactor *reactor;
  31. PacketPassInterface input;
  32. BTimer timer;
  33. DebugObject d_obj;
  34. } RandomPacketSink;
  35. static void _RandomPacketSink_input_handler_send (RandomPacketSink *s, uint8_t *data, int data_len)
  36. {
  37. DebugObject_Access(&s->d_obj);
  38. printf("sink: send '");
  39. fwrite(data, data_len, 1, stdout);
  40. uint8_t r;
  41. BRandom_randomize(&r, sizeof(r));
  42. if (r&(uint8_t)1) {
  43. printf("' accepting\n");
  44. PacketPassInterface_Done(&s->input);
  45. } else {
  46. printf("' delaying\n");
  47. BReactor_SetTimer(s->reactor, &s->timer);
  48. }
  49. }
  50. static void _RandomPacketSink_input_handler_requestcancel (RandomPacketSink *s)
  51. {
  52. DebugObject_Access(&s->d_obj);
  53. printf("sink: cancelled\n");
  54. BReactor_RemoveTimer(s->reactor, &s->timer);
  55. PacketPassInterface_Done(&s->input);
  56. }
  57. static void _RandomPacketSink_timer_handler (RandomPacketSink *s)
  58. {
  59. DebugObject_Access(&s->d_obj);
  60. PacketPassInterface_Done(&s->input);
  61. }
  62. static void RandomPacketSink_Init (RandomPacketSink *s, BReactor *reactor, int mtu, int ms)
  63. {
  64. // init arguments
  65. s->reactor = reactor;
  66. // init input
  67. PacketPassInterface_Init(&s->input, mtu, (PacketPassInterface_handler_send)_RandomPacketSink_input_handler_send, s, BReactor_PendingGroup(reactor));
  68. PacketPassInterface_EnableCancel(&s->input, (PacketPassInterface_handler_requestcancel)_RandomPacketSink_input_handler_requestcancel);
  69. // init timer
  70. BTimer_Init(&s->timer, ms, (BTimer_handler)_RandomPacketSink_timer_handler, s);
  71. DebugObject_Init(&s->d_obj);
  72. }
  73. static void RandomPacketSink_Free (RandomPacketSink *s)
  74. {
  75. DebugObject_Free(&s->d_obj);
  76. // free timer
  77. BReactor_RemoveTimer(s->reactor, &s->timer);
  78. // free input
  79. PacketPassInterface_Free(&s->input);
  80. }
  81. static PacketPassInterface * RandomPacketSink_GetInput (RandomPacketSink *s)
  82. {
  83. DebugObject_Access(&s->d_obj);
  84. return &s->input;
  85. }
  86. #endif