RandomPacketSink.h 3.7 KB

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