PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  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
  971. * simply calling the @ref connect() again, even if the socket is connected already.
  972. * Any datagrams queued for receipt are discarded from the previous
  973. * @ref connect(). For connectionless sockets, the @c name can indicate any
  974. * valid address, including a broadcast address.@n
  975. * @n
  976. * If the socket @c s is unbound, unique values are assigned to the local
  977. * association by the stack, and the socket is marked as bound.
  978. *
  979. ******************************************************************************/
  980. int connect( SOCKET s, struct sockaddr *name, unsigned int namelen );
  981. /***************************************************************************/ /*!
  982. *
  983. * @brief Receives the data from a connected socket.
  984. *
  985. *
  986. * @param s Descriptor identifying a connected socket.
  987. *
  988. * @param buf Buffer for the incoming data.
  989. *
  990. * @param len Length of the @c buf.
  991. *
  992. * @param flags Optional flag specifying the way, in which the call is made.
  993. * It can be constructed by using the bitwise OR operator with
  994. * any of the values defined by the @ref fnet_flags_t.
  995. *
  996. *
  997. * @return This function returns:
  998. * - The number of bytes received, if no error occurs.
  999. * The return value is set to zero, if there
  1000. * is no input data.
  1001. * - @ref SOCKET_ERROR if an error occurs. @n
  1002. * The specific error code can be retrieved using the @ref fnet_error_get().
  1003. *
  1004. * @see connect(), recvfrom()
  1005. *
  1006. ******************************************************************************
  1007. *
  1008. * For connection-oriented sockets (@ref SOCK_STREAM), calling the @ref recv()
  1009. * will return as much information as is currently available up to the size
  1010. * of the buffer supplied.@n
  1011. * @n
  1012. * For connectionless sockets (@ref SOCK_DGRAM), the data is extracted from the
  1013. * first enqueued datagram (message), whose destination address is specified
  1014. * by the @ref connect() function.
  1015. * If the datagram is larger than the supplied buffer, the buffer is filled
  1016. * with the first part of the datagram, and the @ref recv() generates the error
  1017. * @ref FNET_ERR_MSGSIZE and the excess data is lost.@n
  1018. * @n
  1019. * The @c flags parameter can be used to influence the behavior of the function
  1020. * invocation beyond the options specified for the associated socket.
  1021. *
  1022. ******************************************************************************/
  1023. int recv( SOCKET s, char *buf, unsigned int len, unsigned int flags );
  1024. /***************************************************************************/ /*!
  1025. *
  1026. * @brief Receives the data and captures the address, from which the data
  1027. * was sent.
  1028. *
  1029. *
  1030. * @param s Descriptor, identifying a bound socket.
  1031. *
  1032. * @param buf Buffer for the incoming data.
  1033. *
  1034. * @param len Length of the @c buf.
  1035. *
  1036. * @param flags Optional flag specifying the way in which the call is made.
  1037. * It can be constructed by using the bitwise OR operator with
  1038. * any of the values defined by the @ref fnet_flags_t.
  1039. *
  1040. * @param from Optional pointer to a buffer that will hold the
  1041. * source address upon return.
  1042. *
  1043. * @param fromlen Optional pointer to the size of the @c from buffer.
  1044. *
  1045. *
  1046. * @return This function returns:
  1047. * - The number of bytes received, if no error occurs.
  1048. * The return value is set to zero, if the socket is nonblocking and there
  1049. * is no input data.
  1050. * - @ref SOCKET_ERROR if an error occurs. @n
  1051. * The specific error code can be retrieved using the @ref fnet_error_get().
  1052. *
  1053. * @see bind(), recv()
  1054. *
  1055. ******************************************************************************
  1056. *
  1057. * For connection-oriented sockets (@ref SOCK_STREAM), calling the @ref recvfrom()
  1058. * will return as much information as is currently available up to the size of
  1059. * the buffer supplied. The @ref recvfrom() call is rarely used for
  1060. * stream sockets, because the routine provides the peer-socket information,
  1061. * and stream sockets are tied to a specific peer.@n
  1062. * @n
  1063. * For connectionless sockets (@ref SOCK_DGRAM), the data is extracted from the
  1064. * first enqueued datagram.
  1065. * If the datagram is larger than the buffer supplied, the buffer is filled
  1066. * with the first part of the datagram, and the @ref recvfrom() generates the
  1067. * error @ref FNET_ERR_MSGSIZE and the excess data is lost.@n
  1068. * @n
  1069. * If the @c from parameter is non-zero, the network address of the peer
  1070. * that sent the data is copied to the corresponding @ref sockaddr structure.
  1071. * The value pointed to by @c fromlen is initialized to the size of this
  1072. * structure and is modified on return, to indicate the actual size of
  1073. * the address stored in the @ref sockaddr structure.@n
  1074. * @n
  1075. * The flags parameter can be used to influence the behavior of the function
  1076. * invocation beyond the options specified for the associated socket.
  1077. *
  1078. ******************************************************************************/
  1079. int recvfrom( SOCKET s, char *buf, unsigned int len, unsigned int flags, struct sockaddr *from, unsigned int *fromlen );
  1080. /***************************************************************************/ /*!
  1081. *
  1082. * @brief Sends the data on a connected socket.
  1083. *
  1084. *
  1085. * @param s Descriptor, identifying a connected socket.
  1086. *
  1087. * @param buf Buffer containing the data to be transmitted.
  1088. *
  1089. * @param len Length of the data in @c buf.
  1090. *
  1091. * @param flags Optional flag specifying the way in which the call is made.
  1092. * It can be constructed by using the bitwise OR operator with
  1093. * any of the values defined by the @ref fnet_flags_t.
  1094. *
  1095. *
  1096. * @return This function returns:
  1097. * - The total number of bytes sent, if no error occurs.
  1098. * It can be less than the number indicated by @c len.
  1099. * - @ref SOCKET_ERROR if an error occurs. @n
  1100. * The specific error code can be retrieved using the @ref fnet_error_get().
  1101. *
  1102. * @see connect(), sendto()
  1103. *
  1104. ******************************************************************************
  1105. *
  1106. * For stream-oriented sockets (@ref SOCK_STREAM), the number of
  1107. * actually sent bytes can be between @c 1 and the requested length, depending on
  1108. * buffer availability on both client and server machines.@n
  1109. * @n
  1110. * For message-oriented sockets (@ref SOCK_DGRAM), care must be taken not to
  1111. * exceed the maximum datagram size, which can be obtained by using the @ref
  1112. * getsockopt() to retrieve the value of the socket option @ref SO_SNDBUF.
  1113. * If the data is too long to pass atomically through the underlying
  1114. * datagram protocol, the error @ref FNET_ERR_MSGSIZE is returned
  1115. * and no data is transmitted.@n
  1116. * Calling the @ref send() with a zero @c len parameter is permissible.
  1117. * In such cases, the @ref send() will return zero as a valid value, and
  1118. * for message-oriented sockets (@ref SOCK_DGRAM) a zero-length transport
  1119. * datagram is sent.@n
  1120. * @n
  1121. * The @c flags parameter can be used to influence the behavior of the function
  1122. * invocation beyond the options specified for the associated socket.
  1123. *
  1124. ******************************************************************************/
  1125. int send( SOCKET s, char *buf, unsigned int len, unsigned int flags );
  1126. /***************************************************************************/ /*!
  1127. *
  1128. * @brief Sends the data to a specific destination.
  1129. *
  1130. *
  1131. * @param s Descriptor, identifying a socket.
  1132. *
  1133. * @param buf Buffer containing the data to be transmitted.
  1134. *
  1135. * @param len Length of the data in @c buf.
  1136. *
  1137. * @param flags Optional flag specifying the way, in which the call is made.
  1138. * It can be constructed by using the bitwise OR operator with
  1139. * any of the values defined by the @ref fnet_flags_t.
  1140. *
  1141. * @param to Pointer to the address of the target socket.
  1142. *
  1143. * @param tolen Size of the address in @c to.
  1144. *
  1145. *
  1146. * @return This function returns:
  1147. * - The total number of bytes sent, if no error occurs.
  1148. * It can be lesser than the number indicated by @c len.
  1149. * - @ref SOCKET_ERROR if an error occurs. @n
  1150. * The specific error code can be retrieved using the @ref fnet_error_get().
  1151. *
  1152. * @see connect(), send()
  1153. *
  1154. ******************************************************************************
  1155. *
  1156. * For nonb-locking stream-oriented sockets (@ref SOCK_STREAM), the number of
  1157. * actually sent bytes can be between @c 1 and the requested length, depending on
  1158. * buffer availability on both client and server machines.@n
  1159. * @n
  1160. * For message-oriented sockets (@ref SOCK_DGRAM), care must be taken not not
  1161. * exceed the maximum datagram size, which can be obtained by using the @ref
  1162. * getsockopt() to retrieve the value of the socket option @ref SO_SNDBUF.
  1163. * If the data is too long to pass atomically through the underlying
  1164. * datagram protocol, the error @ref FNET_ERR_MSGSIZE is returned
  1165. * and no data is transmitted.@n
  1166. * Calling @ref sendto() with a zero @c len parameter is permissible.
  1167. * In such cases, the @ref sendto() will return zero as a valid value, and
  1168. * for message-oriented sockets (@ref SOCK_DGRAM), a zero-length transport
  1169. * datagram is sent.@n
  1170. * @n
  1171. * The @ref sendto() function is normally used on a connectionless socket
  1172. * (@ref SOCK_DGRAM) to send a datagram to a specific peer socket identified
  1173. * by the @c to parameter. Even if the connectionless socket has been previously
  1174. * connected to a specific address, the @c to parameter overrides the
  1175. * destination address for that particular datagram only.@n
  1176. * On a connection-oriented socket (@ref SOCK_STREAM), the @c to and @c tolen
  1177. * parameters are ignored, making the @ref sendto() equivalent to @ref send().@n
  1178. * @n
  1179. * The @c flags parameter can be used to influence the behavior of the function
  1180. * invocation beyond the options specified for the associated socket.
  1181. *
  1182. ******************************************************************************/
  1183. int sendto( SOCKET s, char *buf, unsigned int len, int flags, const struct sockaddr *to, unsigned int tolen );
  1184. /***************************************************************************/ /*!
  1185. *
  1186. * @brief Terminates the connection in one or both directions.
  1187. *
  1188. *
  1189. * @param s Descriptor, identifying a socket.
  1190. *
  1191. * @param how Flag that describes what types of operation will no longer
  1192. * be allowed, it's defined by the @ref fnet_sd_flags_t.
  1193. *
  1194. *
  1195. * @return This function returns:
  1196. * - @ref FNET_OK if no error occurs.
  1197. * - @ref SOCKET_ERROR if an error occurs. @n
  1198. * The specific error code can be retrieved using the @ref fnet_error_get().
  1199. *
  1200. * @see closesocket()
  1201. *
  1202. ******************************************************************************
  1203. *
  1204. * This function is used on all types of sockets to disable reception,
  1205. * transmission, or both.@n
  1206. * If the @c how parameter is @ref SD_READ, subsequent calls to the @ref recv()
  1207. * function on the socket will be unallowed. If the @c how parameter is
  1208. * @ref SD_WRITE, subsequent calls to the @ref send() function are unallowed.
  1209. * Setting the @c how to the @ref SD_BOTH disables both sends and receives as
  1210. * described above.@n
  1211. * The @ref shutdown() function does not close the socket. Any resources
  1212. * attached to the socket will not be freed until the @ref closesocket() is invoked.
  1213. *
  1214. ******************************************************************************/
  1215. int shutdown( SOCKET s, unsigned int how );
  1216. /***************************************************************************/ /*!
  1217. *
  1218. * @brief Closes an existing socket.
  1219. *
  1220. *
  1221. * @param s Descriptor, identifying a socket to close.
  1222. *
  1223. *
  1224. * @return This function returns:
  1225. * - @ref FNET_OK if no error occurs.
  1226. * - @ref SOCKET_ERROR if an error occurs. @n
  1227. * The specific error code can be retrieved using the @ref fnet_error_get().
  1228. *
  1229. * @see socket()
  1230. *
  1231. ******************************************************************************
  1232. *
  1233. * This function releases the socket descriptor @c s, so that further
  1234. * references to @c s fail with the error @ref FNET_ERR_BAD_DESC.@n
  1235. * An application should always have a matching call to the @ref closesocket() for
  1236. * each successful call to the @ref socket() to return any socket resources to
  1237. * the system.@n
  1238. * @n
  1239. * For the @ref SOCK_STREAM sockets, the @ref SO_LINGER option controls the action
  1240. * of the closing, when unsent data is present on the socket and the @ref
  1241. * closesocket() function is performed. This option has effect on
  1242. * TCP sockets only. To enable or disable the @ref SO_LINGER option, the application
  1243. * should use the @ref linger structure. This structure includes the
  1244. * @c l_onoff and the @c l_linger fields. If the @c l_onoff has a non-zero value,
  1245. * then the @ref SO_LINGER is enabled. Otherwise, the @ref SO_LINGER option is
  1246. * disabled. The @c l_linger field includes the timeout in seconds.
  1247. * If the @ref SO_LINGER option is not used
  1248. * , the remaining data of the output buffer can be sent after the execution
  1249. * of the @ref closesocket() in the background.@n
  1250. * If a timeout expires and unsent data is present, the hard reset occurs and
  1251. * unsent data will be deleted.@n
  1252. * If the @ref SO_LINGER option is used with a zero timeout interval, the
  1253. * hard reset occurs immediately and unsent data will be deleted.
  1254. *
  1255. ******************************************************************************/
  1256. int closesocket( SOCKET s );
  1257. /***************************************************************************/ /*!
  1258. *
  1259. * @brief Sets a socket option.
  1260. *
  1261. *
  1262. * @param s Descriptor identifying a socket.
  1263. *
  1264. * @param level Level, at which the option is defined.
  1265. * The supported levels are @ref SOL_SOCKET,
  1266. * @ref IPPROTO_IP, @ref IPPROTO_IPV6 or @ref IPPROTO_TCP.
  1267. *
  1268. * @param optname Socket option for which the value is to be set.
  1269. * - for the @ref SOL_SOCKET level the options are defined by
  1270. * @ref fnet_socket_options_t.
  1271. * - for the @ref IPPROTO_IP level the options are defined by
  1272. * @ref fnet_ip_options_t.
  1273. * - for the @ref IPPROTO_IPV6 level the options are defined by
  1274. * @ref fnet_ip6_options_t.
  1275. * - for the @ref IPPROTO_TCP level the options are defined by
  1276. * @ref fnet_tcp_options_t.
  1277. *
  1278. * @param optval Pointer to a buffer, in which the value for the requested
  1279. * option is supplied.
  1280. *
  1281. * @param optlen Size of the @c optval buffer.
  1282. *
  1283. *
  1284. * @return This function returns:
  1285. * - @ref FNET_OK if no error occurs.
  1286. * - @ref SOCKET_ERROR if an error occurs. @n
  1287. * The specific error code can be retrieved using the @ref fnet_error_get().
  1288. *
  1289. * @see getsockopt()
  1290. *
  1291. ******************************************************************************
  1292. *
  1293. * This function sets the current value for a socket option associated with the
  1294. * specified socket.@n
  1295. * The various socket options are described by the @ref fnet_socket_options_t,
  1296. * @ref fnet_ip_options_t, and @ref fnet_tcp_options_t.
  1297. *
  1298. ******************************************************************************/
  1299. int setsockopt( SOCKET s, int level, int optname, char *optval, unsigned int optlen );
  1300. /***************************************************************************/ /*!
  1301. *
  1302. * @brief Gets a socket option.
  1303. *
  1304. *
  1305. * @param s Descriptor identifying a socket.
  1306. *
  1307. * @param level Level, at which the option is defined.
  1308. * The supported levels are the @ref SOL_SOCKET,
  1309. * @ref IPPROTO_IP, @ref IPPROTO_IPV6 or @ref IPPROTO_TCP.
  1310. *
  1311. * @param optname Socket option, for which the value is to be retrievied.
  1312. * - for the @ref SOL_SOCKET level the options are defined by
  1313. * @ref fnet_socket_options_t.
  1314. * - for the @ref IPPROTO_IP level the options are defined by
  1315. * @ref fnet_ip_options_t.
  1316. * - for the @ref IPPROTO_IPV6 level the options are defined by
  1317. * @ref fnet_ip6_options_t.
  1318. * - for the @ref IPPROTO_TCP level the options are defined by
  1319. * @ref fnet_tcp_options_t.
  1320. *
  1321. * @param optval Pointer to a buffer in which the value for the requested
  1322. * option has to be returned.
  1323. *
  1324. * @param optlen Pointer to the size of the @c optval buffer.
  1325. *
  1326. *
  1327. * @return This function returns:
  1328. * - @ref FNET_OK if no error occurs.
  1329. * - @ref SOCKET_ERROR if an error occurs. @n
  1330. * The specific error code can be retrieved using the @ref fnet_error_get().
  1331. *
  1332. * @see setsockopt()
  1333. *
  1334. ******************************************************************************
  1335. *
  1336. * This function retrieves the current value for a socket option associated
  1337. * with the socket @c s, and stores the result in the @c optval.@n
  1338. * The various socket options are described by the @ref fnet_socket_options_t,
  1339. * @ref fnet_ip_options_t, and @ref fnet_tcp_options_t.
  1340. *
  1341. ******************************************************************************/
  1342. int getsockopt( SOCKET s, int level, int optname, char *optval, unsigned int *optlen );
  1343. /***************************************************************************/ /*!
  1344. *
  1345. * @brief Retrieves the name of a peer connected to a socket.
  1346. *
  1347. *
  1348. * @param s Descriptor identifying a connected socket.
  1349. *
  1350. * @param name Structure that receives the name (address) of the peer.
  1351. *
  1352. * @param namelen Pointer to the size of the @c name structure.
  1353. *
  1354. *
  1355. * @return This function returns:
  1356. * - @ref FNET_OK if no error occurs.
  1357. * - @ref SOCKET_ERROR if an error occurs. @n
  1358. * The specific error code can be retrieved using the @ref fnet_error_get().
  1359. *
  1360. * @see getsockname()
  1361. *
  1362. ******************************************************************************
  1363. *
  1364. * This function retrieves the name of the peer connected to the socket @c s
  1365. * and stores it in the @ref sockaddr structure identified by @c name.@n
  1366. * The @ref getpeername() function can be used only with a connected socket.
  1367. * For datagram sockets, only the name of a peer specified in a previous
  1368. * @ref connect() call will be returned - any name specified by a previous
  1369. * @ref sendto() call will not be returned by the @ref getpeername().
  1370. *
  1371. ******************************************************************************/
  1372. int getpeername( SOCKET s, struct sockaddr *name, unsigned int *namelen );
  1373. /***************************************************************************/ /*!
  1374. *
  1375. * @brief Retrieves the current name for the specified socket.
  1376. *
  1377. *
  1378. * @param s Descriptor identifying a socket.
  1379. *
  1380. * @param name Structure that receives the name (address) of the
  1381. * socket @c s.
  1382. *
  1383. * @param namelen Pointer to the size of the @c name structure.
  1384. *
  1385. *
  1386. * @return This function returns:
  1387. * - @ref FNET_OK if no error occurs.
  1388. * - @ref SOCKET_ERROR if an error occurs. @n
  1389. * The specific error code can be retrieved using the @ref fnet_error_get().
  1390. *
  1391. * @see getpeername()
  1392. *
  1393. ******************************************************************************
  1394. *
  1395. * This function retrieves the current name for the specified socket @c s
  1396. * and stores it in the @c name.@n
  1397. * It is used on a bound or connected socket.
  1398. *
  1399. ******************************************************************************/
  1400. int getsockname( SOCKET s, struct sockaddr *name, unsigned int *namelen );
  1401. /***************************************************************************/ /*!
  1402. *
  1403. * @brief Compares socket addresses.
  1404. *
  1405. * @param addr1 First socket address to be compared.
  1406. *
  1407. * @param addr2 Second socket address to be compared.
  1408. *
  1409. * @return This function returns:
  1410. * - @ref FNET_TRUE if addresses are equal.
  1411. * - @ref FNET_FALSE if addresses are not equal.
  1412. *
  1413. ******************************************************************************
  1414. *
  1415. * This function compares specified socket addresses, depending on address family. @n
  1416. * Port number and Scope Id do not take part in process of comparison.
  1417. *
  1418. ******************************************************************************/
  1419. int fnet_socket_addr_are_equal(const struct sockaddr *addr1, const struct sockaddr *addr2);
  1420. /***************************************************************************/ /*!
  1421. *
  1422. * @brief Determines, if socket address is unspecified.
  1423. *
  1424. * @param addr Socket address.
  1425. *
  1426. * @return This function returns:
  1427. * - @c FNET_TRUE if the address is unspecified.
  1428. * - @c FNET_FALSE if the address is specified.
  1429. *
  1430. ******************************************************************************
  1431. *
  1432. * This function determines if the socket address is unspecified
  1433. * (IP address is set to all zeros) or not.
  1434. *
  1435. ******************************************************************************/
  1436. int fnet_socket_addr_is_unspecified(const struct sockaddr *addr);
  1437. /***************************************************************************/ /*!
  1438. *
  1439. * @brief Determines, if socket address is multicast.
  1440. *
  1441. * @param addr Socket address.
  1442. *
  1443. * @return This function returns:
  1444. * - @c FNET_TRUE if the address is multicast.
  1445. * - @c FNET_FALSE if the address is not multicast.
  1446. *
  1447. ******************************************************************************
  1448. *
  1449. * This function determines if the socket address is multicast or not.
  1450. *
  1451. ******************************************************************************/
  1452. int fnet_socket_addr_is_multicast(const struct sockaddr *addr);
  1453. /*! @} */
  1454. #endif /* _FNET_SOCKET_H_ */