DPRelay.c 8.4 KB

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