DPRelay.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * @file DPRelay.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 <stdlib.h>
  23. #include <string.h>
  24. #include <misc/offset.h>
  25. #include <system/BLog.h>
  26. #include <client/DPRelay.h>
  27. #include <generated/blog_channel_DPRelay.h>
  28. static struct DPRelay_flow * create_flow (DPRelaySource *src, DPRelaySink *sink, int num_packets)
  29. {
  30. ASSERT(num_packets > 0)
  31. // allocate structure
  32. struct DPRelay_flow *flow = malloc(sizeof(*flow));
  33. if (!flow) {
  34. BLog(BLOG_ERROR, "relay flow %d->%d: malloc failed", (int)src->source_id, (int)sink->dest_id);
  35. goto fail0;
  36. }
  37. // set src and sink
  38. flow->src = src;
  39. flow->sink = sink;
  40. // init DataProtoLocalSource
  41. if (!DataProtoLocalSource_Init(&flow->dpls, &src->device, src->source_id, sink->dest_id, num_packets)) {
  42. BLog(BLOG_ERROR, "relay flow %d->%d: DataProtoLocalSource_Init failed", (int)src->source_id, (int)sink->dest_id);
  43. goto fail1;
  44. }
  45. // insert to source list
  46. LinkedList1_Append(&src->flows_list, &flow->src_list_node);
  47. // insert to sink list
  48. LinkedList1_Append(&sink->flows_list, &flow->sink_list_node);
  49. // attach flow if needed
  50. if (sink->dest) {
  51. DataProtoLocalSource_Attach(&flow->dpls, sink->dest);
  52. }
  53. return flow;
  54. fail1:
  55. free(flow);
  56. fail0:
  57. return NULL;
  58. }
  59. static void free_flow (struct DPRelay_flow *flow)
  60. {
  61. // detach flow if needed
  62. if (flow->sink->dest) {
  63. DataProtoLocalSource_Detach(&flow->dpls);
  64. }
  65. // remove from source's current flow
  66. if (flow->src->current_flow == flow) {
  67. flow->src->current_flow = NULL;
  68. }
  69. // remove from sink list
  70. LinkedList1_Remove(&flow->sink->flows_list, &flow->sink_list_node);
  71. // remove from source list
  72. LinkedList1_Remove(&flow->src->flows_list, &flow->src_list_node);
  73. // free DataProtoLocalSource
  74. DataProtoLocalSource_Free(&flow->dpls);
  75. // free structore
  76. free(flow);
  77. }
  78. static struct DPRelay_flow * source_find_flow (DPRelaySource *o, DPRelaySink *sink)
  79. {
  80. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  81. while (node) {
  82. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  83. ASSERT(flow->src == o)
  84. if (flow->sink == sink) {
  85. return flow;
  86. }
  87. node = LinkedList1Node_Next(node);
  88. }
  89. return NULL;
  90. }
  91. static void source_device_handler (DPRelaySource *o, const uint8_t *frame, int frame_len)
  92. {
  93. DebugObject_Access(&o->d_obj);
  94. if (!o->current_flow) {
  95. return;
  96. }
  97. // route frame to current flow
  98. DataProtoLocalSource_Route(&o->current_flow->dpls, 0);
  99. // set no current flow
  100. o->current_flow = NULL;
  101. }
  102. int DPRelaySource_Init (DPRelaySource *o, peerid_t source_id, int frame_mtu, BReactor *reactor)
  103. {
  104. ASSERT(frame_mtu >= 0)
  105. ASSERT(frame_mtu <= INT_MAX - DATAPROTO_MAX_OVERHEAD)
  106. // init arguments
  107. o->source_id = source_id;
  108. o->frame_mtu = frame_mtu;
  109. // init BufferWriter
  110. BufferWriter_Init(&o->writer, frame_mtu, BReactor_PendingGroup(reactor));
  111. // init DataProtoDevice
  112. if (!DataProtoDevice_Init(&o->device, BufferWriter_GetOutput(&o->writer), (DataProtoDevice_handler)source_device_handler, o, reactor)) {
  113. goto fail1;
  114. }
  115. // init flows list
  116. LinkedList1_Init(&o->flows_list);
  117. // have no current flow
  118. o->current_flow = NULL;
  119. DebugObject_Init(&o->d_obj);
  120. return 1;
  121. fail1:
  122. BufferWriter_Free(&o->writer);
  123. return 0;
  124. }
  125. void DPRelaySource_Free (DPRelaySource *o)
  126. {
  127. DebugObject_Free(&o->d_obj);
  128. // free flows, detaching them if needed
  129. LinkedList1Node *node;
  130. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  131. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  132. free_flow(flow);
  133. }
  134. ASSERT(!o->current_flow)
  135. // free DataProtoDevice
  136. DataProtoDevice_Free(&o->device);
  137. // free BufferWriter
  138. BufferWriter_Free(&o->writer);
  139. }
  140. void DPRelaySource_SubmitFrame (DPRelaySource *o, DPRelaySink *sink, uint8_t *data, int data_len, int num_packets)
  141. {
  142. ASSERT(data_len >= 0)
  143. ASSERT(data_len <= o->frame_mtu)
  144. ASSERT(num_packets > 0)
  145. ASSERT(!o->current_flow)
  146. DebugObject_Access(&o->d_obj);
  147. DebugObject_Access(&sink->d_obj);
  148. // get memory location
  149. uint8_t *out;
  150. if (!BufferWriter_StartPacket(&o->writer, &out)) {
  151. BLog(BLOG_ERROR, "BufferWriter_StartPacket failed for frame %d->%d !?", (int)o->source_id, (int)sink->dest_id);
  152. return;
  153. }
  154. // write frame
  155. memcpy(out, data, data_len);
  156. // submit frame
  157. BufferWriter_EndPacket(&o->writer, data_len);
  158. // get a flow
  159. // this comes _after_ writing the packet, in case flow initialization schedules jobs
  160. struct DPRelay_flow *flow = source_find_flow(o, sink);
  161. if (!flow) {
  162. if (!(flow = create_flow(o, sink, num_packets))) {
  163. return;
  164. }
  165. }
  166. // remember flow so we know where to route the frame in source_device_handler
  167. o->current_flow = flow;
  168. }
  169. void DPRelaySource_PrepareFreeDestinations (DPRelaySource *o)
  170. {
  171. DebugObject_Access(&o->d_obj);
  172. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  173. while (node) {
  174. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  175. if (flow->sink->dest) {
  176. DataProtoDest_PrepareFree(flow->sink->dest);
  177. }
  178. node = LinkedList1Node_Next(node);
  179. }
  180. }
  181. void DPRelaySink_Init (DPRelaySink *o, peerid_t dest_id, BReactor *reactor)
  182. {
  183. // init arguments
  184. o->dest_id = dest_id;
  185. // have no dest
  186. o->dest = NULL;
  187. // init flows list
  188. LinkedList1_Init(&o->flows_list);
  189. DebugObject_Init(&o->d_obj);
  190. }
  191. void DPRelaySink_Free (DPRelaySink *o)
  192. {
  193. ASSERT(!o->dest)
  194. DebugObject_Free(&o->d_obj);
  195. // free flows
  196. LinkedList1Node *node;
  197. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  198. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  199. free_flow(flow);
  200. }
  201. }
  202. void DPRelaySink_Attach (DPRelaySink *o, DataProtoDest *dest)
  203. {
  204. ASSERT(!o->dest)
  205. DebugObject_Access(&o->d_obj);
  206. // set dest
  207. o->dest = dest;
  208. // attach flows
  209. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  210. while (node) {
  211. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  212. DataProtoLocalSource_Attach(&flow->dpls, o->dest);
  213. node = LinkedList1Node_Next(node);
  214. }
  215. }
  216. void DPRelaySink_Detach (DPRelaySink *o)
  217. {
  218. ASSERT(o->dest)
  219. DebugObject_Access(&o->d_obj);
  220. // set no dest
  221. o->dest = NULL;
  222. // detach flows
  223. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  224. while (node) {
  225. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  226. DataProtoLocalSource_Detach(&flow->dpls);
  227. node = LinkedList1Node_Next(node);
  228. }
  229. }