DPRelay.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <base/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->dp_flow, &src->router->dp_source, src->source_id, sink->dest_id, num_packets, inactivity_time, flow, (DataProtoFlow_handler_inactivity)flow_inactivity_handler)) {
  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->dp_sink) {
  52. DataProtoFlow_Attach(&flow->dp_flow, sink->dp_sink);
  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->dp_sink) {
  65. DataProtoFlow_Detach(&flow->dp_flow);
  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->dp_flow);
  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. for (LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list); node; node = LinkedList1Node_Next(node)) {
  88. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  89. ASSERT(flow->src == o)
  90. if (flow->sink == sink) {
  91. return flow;
  92. }
  93. }
  94. return NULL;
  95. }
  96. static void router_dp_source_handler (DPRelayRouter *o, const uint8_t *frame, int frame_len)
  97. {
  98. DebugObject_Access(&o->d_obj);
  99. if (!o->current_flow) {
  100. return;
  101. }
  102. // route frame to current flow
  103. DataProtoFlow_Route(&o->current_flow->dp_flow, 0);
  104. // set no current flow
  105. o->current_flow = NULL;
  106. }
  107. int DPRelayRouter_Init (DPRelayRouter *o, int frame_mtu, BReactor *reactor)
  108. {
  109. ASSERT(frame_mtu >= 0)
  110. ASSERT(frame_mtu <= INT_MAX - DATAPROTO_MAX_OVERHEAD)
  111. // init arguments
  112. o->frame_mtu = frame_mtu;
  113. // init BufferWriter
  114. BufferWriter_Init(&o->writer, frame_mtu, BReactor_PendingGroup(reactor));
  115. // init DataProtoSource
  116. if (!DataProtoSource_Init(&o->dp_source, BufferWriter_GetOutput(&o->writer), (DataProtoSource_handler)router_dp_source_handler, o, reactor)) {
  117. BLog(BLOG_ERROR, "DataProtoSource_Init failed");
  118. goto fail1;
  119. }
  120. // have no current flow
  121. o->current_flow = NULL;
  122. DebugCounter_Init(&o->d_ctr);
  123. DebugObject_Init(&o->d_obj);
  124. return 1;
  125. fail1:
  126. BufferWriter_Free(&o->writer);
  127. return 0;
  128. }
  129. void DPRelayRouter_Free (DPRelayRouter *o)
  130. {
  131. DebugObject_Free(&o->d_obj);
  132. DebugCounter_Free(&o->d_ctr);
  133. ASSERT(!o->current_flow) // have no sources
  134. // free DataProtoSource
  135. DataProtoSource_Free(&o->dp_source);
  136. // free BufferWriter
  137. BufferWriter_Free(&o->writer);
  138. }
  139. void DPRelayRouter_SubmitFrame (DPRelayRouter *o, DPRelaySource *src, DPRelaySink *sink, uint8_t *data, int data_len, int num_packets, int inactivity_time)
  140. {
  141. DebugObject_Access(&o->d_obj);
  142. DebugObject_Access(&src->d_obj);
  143. DebugObject_Access(&sink->d_obj);
  144. ASSERT(!o->current_flow)
  145. ASSERT(src->router == o)
  146. ASSERT(data_len >= 0)
  147. ASSERT(data_len <= o->frame_mtu)
  148. ASSERT(num_packets > 0)
  149. // get memory location
  150. uint8_t *out;
  151. if (!BufferWriter_StartPacket(&o->writer, &out)) {
  152. BLog(BLOG_ERROR, "BufferWriter_StartPacket failed for frame %d->%d !?", (int)src->source_id, (int)sink->dest_id);
  153. return;
  154. }
  155. // write frame
  156. memcpy(out, data, data_len);
  157. // submit frame
  158. BufferWriter_EndPacket(&o->writer, data_len);
  159. // get a flow
  160. // this comes _after_ writing the packet, in case flow initialization schedules jobs
  161. struct DPRelay_flow *flow = source_find_flow(src, sink);
  162. if (!flow) {
  163. if (!(flow = create_flow(src, sink, num_packets, inactivity_time))) {
  164. return;
  165. }
  166. }
  167. // remember flow so we know where to route the frame in router_dp_source_handler
  168. o->current_flow = flow;
  169. }
  170. void DPRelaySource_Init (DPRelaySource *o, DPRelayRouter *router, peerid_t source_id, BReactor *reactor)
  171. {
  172. DebugObject_Access(&router->d_obj);
  173. // init arguments
  174. o->router = router;
  175. o->source_id = source_id;
  176. // init flows list
  177. LinkedList1_Init(&o->flows_list);
  178. DebugCounter_Increment(&o->router->d_ctr);
  179. DebugObject_Init(&o->d_obj);
  180. }
  181. void DPRelaySource_Free (DPRelaySource *o)
  182. {
  183. DebugObject_Free(&o->d_obj);
  184. DebugCounter_Decrement(&o->router->d_ctr);
  185. // free flows, detaching them if needed
  186. LinkedList1Node *node;
  187. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  188. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  189. free_flow(flow);
  190. }
  191. }
  192. void DPRelaySink_Init (DPRelaySink *o, peerid_t dest_id)
  193. {
  194. // init arguments
  195. o->dest_id = dest_id;
  196. // init flows list
  197. LinkedList1_Init(&o->flows_list);
  198. // have no sink
  199. o->dp_sink = NULL;
  200. DebugObject_Init(&o->d_obj);
  201. }
  202. void DPRelaySink_Free (DPRelaySink *o)
  203. {
  204. DebugObject_Free(&o->d_obj);
  205. ASSERT(!o->dp_sink)
  206. // free flows
  207. LinkedList1Node *node;
  208. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  209. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  210. free_flow(flow);
  211. }
  212. }
  213. void DPRelaySink_Attach (DPRelaySink *o, DataProtoSink *dp_sink)
  214. {
  215. DebugObject_Access(&o->d_obj);
  216. ASSERT(!o->dp_sink)
  217. ASSERT(dp_sink)
  218. // attach flows
  219. for (LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list); node; node = LinkedList1Node_Next(node)) {
  220. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  221. DataProtoFlow_Attach(&flow->dp_flow, dp_sink);
  222. }
  223. // set sink
  224. o->dp_sink = dp_sink;
  225. }
  226. void DPRelaySink_Detach (DPRelaySink *o)
  227. {
  228. DebugObject_Access(&o->d_obj);
  229. ASSERT(o->dp_sink)
  230. // detach flows
  231. for (LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list); node; node = LinkedList1Node_Next(node)) {
  232. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  233. DataProtoFlow_Detach(&flow->dp_flow);
  234. }
  235. // set no sink
  236. o->dp_sink = NULL;
  237. }