DPReceive.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * @file DPReceive.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stddef.h>
  30. #include <limits.h>
  31. #include <string.h>
  32. #include <protocol/dataproto.h>
  33. #include <misc/byteorder.h>
  34. #include <misc/offset.h>
  35. #include <base/BLog.h>
  36. #include <client/DPReceive.h>
  37. #include <generated/blog_channel_DPReceive.h>
  38. static DPReceivePeer * find_peer (DPReceiveDevice *o, peerid_t id)
  39. {
  40. for (LinkedList1Node *node = LinkedList1_GetFirst(&o->peers_list); node; node = LinkedList1Node_Next(node)) {
  41. DPReceivePeer *p = UPPER_OBJECT(node, DPReceivePeer, list_node);
  42. if (p->peer_id == id) {
  43. return p;
  44. }
  45. }
  46. return NULL;
  47. }
  48. static void receiver_recv_handler_send (DPReceiveReceiver *o, uint8_t *packet, int packet_len)
  49. {
  50. DebugObject_Access(&o->d_obj);
  51. DPReceivePeer *peer = o->peer;
  52. DPReceiveDevice *device = peer->device;
  53. ASSERT(packet_len >= 0)
  54. ASSERT(packet_len <= device->packet_mtu)
  55. uint8_t *data = packet;
  56. int data_len = packet_len;
  57. int local = 0;
  58. DPReceivePeer *src_peer;
  59. DPReceivePeer *relay_dest_peer = NULL;
  60. // check header
  61. if (data_len < sizeof(struct dataproto_header)) {
  62. BLog(BLOG_WARNING, "no dataproto header");
  63. goto out;
  64. }
  65. struct dataproto_header header;
  66. memcpy(&header, data, sizeof(header));
  67. data += sizeof(header);
  68. data_len -= sizeof(header);
  69. uint8_t flags = ltoh8(header.flags);
  70. peerid_t from_id = ltoh16(header.from_id);
  71. int num_ids = ltoh16(header.num_peer_ids);
  72. // check destination ID
  73. if (!(num_ids == 0 || num_ids == 1)) {
  74. BLog(BLOG_WARNING, "wrong number of destinations");
  75. goto out;
  76. }
  77. peerid_t to_id = 0; // to remove warning
  78. if (num_ids == 1) {
  79. if (data_len < sizeof(struct dataproto_peer_id)) {
  80. BLog(BLOG_WARNING, "missing destination");
  81. goto out;
  82. }
  83. struct dataproto_peer_id id;
  84. memcpy(&id, data, sizeof(id));
  85. to_id = ltoh16(id.id);
  86. data += sizeof(id);
  87. data_len -= sizeof(id);
  88. }
  89. // check remaining data
  90. if (data_len > device->device_mtu) {
  91. BLog(BLOG_WARNING, "frame too large");
  92. goto out;
  93. }
  94. // inform sink of received packet
  95. if (peer->dp_sink) {
  96. DataProtoSink_Received(peer->dp_sink, !!(flags & DATAPROTO_FLAGS_RECEIVING_KEEPALIVES));
  97. }
  98. if (num_ids == 1) {
  99. // find source peer
  100. if (!(src_peer = find_peer(device, from_id))) {
  101. BLog(BLOG_INFO, "source peer %d not known", (int)from_id);
  102. goto out;
  103. }
  104. // is frame for device or another peer?
  105. if (device->have_peer_id && to_id == device->peer_id) {
  106. // let the frame decider analyze the frame
  107. FrameDeciderPeer_Analyze(src_peer->decider_peer, data, data_len);
  108. // pass frame to device
  109. local = 1;
  110. } else {
  111. // check if relaying is allowed
  112. if (!peer->is_relay_client) {
  113. BLog(BLOG_WARNING, "relaying not allowed");
  114. goto out;
  115. }
  116. // provided source ID must be the peer sending the frame
  117. if (src_peer != peer) {
  118. BLog(BLOG_WARNING, "relay source must be the sending peer");
  119. goto out;
  120. }
  121. // find destination peer
  122. DPReceivePeer *dest_peer = find_peer(device, to_id);
  123. if (!dest_peer) {
  124. BLog(BLOG_INFO, "relay destination peer not known");
  125. goto out;
  126. }
  127. // destination cannot be source
  128. if (dest_peer == src_peer) {
  129. BLog(BLOG_WARNING, "relay destination cannot be the source");
  130. goto out;
  131. }
  132. relay_dest_peer = dest_peer;
  133. }
  134. }
  135. out:
  136. // accept packet
  137. PacketPassInterface_Done(&o->recv_if);
  138. // pass packet to device
  139. if (local) {
  140. o->device->output_func(o->device->output_func_user, data, data_len);
  141. }
  142. // relay frame
  143. if (relay_dest_peer) {
  144. DPRelayRouter_SubmitFrame(&device->relay_router, &src_peer->relay_source, &relay_dest_peer->relay_sink, data, data_len, device->relay_flow_buffer_size, device->relay_flow_inactivity_time);
  145. }
  146. }
  147. int DPReceiveDevice_Init (DPReceiveDevice *o, int device_mtu, DPReceiveDevice_output_func output_func, void *output_func_user, BReactor *reactor, int relay_flow_buffer_size, int relay_flow_inactivity_time)
  148. {
  149. ASSERT(device_mtu >= 0)
  150. ASSERT(device_mtu <= INT_MAX - DATAPROTO_MAX_OVERHEAD)
  151. ASSERT(output_func)
  152. ASSERT(relay_flow_buffer_size > 0)
  153. // init arguments
  154. o->device_mtu = device_mtu;
  155. o->output_func = output_func;
  156. o->output_func_user = output_func_user;
  157. o->reactor = reactor;
  158. o->relay_flow_buffer_size = relay_flow_buffer_size;
  159. o->relay_flow_inactivity_time = relay_flow_inactivity_time;
  160. // remember packet MTU
  161. o->packet_mtu = DATAPROTO_MAX_OVERHEAD + o->device_mtu;
  162. // init relay router
  163. if (!DPRelayRouter_Init(&o->relay_router, o->device_mtu, o->reactor)) {
  164. BLog(BLOG_ERROR, "DPRelayRouter_Init failed");
  165. goto fail0;
  166. }
  167. // have no peer ID
  168. o->have_peer_id = 0;
  169. // init peers list
  170. LinkedList1_Init(&o->peers_list);
  171. DebugObject_Init(&o->d_obj);
  172. return 1;
  173. fail0:
  174. return 0;
  175. }
  176. void DPReceiveDevice_Free (DPReceiveDevice *o)
  177. {
  178. DebugObject_Free(&o->d_obj);
  179. ASSERT(LinkedList1_IsEmpty(&o->peers_list))
  180. // free relay router
  181. DPRelayRouter_Free(&o->relay_router);
  182. }
  183. void DPReceiveDevice_SetPeerID (DPReceiveDevice *o, peerid_t peer_id)
  184. {
  185. DebugObject_Access(&o->d_obj);
  186. // remember peer ID
  187. o->peer_id = peer_id;
  188. o->have_peer_id = 1;
  189. }
  190. void DPReceivePeer_Init (DPReceivePeer *o, DPReceiveDevice *device, peerid_t peer_id, FrameDeciderPeer *decider_peer, int is_relay_client)
  191. {
  192. DebugObject_Access(&device->d_obj);
  193. ASSERT(is_relay_client == 0 || is_relay_client == 1)
  194. // init arguments
  195. o->device = device;
  196. o->peer_id = peer_id;
  197. o->decider_peer = decider_peer;
  198. o->is_relay_client = is_relay_client;
  199. // init relay source
  200. DPRelaySource_Init(&o->relay_source, &device->relay_router, o->peer_id, device->reactor);
  201. // init relay sink
  202. DPRelaySink_Init(&o->relay_sink, o->peer_id);
  203. // have no sink
  204. o->dp_sink = NULL;
  205. // insert to peers list
  206. LinkedList1_Append(&device->peers_list, &o->list_node);
  207. DebugCounter_Init(&o->d_receivers_ctr);
  208. DebugObject_Init(&o->d_obj);
  209. }
  210. void DPReceivePeer_Free (DPReceivePeer *o)
  211. {
  212. DebugObject_Free(&o->d_obj);
  213. DebugCounter_Free(&o->d_receivers_ctr);
  214. ASSERT(!o->dp_sink)
  215. // remove from peers list
  216. LinkedList1_Remove(&o->device->peers_list, &o->list_node);
  217. // free relay sink
  218. DPRelaySink_Free(&o->relay_sink);
  219. // free relay source
  220. DPRelaySource_Free(&o->relay_source);
  221. }
  222. void DPReceivePeer_AttachSink (DPReceivePeer *o, DataProtoSink *dp_sink)
  223. {
  224. DebugObject_Access(&o->d_obj);
  225. ASSERT(!o->dp_sink)
  226. ASSERT(dp_sink)
  227. // attach relay sink
  228. DPRelaySink_Attach(&o->relay_sink, dp_sink);
  229. o->dp_sink = dp_sink;
  230. }
  231. void DPReceivePeer_DetachSink (DPReceivePeer *o)
  232. {
  233. DebugObject_Access(&o->d_obj);
  234. ASSERT(o->dp_sink)
  235. // detach relay sink
  236. DPRelaySink_Detach(&o->relay_sink);
  237. o->dp_sink = NULL;
  238. }
  239. void DPReceiveReceiver_Init (DPReceiveReceiver *o, DPReceivePeer *peer)
  240. {
  241. DebugObject_Access(&peer->d_obj);
  242. DPReceiveDevice *device = peer->device;
  243. // init arguments
  244. o->peer = peer;
  245. // remember device
  246. o->device = device;
  247. // init receive interface
  248. PacketPassInterface_Init(&o->recv_if, device->packet_mtu, (PacketPassInterface_handler_send)receiver_recv_handler_send, o, BReactor_PendingGroup(device->reactor));
  249. DebugCounter_Increment(&peer->d_receivers_ctr);
  250. DebugObject_Init(&o->d_obj);
  251. }
  252. void DPReceiveReceiver_Free (DPReceiveReceiver *o)
  253. {
  254. DebugObject_Free(&o->d_obj);
  255. DebugCounter_Decrement(&o->peer->d_receivers_ctr);
  256. // free receive interface
  257. PacketPassInterface_Free(&o->recv_if);
  258. }
  259. PacketPassInterface * DPReceiveReceiver_GetInput (DPReceiveReceiver *o)
  260. {
  261. DebugObject_Access(&o->d_obj);
  262. return &o->recv_if;
  263. }