RandomPacketSink.h 3.7 KB

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