PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/platform/FNET/fnet_stack/stack/fnet_socket.h

https://gitlab.com/fuggles/ucos
C Header | 1512 lines | 174 code | 58 blank | 1280 comment | 5 complexity | cf7ed5ddff4fe02eb8c9698813fbcdb5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /**************************************************************************
  2. *
  3. * Copyright 2011-2015 by Andrey Butok. FNET Community.
  4. * Copyright 2008-2010 by Andrey Butok. Freescale Semiconductor, Inc.
  5. * Copyright 2003 by Andrey Butok. Motorola SPS.
  6. *
  7. ***************************************************************************
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License Version 3
  10. * or later (the "LGPL").
  11. *
  12. * As a special exception, the copyright holders of the FNET project give you
  13. * permission to link the FNET sources with independent modules to produce an
  14. * executable, regardless of the license terms of these independent modules,
  15. * and to copy and distribute the resulting executable under terms of your
  16. * choice, provided that you also meet, for each linked independent module,
  17. * the terms and conditions of the license of that module.
  18. * An independent module is a module which is not derived from or based
  19. * on this library.
  20. * If you modify the FNET sources, you may extend this exception
  21. * to your version of the FNET sources, but you are not obligated
  22. * to do so. If you do not wish to do so, delete this
  23. * exception statement from your version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * and the GNU Lesser General Public License along with this program.
  31. * If not, see <http://www.gnu.org/licenses/>.
  32. *
  33. **********************************************************************/ /*!
  34. *
  35. * @file fnet_socket.h
  36. *
  37. * @author Andrey Butok
  38. *
  39. * @brief Socket API.
  40. *
  41. ***************************************************************************/
  42. #ifndef _FNET_SOCKET_H_
  43. #define _FNET_SOCKET_H_
  44. #include "fnet_ip.h"
  45. #include "fnet_ip6.h"
  46. /*! @addtogroup fnet_socket
  47. * The Socket Application Program Interface (API) defines the way, in which the
  48. * application program interacts with the TCP/IP stack. It has the
  49. * BSD-like, non-blocking socket interface that makes porting of existing network applications
  50. * to the FNET more convenient.@n
  51. * @n
  52. * The following table summarizes the supported socket API functions:
  53. * <table>
  54. * <caption>Socket functions</caption>
  55. * <tr class="fnet_td_grey">
  56. * <th ALIGN=CENTER>Category</th><th ALIGN=CENTER>Routine</th>
  57. * <th ALIGN=CENTER>Meaning</th><th ALIGN=CENTER>Server</th>
  58. * <th ALIGN=CENTER>Client</th><th ALIGN=CENTER>@ref SOCK_STREAM</th>
  59. * <th ALIGN=CENTER>@ref SOCK_DGRAM</th>
  60. * </tr>
  61. * <tr>
  62. * <td>setup</td><td>@ref socket()</td><td>Creates a new unnamed socket within
  63. * a specified communication domain family.</td><td>X</td><td>X</td><td>X</td>
  64. * <td>X</td>
  65. * </tr>
  66. * <tr>
  67. * <td>setup</td><td>@ref bind()</td><td>Assigns a local address to
  68. * a socket.</td><td>X</td><td>X</td><td>X</td><td>X</td>
  69. * </tr>
  70. * <tr>
  71. * <td>server</td><td>@ref listen()</td><td> Prepares a socket to accept
  72. * the incoming connections.</td><td>X</td><td>@n</td><td>X</td><td>@n</td>
  73. * </tr>
  74. * <tr>
  75. * <td>server</td><td>@ref accept()</td><td>Accepts the connections.</td>
  76. * <td>X</td><td>@n</td><td>X</td><td>@n</td>
  77. * </tr>
  78. * <tr>
  79. * <td>client</td><td>@ref connect()</td><td> Establishes a connection to
  80. * a foreign socket.</td><td>@n</td><td>X</td><td>X</td><td>X</td>
  81. * </tr>
  82. * <tr>
  83. * <td>input</td><td>@ref recv()</td><td>Receives the data.</td><td>X</td>
  84. * <td>X</td><td>X</td><td>X</td>
  85. * </tr>
  86. * <tr>
  87. * <td>input</td><td>@ref recvfrom()</td><td>Receives the data and address of
  88. * the sender.</td><td>X</td><td>X</td><td>@n</td><td>X</td>
  89. * </tr>
  90. * <tr>
  91. * <td>output</td><td>@ref send()</td><td>Sends the data.</td><td>X</td>
  92. * <td>X</td><td>X</td><td>X</td>
  93. * </tr>
  94. * <tr>
  95. * <td>output</td><td>@ref sendto()</td><td>Sends the data to a specified
  96. * address.</td><td>X</td><td>X</td><td>@n</td><td>X</td>
  97. * </tr>
  98. * <tr>
  99. * <td>termination</td><td>@ref shutdown()</td><td>Terminates a connection
  100. * in one or both directions.</td><td>X</td><td>X</td><td>X</td><td>X</td>
  101. * </tr>
  102. * <tr>
  103. * <td>termination</td><td>@ref closesocket()</td><td>Terminates a connection
  104. * and releases the socket.</td><td>X</td><td>X</td><td>X</td><td>X</td>
  105. * </tr>
  106. * <tr>
  107. * <td>administration</td><td>@ref setsockopt()</td><td>Sets socket or protocol
  108. * options.</td><td>X</td><td>X</td><td>X</td><td>X</td>
  109. * </tr>
  110. * <tr>
  111. * <td>administration</td><td>@ref getsockopt()</td><td>Gets socket or protocol
  112. * options.</td><td>X</td><td>X</td><td>X</td><td>X</td>
  113. * </tr>
  114. * <tr>
  115. * <td>administration</td><td>@ref getsockname()</td><td>Gets a local address
  116. * assigned to a socket.</td><td>X</td><td>X</td><td>X</td><td>X</td>
  117. * </tr>
  118. * <tr>
  119. * <td>administration</td><td>@ref getpeername()</td><td>Gets a foreign address
  120. * assigned to a socket.</td><td>X</td><td>X</td><td>X</td><td>X</td>
  121. * </tr>
  122. * </table>
  123. * Configuration parameters:
  124. * - @ref FNET_CFG_SOCKET_MAX
  125. * - @ref FNET_CFG_SOCKET_TCP_TX_BUF_SIZE
  126. * - @ref FNET_CFG_SOCKET_TCP_RX_BUF_SIZE
  127. * - @ref FNET_CFG_SOCKET_UDP_TX_BUF_SIZE
  128. * - @ref FNET_CFG_SOCKET_UDP_RX_BUF_SIZE
  129. * - @ref FNET_CFG_SOCKET_TCP_MSS
  130. * - @ref FNET_CFG_RAW
  131. */
  132. /*! @{ */
  133. /* Special addresses */
  134. /**************************************************************************/ /*!
  135. * @brief It means to use any network interface.
  136. ******************************************************************************/
  137. #define INADDR_ANY (unsigned long)(0x00000000U)
  138. /**************************************************************************/ /*!
  139. * @brief Broadcast address. @n
  140. * It can be used to send the broadcast UDP datagrams over an IP network.
  141. ******************************************************************************/
  142. #define INADDR_BROADCAST (unsigned long)(0xffffffffU)
  143. /**************************************************************************/ /*!
  144. * @brief Address family type.
  145. * @see AF_UNSPEC, AF_INET, AF_INET6, AF_SUPPORTED
  146. ******************************************************************************/
  147. typedef unsigned short fnet_address_family_t;
  148. /**************************************************************************/ /*!
  149. * @brief Unspecified address family.
  150. ******************************************************************************/
  151. #define AF_UNSPEC (0U)
  152. /**************************************************************************/ /*!
  153. * @brief IPv4 address family.
  154. ******************************************************************************/
  155. #define AF_INET (1U)
  156. /**************************************************************************/ /*!
  157. * @brief IPv6 address family.
  158. ******************************************************************************/
  159. #define AF_INET6 (2U)
  160. /**************************************************************************/ /*!
  161. * @brief Bitmask of supported address families.
  162. * @showinitializer
  163. ******************************************************************************/
  164. #define AF_SUPPORTED ((fnet_address_family_t)((fnet_address_family_t)(AF_INET6*(fnet_address_family_t)FNET_CFG_IP6) | (fnet_address_family_t)(AF_INET*(fnet_address_family_t)FNET_CFG_IP4)))
  165. /* Size of sa_data[]*/
  166. #if FNET_CFG_IP6
  167. #define FNET_SA_DATA_SIZE (20U) /* To cover sockaddr_in and sockaddr_in6. */
  168. #else /* IPv4 */
  169. #define FNET_SA_DATA_SIZE (4U)
  170. #endif
  171. /**************************************************************************/ /*!
  172. * @def FNET_IP_ADDR_STR_SIZE
  173. * @brief Size of the string buffer that will contain
  174. * null-terminated ASCII string of an IP address. It depends on
  175. * if disabled or enabled IPv6.
  176. * @see fnet_inet_ntoa, fnet_inet_ntop
  177. * @showinitializer
  178. ******************************************************************************/
  179. #if FNET_CFG_IP6
  180. #define FNET_IP_ADDR_STR_SIZE FNET_IP6_ADDR_STR_SIZE
  181. #else /* IPv4 */
  182. #define FNET_IP_ADDR_STR_SIZE FNET_IP4_ADDR_STR_SIZE
  183. #endif
  184. /**************************************************************************/ /*!
  185. * @brief Socket address structure.
  186. *
  187. * @see sockaddr_in, sockaddr_in6
  188. *
  189. * The original goal of the @ref sockaddr structure is to support multiple
  190. * protocols. For the TCP/IP stack @c sa_data, it contains a destination address
  191. * and port number for a socket.
  192. ******************************************************************************/
  193. struct sockaddr
  194. {
  195. fnet_address_family_t sa_family;/**< @brief Address family. Specifies the
  196. * address family, to which the address belongs. @n
  197. * It is defined by @ref fnet_address_family_t.
  198. */
  199. unsigned short sa_port; /**< @brief 16-bit port number used to
  200. * demultiplex the transport-level messages
  201. * (in network byte order).
  202. */
  203. char sa_data[FNET_SA_DATA_SIZE];/**< @brief Address value. For the TCP/IP stack,
  204. * it contains the destination address and port
  205. * number for a socket.
  206. */
  207. };
  208. /**************************************************************************/ /*!
  209. * @brief IPv4 address structure.
  210. *
  211. * @see sockaddr_in
  212. *
  213. * Actually, it represents an IPv4 address (in network endian).
  214. ******************************************************************************/
  215. struct in_addr
  216. {
  217. fnet_ip4_addr_t s_addr; /**< @brief 32-bit IPv4 address (in network byte order).
  218. */
  219. };
  220. /**************************************************************************/ /*!
  221. * @brief IPv4 Socket address structure.
  222. *
  223. * @see sockaddr
  224. *
  225. * To make manipulation of the @ref sockaddr structure easier, the TCP/IPv4 stack
  226. * also defines the equivalent structure @ref sockaddr_in
  227. * ("in" means "Internet").
  228. ******************************************************************************/
  229. struct sockaddr_in
  230. {
  231. fnet_address_family_t sin_family; /**< @brief Specifies the address family. @n
  232. * It must ne set to @ref AF_INET.
  233. */
  234. unsigned short sin_port; /**< @brief 16-bit port number used to
  235. * demultiplex the transport-level messages
  236. * (in network byte order).
  237. */
  238. struct in_addr sin_addr; /**< @brief 32-bit internet address.
  239. */
  240. };
  241. /************************************************************************
  242. * IPv6
  243. *************************************************************************/
  244. /**************************************************************************/ /*!
  245. * @brief IPv6 address structure.
  246. *
  247. * @see sockaddr_in6
  248. *
  249. * Actually, it represents an IPv6 address.
  250. ******************************************************************************/
  251. struct in6_addr
  252. {
  253. fnet_ip6_addr_t s6_addr; /**< @brief 128-bit IPv6 address.*/
  254. };
  255. /**************************************************************************/ /*!
  256. * @brief IPv6 Socket address structure.
  257. *
  258. * @see sockaddr
  259. *
  260. * To make manipulation of the @ref sockaddr structure easier, the TCP/IPv6 stack
  261. * also defines the equivalent structure @ref sockaddr_in6
  262. * ("in" means "Internet").
  263. ******************************************************************************/
  264. struct sockaddr_in6
  265. {
  266. fnet_address_family_t sin6_family; /**< @brief Specifies the address family. @n
  267. * It must ne set to @ref AF_INET6.
  268. */
  269. unsigned short sin6_port; /**< @brief 16-bit port number used to
  270. * demultiplex the transport-level messages
  271. * (in network byte order).
  272. */
  273. struct in6_addr sin6_addr; /**< @brief 128-bit IPv6 internet address.
  274. */
  275. unsigned long sin6_scope_id; /**< @brief Scope zone index, defining network interface.
  276. */
  277. };
  278. /**************************************************************************/ /*!
  279. * @brief IPv4 multicast group information.
  280. *
  281. * The structure is used with the @ref IP_ADD_MEMBERSHIP and
  282. * @ref IP_DROP_MEMBERSHIP socket options.
  283. *
  284. * @see IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP
  285. *
  286. ******************************************************************************/
  287. struct ip_mreq
  288. {
  289. struct in_addr imr_multiaddr; /**< @brief IPv4 multicast address of group. */
  290. struct in_addr imr_interface; /**< @brief Local IPv4 address of interface on which
  291. * the multicast group should be joined or dropped.@n
  292. * If this member specifies an IPv4 address of 0.0.0.0
  293. * or @ref INADDR_ANY,
  294. * the default interface is used. */
  295. };
  296. /**************************************************************************/ /*!
  297. * @brief IPv6 multicast group information.
  298. *
  299. * The structure is used with the @ref IPV6_JOIN_GROUP and
  300. * @ref IPV6_LEAVE_GROUP socket options.
  301. *
  302. * @see IPV6_JOIN_GROUP, IPV6_LEAVE_GROUP
  303. *
  304. ******************************************************************************/
  305. struct ipv6_mreq
  306. {
  307. struct in6_addr ipv6imr_multiaddr; /**< @brief IPv6 multicast address of group. */
  308. unsigned int ipv6imr_interface; /**< @brief Interface index. It equals to the scope zone index, defining network interface.@n
  309. * If this member is zero, the default interface is used. */
  310. };
  311. /**************************************************************************/ /*!
  312. * @brief Socket types.
  313. ******************************************************************************/
  314. typedef enum
  315. {
  316. SOCK_UNSPEC = (0U), /**< @brief Unspecified socket type.
  317. */
  318. SOCK_STREAM = (1U), /**< @brief Stream socket.@n
  319. * Provides reliable, two-way, connection-based
  320. * byte stream. It corresponds to the TCP protocol
  321. */
  322. SOCK_DGRAM = (2U), /**< @brief Datagram socket.@n
  323. * Provides unreliable, connectionless datagrams.
  324. * It corresponds to the UDP protocol.
  325. */
  326. SOCK_RAW = (3U) /**< @brief Raw socket.@n
  327. * Raw sockets allow an application to have direct access to
  328. * lower-level communication protocols.
  329. * Raw sockets are intended to take advantage of some protocol feature
  330. * that is not directly accessible through a normal interface,
  331. * or to build new protocols on top of existing low-level protocols.@n
  332. * It can be enabled by the @ref FNET_CFG_RAW option.
  333. */
  334. } fnet_socket_type_t;
  335. /**************************************************************************/ /*!
  336. * @brief Socket state.
  337. ******************************************************************************/
  338. typedef enum
  339. {
  340. SS_UNCONNECTED = (0), /**< @brief Not connected to any socket.
  341. */
  342. SS_CONNECTING = (1), /**< @brief In process of connecting.
  343. */
  344. SS_CONNECTED = (2), /**< @brief Connected to a socket.
  345. */
  346. SS_LISTENING = (3) /**< @brief In listening state.
  347. */
  348. } fnet_socket_state_t;
  349. /**************************************************************************/ /*!
  350. * @brief Protocol numbers and Level numbers for the @ref setsockopt()
  351. * and the @ref getsockopt().
  352. ******************************************************************************/
  353. typedef enum
  354. {
  355. IPPROTO_IP = (0), /**< @brief IPv4 options level number
  356. * for @ref getsockopt() and @ref setsockopt().
  357. */
  358. IPPROTO_ICMP = (1), /**< @brief ICMPv4 protocol number.
  359. */
  360. IPPROTO_IGMP = (2), /**< @brief IGMP protocol number.
  361. */
  362. IPPROTO_TCP = (6), /**< @brief TCP protocol number; TCP options level number
  363. * for @ref getsockopt() and @ref setsockopt().
  364. */
  365. IPPROTO_UDP = (17),/**< @brief UDP protocol number.
  366. */
  367. IPPROTO_IPV6 = (41), /**< @brief IPv6 options level number
  368. * for @ref getsockopt() and @ref setsockopt().
  369. */
  370. IPPROTO_ICMPV6 = (58),/**< @brief ICMPv6 protocol number.
  371. */
  372. SOL_SOCKET = (255255) /**< @brief Socket options level number for
  373. * @ref getsockopt() and @ref setsockopt().
  374. */
  375. } fnet_protocol_t;
  376. /**************************************************************************/ /*!
  377. * @brief Socket level (@ref SOL_SOCKET) options for the @ref setsockopt() and the @ref getsockopt().
  378. *
  379. * <table>
  380. *<caption>Socket SOL_SOCKET level options</caption>
  381. *<tr class="fnet_td_grey">
  382. *<th ALIGN=CENTER>Option</th><th ALIGN=CENTER>Option Type</th><th ALIGN=CENTER>
  383. * Default Value</th><th ALIGN=CENTER>Read/Write</th>
  384. *</tr>
  385. *<tr>
  386. *<td>@ref SO_ACCEPTCONN</td><td>int</td><td>0</td><td>R</td>
  387. *</tr>
  388. *<tr>
  389. *<td>@ref SO_KEEPALIVE</td><td>int</td><td>1</td><td>RW</td>
  390. *</tr>
  391. *<tr>
  392. *<td>@ref SO_DONTROUTE</td><td>int</td><td>0</td><td>RW</td>
  393. *</tr>
  394. *<tr>
  395. *<td>@ref SO_LINGER</td><td>struct @ref linger</td><td>{0,0}</td><td>RW</td>
  396. *</tr>
  397. *<tr>
  398. *<td>@ref SO_OOBINLINE</td><td>int</td><td>0</td><td>RW</td>
  399. *</tr>
  400. *<tr>
  401. *<td>@ref SO_SNDBUF</td><td>unsigned long</td>
  402. *<td>@ref FNET_CFG_SOCKET_UDP_TX_BUF_SIZE for UDP @n @ref FNET_CFG_SOCKET_TCP_TX_BUF_SIZE for TCP</td><td>RW</td>
  403. *</tr>
  404. *<tr>
  405. *<td>@ref SO_RCVBUF</td><td>unsigned long</td>
  406. *<td>@ref FNET_CFG_SOCKET_UDP_RX_BUF_SIZE for UDP @n @ref FNET_CFG_SOCKET_TCP_RX_BUF_SIZE for TCP</td><td>RW</td>
  407. *</tr>
  408. *<tr>
  409. *<td>@ref SO_STATE</td><td>@ref fnet_socket_state_t</td><td>@ref SS_UNCONNECTED</td><td>R</td>
  410. *</tr>
  411. *<tr>
  412. *<td>@ref SO_ERROR</td><td>@ref fnet_error_t</td><td>@ref FNET_ERR_OK</td><td>R</td>
  413. *</tr>
  414. *<tr>
  415. *<td>@ref SO_TYPE</td><td>@ref fnet_socket_type_t</td><td>@ref SOCK_DGRAM for
  416. * UDP @n @ref SOCK_STREAM for TCP</td><td>R</td>
  417. *</tr>
  418. *<tr>
  419. *<td>@ref SO_RCVNUM</td><td>unsigned long</td><td>0</td><td>R</td>
  420. *</tr>
  421. *<tr>
  422. *<td>@ref SO_SNDNUM</td><td>unsigned long</td><td>0</td><td>R</td>
  423. *</tr>
  424. *</table>
  425. ******************************************************************************/
  426. typedef enum
  427. {
  428. SO_ACCEPTCONN = (1), /**< @brief Returns @c 1 if a socket is in
  429. * listening mode and returns @c 0 when vice versa.
  430. * This is the read-only option and it is
  431. * valid only for connection-oriented protocols (TCP).
  432. */
  433. SO_KEEPALIVE = (2), /**< @brief This option enables keep-alive probes
  434. * for a socket connection. These probes are used
  435. * to maintain a TCP connection and regularly
  436. * test the connection to ensure that it's
  437. * still available. It is only valid for
  438. * connection-oriented protocols (TCP).
  439. */
  440. SO_DONTROUTE = (4), /**< @brief This option enables bypassing of a routing algorithm.
  441. * It means that the network interface tries to send a datagram without
  442. * a gateway.
  443. */
  444. SO_LINGER = (8), /**< @brief This option controls the action
  445. * taken when unsent data is present, and
  446. * @ref closesocket() is called. This option
  447. * is defined by the @ref linger structure.
  448. */
  449. #if FNET_CFG_TCP_URGENT || defined(__DOXYGEN__)
  450. SO_OOBINLINE = (10), /**< @brief This option specifies that out-of-band (OOB)
  451. * data will be received in line with regular data.
  452. * It is valid only for the TCP protocol. @n
  453. * This option is avalable only if
  454. * @ref FNET_CFG_TCP_URGENT is set to @c 1.
  455. */
  456. #endif /* FNET_CFG_TCP_URGENT */
  457. SO_SNDBUF = (1001), /**< @brief This option defines the maximum per-socket
  458. * buffer size for output data.
  459. */
  460. SO_RCVBUF = (1002), /**< @brief This option defines the maximum per-socket
  461. * buffer size for input data.
  462. */
  463. SO_STATE = (1003), /**< @brief This option defines the current state of the socket.@n
  464. * This is the read-only option and it is defined by the @ref fnet_socket_state_t type.
  465. */
  466. SO_ERROR = (1006), /**< @brief This option returns a per-socket-based error code.@n
  467. * The error code is defined by the @ref fnet_error_t type.@n
  468. * After the error value has been read in the @ref getsockopt function, it is cleared.
  469. * But a successful call using the socket routines does not clear
  470. * the socket-based error.
  471. */
  472. SO_TYPE = (1007), /**< @brief This option defines the type of the socket.
  473. * This is a read-only option and it is defined by the @ref fnet_socket_type_t type.
  474. */
  475. SO_RCVNUM = (1008), /**< @brief This option is used to determine the amount of data
  476. * pending in the socket-input buffer.@n
  477. * This is a read-only option.
  478. */
  479. SO_SNDNUM = (1009) /**< @brief This option is used to determine the amount of data
  480. * in the socket output buffer. @n
  481. * This is a read-only option.
  482. */
  483. } fnet_socket_options_t;
  484. /**************************************************************************/ /*!
  485. * @brief TCP level (@ref IPPROTO_TCP) options for the @ref setsockopt()
  486. * and the @ref getsockopt().
  487. *
  488. * <table>
  489. *<caption>Socket IPPROTO_TCP level options</caption>
  490. *<tr class="fnet_td_grey">
  491. *<th ALIGN=CENTER>Option</th><th ALIGN=CENTER>Option Type</th><th ALIGN=CENTER>
  492. * Default Value</th><th ALIGN=CENTER>Read/Write</th>
  493. *</tr>
  494. *<tr>
  495. *<td>@ref TCP_MSS</td><td>unsigned short</td><td>536</td><td>RW</td>
  496. *</tr>
  497. *<tr>
  498. *<td>@ref TCP_BSD</td><td>int</td><td>1</td><td>RW</td>
  499. *</tr>
  500. *<tr>
  501. *<td>@ref TCP_NODELAY</td><td>int</td><td>1</td><td>RW</td>
  502. *</tr>
  503. *<tr>
  504. *<td>@ref TCP_FINRCVD</td><td>int</td><td>0</td><td>R</td>
  505. *</tr>
  506. *<tr>
  507. *<td>@ref TCP_URGRCVD</td><td>int</td><td>0</td><td>R</td>
  508. *</tr>
  509. *<tr>
  510. *<td>@ref TCP_KEEPIDLE</td><td>int</td><td>7200</td><td>RW</td>
  511. *</tr>
  512. *<tr>
  513. *<td>@ref TCP_KEEPINTVL</td><td>int</td><td>75</td><td>RW</td>
  514. *</tr>
  515. *<tr>
  516. *<td>@ref TCP_KEEPCNT</td><td>int</td><td>8</td><td>RW</td>
  517. *</tr>
  518. *</table>
  519. ******************************************************************************/
  520. typedef enum
  521. {
  522. TCP_MSS = (0x01), /**< @brief This option defines the maximum size of
  523. * the input segments (MSS). @n
  524. * The TCP Maximum Segment Size (MSS) defines the maximum amount
  525. * of data that a host is willing to accept in a single TCP segment.@n
  526. * This Maximum Segment Size (MSS) announcement is sent from the
  527. * data receiver to the data sender and says "I can accept TCP segments
  528. * up to size X". The size (X) may be larger or smaller than the
  529. * default.@n
  530. * The MSS counts only data octets in the segment, it does not count the
  531. * TCP header or the IP header.@n
  532. * The default value is defined by the @ref FNET_CFG_SOCKET_TCP_MSS
  533. * user-configuration option. @n
  534. * This option can be set to:
  535. * - @c 0 = The selection of the MSS is
  536. * automatic and is based on the MTU of the outgoing
  537. * interface minus 40 (does not include
  538. * the 20 byte IP header and the 20 byte TCP header).@n
  539. * It is done to assist in avoiding of IP fragmentation
  540. * at the endpoints of the TCP connection.
  541. * - Non-zero value (up to 64K) = The TCP segment could be as large as 64K
  542. * (the maximum IP datagram size), but it could be fragmented
  543. * at the IP layer in order to be transmitted
  544. * across the network to the receiving host.
  545. */
  546. #if FNET_CFG_TCP_URGENT || defined(__DOXYGEN__)
  547. TCP_BSD = (0x02), /**< @brief If this option is set to @c 1, the BSD interpretation of
  548. * the urgent pointer is used. In this case the
  549. * urgent pointer of the TCP segment points to
  550. * the next byte following after the urgent byte.
  551. * Most of the TCP implementations use this
  552. * interpretation by default.@n
  553. * If this option is set to @c 0, the interpretation of
  554. * the TCP specification is used. In this case the
  555. * urgent pointer of the TCP segment points
  556. * to the urgent byte. @n
  557. * This option is avalable only if
  558. * @ref FNET_CFG_TCP_URGENT is set to @c 1.
  559. */
  560. #endif
  561. TCP_NODELAY = (0x04), /**< @brief If this option is set to @c 1, the Nagle algorithm
  562. * is disabled (and vice versa). @n
  563. * The Nagle algorithm is effective in reducing the number
  564. * of small packets sent by the host by essentially buffering
  565. * send data, if there is unacknowledged data already "in flight",
  566. * or until a full-size packet can be sent.@n
  567. * But for some applications this algorithm can impede
  568. * performance, especially for a bulky data transfer.
  569. */
  570. TCP_FINRCVD = (0x08), /**< @brief This option is set when the final (FIN) segment arrives. @n
  571. * This option indicates that another side will not send any data
  572. * in the current connection.@n
  573. * This is the read-only option.
  574. */
  575. #if FNET_CFG_TCP_URGENT || defined(__DOXYGEN__)
  576. TCP_URGRCVD = (0x10), /**< @brief This option is set when the urgent byte arrives, and
  577. * reset when this byte is read.@n
  578. * This option can be set only if the @ref SO_OOBINLINE option is set to @c 0.@n
  579. * This is the read-only option. @n
  580. * This option is avalable only if
  581. * @ref FNET_CFG_TCP_URGENT is set to @c 1.
  582. */
  583. #endif
  584. TCP_KEEPIDLE = (0x20), /**< @brief When the @ref SO_KEEPALIVE option is enabled, TCP probes a connection that
  585. * has been idle for some amount of time. The default value for this idle
  586. * period is @c 7200 seconds (2 hours). The @ref TCP_KEEPIDLE option can be used to affect this
  587. * value for a given socket, and specifies the number of seconds of idle
  588. * time between keepalive probes.
  589. */
  590. TCP_KEEPINTVL = (0x40), /**< @brief When the @ref SO_KEEPALIVE option is enabled, TCP probes a connection that
  591. * has been idle for some amount of time. If the remote system does not
  592. * respond to a keepalive probe, TCP retransmits the probe after some
  593. * amount of time. The default value for this retransmit interval is @c 75
  594. * seconds. The @ref TCP_KEEPINTVL option can be used to affect this value for
  595. * a given socket, and specifies the number of seconds to wait before
  596. * retransmitting a keepalive probe.
  597. */
  598. TCP_KEEPCNT = (0x80) /**< @brief When the @ref SO_KEEPALIVE option is enabled, TCP probes a connection that
  599. * has been idle for some amount of time. If the remote system does not
  600. * respond to a keepalive probe, TCP retransmits the probe a certain
  601. * number of times before a connection is considered to be broken. The
  602. * default value for this keepalive probe retransmit limit is @c 8. The
  603. * @ref TCP_KEEPCNT option can be used to affect this value for a given socket,
  604. * and specifies the maximum number of keepalive probes to be sent.
  605. */
  606. } fnet_tcp_options_t;
  607. /**************************************************************************/ /*!
  608. * @brief IPv4 level (@ref IPPROTO_IP) options for the @ref setsockopt() and
  609. * the @ref getsockopt().
  610. *
  611. * <table>
  612. *<caption>Socket IPPROTO_IP level options</caption>
  613. *<tr class="fnet_td_grey">
  614. *<th ALIGN=CENTER>Option</th><th ALIGN=CENTER>Option Type</th><th ALIGN=CENTER>
  615. * Default Value</th><th ALIGN=CENTER>Read/Write</th>
  616. *</tr>
  617. *<tr>
  618. *<td>@ref IP_TOS</td><td>int</td><td>0</td><td>RW</td>
  619. *</tr>
  620. *<tr>
  621. *<td>@ref IP_TTL</td><td>int</td><td>64</td><td>RW</td>
  622. *</tr>
  623. *<tr>
  624. *<td>@ref IP_MULTICAST_TTL</td><td>int</td><td>1</td><td>RW</td>
  625. *</tr>
  626. *<tr>
  627. *<td>@ref IP_ADD_MEMBERSHIP</td><td>@ref ip_mreq</td><td>N/A</td><td>W</td>
  628. *</tr>
  629. *<tr>
  630. *<td>@ref IP_DROP_MEMBERSHIP</td><td>@ref ip_mreq</td><td>N/A</td><td>W</td>
  631. *</tr>
  632. *</table>
  633. ******************************************************************************/
  634. typedef enum
  635. {
  636. IP_TOS = (3), /**< @brief This option defines the IPv4 TOS
  637. * (type-of-service) field for outgoing datagrams.
  638. */
  639. IP_TTL = (4) /**< @brief This option defines the IPv4 TTL
  640. * (time-to-live) vlaue for outgoing datagrams.
  641. */
  642. #if FNET_CFG_MULTICAST || defined(__DOXYGEN__)
  643. ,
  644. IP_MULTICAST_TTL = (6), /**< @brief This option allows to change IPv4 "time to live" (TTL)
  645. * value for outgoing multicast datagrams.
  646. * Otherwise, multicast datagrams are sent with a default value of 1,
  647. * to prevent them to be forwarded beyond the local network.@n
  648. * This option is available only if @ref FNET_CFG_MULTICAST is set to @c 1.
  649. */
  650. IP_ADD_MEMBERSHIP = (7), /**< @brief Join the socket to the IPv4 multicast group on
  651. * the specified interface. It tells the system to receive packets on
  652. * the network whose destination is the group address (but not its own).
  653. * It is valid only for the SOCK_DGRAM (UDP) sockets.@n
  654. * This option is available only if @ref FNET_CFG_MULTICAST is set to @c 1.
  655. */
  656. IP_DROP_MEMBERSHIP = (8) /**< @brief Drops membership to a IPv4 multicast group and interface.@n
  657. * This option is available only if @ref FNET_CFG_MULTICAST is set to @c 1.
  658. */
  659. #endif /* FNET_CFG_MULTICAST */
  660. } fnet_ip_options_t;
  661. /**************************************************************************/ /*!
  662. * @brief IPv6 level (@ref IPPROTO_IPV6) options (RFC3493) for the @ref setsockopt()
  663. * and the @ref getsockopt().
  664. *
  665. * <table>
  666. *<caption>Socket IPPROTO_IPV6 level options</caption>
  667. *<tr class="fnet_td_grey">
  668. *<th ALIGN=CENTER>Option</th><th ALIGN=CENTER>Option Type</th><th ALIGN=CENTER>
  669. * Default Value</th><th ALIGN=CENTER>Read/Write</th>
  670. *</tr>
  671. *<tr>
  672. *<td>@ref IPV6_UNICAST_HOPS</td><td>int</td><td>0 (64)</td><td>RW</td>
  673. *</tr>
  674. *<tr>
  675. *<td>@ref IPV6_MULTICAST_HOPS</td><td>int</td><td>1</td><td>RW</td>
  676. *</tr>
  677. *<tr>
  678. *<td>@ref IPV6_JOIN_GROUP</td><td>@ref ipv6_mreq</td><td>N/A</td><td>W</td>
  679. *</tr>
  680. *<tr>
  681. *<td>@ref IPV6_LEAVE_GROUP</td><td>@ref ipv6_mreq</td><td>N/A</td><td>W</td>
  682. *</tr>
  683. *</table>
  684. ******************************************************************************/
  685. typedef enum
  686. {
  687. IPV6_UNICAST_HOPS = (4) /**< @brief This option defines hop limit used
  688. * for outgoing unicast IPv6 packets. @n
  689. * Its value can be from 0 till 255.@n
  690. * By default the option is set to 0. It means that the hop limit is defined
  691. * by local IPv6 router, otherwise it equals to 64.
  692. */
  693. ,IPV6_MULTICAST_HOPS = (5) /**< @brief Set the hop limit to use for outgoing multicast IPv6 packets.@n
  694. * If IPV6_MULTICAST_HOPS is not set, the default is 1.
  695. */
  696. ,IPV6_JOIN_GROUP = (6) /**< @brief (RFC3493) Join a multicast group on a specified local interface.@n
  697. * It is valid only for the SOCK_DGRAM (UDP) sockets.
  698. */
  699. ,IPV6_LEAVE_GROUP = (7) /**< @brief (RFC3493) Leave a multicast group on a specified interface.@n
  700. * It is valid only for the SOCK_DGRAM (UDP) sockets.
  701. */
  702. } fnet_ip6_options_t;
  703. /**************************************************************************/ /*!
  704. * @brief This structure is used for the @ref SO_LINGER option.
  705. ******************************************************************************/
  706. struct linger
  707. {
  708. unsigned short l_onoff; /**< @brief Determines, whether the option will be
  709. * turned on @c 1, or off @c 0.
  710. */
  711. unsigned short l_linger; /**< @brief Specifies the amount of time (in seconds)
  712. * to wait when the connection is closed and unsent data
  713. * is discarded.
  714. * If @c l_onoff is @c 0, the @c l_linger value is ignored
  715. * and the stack continues to try to send the data as usually.
  716. */
  717. };
  718. /**************************************************************************/ /*!
  719. * @brief Socket descriptor.
  720. ******************************************************************************/
  721. typedef int SOCKET;
  722. #define SOCKET_INVALID (FNET_ERR) /**< @brief Invalid socket. @n
  723. * This is the return value of the @ref
  724. * socket() and @ref accept() functions
  725. * if an error occurs.
  726. */
  727. #define SOCKET_ERROR (FNET_ERR) /**< @brief Socket error.@n
  728. * This is the return value of
  729. * the socket API functions if an error occurs.
  730. */
  731. /**************************************************************************/ /*!
  732. * @brief The flags parameters for receiving and sending functions
  733. * @ref recv(), @ref recvfrom(), @ref send(), and @ref sendto().
  734. *
  735. * The optional flag specifies the way, in which the call is made.
  736. * It can be constructed by using the bitwise OR.
  737. ******************************************************************************/
  738. typedef enum
  739. {
  740. #if FNET_CFG_TCP_URGENT || defined(__DOXYGEN__)
  741. MSG_OOB = (0x1U), /**< @brief Process out-of-band
  742. * data instead of regular data. @n
  743. * This option is avalable only if
  744. * @ref FNET_CFG_TCP_URGENT is set to @c 1.
  745. */
  746. #endif /* FNET_CFG_TCP_URGENT */
  747. MSG_PEEK = (0x2U), /**< @brief Receive a copy of the
  748. * data without consuming it.
  749. */
  750. MSG_DONTROUTE = (0x4U) /**< @brief Send without using
  751. * routing tables.
  752. */
  753. } fnet_flags_t;
  754. /**************************************************************************/ /*!
  755. * @brief The flags used by @ref shutdown().
  756. *
  757. * They describe what types of socket operation will no longer be allowed.
  758. ******************************************************************************/
  759. typedef enum
  760. {
  761. SD_READ = (0x1U), /**< @brief Data receiving is disabled.
  762. */
  763. SD_WRITE = (0x2U), /**< @brief Data sending is disabled.
  764. */
  765. SD_BOTH = (SD_READ | SD_WRITE) /**< @brief Both receiving and
  766. * sending are disabled.
  767. */
  768. } fnet_sd_flags_t;
  769. /***************************************************************************/ /*!
  770. *
  771. * @brief Creates a socket.
  772. *
  773. *
  774. * @param family Address family that the socket will use, defined
  775. * by the @ref fnet_address_family_t.
  776. *
  777. * @param type Type specification for the new socket, defined by @ref
  778. * fnet_socket_type_t. It can be @ref SOCK_STREAM (TCP) or
  779. * @ref SOCK_DGRAM (UDP).
  780. *
  781. * @param protocol Protocol to be used with the socket that is specific to
  782. * the indicated address family. This stack supports
  783. * @ref IPPROTO_TCP and @ref IPPROTO_UDP.@n
  784. * This parameter is optional, and can be set to zero, as
  785. * the @c type already defines the proper protocol.
  786. *
  787. * @return This function returns:
  788. * - Socket descriptor referencing the new socket, if no error occurs.
  789. * - @ref SOCKET_INVALID if an error occurs. @n
  790. * The specific error code can be retrieved using the @ref fnet_error_get().
  791. *
  792. * @see closesocket()
  793. *
  794. ******************************************************************************
  795. *
  796. * This function creates a socket and returns its descriptor.@n
  797. * The @ref socket() function causes a socket descriptor and any related
  798. * resources to be allocated and bound to a specific transport-service
  799. * provider that supports the requested combination of address family,
  800. * socket type, and protocol parameters.@n
  801. * @n
  802. * After a socket is created:
  803. * - Connection-oriented sockets, such as the @ref SOCK_STREAM, provide
  804. * full-duplex connections. Before any data can be sent or received,
  805. * it must be in a connected state . A connection to
  806. * another socket is established with the @ref connect() call.
  807. * Once connected, the data can be transferred using the @ref send() and the @ref
  808. * recv() calls. When a session has been completed, the @ref closesocket()
  809. * must be performed.
  810. * - Connectionless, message-oriented sockets, such as the @ref SOCK_DGRAM, allow
  811. * sending and receiving of datagrams to and from arbitrary peers using
  812. * the @ref sendto() and the @ref recvfrom(). If such a socket is connected
  813. * to a specific peer, datagrams can be sent to that peer using the
  814. * @ref send(), and can be received only from this peer using the @ref recv().
  815. *
  816. ******************************************************************************/
  817. SOCKET socket( fnet_address_family_t family, fnet_socket_type_t type, int protocol );
  818. /***************************************************************************/ /*!
  819. *
  820. * @brief Assigns a local address to a socket.
  821. *
  822. *
  823. * @param s Descriptor, identifying a socket to bind.
  824. *
  825. * @param name The address to be assigned to the socket, from the @ref
  826. * sockaddr structure.
  827. *
  828. * @param namelen The length of the @c name parameter.
  829. * Normally @c namelen is set to @c sizeof(name).
  830. *
  831. * @return This function returns:
  832. * - @ref FNET_OK if no error occurs.
  833. * - @ref SOCKET_ERROR if an error occurs. @n
  834. * The specific error code can be retrieved using the @ref fnet_error_get().
  835. *
  836. * @see socket()
  837. *
  838. ******************************************************************************
  839. *
  840. * This function associates a local address with the socket.@n
  841. * The @ref bind() function is used on an unconnected socket before
  842. * subsequent calls to the @ref connect() or the @ref listen() functions.
  843. * It is used to bind to either connection-oriented (stream) or connectionless
  844. * (datagram) sockets.@n
  845. * Within the @ref sockaddr structure, the address may be the address assigned
  846. * to a network interface on the host or the @ref INADDR_ANY.
  847. * Using the @ref INADDR_ANY causes the stack to use the default network
  848. * interface address. Using a port number of 0 causes the service provider
  849. * to assign a unique port to the socket with a value between 1024 and 5000.
  850. *
  851. ******************************************************************************/
  852. int bind( SOCKET s, const struct sockaddr *name, unsigned int namelen );
  853. /***************************************************************************/ /*!
  854. *
  855. * @brief Places the socket into a state, where it is listening for
  856. * an incoming connection.
  857. *
  858. *
  859. * @param s Descriptor identifying a socket that will be
  860. * used for listening.
  861. *
  862. * @param backlog Maximum length of the queue of pending connections.
  863. *
  864. *
  865. * @return This function returns:
  866. * - @ref FNET_OK if no error occurs.
  867. * - @ref SOCKET_ERROR if an error occurs. @n
  868. * The specific error code can be retrieved using the @ref fnet_error_get().
  869. *
  870. * @see socket(), bind(), accept()
  871. *
  872. ******************************************************************************
  873. *
  874. * Only connection-oriented sockets (@ref SOCK_STREAM) are used with the
  875. * @ref listen().@n
  876. * The socket is put into passive mode, where the incoming connection
  877. * requests are acknowledged and queued pending acceptance by the listening socket.
  878. * The @ref listen() function is typically used by servers that can have
  879. * more than one connection request at a time. @n
  880. * An application can call @ref listen() more than once on the same socket.
  881. * If there are more pending connections than the new backlog value,
  882. * the excess pending connections will be reset and dropped.
  883. *
  884. ******************************************************************************/
  885. int listen( SOCKET s, int backlog );
  886. /***************************************************************************/ /*!
  887. *
  888. * @brief Accepts a connection on the specified socket.
  889. *
  890. *
  891. * @param s Descriptor, identifying an unconnected socket.
  892. *
  893. * @param addr Optional pointer to a buffer that receives the address
  894. * of the remote host at the other end of the connection.
  895. *
  896. * @param addrlen Optional pointer to an integer that contains the
  897. * length of the @c addr parameter.
  898. *
  899. *
  900. * @return This function returns:
  901. * - Socket descriptor referencing the new socket, if no error occurs.
  902. * - @ref SOCKET_INVALID if an error occurs. @n
  903. * The specific error code can be retrieved using @ref fnet_error_get().
  904. *
  905. * @see socket(), bind()
  906. *
  907. ******************************************************************************
  908. *
  909. * The function extracts the first connection in the queue of pending
  910. * connections on the listening socket @c s, and returns the new socket descriptor.@n
  911. * The newly-created socket is the socket that will handle the actual
  912. * connection and has the same properties as the socket @c s.
  913. * The original socket remains open and listens for new connection requests.
  914. * If no pending connections are present in the queue of the socket,
  915. * the @ref accept() returns an error and the specific error code is set to
  916. * @ref FNET_ERR_AGAIN.@n
  917. * The parameter @c addr is a result parameter that is filled in with the
  918. * address of the remote host. The @c addrlen should initially contain the
  919. * amount of space pointed to by the @c addr; on return it will contain the
  920. * actual length of the address returned (in bytes).@n
  921. * The @ref accept() function is used only with connection-oriented socket
  922. * types (@ref SOCK_STREAM).@n
  923. * If @c addr and/or @c addrlen are equal to @c 0, then no information
  924. * about the remote address of the accepted socket is returned.
  925. *
  926. ******************************************************************************/
  927. SOCKET accept( SOCKET s, struct sockaddr *addr, unsigned int *addrlen );
  928. /***************************************************************************/ /*!
  929. *
  930. * @brief Establishes a connection with the specified socket.
  931. *
  932. *
  933. * @param s Descriptor identifying an unconnected socket.
  934. *
  935. * @param name Address (name) of the socket, with which the connection
  936. * should be established.
  937. *
  938. * @param namelen Length of the @c name.
  939. *
  940. *
  941. * @return This function returns:
  942. * - @ref FNET_OK if no error occurs.
  943. * - @ref SOCKET_ERROR if an error occurs. @n
  944. * The specific error code can be retrieved using the @ref fnet_error_get().
  945. *
  946. * @see socket(), bind()
  947. *
  948. ******************************************************************************
  949. *
  950. * For connection-oriented sockets (@ref SOCK_STREAM), this function activates
  951. * an active connection with the foreign host.
  952. *
  953. * When the socket call completes successfully, the socket is ready to send
  954. * and receive data. Any attempt to reconnect the active connection will fail
  955. * with the error code @ref FNET_ERR_ISCONN. For connection-oriented
  956. * sockets, it is often not possible to complete the connection
  957. * immediately. Until the connection attempt
  958. * completes on a socket, all subsequent calls to @ref connect()
  959. * on the same socket will fail with the error code @ref FNET_ERR_INPROGRESS,
  960. * or succeed with @ref FNET_ERR_ISCONN when the connection completes successfully.
  961. * Use the @ref getsockopt() function to determine the completion of the
  962. * connection request by checking the @ref SO_STATE to see if the socket is
  963. * connected (@ref SS_CONNECTED), is still connecting (@ref SS_CONNECTING)
  964. * or the connection has failed (@ref SS_UNCONNECTED). @n
  965. * @n
  966. * For a connectionless socket (@ref SOCK_DGRAM), the operation performed
  967. * by @ref connect() is merely to establish a default destination address
  968. * that can be used on subsequent @ref send() and @ref recv() calls.
  969. * Any datagrams received from an address other than the destination address
  970. * specified will be discarded. The default destination can be changed by…

Large files files are truncated, but you can click here to view the full file