igmp_poll.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. * net/igmp/igmp_poll.c
  3. *
  4. * Copyright (C) 2010, 2018-2019 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * The NuttX implementation of IGMP was inspired by the IGMP add-on for the
  8. * lwIP TCP/IP stack by Steve Reynolds:
  9. *
  10. * Copyright (c) 2002 CITEL Technologies Ltd.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. Neither the name of CITEL Technologies Ltd nor the names of its
  23. * contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <inttypes.h>
  46. #include <stdint.h>
  47. #include <nuttx/semaphore.h>
  48. #include <nuttx/net/netconfig.h>
  49. #include <nuttx/net/netdev.h>
  50. #include <nuttx/net/netstats.h>
  51. #include <nuttx/net/ip.h>
  52. #include "devif/devif.h"
  53. #include "igmp/igmp.h"
  54. #ifdef CONFIG_NET_IGMP
  55. /****************************************************************************
  56. * Pre-processor Definitions
  57. ****************************************************************************/
  58. /* Buffer layout */
  59. #define RASIZE (4)
  60. #define IPv4BUF ((FAR struct igmp_iphdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  61. #define IGMPBUF(hl) ((FAR struct igmp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + (hl)])
  62. /****************************************************************************
  63. * Private Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Name: igmp_sched_send
  67. *
  68. * Description:
  69. * Construct and send the IGMP message.
  70. *
  71. * Returned Value:
  72. * Returns a non-zero value if an IGMP message is sent.
  73. *
  74. * Assumptions:
  75. * This function ust be called with the network locked.
  76. *
  77. ****************************************************************************/
  78. static inline void igmp_sched_send(FAR struct net_driver_s *dev,
  79. FAR struct igmp_group_s *group)
  80. {
  81. in_addr_t *dest;
  82. /* REVISIT: This should be deferred to a work queue */
  83. /* Check what kind of message we need to send. There are only two
  84. * possibilities:
  85. */
  86. if (group->msgid == IGMPv2_MEMBERSHIP_REPORT)
  87. {
  88. dest = &group->grpaddr;
  89. ninfo("Send IGMPv2_MEMBERSHIP_REPORT, dest=%08" PRIx32 " flags=%02x\n",
  90. (uint32_t)*dest, group->flags);
  91. SET_LASTREPORT(group->flags); /* Remember we were the last to report */
  92. }
  93. else
  94. {
  95. DEBUGASSERT(group->msgid == IGMP_LEAVE_GROUP);
  96. dest = &g_ipv4_allrouters;
  97. ninfo("Send IGMP_LEAVE_GROUP, dest=%08" PRIx32 " flags=%02x\n",
  98. (uint32_t)*dest, group->flags);
  99. }
  100. /* Send the message */
  101. igmp_send(dev, group, dest, group->msgid);
  102. /* Indicate that the message has been sent */
  103. CLR_SCHEDMSG(group->flags);
  104. group->msgid = 0;
  105. /* If there is a thread waiting fore the message to be sent, wake it up */
  106. if (IS_WAITMSG(group->flags))
  107. {
  108. ninfo("Awakening waiter\n");
  109. nxsem_post(&group->sem);
  110. }
  111. }
  112. /****************************************************************************
  113. * Public Functions
  114. ****************************************************************************/
  115. /****************************************************************************
  116. * Name: igmp_poll
  117. *
  118. * Description:
  119. * Poll the groups associated with the device to see if any IGMP messages
  120. * are pending transfer.
  121. *
  122. * Returned Value:
  123. * Returns a non-zero value if a IGP message is sent.
  124. *
  125. * Assumptions:
  126. * This function must be called with the network locked.
  127. *
  128. ****************************************************************************/
  129. void igmp_poll(FAR struct net_driver_s *dev)
  130. {
  131. FAR struct igmp_group_s *group;
  132. uint16_t iphdrlen;
  133. /* Setup the poll operation */
  134. iphdrlen = IPv4_HDRLEN + RASIZE;
  135. dev->d_appdata = &dev->d_buf[NET_LL_HDRLEN(dev) + iphdrlen + IGMP_HDRLEN];
  136. dev->d_len = 0;
  137. dev->d_sndlen = 0;
  138. /* Check each member of the group */
  139. for (group = (FAR struct igmp_group_s *)dev->d_igmp_grplist.head;
  140. group;
  141. group = group->next)
  142. {
  143. /* Does this member have a pending outgoing message? */
  144. if (IS_SCHEDMSG(group->flags))
  145. {
  146. /* Yes, create the IGMP message in the driver buffer */
  147. igmp_sched_send(dev, group);
  148. break;
  149. }
  150. }
  151. }
  152. #endif /* CONFIG_NET_IGMP */