icmp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /****************************************************************************
  2. * net/icmp/icmp.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __NET_ICMP_ICMP_H
  21. #define __NET_ICMP_ICMP_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <sys/types.h>
  27. #include <stdint.h>
  28. #include <queue.h>
  29. #include <assert.h>
  30. #include <nuttx/mm/iob.h>
  31. #include <nuttx/net/ip.h>
  32. #include <nuttx/net/netdev.h>
  33. #if defined(CONFIG_NET_ICMP) && !defined(CONFIG_NET_ICMP_NO_STACK)
  34. /****************************************************************************
  35. * Pre-processor Definitions
  36. ****************************************************************************/
  37. #define NET_ICMP_HAVE_STACK 1
  38. /* Allocate/free an ICMP data callback */
  39. #define icmp_callback_alloc(dev, conn) \
  40. devif_callback_alloc((dev), &(conn)->list)
  41. #define icmp_callback_free(dev, conn, cb) \
  42. devif_conn_callback_free((dev), (cb), &(conn)->list)
  43. /****************************************************************************
  44. * Public types
  45. ****************************************************************************/
  46. struct socket; /* Forward reference */
  47. struct sockaddr; /* Forward reference */
  48. struct pollfd; /* Forward reference */
  49. #ifdef CONFIG_NET_ICMP_SOCKET
  50. /* Representation of a IPPROTO_ICMP socket connection */
  51. struct devif_callback_s; /* Forward reference */
  52. /* This is a container that holds the poll-related information */
  53. struct icmp_poll_s
  54. {
  55. FAR struct socket *psock; /* IPPROTO_ICMP socket structure */
  56. FAR struct pollfd *fds; /* Needed to handle poll events */
  57. FAR struct devif_callback_s *cb; /* Needed to teardown the poll */
  58. };
  59. struct icmp_conn_s
  60. {
  61. /* Common prologue of all connection structures. */
  62. dq_entry_t node; /* Supports a doubly linked list */
  63. /* This is a list of ICMP callbacks. Each callback represents a thread
  64. * that is stalled, waiting for a device-specific event.
  65. */
  66. FAR struct devif_callback_s *list;
  67. /* ICMP-specific content follows */
  68. uint16_t id; /* ICMP ECHO request ID */
  69. uint8_t nreqs; /* Number of requests with no response received */
  70. uint8_t crefs; /* Reference counts on this instance */
  71. /* The device that the ICMP request was sent on */
  72. FAR struct net_driver_s *dev; /* Needed to free the callback structure */
  73. /* ICMP response read-ahead list. A singly linked list of type struct
  74. * iob_qentry_s where the ICMP read-ahead data for the current ID is
  75. * retained.
  76. */
  77. struct iob_queue_s readahead; /* Read-ahead buffering */
  78. /* The following is a list of poll structures of threads waiting for
  79. * socket events.
  80. */
  81. struct icmp_poll_s pollinfo[CONFIG_NET_ICMP_NPOLLWAITERS];
  82. };
  83. #endif
  84. /****************************************************************************
  85. * Public Data
  86. ****************************************************************************/
  87. #ifdef __cplusplus
  88. # define EXTERN extern "C"
  89. extern "C"
  90. {
  91. #else
  92. # define EXTERN extern
  93. #endif
  94. #ifdef CONFIG_NET_ICMP_SOCKET
  95. /* PF_INET socket address family, IPPROTO_ICMP protocol interface */
  96. EXTERN const struct sock_intf_s g_icmp_sockif;
  97. #endif
  98. /****************************************************************************
  99. * Public Function Prototypes
  100. ****************************************************************************/
  101. /****************************************************************************
  102. * Name: icmp_input
  103. *
  104. * Description:
  105. * Handle incoming ICMP input
  106. *
  107. * Input Parameters:
  108. * dev - The device driver structure containing the received ICMP
  109. * packet
  110. *
  111. * Returned Value:
  112. * None
  113. *
  114. * Assumptions:
  115. * The network is locked.
  116. *
  117. ****************************************************************************/
  118. void icmp_input(FAR struct net_driver_s *dev);
  119. /****************************************************************************
  120. * Name: icmp_sock_initialize
  121. *
  122. * Description:
  123. * Initialize the IPPROTO_ICMP socket connection structures. Called once
  124. * and only from the network initialization layer.
  125. *
  126. ****************************************************************************/
  127. #ifdef CONFIG_NET_ICMP_SOCKET
  128. void icmp_sock_initialize(void);
  129. #endif
  130. /****************************************************************************
  131. * Name: icmp_alloc
  132. *
  133. * Description:
  134. * Allocate a new, uninitialized IPPROTO_ICMP socket connection structure.
  135. * This is normally something done by the implementation of the socket()
  136. * interface.
  137. *
  138. ****************************************************************************/
  139. #ifdef CONFIG_NET_ICMP_SOCKET
  140. FAR struct icmp_conn_s *icmp_alloc(void);
  141. #endif
  142. /****************************************************************************
  143. * Name: icmp_free
  144. *
  145. * Description:
  146. * Free a IPPROTO_ICMP socket connection structure that is no longer in
  147. * use. This should be done by the implementation of close().
  148. *
  149. ****************************************************************************/
  150. #ifdef CONFIG_NET_ICMP_SOCKET
  151. void icmp_free(FAR struct icmp_conn_s *conn);
  152. #endif
  153. /****************************************************************************
  154. * Name: icmp_active()
  155. *
  156. * Description:
  157. * Find a connection structure that is the appropriate connection to be
  158. * used with the provided ECHO request ID.
  159. *
  160. * Assumptions:
  161. * This function is called from network logic at with the network locked.
  162. *
  163. ****************************************************************************/
  164. #ifdef CONFIG_NET_ICMP_SOCKET
  165. FAR struct icmp_conn_s *icmp_active(uint16_t id);
  166. #endif
  167. /****************************************************************************
  168. * Name: icmp_nextconn
  169. *
  170. * Description:
  171. * Traverse the list of allocated packet connections
  172. *
  173. * Assumptions:
  174. * This function is called from network logic at with the network locked.
  175. *
  176. ****************************************************************************/
  177. #ifdef CONFIG_NET_ICMP_SOCKET
  178. FAR struct icmp_conn_s *icmp_nextconn(FAR struct icmp_conn_s *conn);
  179. #endif
  180. /****************************************************************************
  181. * Name: icmp_findconn
  182. *
  183. * Description:
  184. * Find an ICMP connection structure that is expecting a ICMP ECHO response
  185. * with this ID from this device
  186. *
  187. * Assumptions:
  188. * This function is called from network logic at with the network locked.
  189. *
  190. ****************************************************************************/
  191. #ifdef CONFIG_NET_ICMP_SOCKET
  192. FAR struct icmp_conn_s *icmp_findconn(FAR struct net_driver_s *dev,
  193. uint16_t id);
  194. #endif
  195. /****************************************************************************
  196. * Name: icmp_poll
  197. *
  198. * Description:
  199. * Poll a device "connection" structure for availability of ICMP TX data
  200. *
  201. * Input Parameters:
  202. * dev - The device driver structure to use in the send operation
  203. * conn - A pointer to the ICMP connection structure
  204. *
  205. * Returned Value:
  206. * None
  207. *
  208. * Assumptions:
  209. * The network is locked.
  210. *
  211. ****************************************************************************/
  212. #ifdef CONFIG_NET_ICMP_SOCKET
  213. void icmp_poll(FAR struct net_driver_s *dev, FAR struct icmp_conn_s *conn);
  214. #endif
  215. /****************************************************************************
  216. * Name: icmp_sendmsg
  217. *
  218. * Description:
  219. * Implements the sendmsg() operation for the case of the IPPROTO_ICMP
  220. * socket. The 'buf' parameter points to a block of memory that includes
  221. * an ICMP request header, followed by any payload that accompanies the
  222. * request. The 'len' parameter includes both the size of the ICMP header
  223. * and the following payload.
  224. *
  225. * Input Parameters:
  226. * psock A pointer to a NuttX-specific, internal socket structure
  227. * msg Message to send
  228. * flags Send flags
  229. *
  230. * Returned Value:
  231. * On success, returns the number of characters sent. On error, a negated
  232. * errno value is returned (see sendmsg() for the list of appropriate error
  233. * values.
  234. *
  235. ****************************************************************************/
  236. #ifdef CONFIG_NET_ICMP_SOCKET
  237. ssize_t icmp_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
  238. int flags);
  239. #endif
  240. /****************************************************************************
  241. * Name: icmp_recvmsg
  242. *
  243. * Description:
  244. * Implements the socket recvfrom interface for the case of the AF_INET
  245. * data gram socket with the IPPROTO_ICMP protocol. icmp_recvmsg()
  246. * receives ICMP ECHO replies for the a socket.
  247. *
  248. * If msg_name is not NULL, and the underlying protocol provides the source
  249. * address, this source address is filled in. The argument 'msg_namelen' is
  250. * initialized to the size of the buffer associated with msg_name, and
  251. * modified on return to indicate the actual size of the address stored
  252. * there.
  253. *
  254. * Input Parameters:
  255. * psock A pointer to a NuttX-specific, internal socket structure
  256. * msg Buffer to receive the message
  257. * flags Receive flags
  258. *
  259. * Returned Value:
  260. * On success, returns the number of characters received. If no data is
  261. * available to be received and the peer has performed an orderly shutdown,
  262. * recvmsg() will return 0. Otherwise, on errors, a negated errno value is
  263. * returned (see recvmsg() for the list of appropriate error values).
  264. *
  265. ****************************************************************************/
  266. #ifdef CONFIG_NET_ICMP_SOCKET
  267. ssize_t icmp_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
  268. int flags);
  269. #endif
  270. /****************************************************************************
  271. * Name: icmp_pollsetup
  272. *
  273. * Description:
  274. * Setup to monitor events on one ICMP socket
  275. *
  276. * Input Parameters:
  277. * psock - The IPPROTO_ICMP socket of interest
  278. * fds - The structure describing the events to be monitored, OR NULL if
  279. * this is a request to stop monitoring events.
  280. *
  281. * Returned Value:
  282. * 0: Success; Negated errno on failure
  283. *
  284. ****************************************************************************/
  285. #ifdef CONFIG_NET_ICMP_SOCKET
  286. int icmp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds);
  287. #endif
  288. /****************************************************************************
  289. * Name: icmp_pollteardown
  290. *
  291. * Description:
  292. * Teardown monitoring of events on an ICMP socket
  293. *
  294. * Input Parameters:
  295. * psock - The IPPROTO_ICMP socket of interest
  296. * fds - The structure describing the events to be monitored, OR NULL if
  297. * this is a request to stop monitoring events.
  298. *
  299. * Returned Value:
  300. * 0: Success; Negated errno on failure
  301. *
  302. ****************************************************************************/
  303. #ifdef CONFIG_NET_ICMP_SOCKET
  304. int icmp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds);
  305. #endif
  306. #undef EXTERN
  307. #ifdef __cplusplus
  308. }
  309. #endif
  310. #endif /* CONFIG_NET_ICMP && !CONFIG_NET_ICMP_NO_STACK */
  311. #endif /* __NET_ICMP_ICMP_H */