igmp_poll.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /****************************************************************************
  2. * net/igmp/igmp_poll.c
  3. *
  4. * Copyright (C) 2010, 2018 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 contributors
  23. * may be used to endorse or promote products derived from this software
  24. * 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 LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <nuttx/semaphore.h>
  46. #include <nuttx/net/netconfig.h>
  47. #include <nuttx/net/netdev.h>
  48. #include <nuttx/net/netstats.h>
  49. #include <nuttx/net/ip.h>
  50. #include "devif/devif.h"
  51. #include "igmp/igmp.h"
  52. #ifdef CONFIG_NET_IGMP
  53. /****************************************************************************
  54. * Private Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: igmp_sched_send
  58. *
  59. * Description:
  60. * Construct and send the IGMP message.
  61. *
  62. * Returned Value:
  63. * Returns a non-zero value if an IGMP message is sent.
  64. *
  65. * Assumptions:
  66. * This function ust be called with the network locked.
  67. *
  68. ****************************************************************************/
  69. static inline void igmp_sched_send(FAR struct net_driver_s *dev,
  70. FAR struct igmp_group_s *group)
  71. {
  72. in_addr_t *dest;
  73. /* REVISIT: This should be deferred to a work queue */
  74. /* Check what kind of message we need to send. There are only two
  75. * possibilities:
  76. */
  77. if (group->msgid == IGMPv2_MEMBERSHIP_REPORT)
  78. {
  79. dest = &group->grpaddr;
  80. ninfo("Send IGMPv2_MEMBERSHIP_REPORT, dest=%08x flags=%02x\n",
  81. *dest, group->flags);
  82. SET_LASTREPORT(group->flags); /* Remember we were the last to report */
  83. }
  84. else
  85. {
  86. DEBUGASSERT(group->msgid == IGMP_LEAVE_GROUP);
  87. dest = &g_ipv4_allrouters;
  88. ninfo("Send IGMP_LEAVE_GROUP, dest=%08x flags=%02x\n",
  89. *dest, group->flags);
  90. }
  91. /* Send the message */
  92. igmp_send(dev, group, dest, group->msgid);
  93. /* Indicate that the message has been sent */
  94. CLR_SCHEDMSG(group->flags);
  95. group->msgid = 0;
  96. /* If there is a thread waiting fore the message to be sent, wake it up */
  97. if (IS_WAITMSG(group->flags))
  98. {
  99. ninfo("Awakening waiter\n");
  100. nxsem_post(&group->sem);
  101. }
  102. }
  103. /****************************************************************************
  104. * Public Functions
  105. ****************************************************************************/
  106. /****************************************************************************
  107. * Name: igmp_poll
  108. *
  109. * Description:
  110. * Poll the groups associated with the device to see if any IGMP messages
  111. * are pending transfer.
  112. *
  113. * Returned Value:
  114. * Returns a non-zero value if a IGP message is sent.
  115. *
  116. * Assumptions:
  117. * This function must be called with the network locked.
  118. *
  119. ****************************************************************************/
  120. void igmp_poll(FAR struct net_driver_s *dev)
  121. {
  122. FAR struct igmp_group_s *group;
  123. /* Setup the poll operation */
  124. dev->d_appdata = &dev->d_buf[NET_LL_HDRLEN(dev) + IPIGMP_HDRLEN];
  125. dev->d_len = 0;
  126. dev->d_sndlen = 0;
  127. /* Check each member of the group */
  128. for (group = (FAR struct igmp_group_s *)dev->d_igmp_grplist.head;
  129. group;
  130. group = group->next)
  131. {
  132. /* Does this member have a pending outgoing message? */
  133. if (IS_SCHEDMSG(group->flags))
  134. {
  135. /* Yes, create the IGMP message in the driver buffer */
  136. igmp_sched_send(dev, group);
  137. break;
  138. }
  139. }
  140. }
  141. #endif /* CONFIG_NET_IGMP */