DPReceive.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <protocol/dataproto.h>
  32. #include <misc/byteorder.h>
  33. #include <misc/offset.h>
  34. #include <base/BLog.h>
  35. #include <client/DPReceive.h>
  36. #include <generated/blog_channel_DPReceive.h>
  37. static DPReceivePeer * find_peer (DPReceiveDevice *o, peerid_t id)
  38. {
  39. for (LinkedList1Node *node = LinkedList1_GetFirst(&o->peers_list); node; node = LinkedList1Node_Next(node)) {
  40. DPReceivePeer *p = UPPER_OBJECT(node, DPReceivePeer, list_node);
  41. if (p->peer_id == id) {
  42. return p;
  43. }
  44. }
  45. return NULL;
  46. }
  47. static void receiver_recv_handler_send (DPReceiveReceiver *o, uint8_t *packet, int packet_len)
  48. {
  49. DebugObject_Access(&o->d_obj);
  50. DPReceivePeer *peer = o->peer;
  51. DPReceiveDevice *device = peer->device;
  52. ASSERT(packet_len >= 0)
  53. ASSERT(packet_len <= device->packet_mtu)
  54. uint8_t *data = packet;
  55. int data_len = packet_len;
  56. int local = 0;
  57. DPReceivePeer *src_peer;
  58. DPReceivePeer *relay_dest_peer = NULL;
  59. // check header
  60. if (data_len < sizeof(struct dataproto_header)) {
  61. BLog(BLOG_WARNING, "no dataproto header");
  62. goto out;
  63. }
  64. struct dataproto_header *header = (struct dataproto_header *)data;
  65. data += sizeof(*header);
  66. data_len -= sizeof(*header);
  67. uint8_t flags = ltoh8(header->flags);
  68. peerid_t from_id = ltoh16(header->from_id);
  69. int num_ids = ltoh16(header->num_peer_ids);
  70. // check destination ID
  71. if (!(num_ids == 0 || num_ids == 1)) {
  72. BLog(BLOG_WARNING, "wrong number of destinations");
  73. goto out;
  74. }
  75. peerid_t to_id = 0; // to remove warning
  76. if (num_ids == 1) {
  77. if (data_len < sizeof(to_id)) {
  78. BLog(BLOG_WARNING, "missing destination");
  79. goto out;
  80. }
  81. to_id = ((struct dataproto_peer_id *)data)->id;
  82. data += sizeof(to_id);
  83. data_len -= sizeof(to_id);
  84. }
  85. // check remaining data
  86. if (data_len > device->device_mtu) {
  87. BLog(BLOG_WARNING, "frame too large");
  88. goto out;
  89. }
  90. // inform sink of received packet
  91. if (peer->dp_sink) {
  92. DataProtoSink_Received(peer->dp_sink, !!(flags & DATAPROTO_FLAGS_RECEIVING_KEEPALIVES));
  93. }
  94. if (num_ids == 1) {
  95. // find source peer
  96. if (!(src_peer = find_peer(device, from_id))) {
  97. BLog(BLOG_INFO, "source peer %d not known", (int)from_id);
  98. goto out;
  99. }
  100. // is frame for device or another peer?
  101. if (device->have_peer_id && to_id == device->peer_id) {
  102. // let the frame decider analyze the frame
  103. FrameDeciderPeer_Analyze(src_peer->decider_peer, data, data_len);
  104. // pass frame to device
  105. local = 1;
  106. } else {
  107. // check if relaying is allowed
  108. if (!peer->is_relay_client) {
  109. BLog(BLOG_WARNING, "relaying not allowed");
  110. goto out;
  111. }
  112. // provided source ID must be the peer sending the frame
  113. if (src_peer != peer) {
  114. BLog(BLOG_WARNING, "relay source must be the sending peer");
  115. goto out;
  116. }
  117. // find destination peer
  118. DPReceivePeer *dest_peer = find_peer(device, to_id);
  119. if (!dest_peer) {
  120. BLog(BLOG_INFO, "relay destination peer not known");
  121. goto out;
  122. }
  123. // destination cannot be source
  124. if (dest_peer == src_peer) {
  125. BLog(BLOG_WARNING, "relay destination cannot be the source");
  126. goto out;
  127. }
  128. relay_dest_peer = dest_peer;
  129. }
  130. }
  131. out:
  132. // accept packet
  133. PacketPassInterface_Done(&o->recv_if);
  134. // pass packet to device
  135. if (local) {
  136. o->device->output_func(o->device->output_func_user, data, data_len);
  137. }
  138. // relay frame
  139. if (relay_dest_peer) {
  140. 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);
  141. }
  142. }
  143. 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)
  144. {
  145. ASSERT(device_mtu >= 0)
  146. ASSERT(device_mtu <= INT_MAX - DATAPROTO_MAX_OVERHEAD)
  147. ASSERT(output_func)
  148. ASSERT(relay_flow_buffer_size > 0)
  149. // init arguments
  150. o->device_mtu = device_mtu;
  151. o->output_func = output_func;
  152. o->output_func_user = output_func_user;
  153. o->reactor = reactor;
  154. o->relay_flow_buffer_size = relay_flow_buffer_size;
  155. o->relay_flow_inactivity_time = relay_flow_inactivity_time;
  156. // remember packet MTU
  157. o->packet_mtu = DATAPROTO_MAX_OVERHEAD + o->device_mtu;
  158. // init relay router
  159. if (!DPRelayRouter_Init(&o->relay_router, o->device_mtu, o->reactor)) {
  160. BLog(BLOG_ERROR, "DPRelayRouter_Init failed");
  161. goto fail0;
  162. }
  163. // have no peer ID
  164. o->have_peer_id = 0;
  165. // init peers list
  166. LinkedList1_Init(&o->peers_list);
  167. DebugObject_Init(&o->d_obj);
  168. return 1;
  169. fail0:
  170. return 0;
  171. }
  172. void DPReceiveDevice_Free (DPReceiveDevice *o)
  173. {
  174. DebugObject_Free(&o->d_obj);
  175. ASSERT(LinkedList1_IsEmpty(&o->peers_list))
  176. // free relay router
  177. DPRelayRouter_Free(&o->relay_router);
  178. }
  179. void DPReceiveDevice_SetPeerID (DPReceiveDevice *o, peerid_t peer_id)
  180. {
  181. DebugObject_Access(&o->d_obj);
  182. // remember peer ID
  183. o->peer_id = peer_id;
  184. o->have_peer_id = 1;
  185. }
  186. void DPReceivePeer_Init (DPReceivePeer *o, DPReceiveDevice *device, peerid_t peer_id, FrameDeciderPeer *decider_peer, int is_relay_client)
  187. {
  188. DebugObject_Access(&device->d_obj);
  189. ASSERT(is_relay_client == 0 || is_relay_client == 1)
  190. // init arguments
  191. o->device = device;
  192. o->peer_id = peer_id;
  193. o->decider_peer = decider_peer;
  194. o->is_relay_client = is_relay_client;
  195. // init relay source
  196. DPRelaySource_Init(&o->relay_source, &device->relay_router, o->peer_id, device->reactor);
  197. // init relay sink
  198. DPRelaySink_Init(&o->relay_sink, o->peer_id);
  199. // have no sink
  200. o->dp_sink = NULL;
  201. // insert to peers list
  202. LinkedList1_Append(&device->peers_list, &o->list_node);
  203. DebugCounter_Init(&o->d_receivers_ctr);
  204. DebugObject_Init(&o->d_obj);
  205. }
  206. void DPReceivePeer_Free (DPReceivePeer *o)
  207. {
  208. DebugObject_Free(&o->d_obj);
  209. DebugCounter_Free(&o->d_receivers_ctr);
  210. ASSERT(!o->dp_sink)
  211. // remove from peers list
  212. LinkedList1_Remove(&o->device->peers_list, &o->list_node);
  213. // free relay sink
  214. DPRelaySink_Free(&o->relay_sink);
  215. // free relay source
  216. DPRelaySource_Free(&o->relay_source);
  217. }
  218. void DPReceivePeer_AttachSink (DPReceivePeer *o, DataProtoSink *dp_sink)
  219. {
  220. DebugObject_Access(&o->d_obj);
  221. ASSERT(!o->dp_sink)
  222. ASSERT(dp_sink)
  223. // attach relay sink
  224. DPRelaySink_Attach(&o->relay_sink, dp_sink);
  225. o->dp_sink = dp_sink;
  226. }
  227. void DPReceivePeer_DetachSink (DPReceivePeer *o)
  228. {
  229. DebugObject_Access(&o->d_obj);
  230. ASSERT(o->dp_sink)
  231. // detach relay sink
  232. DPRelaySink_Detach(&o->relay_sink);
  233. o->dp_sink = NULL;
  234. }
  235. void DPReceiveReceiver_Init (DPReceiveReceiver *o, DPReceivePeer *peer)
  236. {
  237. DebugObject_Access(&peer->d_obj);
  238. DPReceiveDevice *device = peer->device;
  239. // init arguments
  240. o->peer = peer;
  241. // remember device
  242. o->device = device;
  243. // init receive interface
  244. PacketPassInterface_Init(&o->recv_if, device->packet_mtu, (PacketPassInterface_handler_send)receiver_recv_handler_send, o, BReactor_PendingGroup(device->reactor));
  245. DebugCounter_Increment(&peer->d_receivers_ctr);
  246. DebugObject_Init(&o->d_obj);
  247. }
  248. void DPReceiveReceiver_Free (DPReceiveReceiver *o)
  249. {
  250. DebugObject_Free(&o->d_obj);
  251. DebugCounter_Decrement(&o->peer->d_receivers_ctr);
  252. // free receive interface
  253. PacketPassInterface_Free(&o->recv_if);
  254. }
  255. PacketPassInterface * DPReceiveReceiver_GetInput (DPReceiveReceiver *o)
  256. {
  257. DebugObject_Access(&o->d_obj);
  258. return &o->recv_if;
  259. }