DPRelay.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 DataProtoLocalSource
  42. if (!DataProtoLocalSource_Init(&flow->dpls, &src->device, src->source_id, sink->dest_id, num_packets, inactivity_time, (DataProtoLocalSource_handler_inactivity)flow_inactivity_handler, flow)) {
  43. BLog(BLOG_ERROR, "relay flow %d->%d: DataProtoLocalSource_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. DataProtoLocalSource_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. DataProtoLocalSource_Detach(&flow->dpls);
  66. }
  67. // remove from source's current flow
  68. if (flow->src->current_flow == flow) {
  69. flow->src->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 DataProtoLocalSource
  76. DataProtoLocalSource_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 (DPRelaySource *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. DataProtoLocalSource_Route(&o->current_flow->dpls, 0);
  106. // set no current flow
  107. o->current_flow = NULL;
  108. }
  109. int DPRelaySource_Init (DPRelaySource *o, peerid_t source_id, 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->source_id = source_id;
  115. o->frame_mtu = frame_mtu;
  116. // init BufferWriter
  117. BufferWriter_Init(&o->writer, frame_mtu, BReactor_PendingGroup(reactor));
  118. // init DataProtoDevice
  119. if (!DataProtoDevice_Init(&o->device, BufferWriter_GetOutput(&o->writer), (DataProtoDevice_handler)source_device_handler, o, reactor)) {
  120. goto fail1;
  121. }
  122. // init flows list
  123. LinkedList1_Init(&o->flows_list);
  124. // have no current flow
  125. o->current_flow = NULL;
  126. DebugObject_Init(&o->d_obj);
  127. return 1;
  128. fail1:
  129. BufferWriter_Free(&o->writer);
  130. return 0;
  131. }
  132. void DPRelaySource_Free (DPRelaySource *o)
  133. {
  134. DebugObject_Free(&o->d_obj);
  135. // free flows, detaching them if needed
  136. LinkedList1Node *node;
  137. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  138. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  139. free_flow(flow);
  140. }
  141. ASSERT(!o->current_flow)
  142. // free DataProtoDevice
  143. DataProtoDevice_Free(&o->device);
  144. // free BufferWriter
  145. BufferWriter_Free(&o->writer);
  146. }
  147. void DPRelaySource_SubmitFrame (DPRelaySource *o, DPRelaySink *sink, uint8_t *data, int data_len, int num_packets, int inactivity_time)
  148. {
  149. ASSERT(data_len >= 0)
  150. ASSERT(data_len <= o->frame_mtu)
  151. ASSERT(num_packets > 0)
  152. ASSERT(!o->current_flow)
  153. DebugObject_Access(&o->d_obj);
  154. DebugObject_Access(&sink->d_obj);
  155. // get memory location
  156. uint8_t *out;
  157. if (!BufferWriter_StartPacket(&o->writer, &out)) {
  158. BLog(BLOG_ERROR, "BufferWriter_StartPacket failed for frame %d->%d !?", (int)o->source_id, (int)sink->dest_id);
  159. return;
  160. }
  161. // write frame
  162. memcpy(out, data, data_len);
  163. // submit frame
  164. BufferWriter_EndPacket(&o->writer, data_len);
  165. // get a flow
  166. // this comes _after_ writing the packet, in case flow initialization schedules jobs
  167. struct DPRelay_flow *flow = source_find_flow(o, sink);
  168. if (!flow) {
  169. if (!(flow = create_flow(o, sink, num_packets, inactivity_time))) {
  170. return;
  171. }
  172. }
  173. // remember flow so we know where to route the frame in source_device_handler
  174. o->current_flow = flow;
  175. }
  176. void DPRelaySource_PrepareFreeDestinations (DPRelaySource *o)
  177. {
  178. DebugObject_Access(&o->d_obj);
  179. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  180. while (node) {
  181. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  182. if (flow->sink->dest) {
  183. DataProtoDest_PrepareFree(flow->sink->dest);
  184. }
  185. node = LinkedList1Node_Next(node);
  186. }
  187. }
  188. void DPRelaySink_Init (DPRelaySink *o, peerid_t dest_id, BReactor *reactor)
  189. {
  190. // init arguments
  191. o->dest_id = dest_id;
  192. // have no dest
  193. o->dest = NULL;
  194. // init flows list
  195. LinkedList1_Init(&o->flows_list);
  196. DebugObject_Init(&o->d_obj);
  197. }
  198. void DPRelaySink_Free (DPRelaySink *o)
  199. {
  200. ASSERT(!o->dest)
  201. DebugObject_Free(&o->d_obj);
  202. // free flows
  203. LinkedList1Node *node;
  204. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  205. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  206. free_flow(flow);
  207. }
  208. }
  209. void DPRelaySink_Attach (DPRelaySink *o, DataProtoDest *dest)
  210. {
  211. ASSERT(!o->dest)
  212. DebugObject_Access(&o->d_obj);
  213. // set dest
  214. o->dest = dest;
  215. // attach flows
  216. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  217. while (node) {
  218. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  219. DataProtoLocalSource_Attach(&flow->dpls, o->dest);
  220. node = LinkedList1Node_Next(node);
  221. }
  222. }
  223. void DPRelaySink_Detach (DPRelaySink *o)
  224. {
  225. ASSERT(o->dest)
  226. DebugObject_Access(&o->d_obj);
  227. // set no dest
  228. o->dest = NULL;
  229. // detach flows
  230. LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list);
  231. while (node) {
  232. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  233. DataProtoLocalSource_Detach(&flow->dpls);
  234. node = LinkedList1Node_Next(node);
  235. }
  236. }