icmp_conn.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /****************************************************************************
  2. * net/icmp/icmp_conn.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote
  17. * products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  21. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ****************************************************************************/
  33. /****************************************************************************
  34. * Included Files
  35. ****************************************************************************/
  36. #include <nuttx/config.h>
  37. #include <string.h>
  38. #include <assert.h>
  39. #include <errno.h>
  40. #include <debug.h>
  41. #include <arch/irq.h>
  42. #include <nuttx/semaphore.h>
  43. #include <nuttx/net/netconfig.h>
  44. #include <nuttx/net/net.h>
  45. #include <nuttx/net/netdev.h>
  46. #include "devif/devif.h"
  47. #include "icmp/icmp.h"
  48. #ifdef CONFIG_NET_ICMP_SOCKET
  49. /****************************************************************************
  50. * Private Data
  51. ****************************************************************************/
  52. /* The array containing all IPPROTO_ICMP socket connections */
  53. static struct icmp_conn_s g_icmp_connections[CONFIG_NET_ICMP_NCONNS];
  54. /* A list of all free IPPROTO_ICMP socket connections */
  55. static dq_queue_t g_free_icmp_connections;
  56. static sem_t g_free_sem;
  57. /* A list of all allocated IPPROTO_ICMP socket connections */
  58. static dq_queue_t g_active_icmp_connections;
  59. /****************************************************************************
  60. * Public Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Name: icmp_sock_initialize
  64. *
  65. * Description:
  66. * Initialize the IPPROTO_ICMP socket connection structures. Called once
  67. * and only from the network initialization layer.
  68. *
  69. ****************************************************************************/
  70. void icmp_sock_initialize(void)
  71. {
  72. int i;
  73. /* Initialize the queues */
  74. dq_init(&g_free_icmp_connections);
  75. dq_init(&g_active_icmp_connections);
  76. nxsem_init(&g_free_sem, 0, 1);
  77. for (i = 0; i < CONFIG_NET_ICMP_NCONNS; i++)
  78. {
  79. /* Move the connection structure to the free list */
  80. dq_addlast(&g_icmp_connections[i].node, &g_free_icmp_connections);
  81. }
  82. }
  83. /****************************************************************************
  84. * Name: icmp_alloc
  85. *
  86. * Description:
  87. * Allocate a new, uninitialized IPPROTO_ICMP socket connection structure.
  88. * This is normally something done by the implementation of the socket()
  89. * interface.
  90. *
  91. ****************************************************************************/
  92. FAR struct icmp_conn_s *icmp_alloc(void)
  93. {
  94. FAR struct icmp_conn_s *conn = NULL;
  95. int ret;
  96. /* The free list is protected by a semaphore (that behaves like a mutex). */
  97. ret = net_lockedwait(&g_free_sem);
  98. if (ret >= 0)
  99. {
  100. conn = (FAR struct icmp_conn_s *)dq_remfirst(&g_free_icmp_connections);
  101. if (conn != NULL)
  102. {
  103. /* Clear the connection structure */
  104. memset(conn, 0, sizeof(struct icmp_conn_s));
  105. /* Enqueue the connection into the active list */
  106. dq_addlast(&conn->node, &g_active_icmp_connections);
  107. }
  108. nxsem_post(&g_free_sem);
  109. }
  110. return conn;
  111. }
  112. /****************************************************************************
  113. * Name: icmp_free
  114. *
  115. * Description:
  116. * Free a IPPROTO_ICMP socket connection structure that is no longer in
  117. * use. This should be done by the implementation of close().
  118. *
  119. ****************************************************************************/
  120. void icmp_free(FAR struct icmp_conn_s *conn)
  121. {
  122. int ret;
  123. /* The free list is protected by a semaphore (that behaves like a mutex). */
  124. DEBUGASSERT(conn->crefs == 0);
  125. /* Take the semaphore (perhaps waiting) */
  126. while ((ret = net_lockedwait(&g_free_sem)) < 0)
  127. {
  128. /* The only case that an error should occur here is if
  129. * the wait was awakened by a signal.
  130. */
  131. DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
  132. }
  133. UNUSED(ret);
  134. /* Is this the last reference on the connection? It might not be if the
  135. * socket was cloned.
  136. */
  137. if (conn->crefs > 1)
  138. {
  139. /* No.. just decrement the reference count */
  140. conn->crefs--;
  141. }
  142. else
  143. {
  144. /* Remove the connection from the active list */
  145. dq_rem(&conn->node, &g_active_icmp_connections);
  146. /* Free the connection */
  147. dq_addlast(&conn->node, &g_free_icmp_connections);
  148. nxsem_post(&g_free_sem);
  149. }
  150. }
  151. /****************************************************************************
  152. * Name: icmp_active()
  153. *
  154. * Description:
  155. * Find a connection structure that is the appropriate connection to be
  156. * used with the provided ECHO request ID.
  157. *
  158. * Assumptions:
  159. * This function is called from network logic at with the network locked.
  160. *
  161. ****************************************************************************/
  162. FAR struct icmp_conn_s *icmp_active(uint16_t id)
  163. {
  164. FAR struct icmp_conn_s *conn =
  165. (FAR struct icmp_conn_s *)g_active_icmp_connections.head;
  166. while (conn != NULL)
  167. {
  168. /* FIXME lmac in conn should have been set by icmp_bind() */
  169. if (id == conn->id)
  170. {
  171. /* Matching connection found.. return a reference to it */
  172. break;
  173. }
  174. /* Look at the next active connection */
  175. conn = (FAR struct icmp_conn_s *)conn->node.flink;
  176. }
  177. return conn;
  178. }
  179. /****************************************************************************
  180. * Name: icmp_nextconn
  181. *
  182. * Description:
  183. * Traverse the list of allocated packet connections
  184. *
  185. * Assumptions:
  186. * This function is called from network logic at with the network locked.
  187. *
  188. ****************************************************************************/
  189. FAR struct icmp_conn_s *icmp_nextconn(FAR struct icmp_conn_s *conn)
  190. {
  191. if (conn == NULL)
  192. {
  193. return (FAR struct icmp_conn_s *)g_active_icmp_connections.head;
  194. }
  195. else
  196. {
  197. return (FAR struct icmp_conn_s *)conn->node.flink;
  198. }
  199. }
  200. /****************************************************************************
  201. * Name: icmp_findconn
  202. *
  203. * Description:
  204. * Find an ICMP connection structure that is expecting a ICMP ECHO response
  205. * with this ID from this device
  206. *
  207. * Assumptions:
  208. * This function is called from network logic at with the network locked.
  209. *
  210. ****************************************************************************/
  211. FAR struct icmp_conn_s *icmp_findconn(FAR struct net_driver_s *dev,
  212. uint16_t id)
  213. {
  214. FAR struct icmp_conn_s *conn;
  215. for (conn = icmp_nextconn(NULL); conn != NULL; conn = icmp_nextconn(conn))
  216. {
  217. if (conn->id == id && conn->dev == dev && conn->nreqs > 0)
  218. {
  219. return conn;
  220. }
  221. }
  222. return conn;
  223. }
  224. #endif /* CONFIG_NET_ICMP */