DPRelay.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 void flow_inactivity_handler (struct DPRelay_flow *flow);
  29. static struct DPRelay_flow * create_flow (DPRelaySource *src, DPRelaySink *sink, int num_packets, int inactivity_time)
  30. {
  31. ASSERT(num_packets > 0)
  32. // allocate structure
  33. struct DPRelay_flow *flow = malloc(sizeof(*flow));
  34. if (!flow) {
  35. BLog(BLOG_ERROR, "relay flow %d->%d: malloc failed", (int)src->source_id, (int)sink->dest_id);
  36. goto fail0;
  37. }
  38. // set src and sink
  39. flow->src = src;
  40. flow->sink = sink;
  41. // init DataProtoFlow
  42. if (!DataProtoFlow_Init(&flow->dpls, &src->router->device, src->source_id, sink->dest_id, num_packets, inactivity_time, (DataProtoFlow_handler_inactivity)flow_inactivity_handler, flow)) {
  43. BLog(BLOG_ERROR, "relay flow %d->%d: DataProtoFlow_Init failed", (int)src->source_id, (int)sink->dest_id);
  44. goto fail1;
  45. }
  46. // insert to source list
  47. LinkedList1_Append(&src->flows_list, &flow->src_list_node);
  48. // insert to sink list
  49. LinkedList1_Append(&sink->flows_list, &flow->sink_list_node);
  50. // attach flow if needed
  51. if (sink->dest) {
  52. DataProtoFlow_Attach(&flow->dpls, sink->dest);
  53. }
  54. BLog(BLOG_INFO, "relay flow %d->%d: created", (int)src->source_id, (int)sink->dest_id);
  55. return flow;
  56. fail1:
  57. free(flow);
  58. fail0:
  59. return NULL;
  60. }
  61. static void free_flow (struct DPRelay_flow *flow)
  62. {
  63. // detach flow if needed
  64. if (flow->sink->dest) {
  65. DataProtoFlow_Detach(&flow->dpls);
  66. }
  67. // remove posible router reference
  68. if (flow->src->router->current_flow == flow) {
  69. flow->src->router->current_flow = NULL;
  70. }
  71. // remove from sink list
  72. LinkedList1_Remove(&flow->sink->flows_list, &flow->sink_list_node);
  73. // remove from source list
  74. LinkedList1_Remove(&flow->src->flows_list, &flow->src_list_node);
  75. // free DataProtoFlow
  76. DataProtoFlow_Free(&flow->dpls);
  77. // free structore
  78. free(flow);
  79. }
  80. static void flow_inactivity_handler (struct DPRelay_flow *flow)
  81. {
  82. BLog(BLOG_INFO, "relay flow %d->%d: timed out", (int)flow->src->source_id, (int)flow->sink->dest_id);
  83. free_flow(flow);
  84. }
  85. static struct DPRelay_flow * source_find_flow (DPRelaySource *o, DPRelaySink *sink)
  86. {
  87. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  88. while (node) {
  89. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  90. ASSERT(flow->src == o)
  91. if (flow->sink == sink) {
  92. return flow;
  93. }
  94. node = LinkedList1Node_Next(node);
  95. }
  96. return NULL;
  97. }
  98. static void source_device_handler (DPRelayRouter *o, const uint8_t *frame, int frame_len)
  99. {
  100. DebugObject_Access(&o->d_obj);
  101. if (!o->current_flow) {
  102. return;
  103. }
  104. // route frame to current flow
  105. DataProtoFlow_Route(&o->current_flow->dpls, 0);
  106. // set no current flow
  107. o->current_flow = NULL;
  108. }
  109. int DPRelayRouter_Init (DPRelayRouter *o, int frame_mtu, BReactor *reactor)
  110. {
  111. ASSERT(frame_mtu >= 0)
  112. ASSERT(frame_mtu <= INT_MAX - DATAPROTO_MAX_OVERHEAD)
  113. // init arguments
  114. o->frame_mtu = frame_mtu;
  115. // init BufferWriter
  116. BufferWriter_Init(&o->writer, frame_mtu, BReactor_PendingGroup(reactor));
  117. // init DataProtoSource
  118. if (!DataProtoSource_Init(&o->device, BufferWriter_GetOutput(&o->writer), (DataProtoSource_handler)source_device_handler, o, reactor)) {
  119. goto fail1;
  120. }
  121. // have no current flow
  122. o->current_flow = NULL;
  123. DebugObject_Init(&o->d_obj);
  124. DebugCounter_Init(&o->d_ctr);
  125. return 1;
  126. fail1:
  127. BufferWriter_Free(&o->writer);
  128. return 0;
  129. }
  130. void DPRelayRouter_Free (DPRelayRouter *o)
  131. {
  132. ASSERT(!o->current_flow) // have no sources
  133. DebugCounter_Free(&o->d_ctr);
  134. DebugObject_Free(&o->d_obj);
  135. // free DataProtoSource
  136. DataProtoSource_Free(&o->device);
  137. // free BufferWriter
  138. BufferWriter_Free(&o->writer);
  139. }
  140. void DPRelayRouter_SubmitFrame (DPRelayRouter *o, DPRelaySource *src, DPRelaySink *sink, uint8_t *data, int data_len, int num_packets, int inactivity_time)
  141. {
  142. ASSERT(data_len >= 0)
  143. ASSERT(data_len <= o->frame_mtu)
  144. ASSERT(num_packets > 0)
  145. ASSERT(!o->current_flow)
  146. ASSERT(src->router == o)
  147. DebugObject_Access(&o->d_obj);
  148. DebugObject_Access(&src->d_obj);
  149. DebugObject_Access(&sink->d_obj);
  150. // get memory location
  151. uint8_t *out;
  152. if (!BufferWriter_StartPacket(&o->writer, &out)) {
  153. BLog(BLOG_ERROR, "BufferWriter_StartPacket failed for frame %d->%d !?", (int)src->source_id, (int)sink->dest_id);
  154. return;
  155. }
  156. // write frame
  157. memcpy(out, data, data_len);
  158. // submit frame
  159. BufferWriter_EndPacket(&o->writer, data_len);
  160. // get a flow
  161. // this comes _after_ writing the packet, in case flow initialization schedules jobs
  162. struct DPRelay_flow *flow = source_find_flow(src, sink);
  163. if (!flow) {
  164. if (!(flow = create_flow(src, sink, num_packets, inactivity_time))) {
  165. return;
  166. }
  167. }
  168. // remember flow so we know where to route the frame in source_device_handler
  169. o->current_flow = flow;
  170. }
  171. void DPRelaySource_Init (DPRelaySource *o, DPRelayRouter *router, peerid_t source_id, BReactor *reactor)
  172. {
  173. DebugObject_Access(&router->d_obj);
  174. // init arguments
  175. o->router = router;
  176. o->source_id = source_id;
  177. // init flows list
  178. LinkedList1_Init(&o->flows_list);
  179. DebugObject_Init(&o->d_obj);
  180. DebugCounter_Increment(&o->router->d_ctr);
  181. }
  182. void DPRelaySource_Free (DPRelaySource *o)
  183. {
  184. DebugCounter_Decrement(&o->router->d_ctr);
  185. DebugObject_Free(&o->d_obj);
  186. // free flows, detaching them if needed
  187. LinkedList1Node *node;
  188. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  189. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  190. free_flow(flow);
  191. }
  192. }
  193. void DPRelaySource_PrepareFreeDestinations (DPRelaySource *o)
  194. {
  195. DebugObject_Access(&o->d_obj);
  196. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  197. while (node) {
  198. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  199. if (flow->sink->dest) {
  200. DataProtoSink_PrepareFree(flow->sink->dest);
  201. }
  202. node = LinkedList1Node_Next(node);
  203. }
  204. }
  205. void DPRelaySink_Init (DPRelaySink *o, peerid_t dest_id)
  206. {
  207. // init arguments
  208. o->dest_id = dest_id;
  209. // have no dest
  210. o->dest = NULL;
  211. // init flows list
  212. LinkedList1_Init(&o->flows_list);
  213. DebugObject_Init(&o->d_obj);
  214. }
  215. void DPRelaySink_Free (DPRelaySink *o)
  216. {
  217. ASSERT(!o->dest)
  218. DebugObject_Free(&o->d_obj);
  219. // free flows
  220. LinkedList1Node *node;
  221. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  222. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  223. free_flow(flow);
  224. }
  225. }
  226. void DPRelaySink_Attach (DPRelaySink *o, DataProtoSink *dest)
  227. {
  228. ASSERT(!o->dest)
  229. DebugObject_Access(&o->d_obj);
  230. // set dest
  231. o->dest = dest;
  232. // attach flows
  233. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  234. while (node) {
  235. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  236. DataProtoFlow_Attach(&flow->dpls, o->dest);
  237. node = LinkedList1Node_Next(node);
  238. }
  239. }
  240. void DPRelaySink_Detach (DPRelaySink *o)
  241. {
  242. ASSERT(o->dest)
  243. DebugObject_Access(&o->d_obj);
  244. // set no dest
  245. o->dest = NULL;
  246. // detach flows
  247. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  248. while (node) {
  249. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  250. DataProtoFlow_Detach(&flow->dpls);
  251. node = LinkedList1Node_Next(node);
  252. }
  253. }