RouteBuffer.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @file RouteBuffer.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 <stdlib.h>
  24. #include <string.h>
  25. #include <misc/offset.h>
  26. #include <flow/RouteBuffer.h>
  27. static struct RouteBuffer_packet * alloc_packet (int mtu)
  28. {
  29. if (mtu > SIZE_MAX - sizeof(struct RouteBuffer_packet)) {
  30. return NULL;
  31. }
  32. // allocate memory
  33. struct RouteBuffer_packet *p = malloc(sizeof(*p) + mtu);
  34. if (!p) {
  35. return NULL;
  36. }
  37. return p;
  38. }
  39. static int alloc_free_packet (RouteBuffer *o)
  40. {
  41. struct RouteBuffer_packet *p = alloc_packet(o->mtu);
  42. if (!p) {
  43. return 0;
  44. }
  45. // add to free packets list
  46. LinkedList1_Append(&o->packets_free, &p->node);
  47. return 1;
  48. }
  49. static void free_free_packets (RouteBuffer *o)
  50. {
  51. while (!LinkedList1_IsEmpty(&o->packets_free)) {
  52. // get packet
  53. struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetLast(&o->packets_free), struct RouteBuffer_packet, node);
  54. // remove from free packets list
  55. LinkedList1_Remove(&o->packets_free, &p->node);
  56. // free memory
  57. free(p);
  58. }
  59. }
  60. static void release_used_packet (RouteBuffer *o)
  61. {
  62. ASSERT(!LinkedList1_IsEmpty(&o->packets_used))
  63. // get packet
  64. struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetFirst(&o->packets_used), struct RouteBuffer_packet, node);
  65. // remove from used packets list
  66. LinkedList1_Remove(&o->packets_used, &p->node);
  67. // add to free packets list
  68. LinkedList1_Append(&o->packets_free, &p->node);
  69. }
  70. static void send_used_packet (RouteBuffer *o)
  71. {
  72. ASSERT(!LinkedList1_IsEmpty(&o->packets_used))
  73. // get packet
  74. struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetFirst(&o->packets_used), struct RouteBuffer_packet, node);
  75. // send
  76. PacketPassInterface_Sender_Send(o->output, (uint8_t *)(p + 1), p->len);
  77. }
  78. static void output_handler_done (RouteBuffer *o)
  79. {
  80. ASSERT(!LinkedList1_IsEmpty(&o->packets_used))
  81. DebugObject_Access(&o->d_obj);
  82. // release packet
  83. release_used_packet(o);
  84. // send next packet if there is one
  85. if (!LinkedList1_IsEmpty(&o->packets_used)) {
  86. send_used_packet(o);
  87. }
  88. }
  89. int RouteBuffer_Init (RouteBuffer *o, int mtu, PacketPassInterface *output, int buf_size)
  90. {
  91. ASSERT(mtu >= 0)
  92. ASSERT(PacketPassInterface_GetMTU(output) >= mtu)
  93. ASSERT(buf_size > 0)
  94. // init arguments
  95. o->mtu = mtu;
  96. o->output = output;
  97. // init output
  98. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  99. // init free packets list
  100. LinkedList1_Init(&o->packets_free);
  101. // init used packets list
  102. LinkedList1_Init(&o->packets_used);
  103. // allocate packets
  104. for (int i = 0; i < buf_size; i++) {
  105. if (!alloc_free_packet(o)) {
  106. goto fail1;
  107. }
  108. }
  109. DebugObject_Init(&o->d_obj);
  110. return 1;
  111. fail1:
  112. free_free_packets(o);
  113. return 0;
  114. }
  115. void RouteBuffer_Free (RouteBuffer *o)
  116. {
  117. DebugObject_Free(&o->d_obj);
  118. // release packets so they can be freed
  119. while (!LinkedList1_IsEmpty(&o->packets_used)) {
  120. release_used_packet(o);
  121. }
  122. // free packets
  123. free_free_packets(o);
  124. }
  125. int RouteBuffer_GetMTU (RouteBuffer *o)
  126. {
  127. DebugObject_Access(&o->d_obj);
  128. return o->mtu;
  129. }
  130. int RouteBufferSource_Init (RouteBufferSource *o, int mtu)
  131. {
  132. ASSERT(mtu >= 0)
  133. // init arguments
  134. o->mtu = mtu;
  135. // allocate current packet
  136. if (!(o->current_packet = alloc_packet(o->mtu))) {
  137. goto fail0;
  138. }
  139. DebugObject_Init(&o->d_obj);
  140. return 1;
  141. fail0:
  142. return 0;
  143. }
  144. void RouteBufferSource_Free (RouteBufferSource *o)
  145. {
  146. DebugObject_Free(&o->d_obj);
  147. // free current packet
  148. free(o->current_packet);
  149. }
  150. uint8_t * RouteBufferSource_Pointer (RouteBufferSource *o)
  151. {
  152. DebugObject_Access(&o->d_obj);
  153. return (uint8_t *)(o->current_packet + 1);
  154. }
  155. int RouteBufferSource_Route (RouteBufferSource *o, int len, RouteBuffer *b, int copy_offset, int copy_len)
  156. {
  157. ASSERT(len >= 0)
  158. ASSERT(len <= o->mtu)
  159. ASSERT(b->mtu == o->mtu)
  160. ASSERT(copy_offset >= 0)
  161. ASSERT(copy_offset <= o->mtu)
  162. ASSERT(copy_len >= 0)
  163. ASSERT(copy_len <= o->mtu - copy_offset)
  164. DebugObject_Access(&b->d_obj);
  165. DebugObject_Access(&o->d_obj);
  166. // check if there's space in the buffer
  167. if (LinkedList1_IsEmpty(&b->packets_free)) {
  168. return 0;
  169. }
  170. int was_empty = LinkedList1_IsEmpty(&b->packets_used);
  171. struct RouteBuffer_packet *p = o->current_packet;
  172. // set packet length
  173. p->len = len;
  174. // append packet to used packets list
  175. LinkedList1_Append(&b->packets_used, &p->node);
  176. // get a free packet
  177. struct RouteBuffer_packet *np = UPPER_OBJECT(LinkedList1_GetLast(&b->packets_free), struct RouteBuffer_packet, node);
  178. // remove it from free packets list
  179. LinkedList1_Remove(&b->packets_free, &np->node);
  180. // make it the current packet
  181. o->current_packet = np;
  182. // copy packet
  183. if (copy_len > 0) {
  184. memcpy((uint8_t *)(np + 1) + copy_offset, (uint8_t *)(p + 1) + copy_offset, copy_len);
  185. }
  186. // start sending if required
  187. if (was_empty) {
  188. send_used_packet(b);
  189. }
  190. return 1;
  191. }