socket_api.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /**
  2. * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file socket_api.h
  41. *
  42. * @defgroup iot_socket BSD Socket interface
  43. * @ingroup iot_sdk_socket
  44. * @{
  45. * @brief Nordic socket interface for IoT.
  46. *
  47. * @details This module provides the socket interface for writing IoT applications. The API is
  48. * designed to be compatible with the POSIX/BSD socket interface for the purpose of
  49. * making porting easy. The socket options API has been extended to support configuring
  50. * Nordic BLE stack, tuning of RF parameters as well as security options.
  51. */
  52. #ifndef SOCKET_API_H__
  53. #define SOCKET_API_H__
  54. #include <stdint.h>
  55. #include "sdk_common.h"
  56. #include "iot_defines.h"
  57. #include "errno.h"
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. #if !defined(__GNUC__) || (__GNUC__ == 0)
  62. typedef int32_t ssize_t;
  63. #else
  64. #include <sys/types.h>
  65. #ifdef __SES_ARM
  66. typedef int32_t ssize_t;
  67. #endif
  68. #endif
  69. #ifndef htons
  70. #define htons(x) HTONS(x) /**< Convert byte order from host to network (short). */
  71. #endif
  72. #ifndef htonl
  73. #define htonl(x) HTONL(x) /**< Convert byte order from host to network (long). */
  74. #endif
  75. #ifndef ntohs
  76. #define ntohs(x) NTOHS(x) /**< Convert byte order from network to host (short). */
  77. #endif
  78. #ifndef ntohl
  79. #define ntohl(x) NTOHL(x) /**< Convert byte order from network to host (long). */
  80. #endif
  81. /**@defgroup socket_families Values for socket_family_t
  82. * @ingroup iot_socket
  83. * @{
  84. */
  85. #define AF_INET 2 /**< IPv4 socket family. */
  86. #define AF_INET6 10 /**< IPv6 socket family. */
  87. #if defined(NRF52) || defined(NRF52_SERIES)
  88. #define AF_NRF_CFG 39 /**< nRF configuration socket.*/
  89. #endif
  90. /**@} */
  91. /**@defgroup socket_types Values for socket_type_t
  92. * @ingroup iot_socket
  93. * @{
  94. */
  95. #define SOCK_STREAM 1 /**< TCP socket type. */
  96. #define SOCK_DGRAM 2 /**< UDP socket type. */
  97. /**@} */
  98. /**@defgroup socket_protocols Values for socket_protocol_t
  99. * @ingroup iot_socket
  100. * @{
  101. */
  102. #define IPPROTO_TCP 1 /**< Use TCP as transport protocol. */
  103. #define IPPROTO_UDP 2 /**< Use UDP as transport protocol. */
  104. /**@} */
  105. /**@defgroup socket_send_recv_flags Socket send/recv flags
  106. * @ingroup iot_socket
  107. * @{
  108. */
  109. #define MSG_DONTROUTE 0x01 /**< Send only to hosts on directly connected networks. */
  110. #define MSG_DONTWAIT 0x02 /**< Enables non-blocking operation. */
  111. #define MSG_OOB 0x04 /**< Sends out-of-band data on sockets that support this. */
  112. #define MSG_PEEK 0x08 /**< Return data from the beginning of receive queue without removing data from the queue. */
  113. #define MSG_WAITALL 0x10 /**< Request a blocking operation until the request is satisfied. */
  114. /**@} */
  115. #if defined(NRF52) || defined(NRF52_SERIES)
  116. /**
  117. * @defgroup socket_option_levels Values for socket_opt_lvl_t
  118. * @ingroup iot_socket
  119. * @{
  120. */
  121. #define SOL_SOCKET 1 /**< Standard socket options. */
  122. #define SOL_NRF_MEDIUM 2 /**< Nordic medium socket options. Use this to control medium parameters. */
  123. /**@} */
  124. /**@defgroup socket_medium_options Medium socket option level types
  125. * @ingroup iot_socket
  126. * @{
  127. */
  128. #define MEDIUM_INIT_PARAMS 1 /**< Medium initialization parameters. */
  129. /**@}
  130. */
  131. #endif
  132. /**@defgroup fcnt_commands fcntl commands
  133. * @ingroup iot_socket
  134. * @{
  135. */
  136. #define F_SETFL 1 /**< Set flag. */
  137. #define F_GETFL 2 /**< Get flag. */
  138. /**@} */
  139. /**@defgroup fcnt_flags fcntl flags
  140. * @ingroup iot_socket
  141. * @{
  142. */
  143. #define O_NONBLOCK 0x01 /**< Use non-blocking I/O. */
  144. /**@} */
  145. /**
  146. * @brief Socket module size type.
  147. */
  148. typedef uint32_t socklen_t;
  149. /**
  150. * @brief Socket port type.
  151. */
  152. typedef uint16_t in_port_t;
  153. /**
  154. * @brief Structure specifying time interval.
  155. */
  156. struct timeval
  157. {
  158. uint32_t tv_sec; /**< Time interval seconds. */
  159. uint32_t tv_usec; /**< Time interval microseconds. */
  160. };
  161. /**
  162. * @brief Socket families.
  163. *
  164. * @details For a list of valid values, refer to @ref socket_families.
  165. */
  166. typedef int socket_family_t;
  167. typedef socket_family_t sa_family_t;
  168. /**
  169. * @brief Socket types.
  170. *
  171. * @details For a list of valid values refer to @ref socket_types.
  172. */
  173. typedef int socket_type_t;
  174. /**
  175. * @brief Socket protocols.
  176. *
  177. * @details Use 0 if you do not want do specify socket protocol, which should be sufficient for most users.
  178. * Other values are only provided for socket API compatibility, see @ref socket_protocols.
  179. */
  180. typedef int socket_protocol_t;
  181. /**
  182. * @if (IOT)
  183. * @brief Socket option levels.
  184. *
  185. * @details For a list of valid values, refer to @ref socket_option_levels.
  186. * @endif
  187. */
  188. typedef int socket_opt_lvl_t;
  189. /**
  190. * @brief Generic socket address.
  191. *
  192. * @details Only provided for API compatibility.
  193. */
  194. struct sockaddr
  195. {
  196. uint8_t sa_len;
  197. socket_family_t sa_family;
  198. char sa_data[];
  199. };
  200. /**
  201. * @brief IPv6 address.
  202. */
  203. struct in6_addr
  204. {
  205. uint8_t s6_addr[16];
  206. };
  207. /**
  208. * @brief IPv4 address.
  209. */
  210. typedef uint32_t in_addr_t;
  211. /**
  212. * @brief IPv4 address structure.
  213. */
  214. struct in_addr
  215. {
  216. in_addr_t s_addr;
  217. };
  218. /**
  219. * @brief Global IPv6 any-address.
  220. */
  221. extern const struct in6_addr in6addr_any;
  222. /**
  223. * @brief Global IPv4 any-address.
  224. */
  225. extern const struct in_addr inaddr_any;
  226. /**
  227. * @brief Address record for IPv6 addresses.
  228. *
  229. * @details Contains the address and port of the host, as well as other socket options. All fields
  230. * in this structure are compatible with the POSIX variant for API compatibility.
  231. */
  232. struct sockaddr_in6
  233. {
  234. uint8_t sin6_len; /**< Length of this data structure. */
  235. sa_family_t sin6_family; /**< Socket family. */
  236. in_port_t sin6_port; /**< Port, in network byte order. */
  237. uint32_t sin6_flowinfo; /**< IPv6 flow info parameters. Not used. */
  238. struct in6_addr sin6_addr; /**< IPv6 address. */
  239. uint32_t sin6_scope_id; /**< IPv6 scope ID. Not used. */
  240. };
  241. /**
  242. * @brief Address record for IPv4 addresses.
  243. *
  244. * @details Contains the address and port of the host. All fields
  245. * in this structure are compatible with the POSIX variant for API compatibility.
  246. */
  247. struct sockaddr_in
  248. {
  249. uint8_t sin_len; /**< Length of this data structure. */
  250. sa_family_t sin_family; /**< Socket family. */
  251. in_port_t sin_port; /**< Port, in network byte order. */
  252. struct in_addr sin_addr; /**< IPv4 address. */
  253. };
  254. typedef struct sockaddr sockaddr_t;
  255. typedef struct sockaddr_in6 sockaddr_in6_t;
  256. typedef struct in6_addr in6_addr_t;
  257. typedef struct sockaddr_in sockaddr_in_t;
  258. /**
  259. * @brief Function for creating a socket.
  260. *
  261. * @details API to create a socket that can be used for network communication independently
  262. * of lower protocol layers.
  263. *
  264. * @param[in] family The protocol family of the network protocol to use. Currently, only
  265. * AF_INET6 is supported.
  266. * @param[in] type The protocol type to use for this socket.
  267. * @param[in] protocol The transport protocol to use for this socket.
  268. *
  269. * @return A non-negative socket descriptor on success, or -1 on error.
  270. */
  271. int socket(socket_family_t family, socket_type_t type, socket_protocol_t protocol);
  272. /**
  273. * @brief Function for closing a socket and freeing any resources held by it.
  274. *
  275. * @details If the socket is already closed, this function is a noop.
  276. *
  277. * @param[in] sock The socket to close.
  278. *
  279. * @return 0 on success, or -1 on error.
  280. */
  281. int close(int sock);
  282. /**
  283. * @brief Function for controlling file descriptor options.
  284. *
  285. * @details Set or get file descriptor options or flags. For a list of supported commands, refer to @ref fcnt_commands.
  286. * For a list of supported flags, refer to @ref fcnt_flags.
  287. *
  288. * @param[in] fd The descriptor to set options on.
  289. * @param[in] cmd The command class for options.
  290. * @param[in] flags The flags to set.
  291. */
  292. int fcntl(int fd, int cmd, int flags);
  293. /**
  294. * @brief Function for connecting to an endpoint with a given address.
  295. *
  296. * @details The socket handle must be a valid handle that has not yet been connected. Running
  297. * connect on a connected handle will return an error.
  298. *
  299. * @param[in] sock The socket to use for connection.
  300. * @param[in] p_servaddr The address of the server to connect to. Currently, sockaddr_in6 is
  301. * the only supported type.
  302. * @param[in] addrlen The size of the p_servaddr argument.
  303. *
  304. * @return 0 on success, or -1 on error.
  305. */
  306. int connect(int sock, const void * p_servaddr, socklen_t addrlen);
  307. /**
  308. * @brief Function for sending data through a socket.
  309. *
  310. * @details By default, this function will block unless the O_NONBLOCK
  311. * socket option has been set, OR MSG_DONTWAIT is passed as a flag. In that case, the
  312. * method will return immediately.
  313. *
  314. * @param[in] sock The socket to write data to.
  315. * @param[in] p_buff Buffer containing the data to send.
  316. * @param[in] nbytes Size of data contained on p_buff.
  317. * @param[in] flags Flags to control send behavior.
  318. *
  319. * @return The number of bytes that were sent on success, or -1 on error.
  320. */
  321. ssize_t send(int sock, const void * p_buff, size_t nbytes, int flags);
  322. /**
  323. * @brief Function for sending datagram through a socket.
  324. *
  325. * @details By default, this function will block if the lower layers are not able to process the
  326. * packet, unless the O_NONBLOCK socket option has been set, OR MSG_DONTWAIT is passed as a flag.
  327. * In that case, the method will return immediately.
  328. *
  329. * @param[in] sock The socket to write data to.
  330. * @param[in] p_buff Buffer containing the data to send.
  331. * @param[in] nbytes Size of data contained in p_buff.
  332. * @param[in] flags Flags to control send behavior.
  333. * @param[in] p_servaddr The address of the server to send to. Currently, sockaddr_in6 is
  334. * the only supported type.
  335. * @param[in] addrlen The size of the p_servaddr argument.
  336. *
  337. * @return The number of bytes that were sent on success, or -1 on error.
  338. */
  339. ssize_t sendto(int sock,
  340. const void * p_buff,
  341. size_t nbytes,
  342. int flags,
  343. const void * p_servaddr,
  344. socklen_t addrlen);
  345. /**
  346. * @brief Function for writing data to a socket. See \ref send() for details.
  347. *
  348. * @param[in] sock The socket to write data to.
  349. * @param[in] p_buff Buffer containing the data to send.
  350. * @param[in] nbytes Size of data contained in p_buff.
  351. *
  352. * @return The number of bytes that were sent on success, or -1 on error.
  353. */
  354. ssize_t write(int sock, const void * p_buff, size_t nbytes);
  355. /**
  356. * @brief Function for receiving data on a socket.
  357. *
  358. * @details API for receiving data from a socket. By default, this function will block, unless the
  359. * O_NONBLOCK socket option has been set, or MSG_DONTWAIT is passed as a flag.
  360. *
  361. * @param[in] sock The socket to receive data from.
  362. * @param[out] p_buff Buffer to hold the data to be read.
  363. * @param[in] nbytes Number of bytes to read. Should not be larger than the size of p_buff.
  364. * @param[in] flags Flags to control receive behavior.
  365. *
  366. * @return The number of bytes that were read, or -1 on error.
  367. */
  368. ssize_t recv(int sock, void * p_buff, size_t nbytes, int flags);
  369. /**
  370. * @brief Function for receiving datagram on a socket.
  371. *
  372. * @details API for receiving data from a socket. By default, this function will block, unless the
  373. * O_NONBLOCK socket option has been set, or MSG_DONTWAIT is passed as a flag.
  374. *
  375. * @param[in] sock The socket to receive data from.
  376. * @param[out] p_buff Buffer to hold the data to be read.
  377. * @param[in] nbytes Number of bytes to read. Should not be larger than the size of p_buff.
  378. * @param[in] flags Flags to control receive behavior.
  379. * @param[out] p_cliaddr Socket address that will be set to the client's address.
  380. * @param[inout] p_addrlen The size of the p_cliaddr passed. Might be modified by the function.
  381. *
  382. * @return The number of bytes that were read, or -1 on error.
  383. */
  384. ssize_t recvfrom(int sock,
  385. void * p_buff,
  386. size_t nbytes,
  387. int flags,
  388. void * p_cliaddr,
  389. socklen_t * p_addrlen);
  390. /**
  391. * @brief Function for reading data from a socket. See \ref recv() for details.
  392. *
  393. * @param[in] sock The socket to receive data from.
  394. * @param[out] p_buff Buffer to hold the data to be read.
  395. * @param[in] nbytes Number of bytes to read. Should not be larger than the size of p_buff.
  396. *
  397. * @return The number of bytes that were read, or -1 on error.
  398. */
  399. ssize_t read(int sock, void * p_buff, size_t nbytes);
  400. /**
  401. * @defgroup fd_set_api API for file descriptor set
  402. * @ingroup iot_socket
  403. * @details File descriptor sets are used as input to the select() function for doing I/O
  404. * multiplexing. The maximum number of descriptors contained in a set is defined by
  405. * FD_SETSIZE.
  406. *
  407. * @{
  408. */
  409. #ifndef FD_ZERO
  410. typedef uint32_t fd_set;
  411. #define FD_ZERO(set) (*(set) = 0) /**< Clear the entire set. */
  412. #define FD_SET(fd, set) (*(set) |= (1u << (fd))) /**< Set a bit in the set. */
  413. #define FD_CLR(fd, set) (*(set) &= ~(1u << (fd))) /**< Clear a bit in the set. */
  414. #define FD_ISSET(fd, set) (*(set) & (1u << (fd))) /**< Check if a bit in the set is set. */
  415. #define FD_SETSIZE sizeof(fd_set) /**< The max size of a set. */
  416. #endif
  417. /**@} */
  418. /**
  419. * @brief Function for waiting for read, write, or exception events on a socket.
  420. *
  421. * @details Wait for a set of socket descriptors to be ready for reading, writing, or having
  422. * exceptions. The set of socket descriptors is configured before calling this function.
  423. * This function will block until any of the descriptors in the set has any of the required
  424. * events. This function is mostly useful when using O_NONBLOCK or MSG_DONTWAIT options
  425. * to enable async operation.
  426. *
  427. * @param[in] nfds The highest socket descriptor value contained in the sets.
  428. * @param[inout] p_readset The set of descriptors for which to wait for read events. Set to NULL
  429. * if not used.
  430. * @param[inout] p_writeset The set of descriptors for which to wait for write events. Set to NULL
  431. * if not used.
  432. * @param[inout] p_exceptset The set of descriptors for which to wait for exception events. Set to
  433. * NULL if not used.
  434. * @param[in] p_timeout The timeout to use for select call. Set to NULL if waiting forever.
  435. *
  436. * @return The number of ready descriptors contained in the descriptor sets on success, or -1 on error.
  437. */
  438. int select(int nfds,
  439. fd_set * p_readset,
  440. fd_set * p_writeset,
  441. fd_set * p_exceptset,
  442. const struct timeval * p_timeout);
  443. /**
  444. * @brief Function for setting socket options for a given socket.
  445. *
  446. * @details The options are grouped by level, and the option value should be the expected for the
  447. * given option, and the lifetime must be longer than that of the socket.
  448. *
  449. * @param[in] sock The socket for which to set the option.
  450. * @param[in] level The level or group to which the option belongs.
  451. * @param[in] optname The name of the socket option.
  452. * @param[in] p_optval The value to be stored for this option.
  453. * @param[in] optlen The size of p_optval.
  454. *
  455. * @return 0 on success, or -1 on error.
  456. */
  457. int setsockopt(int sock,
  458. socket_opt_lvl_t level,
  459. int optname,
  460. const void * p_optval,
  461. socklen_t optlen);
  462. /**
  463. * @brief Function for getting socket options for a given socket.
  464. *
  465. * @details The options are grouped by level, and the option value is the value described by the
  466. * option name.
  467. *
  468. * @param[in] sock The socket for which to set the option.
  469. * @param[in] level The level or group to which the option belongs.
  470. * @param[in] optname The name of the socket option.
  471. * @param[out] p_optval Pointer to the storage for the option value.
  472. * @param[inout] p_optlen The size of p_optval. Can be modified to the actual size of p_optval.
  473. *
  474. * @return 0 on success, or -1 on error.
  475. */
  476. int getsockopt(int sock,
  477. socket_opt_lvl_t level,
  478. int optname,
  479. void * p_optval,
  480. socklen_t * p_optlen);
  481. /**
  482. * @brief Function for binding a socket to an address and port.
  483. *
  484. * @details The provided address must be supported by the socket protocol family.
  485. *
  486. * @param[in] sock The socket descriptor to bind.
  487. * @param[in] p_myaddr The address to bind this socket to.
  488. * @param[in] addrlen The size of p_myaddr.
  489. *
  490. * @return 0 on success, or -1 on error.
  491. */
  492. int bind(int sock, const void * p_myaddr, socklen_t addrlen);
  493. /**
  494. * @brief Function for marking a socket as listenable.
  495. *
  496. * @details Once a socket is marked as listenable, it cannot be unmarked. It is important to
  497. * consider the backlog parameter, as it will affect how much memory your application will
  498. * use in the worst case.
  499. *
  500. * @param[in] sock The socket descriptor on which to set the listening options.
  501. * @param[in] backlog The max length of the queue of pending connections. A value of 0 means
  502. * infinite.
  503. *
  504. * @return 0 on success, or -1 on error.
  505. */
  506. int listen(int sock, int backlog);
  507. /**
  508. * @brief Function for waiting for the next client to connect.
  509. *
  510. * @details This function will block if there are no clients attempting to connect.
  511. *
  512. * @param[in] sock The socket descriptor to use for waiting on client connections.
  513. * @param[out] p_cliaddr Socket address that will be set to the client's address.
  514. * @param[out] p_addrlen The size of the p_cliaddr passed. Might be modified by the function.
  515. *
  516. * @return A non-negative client descriptor on success, or -1 on error.
  517. */
  518. int accept(int sock, void * p_cliaddr, socklen_t * p_addrlen);
  519. /**
  520. * @brief Function for converting a human-readable IP address to a form usable by the socket API.
  521. *
  522. * @details This function will convert a string form of addresses and encode it into a byte array.
  523. *
  524. * @param[in] af Address family. Only AF_INET6 supported.
  525. * @param[in] p_src Null-terminated string containing the address to convert.
  526. * @param[out] p_dst Pointer to a struct in6_addr where the address will be stored.
  527. *
  528. * @return 1 on success, 0 if src does not contain a valid address, -1 if af is not a valid address
  529. * family.
  530. */
  531. int inet_pton(socket_family_t af, const char * p_src, void * p_dst);
  532. #ifdef __cplusplus
  533. }
  534. #endif
  535. #endif //SOCKET_API_H__
  536. /**@} */