utils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /****************************************************************************
  2. * net/utils/utils.h
  3. *
  4. * Copyright (C) 2014-2015 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
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __NET_UTILS_UTILS_H
  36. #define __NET_UTILS_UTILS_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <nuttx/net/net.h>
  42. #include <nuttx/net/ip.h>
  43. /****************************************************************************
  44. * Public Types
  45. ****************************************************************************/
  46. /* These values control the behavior of net_timeval2desc */
  47. enum tv2ds_remainder_e
  48. {
  49. TV2DS_TRUNC = 0, /* Truncate microsecond remainder */
  50. TV2DS_ROUND, /* Round to the nearest full decisecond */
  51. TV2DS_CEIL /* Force to next larger full decisecond */
  52. };
  53. /****************************************************************************
  54. * Public Data
  55. ****************************************************************************/
  56. #ifdef __cplusplus
  57. #define EXTERN extern "C"
  58. extern "C"
  59. {
  60. #else
  61. #define EXTERN extern
  62. #endif
  63. /****************************************************************************
  64. * Public Function Prototypes
  65. ****************************************************************************/
  66. struct net_driver_s; /* Forward reference */
  67. struct timeval; /* Forward reference */
  68. /****************************************************************************
  69. * Name: net_lockinitialize
  70. *
  71. * Description:
  72. * Initialize the locking facility
  73. *
  74. ****************************************************************************/
  75. void net_lockinitialize(void);
  76. /****************************************************************************
  77. * Name: net_breaklock
  78. *
  79. * Description:
  80. * Break the lock, return information needed to restore re-entrant lock
  81. * state.
  82. *
  83. ****************************************************************************/
  84. int net_breaklock(FAR unsigned int *count);
  85. /****************************************************************************
  86. * Name: net_breaklock
  87. *
  88. * Description:
  89. * Restore the locked state
  90. *
  91. ****************************************************************************/
  92. void net_restorelock(unsigned int count);
  93. /****************************************************************************
  94. * Name: net_dsec2timeval
  95. *
  96. * Description:
  97. * Convert a decisecond value to a struct timeval. Needed by getsockopt()
  98. * to report timeout values.
  99. *
  100. * Input Parameters:
  101. * dsec The decisecond value to convert
  102. * tv The struct timeval to receive the converted value
  103. *
  104. * Returned Value:
  105. * None
  106. *
  107. * Assumptions:
  108. *
  109. ****************************************************************************/
  110. void net_dsec2timeval(uint16_t dsec, FAR struct timeval *tv);
  111. /****************************************************************************
  112. * Name: net_dsec2tick
  113. *
  114. * Description:
  115. * Convert a decisecond value to a system clock ticks. Used by IGMP logic.
  116. *
  117. * Input Parameters:
  118. * dsec The decisecond value to convert
  119. *
  120. * Returned Value:
  121. * The decisecond value expressed as system clock ticks
  122. *
  123. ****************************************************************************/
  124. unsigned int net_dsec2tick(int dsec);
  125. /****************************************************************************
  126. * Name: net_timeval2dsec
  127. *
  128. * Description:
  129. * Convert a struct timeval to deciseconds. Needed by setsockopt() to
  130. * save new timeout values.
  131. *
  132. * Input Parameters:
  133. * tv - The struct timeval to convert
  134. * remainder - Determines how to handler the microsecond remainder
  135. *
  136. * Returned Value:
  137. * The converted value
  138. *
  139. * Assumptions:
  140. *
  141. ****************************************************************************/
  142. unsigned int net_timeval2dsec(FAR struct timeval *tv,
  143. enum tv2ds_remainder_e remainder);
  144. /****************************************************************************
  145. * Name: net_ipv6_mask2pref
  146. *
  147. * Description:
  148. * Convert a 128-bit netmask to a prefix length. The Nuttx IPv6
  149. * networking uses 128-bit network masks internally. This function
  150. * converts the IPv6 netmask to a prefix length.
  151. *
  152. * The prefix length is the number of MS '1' bits on in the netmask.
  153. * This, of course, assumes that all MS bits are '1' and all LS bits are
  154. * '0' with no intermixed 1's and 0's. This function searches from the MS
  155. * bit until the first '0' is found (this does not necessary mean that
  156. * there might not be additional '1' bits following the firs '0', but that
  157. * will be a malformed netmask.
  158. *
  159. * Input Parameters:
  160. * mask Points to an IPv6 netmask in the form of uint16_t[8]
  161. *
  162. * Returned Value:
  163. * The prefix length, range 0-128 on success; This function will not
  164. * fail.
  165. *
  166. ****************************************************************************/
  167. #ifdef CONFIG_NET_IPv6
  168. uint8_t net_ipv6_mask2pref(FAR const uint16_t *mask);
  169. #endif
  170. /****************************************************************************
  171. * Name: net_ipv6_pref2mask
  172. *
  173. * Description:
  174. * Convert a IPv6 prefix length to a network mask. The prefix length
  175. * specifies the number of MS bits under mask (0-128)
  176. *
  177. * Input Parameters:
  178. * preflen - Determines the width of the netmask (in bits). Range 0-128
  179. * mask - The location to return the netmask.
  180. *
  181. * Returned Value:
  182. * None
  183. *
  184. ****************************************************************************/
  185. #ifdef CONFIG_NET_IPv6
  186. void net_ipv6_pref2mask(uint8_t preflen, net_ipv6addr_t mask);
  187. #endif
  188. /****************************************************************************
  189. * Name: chksum
  190. *
  191. * Description:
  192. * Calculate the raw change some over the memory region described by
  193. * data and len.
  194. *
  195. * Input Parameters:
  196. * sum - Partial calculations carried over from a previous call to chksum().
  197. * This should be zero on the first time that check sum is called.
  198. * data - Beginning of the data to include in the checksum.
  199. * len - Length of the data to include in the checksum.
  200. *
  201. * Returned Value:
  202. * The updated checksum value.
  203. *
  204. ****************************************************************************/
  205. #ifndef CONFIG_NET_ARCH_CHKSUM
  206. uint16_t chksum(uint16_t sum, FAR const uint8_t *data, uint16_t len);
  207. #endif
  208. /****************************************************************************
  209. * Name: net_chksum
  210. *
  211. * Description:
  212. * Calculate the Internet checksum over a buffer.
  213. *
  214. * The Internet checksum is the one's complement of the one's complement
  215. * sum of all 16-bit words in the buffer.
  216. *
  217. * See RFC1071.
  218. *
  219. * If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
  220. * provided by architecture-specific logic.
  221. *
  222. * Input Parameters:
  223. *
  224. * buf - A pointer to the buffer over which the checksum is to be computed.
  225. *
  226. * len - The length of the buffer over which the checksum is to be computed.
  227. *
  228. * Returned Value:
  229. * The Internet checksum of the buffer.
  230. *
  231. ****************************************************************************/
  232. #ifndef CONFIG_NET_ARCH_CHKSUM
  233. uint16_t net_chksum(FAR uint16_t *data, uint16_t len);
  234. #endif
  235. /****************************************************************************
  236. * Name: ipv4_upperlayer_chksum
  237. *
  238. * Description:
  239. * Perform the checksum calcaultion over the IPv4, protocol headers, and
  240. * data payload as necessary.
  241. *
  242. * Input Parameters:
  243. * dev - The network driver instance. The packet data is in the d_buf
  244. * of the device.
  245. * proto - The protocol being supported
  246. *
  247. * Returned Value:
  248. * The calculated checksum
  249. *
  250. ****************************************************************************/
  251. #if !defined(CONFIG_NET_ARCH_CHKSUM) && defined(CONFIG_NET_IPv4)
  252. uint16_t ipv4_upperlayer_chksum(FAR struct net_driver_s *dev, uint8_t proto);
  253. #endif
  254. /****************************************************************************
  255. * Name: ipv6_upperlayer_chksum
  256. *
  257. * Description:
  258. * Perform the checksum calculation over the IPv6, protocol headers, and
  259. * data payload as necessary.
  260. *
  261. * Input Parameters:
  262. * dev - The network driver instance. The packet data is in the d_buf
  263. * of the device.
  264. * proto - The protocol being supported
  265. * iplen - The size of the IPv6 header. This may be larger than
  266. * IPv6_HDRLEN the IPv6 header if IPv6 extension headers are
  267. * present.
  268. *
  269. * Returned Value:
  270. * The calculated checksum
  271. *
  272. ****************************************************************************/
  273. #if !defined(CONFIG_NET_ARCH_CHKSUM) && defined(CONFIG_NET_IPv6)
  274. uint16_t ipv6_upperlayer_chksum(FAR struct net_driver_s *dev,
  275. uint8_t proto, unsigned int iplen);
  276. #endif
  277. /****************************************************************************
  278. * Name: tcp_chksum, tcp_ipv4_chksum, and tcp_ipv6_chksum
  279. *
  280. * Description:
  281. * Calculate the TCP checksum of the packet in d_buf and d_appdata.
  282. *
  283. * The TCP checksum is the Internet checksum of data contents of the
  284. * TCP segment, and a pseudo-header as defined in RFC793.
  285. *
  286. * Note: The d_appdata pointer that points to the packet data may
  287. * point anywhere in memory, so it is not possible to simply calculate
  288. * the Internet checksum of the contents of the d_buf buffer.
  289. *
  290. * Returned Value:
  291. * The TCP checksum of the TCP segment in d_buf and pointed to by
  292. * d_appdata.
  293. *
  294. ****************************************************************************/
  295. #ifdef CONFIG_NET_IPv4
  296. uint16_t tcp_ipv4_chksum(FAR struct net_driver_s *dev);
  297. #endif
  298. #ifdef CONFIG_NET_IPv6
  299. /* REVIST: Is this used? */
  300. uint16_t tcp_ipv6_chksum(FAR struct net_driver_s *dev);
  301. #endif
  302. #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
  303. uint16_t tcp_chksum(FAR struct net_driver_s *dev);
  304. #elif defined(CONFIG_NET_IPv4)
  305. # define tcp_chksum(d) tcp_ipv4_chksum(d)
  306. #else /* if defined(CONFIG_NET_IPv6) */
  307. # define tcp_chksum(d) tcp_ipv6_chksum(d)
  308. #endif
  309. /****************************************************************************
  310. * Name: udp_ipv4_chksum
  311. *
  312. * Description:
  313. * Calculate the UDP/IPv4 checksum of the packet in d_buf and d_appdata.
  314. *
  315. ****************************************************************************/
  316. #if defined(CONFIG_NET_UDP_CHECKSUMS) && defined(CONFIG_NET_IPv4)
  317. uint16_t udp_ipv4_chksum(FAR struct net_driver_s *dev);
  318. #endif
  319. /****************************************************************************
  320. * Name: udp_ipv6_chksum
  321. *
  322. * Description:
  323. * Calculate the UDP/IPv6 checksum of the packet in d_buf and d_appdata.
  324. *
  325. ****************************************************************************/
  326. #if defined(CONFIG_NET_UDP_CHECKSUMS) && defined(CONFIG_NET_IPv6)
  327. uint16_t udp_ipv6_chksum(FAR struct net_driver_s *dev);
  328. #endif
  329. /****************************************************************************
  330. * Name: icmp_chksum
  331. *
  332. * Description:
  333. * Calculate the checksum of the IPv4 ICMP message
  334. *
  335. ****************************************************************************/
  336. #if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_SOCKET)
  337. uint16_t icmp_chksum(FAR struct net_driver_s *dev, int len);
  338. #endif
  339. /****************************************************************************
  340. * Name: icmpv6_chksum
  341. *
  342. * Description:
  343. * Calculate the checksum of the ICMPv6 message
  344. *
  345. ****************************************************************************/
  346. #ifdef CONFIG_NET_ICMPv6
  347. uint16_t icmpv6_chksum(FAR struct net_driver_s *dev, unsigned int iplen);
  348. #endif
  349. #undef EXTERN
  350. #ifdef __cplusplus
  351. }
  352. #endif
  353. #endif /* __NET_UTILS_UTILS_H */