FastPacketSource.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @file FastPacketSource.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 _FASTPACKETSOURCE_H
  23. #define _FASTPACKETSOURCE_H
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <misc/dead.h>
  27. #include <misc/debug.h>
  28. #include <system/BPending.h>
  29. #include <flow/PacketPassInterface.h>
  30. typedef struct {
  31. dead_t dead;
  32. PacketPassInterface *output;
  33. int psize;
  34. uint8_t *data;
  35. int data_len;
  36. BPending start_job;
  37. } FastPacketSource;
  38. static void _FastPacketSource_send (FastPacketSource *s)
  39. {
  40. while (1) {
  41. DEAD_ENTER(s->dead)
  42. int res = PacketPassInterface_Sender_Send(s->output, s->data, s->data_len);
  43. if (DEAD_LEAVE(s->dead)) {
  44. return;
  45. }
  46. ASSERT(res == 0 || res == 1)
  47. if (res == 0) {
  48. return;
  49. }
  50. }
  51. }
  52. static void _FastPacketSource_output_handler_done (FastPacketSource *s)
  53. {
  54. _FastPacketSource_send(s);
  55. return;
  56. }
  57. static void _FastPacketSource_job_handler (FastPacketSource *s)
  58. {
  59. _FastPacketSource_send(s);
  60. return;
  61. }
  62. static void FastPacketSource_Init (FastPacketSource *s, PacketPassInterface *output, uint8_t *data, int data_len, BPendingGroup *pg)
  63. {
  64. ASSERT(data_len >= 0)
  65. ASSERT(data_len <= PacketPassInterface_GetMTU(output));
  66. // init arguments
  67. s->output = output;
  68. s->data = data;
  69. s->data_len = data_len;
  70. // init dead var
  71. DEAD_INIT(s->dead);
  72. // init output
  73. PacketPassInterface_Sender_Init(s->output, (PacketPassInterface_handler_done)_FastPacketSource_output_handler_done, s);
  74. // init start job
  75. BPending_Init(&s->start_job, pg, (BPending_handler)_FastPacketSource_job_handler, s);
  76. BPending_Set(&s->start_job);
  77. }
  78. static void FastPacketSource_Free (FastPacketSource *s)
  79. {
  80. // free start job
  81. BPending_Free(&s->start_job);
  82. // free dead var
  83. DEAD_KILL(s->dead);
  84. }
  85. #endif