Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
socket_api.h
00001 /* 00002 * Copyright (c) 2010-2018, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #ifndef _NS_SOCKET_API_H 00018 #define _NS_SOCKET_API_H 00019 00020 #ifdef __cplusplus 00021 extern "C" { 00022 #endif 00023 /** 00024 * \file socket_api.h 00025 * \brief 6LoWPAN Library Socket API 00026 * 00027 * \section socket-com Common socket API 00028 * - socket_open(), A function to open a socket. 00029 * - socket_close(), A function to close a socket. 00030 * - socket_connect(), A function to connect to a remote peer. 00031 * - socket_bind(), A function to bind a local address or port or both. 00032 * - socket_getpeername(), A function to get remote address and port of a connected socket. 00033 * - socket_getsockname(), A function to get local address and port of a bound socket. 00034 * 00035 * \section socket-read Socket read API at callback 00036 * - socket_read(), A function to read received data buffer from a socket. 00037 * - socket_recvmsg(), A function to read received data buffer from a socket to Posix defined message structure 00038 * 00039 * \section socket-tx Socket TX API 00040 * - socket_send(), A function to write data buffer to a socket. 00041 * - socket_sendto(), A function to write data to a specific destination in the socket. 00042 * - socket_sendmsg(), A function which support socket_send and socket_sendto functionality which supports ancillary data 00043 * 00044 * \section sock-connect TCP socket connection handle 00045 * - socket_listen(), A function to set the socket to listening mode. 00046 * - socket_accept(), A function to accept an incoming connection. 00047 * - socket_shutdown(), A function to shut down a connection. 00048 * 00049 * Sockets are a common abstraction model for network communication and are used in most operating systems. 00050 * 6LoWPAN Library API follows BSD socket API conventions closely with some extensions necessitated 00051 * by the event-based scheduling model. 00052 * 00053 * Library supports the following socket types: 00054 * | Socket name | Socket description | 00055 * | :------------: | :----------------------------: | 00056 * | SOCKET_UDP | UDP socket type | 00057 * | SOCKET_TCP | TCP socket type | 00058 * | SOCKET_ICMP | ICMP raw socket type | 00059 * 00060 * \section socket-raw ICMP RAW socket instruction 00061 * An ICMP raw socket can be created with the function socket_open() and the 00062 * identifier 0xffff. When using ICMP sockets, the minimum packet length is 4 00063 * bytes which is the 4 bytes of ICMP header. The first byte is for type, second 00064 * for code and last two are for the checksum that must always be set to zero. 00065 * The stack will calculate the checksum automatically before transmitting the packet. 00066 * THe header is followed by the payload if there is any. 00067 * NOTE: While it is perfectly legal to send an ICMP packet without payload, 00068 * some packet sniffing softwares might regard such packets as malformed. 00069 * In such a case, a dummy payload can be attached by providing a socket_sendto() 00070 * function with a pointer to your desired data buffer. 00071 * Moreover, the current implementation of the stack allows only certain ICMP types, for example 00072 * ICMP echo, reply, destination unreachable, router advertisement, router solicitation. 00073 * It will drop any other unimplemented types. For example, Private experimentation type (200) will 00074 * be dropped by default. 00075 00076 * | ICMP Type | ICMP Code | Checksum | Payload | Notes | 00077 * | :-------: | :----------: | :-------: | :--------: | :--------------:| 00078 * | 1 | 1 | 2 | n bytes | Length in bytes | 00079 * | 0xXX | 0xXX | 0x00 0x00 | n bytes | Transmit | 00080 * | 0xXX | 0xXX | 0xXX 0xXX | n bytes | Receive | 00081 * 00082 * ICMP echo request with 4 bytes of payload (ping6). 00083 * 00084 * | ICMP Type | ICMP Code | Checksum | Payload | 00085 * | :-------: | :----------: | :-------: | :-----------------: | 00086 * | 0x80 | 0x00 | 0x00 0x00 | 0x00 0x01 0x02 0x03 | 00087 * 00088 * ICMP echo response for the message above. 00089 * 00090 * | ICMP Type | ICMP Code | Checksum | Payload | 00091 * | :-------: | :----------: | :-------: | :-----------------: | 00092 * | 0x81 | 0x00 | 0xXX 0xXX | 0x00 0x01 0x02 0x03 | 00093 * 00094 * \section socket-receive Socket receiving 00095 * When there is data to read from the socket, a receive callback function is called with the event type parameter set to SOCKET_DATA. 00096 * The application must read the received data with socket_read() or socket_recvmsg() during the callback because the stack will release the data 00097 * when returning from the receive callback. 00098 * 00099 * Socket event has event_type and socket_id fields. The receive callback function must be defined when socket is opened using socket_open() API. 00100 * 00101 * All supported socket event types are listed here: 00102 * 00103 * | Event Type | Value | Description | 00104 * | :------------------------: | :---: | :-----------------------------------------------------------------: | 00105 * | SOCKET_EVENT_MASK | 0xF0 | NC Socket event mask. | 00106 * | SOCKET_DATA | 0x00 | Data received, read data length available in d_len field. | 00107 * | SOCKET_CONNECT_DONE | 0x10 | TCP connection ready. | 00108 * | SOCKET_CONNECT_FAIL | 0x20 | TCP connection failed. | 00109 * | SOCKET_INCOMING_CONNECTION | 0x40 | TCP incoming connection on listening socket. | 00110 * | SOCKET_TX_FAIL | 0x50 | Socket data send failed. | 00111 * | SOCKET_CONNECT_CLOSED | 0x60 | TCP connection closed. | 00112 * | SOCKET_CONNECTION_RESET | 0x70 | TCP connection reset. | 00113 * | SOCKET_NO_ROUTE | 0x80 | No route available to destination. | 00114 * | SOCKET_TX_DONE | 0x90 | UDP: link layer TX ready (d_len = length of datagram). | 00115 * | | | TCP: some data acknowledged (d_len = data remaining in send queue) | 00116 * | SOCKET_NO_RAM | 0xA0 | No RAM available. | 00117 * | SOCKET_CONNECTION_PROBLEM | 0xB0 | TCP connection is retrying. | 00118 * 00119 * 00120 * \section socket-tcp How to use TCP sockets: 00121 * 00122 * | API | Socket Type | Description | 00123 * | :---------------------------: | :-----------: | :------------------------------------------------------------: | 00124 * | socket_open() | Server/Client | Open socket to specific or dynamic port number. | 00125 * | socket_shutdown() | Server/Client | Shut down opened TCP connection. | 00126 * | socket_listen() | Server | Set server port to listen state. | 00127 * | socket_accept() | Server | Accept a connection to a listening socket as a new socket. | 00128 * | socket_connect() | Client | Connect client socket to specific destination. | 00129 * | socket_close() | Server/Client | Closes the TCP Socket. | 00130 * | socket_send() | Server/Client | Send data to peer. | 00131 * | socket_recv() | Server/Client | Receive data from peer. | 00132 * 00133 * When the TCP socket is opened it is in closed state. It must be set either to listen or to connect state before it can be used to receive or transmit data. 00134 * 00135 * A socket can be set to listen mode with the socket_listen() function. After the call, the socket can accept an incoming connection from a remote host. 00136 * 00137 * A TCP socket can be connected to a remote host with socket_connect() with correct arguments. After the function call, a (non-blocking) application must wait for the socket event to confirm the successful state change of the socket. 00138 * After the successful state change, data can be sent using socket_send(). 00139 * The connection can be shut down in either direction with socket_shutdown() function - shutting down write signals end-of-data to the peer. 00140 * 00141 * \section socket-udpicmp How to use UDP and RAW socket: 00142 * 00143 * A UDP socket is ready to receive and send data immediately after a successful call of socket_open() and a NET_READY event is received. 00144 * Data can be transmitted with the socket_sendto() function. An ICMP socket works with same function call. 00145 */ 00146 00147 #include "ns_address.h" 00148 00149 /** \name Protocol IDs used with sockets. */ 00150 ///@{ 00151 /** UDP socket type. */ 00152 #define SOCKET_UDP 17 00153 /** Normal TCP socket type. */ 00154 #define SOCKET_TCP 6 00155 /** ICMPv6 raw socket type */ 00156 #define SOCKET_ICMP 32 00157 /** Raw IPv6 socket type (socket_open identifier is IPv6 protocol number) */ 00158 #define SOCKET_RAW 2 00159 /** REMOVED Feature in this release socket open return error Local Sockets for Tun interface functionality to APP-APP trough any BUS */ 00160 #define SOCKET_LOCAL 1 00161 ///@} 00162 00163 /** ICMP socket instruction 00164 * 00165 * ICMP header is comprised of 4 bytes. The first byte is for type, second for code and 00166 * the last two are for checksum that must always be zero. 00167 * The stack will calculate the checksum automatically when sending the packet. 00168 * The payload comes after the header and it can be of any length. It can also be set to 0. 00169 * 00170 * This applies for reading and sending. 00171 * 00172 * ICMP TYPE ICMP Code ICMP Checksum (2 bytes) Payload n bytes 00173 * -------- -------- -------- -------- -------- -------- ....... ------- 00174 * 0xXX 0xXX 0x00 0x00 0xXX 0xXX ...... 0xXX 00175 * 00176 * Example data flow for ping: 00177 * 00178 * ICMP echo request with 4-bytes payload (Ping6). 00179 * 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03 00180 * 00181 * ICMP echo response for the message above. 00182 * 0x81, 0x00, 0xXX, 0xXX, 0x00, 0x01, 0x02, 0x03 00183 */ 00184 00185 /*! 00186 * \struct socket_callback_t 00187 * \brief Socket Callback function structure type. 00188 */ 00189 00190 typedef struct socket_callback_t { 00191 uint8_t event_type; /**< Socket Callback Event check list below */ 00192 int8_t socket_id; /**< Socket id queue which socket cause call back */ 00193 int8_t interface_id; /**< Network Interface ID where Packet came */ 00194 uint16_t d_len; /**< Data length if event type is SOCKET_DATA */ 00195 uint8_t LINK_LQI; /**< LINK LQI info if interface cuold give */ 00196 } socket_callback_t; 00197 00198 /*! 00199 * \struct ns_msghdr_t 00200 * \brief Normal IP socket message structure for socket_recvmsg() and socket_sendmsg(). 00201 */ 00202 00203 typedef struct ns_msghdr { 00204 void *msg_name; /**< Optional address send use for destination and receive it will be source. MUST be ns_address_t */ 00205 uint_fast16_t msg_namelen; /**< Length of optional address use sizeof(ns_address_t) when msg_name is defined */ 00206 ns_iovec_t *msg_iov; /**< Message data vector list */ 00207 uint_fast16_t msg_iovlen; /**< Data vector count in msg_iov */ 00208 void *msg_control; /**< Ancillary data list of ns_cmsghdr_t pointer */ 00209 uint_fast16_t msg_controllen; /**< Ancillary data length */ 00210 int msg_flags; /**< Flags for received messages */ 00211 } ns_msghdr_t; 00212 00213 /*! 00214 * \struct ns_cmsghdr_t 00215 * \brief Control messages. 00216 */ 00217 typedef struct ns_cmsghdr { 00218 uint16_t cmsg_len; /**< Ancillary data control messages length including cmsghdr */ 00219 uint8_t cmsg_level; /**< Originating protocol level should be SOCKET_IPPROTO_IPV6 */ 00220 uint8_t cmsg_type; /**< Protocol Specific types for example SOCKET_IPV6_PKTINFO, */ 00221 } ns_cmsghdr_t; 00222 00223 /** \name Error values 00224 * \anchor ERROR_CODES 00225 */ 00226 ///@{ 00227 /** No data currently available to read, or insufficient queue space for write */ 00228 #define NS_EWOULDBLOCK (-100) 00229 ///@} 00230 00231 /** \name socket_recvfrom() or socket_recvmsg() flags. 00232 * \anchor MSG_HEADER_FLAGS 00233 */ 00234 ///@{ 00235 /** In msg_flags, indicates that given data buffer was smaller than received datagram. Can also be passed as an flag parameter to recvmsg. */ 00236 #define NS_MSG_TRUNC 1 00237 /** Indicates that given ancillary data buffer was smaller than enabled at socket msg->msg_controllen define proper writed data lengths. */ 00238 #define NS_MSG_CTRUNC 2 00239 /** Can be passed as an input flag to socket_recvfrom() to not consume data */ 00240 #define NS_MSG_PEEK 4 00241 /** \deprecated Can be passed as an input flag to get legacy returns of zero - used by socket_read() and socket_sendto() */ 00242 #define NS_MSG_LEGACY0 0x4000 00243 ///@} 00244 /*! 00245 * \struct ns_in6_pktinfo_t 00246 * \brief IPv6 packet info which is used for socket_recvmsg() socket_sendmsg(). 00247 */ 00248 typedef struct ns_in6_pktinfo { 00249 uint8_t ipi6_addr[16]; /**< src/dst IPv6 address */ 00250 int8_t ipi6_ifindex; /**< send/recv interface index */ 00251 } ns_in6_pktinfo_t; 00252 00253 00254 /** \name Alignment macros for control message headers 00255 * \anchor CMSG_ALIGN_FLAGS 00256 */ 00257 ///@{ 00258 /** Base header alignment size */ 00259 #define CMSG_HEADER_ALIGN sizeof(long) 00260 /** Base data alignment size */ 00261 #define CMSG_DATA_ALIGN CMSG_HEADER_ALIGN 00262 /** Returns control message alignment size for data or header based upon alignment base */ 00263 #ifndef NS_ALIGN_SIZE 00264 #define NS_ALIGN_SIZE(length, aligment_base) \ 00265 ((length + (aligment_base -1 )) & ~(aligment_base -1)) 00266 #endif 00267 ///@} 00268 00269 /** 00270 * \brief Parse first control message header from message ancillary data. 00271 * 00272 * \param msgh Pointer for socket message. 00273 * 00274 * \return Pointer to first control message header , Could be NULL when control_message is undefined. 00275 */ 00276 #define NS_CMSG_FIRSTHDR(msgh) \ 00277 ((msgh)->msg_controllen >= sizeof(struct ns_cmsghdr) ? \ 00278 (struct ns_cmsghdr *)(msgh)->msg_control : \ 00279 (struct ns_cmsghdr *)NULL ) 00280 /** 00281 * \brief Parse next control message from message by current control message header. 00282 * 00283 * \param msgh Pointer for socket message. 00284 * \param cmsg Pointer for last parsed control message 00285 * 00286 * \return Pointer to Next control message header , Could be NULL when no more control messages data. 00287 */ 00288 00289 ns_cmsghdr_t *NS_CMSG_NXTHDR(const ns_msghdr_t *msgh, const ns_cmsghdr_t *cmsg); 00290 00291 /** 00292 * \brief Get Data pointer from control message header. 00293 * 00294 * \param cmsg Pointer (ns_cmsghdr_t) for last parsed control message 00295 * 00296 * \return Data pointer. 00297 */ 00298 #define NS_CMSG_DATA(cmsg) \ 00299 ((uint8_t *)(cmsg) + NS_ALIGN_SIZE(sizeof(ns_cmsghdr_t), CMSG_DATA_ALIGN)) 00300 00301 /** 00302 * \brief Returns the total space required for a cmsg header plus the specified data, allowing for all padding 00303 * 00304 * \param length size of the ancillary data 00305 * 00306 * For example, the space required for a SOCKET_IPV6_PKTINFO is NS_CMSG_SPACE(sizeof(ns_in6_pktinfo)) 00307 * 00308 * \return Total size of CMSG header, data and trailing padding. 00309 */ 00310 #define NS_CMSG_SPACE(length) \ 00311 (NS_ALIGN_SIZE(sizeof(ns_cmsghdr_t), CMSG_DATA_ALIGN) + NS_ALIGN_SIZE(length, CMSG_HEADER_ALIGN)) 00312 00313 /** 00314 * \brief Calculate length to store in cmsg_len with taking into account any necessary alignment. 00315 * 00316 * \param length size of the ancillary data 00317 * 00318 * For example, cmsg_len for a SOCKET_IPV6_PKTINFO is NS_CMSG_LEN(sizeof(ns_in6_pktinfo)) 00319 * 00320 * \return Size of CMSG header plus data, without trailing padding. 00321 */ 00322 #define NS_CMSG_LEN(length) \ 00323 (NS_ALIGN_SIZE(sizeof(ns_cmsghdr_t), CMSG_DATA_ALIGN) + length) 00324 00325 /** IPv6 wildcard address IN_ANY */ 00326 extern const uint8_t ns_in6addr_any[16]; 00327 00328 /** 00329 * \brief Create and initialize a socket for communication. 00330 * 00331 * \param protocol Defines the protocol to use. 00332 * \param identifier The socket port. 0 indicates that port is not specified and it will be selected automatically when using the socket. 00333 * \param passed_fptr A function pointer to a function that is called whenever a data frame is received to this socket. 00334 * 00335 * \return 0 or greater on success; Return value is the socket ID. 00336 * \return -1 on failure. 00337 * \return -2 on port reserved. 00338 */ 00339 int8_t socket_open(uint8_t protocol, uint16_t identifier, void (*passed_fptr)(void *)); 00340 00341 /** 00342 * \brief A function to close a socket. 00343 * 00344 * \param socket ID of the socket to be closed. 00345 * 00346 * \return 0 socket closed. 00347 * \return -1 socket not closed. 00348 */ 00349 int8_t socket_close(int8_t socket); 00350 00351 /** 00352 * \brief A function to set a socket to listening mode. 00353 * 00354 * \param socket The socket ID. 00355 * \param backlog The pending connections queue size. 00356 * \return 0 on success. 00357 * \return -1 on failure. 00358 */ 00359 int8_t socket_listen(int8_t socket, uint8_t backlog); 00360 00361 /** 00362 * \brief A function to accept a new connection on an socket. 00363 * 00364 * \param socket_id The socket ID of the listening socket. 00365 * \param addr Either NULL pointer or pointer to structure where the remote address of the connecting host is copied. 00366 * \param passed_fptr A function pointer to a function that is called whenever a data frame is received to the new socket. 00367 * \return 0 or greater on success; return value is the new socket ID. 00368 * \return -1 on failure. 00369 * \return NS_EWOULDBLOCK if no pending connections. 00370 */ 00371 int8_t socket_accept(int8_t socket_id, ns_address_t *addr, void (*passed_fptr)(void *)); 00372 00373 /** 00374 * \brief A function to connect to remote peer (TCP). 00375 * 00376 * \param socket The socket ID. 00377 * \param address The address of a remote peer. 00378 * \deprecated \param randomly_take_src_number Ignored - random local port is always chosen if not yet bound 00379 * 00380 * \return 0 on success. 00381 * \return -1 in case of an invalid socket ID or parameter. 00382 * \return -2 if memory allocation fails. 00383 * \return -3 if the socket is in listening state. 00384 * \return -4 if the socket is already connected. 00385 * \return -5 connect is not supported on this type of socket. 00386 * \return -6 if the TCP session state is wrong. 00387 * \return -7 if the source address selection fails. 00388 */ 00389 int8_t socket_connect(int8_t socket, ns_address_t *address, uint8_t randomly_take_src_number); 00390 00391 /** 00392 * \brief Bind socket to address. 00393 * 00394 * Used by the application to bind a socket to a port and/or an address. Binding of each 00395 * of address and port can only be done once. 00396 * 00397 * If address is ns_in6addr_any, the address binding is not changed. If port is 0, 00398 * the port binding is not changed. 00399 * 00400 * \param socket Socket ID of the socket to bind. 00401 * \param address Address structure containing the port and/or address to bind. 00402 * 00403 * \return 0 on success. 00404 * \return -1 if the given address is NULL. 00405 * \return -2 if the port is already bound to another socket. 00406 * \return -3 if address is not us. 00407 * \return -4 if the socket is already bound. 00408 * \return -5 bind is not supported on this type of socket. 00409 * 00410 */ 00411 int8_t socket_bind(int8_t socket, const ns_address_t *address); 00412 00413 /** 00414 * \brief Bind a local address to a socket based on the destination address and 00415 * the address selection preferences. 00416 * 00417 * Binding happens to the same address that socket_connect() would bind to. 00418 * Reference: RFC5014 IPv6 Socket API for Source Address Selection. 00419 * 00420 * \param socket The socket ID. 00421 * \param dst_address The destination address to which the socket wants to communicate. 00422 * 00423 * \return 0 on success. 00424 * \return -1 if the given address is NULL or socket ID is invalid. 00425 * \return -2 if the memory allocation failed. 00426 * \return -3 if the socket is already bound to address. 00427 * \return -4 if the interface cannot be found. 00428 * \return -5 if the source address selection fails. 00429 * \return -6 bind2addrsel is not supported on this type of socket. 00430 */ 00431 int8_t socket_bind2addrsel(int8_t socket, const ns_address_t *dst_address); 00432 00433 /** 00434 * Options for the socket_shutdown() parameter 'how'. 00435 */ 00436 #define SOCKET_SHUT_RD 0 ///< Disables further receive operations. 00437 #define SOCKET_SHUT_WR 1 ///< Disables further send operations. 00438 #define SOCKET_SHUT_RDWR 2 ///< Disables further send and receive operations. 00439 00440 /** 00441 * \brief A function to shut down a connection. 00442 * 00443 * \param socket The ID of the socket to be shut down. 00444 * \param how How socket is to be shut down, one of SOCKET_SHUT_XX. 00445 * 00446 * \return 0 on success. 00447 * \return -1 if the given socket ID is not found, if the socket type is wrong or TCP layer returns a failure. 00448 * \return -2 if socket is not connected. 00449 */ 00450 int8_t socket_shutdown(int8_t socket, uint8_t how); 00451 00452 /** 00453 * \brief Send data via a connected socket by client. 00454 * 00455 * Note: The socket connection must be ready before using this function. 00456 * The stack uses automatically the address of the remote connected host as the destination address for the packet. 00457 * 00458 * This call is equivalent to socket_sendto() with address set to NULL - see 00459 * that call for more details. 00460 * 00461 * \param socket The socket ID. 00462 * \param buffer A pointer to data. 00463 * \param length Data length. 00464 */ 00465 int16_t socket_send(int8_t socket, const void *buffer, uint16_t length); 00466 00467 /** 00468 * \brief A function to read received data buffer from a socket. 00469 * \deprecated 00470 * 00471 * Used by the application to get data from a socket. See socket_recvfrom() 00472 * for more details. 00473 * 00474 * This is equivalent to socket_recvfrom, except that it passes the 00475 * flag NS_MSG_LEGACY0, which modifies the return behaviour for zero data. 00476 * 00477 * \param socket The socket ID. 00478 * \param src_addr A pointer to a structure where the sender's address is stored. 00479 * May be NULL if not required. 00480 * \param buffer A pointer to an array where the read data is written to. 00481 * \param length The maximum length of the allocated buffer. 00482 * 00483 * \return >0 indicates the length of the data copied to buffer. 00484 * \return 0 if no data was read (includes zero-length datagram, 00485 * end of stream and no data currently available) 00486 * \return -1 invalid input parameters. 00487 */ 00488 int16_t socket_read(int8_t socket, ns_address_t *src_addr, uint8_t *buffer, uint16_t length); 00489 00490 /** 00491 * \brief A function to read received data buffer from a socket, 00492 * 00493 * Equivalent to socket_recvfrom with src_address set to NULL. 00494 * 00495 * \param socket The socket ID. 00496 * \param buffer A pointer to an array where the read data is written to. 00497 * \param length The maximum length of the allocated buffer. 00498 * \param flags Flags for read call 00499 * 00500 * \return as for socket_recvfrom 00501 */ 00502 int16_t socket_recv(int8_t socket, void *buffer, uint16_t length, int flags); 00503 00504 /** 00505 * \brief A function to read received data buffer from a socket 00506 * 00507 * Used by the application to get data from a socket. 00508 * 00509 * This has two modes of operation. 00510 * 00511 * 1) For non-stream sockets, if the receive queue is disabled (set to 0 via 00512 * SOCKET_SO_RCVBUF), which is the non-stream default and original Nanostack 00513 * behaviour, then applications receive exactly one SOCKET_DATA callback per 00514 * datagram, indicating that datagram's length. They must make 1 read call 00515 * in that callback, and they will be given the data. If not read, the 00516 * datagram is discarded on return from the callback. 00517 * 00518 * 2) Otherwise - stream sockets or SOCKET_SO_RCVBUF non-zero - behaviour is 00519 * akin to traditional BSD. SOCKET_DATA callbacks occur when new data arrives, 00520 * and read calls can be made any time. Data will be queued to an extent 00521 * determined by the receive buffer size. The length in the data callback 00522 * is the total amount of data in the receive queue - possibly multiple 00523 * datagrams. 00524 * 00525 * \param socket The socket ID. 00526 * \param buffer A pointer to an array where the read data is written to. 00527 * \param length The maximum length of the allocated buffer. 00528 * \param flags Flags for read call 00529 * \param src_addr A pointer to a structure where the sender's address is stored. 00530 * May be NULL if not required. 00531 * 00532 * The returned length is normally the length of data actually written to the buffer; if 00533 * NS_MSG_TRUNC is set in flags, then for non-stream sockets, the actual datagram length is 00534 * returned instead, which may be larger than the buffer size. 00535 * 00536 * Return values assume flag NS_MSG_LEGACY0 is not set - if it is set, they are 00537 * as per socket_read(). 00538 * 00539 * \return >0 indicates the length of the data copied to buffer (or original datagram size) 00540 * \return 0 if end of stream or zero-length datagram 00541 * \return -1 invalid input parameters. 00542 * \return NS_EWOULDBLOCK if no data is currently available 00543 */ 00544 int16_t socket_recvfrom(int8_t socket, void *buffer, uint16_t length, int flags, ns_address_t *src_addr); 00545 00546 /** 00547 * \brief A function to read received message with ancillary data from a socket. 00548 * 00549 * Used by the application to get data from a socket. See socket_recvfrom for 00550 * details of the two delivery mechanisms. 00551 * 00552 * Ancillary data must request by socket_setsockopt(). 00553 * 00554 * msg->msg_controllen is updated to indicate actual length of ancillary data output 00555 * 00556 * \param socket The socket ID. 00557 * \param msg A pointer to a structure where messages is stored with or without ancillary data 00558 * \param flags A flags for message read. 00559 * 00560 * \return as for socket_recvfrom 00561 */ 00562 int16_t socket_recvmsg(int8_t socket, ns_msghdr_t *msg, int flags); 00563 00564 /** 00565 * \brief A function to send UDP, TCP or raw ICMP data via the socket. 00566 * 00567 * Used by the application to send data. 00568 * 00569 * The return of 0 on success is unconventional, and obtained by passing 00570 * NS_MSG_LEGACY0 to socket_sendmsg internally - to get conventional 00571 * return values, you can use socket_sendmsg() instead. 00572 * 00573 * \param socket The socket ID. 00574 * \param address A pointer to the destination address information. 00575 * \param buffer A pointer to data to be sent. 00576 * \param length Length of the data to be sent. 00577 * 00578 * \return 0 On success (whole packet queued) 00579 * \return NS_EWOULDBLOCK if nothing written due to lack of queue space. 00580 * 00581 * Error returns: 00582 * 00583 * \return -1 Invalid socket ID. 00584 * \return -2 Socket memory allocation fail. 00585 * \return -3 TCP state not established or address scope not defined . 00586 * \return -4 Unknown interface. 00587 * \return -5 Socket not connected 00588 * \return -6 Packet too short (ICMP raw socket error). 00589 */ 00590 int16_t socket_sendto(int8_t socket, const ns_address_t *address, const void *buffer, uint16_t length); 00591 00592 /** 00593 * \brief A function to send UDP, TCP or raw ICMP data via the socket with or without ancillary data or destination address. 00594 * 00595 * Used by the application to send data message header support also vector list socket_send() and socket_sendto() use this functionality internally. 00596 * 00597 * \param socket The socket ID. 00598 * \param msg A pointer to the Message header which include address, payload and ancillary data. 00599 * \param flags A flags for message send (eg NS_MSG_LEGACY0) 00600 * 00601 * Messages destination address is defined by msg->msg_name which must be ns_address_t. If msg->msg_nme is NULL socket select connected address 00602 * 00603 * Messages payload and length is defined msg->msg_iov and msg->msg_iovlen. API support to send multiple data vector. 00604 * 00605 * Supported ancillary data for send defined by msg->msg_control and msg->msg_controllen. 00606 * 00607 * msg->msg_flags is unused, and need not be initialised. 00608 * 00609 * The following main return values assume flag NS_MSG_LEGACY0 is not set - 00610 * if it is set, they are as per socket_sendto(). 00611 * 00612 * \return length if entire amount written (which could be 0) 00613 * \return value >0 and <length if partial amount written (stream only) 00614 * \return NS_EWOULDBLOCK if nothing written due to lack of queue space. 00615 00616 * Error returns: 00617 * 00618 * \return -1 Invalid socket ID. 00619 * \return -2 Socket memory allocation fail. 00620 * \return -3 TCP state not established or address scope not defined . 00621 * \return -4 Unknown interface. 00622 * \return -5 Socket not connected 00623 * \return -6 Packet too short (ICMP raw socket error). 00624 */ 00625 int16_t socket_sendmsg(int8_t socket, const ns_msghdr_t *msg, int flags); 00626 00627 /** 00628 * \brief A function to read local address and port for a bound socket. 00629 * 00630 * This call writes ns_in6addr_any if address is not bound and 0 if the port is not bound. 00631 * 00632 * \param socket The socket ID. 00633 * \param address A pointer to the address structure where the local address information is written to. 00634 * 00635 * \return 0 on success. 00636 * \return -1 if no socket is found. 00637 */ 00638 int8_t socket_getsockname(int8_t socket, ns_address_t *address); 00639 00640 /** 00641 * \brief A function to read remote address and port for a connected socket. 00642 * 00643 * \param socket The socket ID. 00644 * \param address A pointer to the address structure where the remote address information is written to. 00645 * 00646 * \return 0 on success. 00647 * \return -1 if no socket is found. 00648 * \return -2 if no socket is not connected. 00649 */ 00650 int8_t socket_getpeername(int8_t socket, ns_address_t *address); 00651 00652 /* Backwards compatibility */ 00653 static inline int8_t socket_read_session_address(int8_t socket, ns_address_t *address) 00654 { 00655 return socket_getpeername(socket, address); 00656 } 00657 00658 /** \name Flags for SOCKET_IPV6_ADDR_PREFERENCES - opposites 16 bits apart. */ 00659 ///@{ 00660 #define SOCKET_IPV6_PREFER_SRC_TMP 0x00000001 /**< Prefer temporary address (RFC 4941); default. */ 00661 #define SOCKET_IPV6_PREFER_SRC_PUBLIC 0x00010000 /**< Prefer public address (RFC 4941). */ 00662 #define SOCKET_IPV6_PREFER_SRC_6LOWPAN_SHORT 0x00000100 /**< Prefer 6LoWPAN short address. */ 00663 #define SOCKET_IPV6_PREFER_SRC_6LOWPAN_LONG 0x01000000 /**< Prefer 6LoWPAN long address. */ 00664 ///@} 00665 00666 /** \name Options for SOCKET_IPV6_ADDRESS_SELECT. */ 00667 ///@{ 00668 #define SOCKET_SRC_ADDRESS_MODE_PRIMARY 0 /**< Default value always. */ 00669 #define SOCKET_SRC_ADDRESS_MODE_SECONDARY 1 /**< This mode typically selects the secondary address. */ 00670 ///@} 00671 00672 /** \name Protocol levels used for socket_setsockopt. */ 00673 ///@{ 00674 #define SOCKET_SOL_SOCKET 0 /**< Socket level */ 00675 #define SOCKET_IPPROTO_IPV6 41 /**< IPv6. */ 00676 ///@} 00677 00678 /** \name Option names for protocol level SOCKET_SOL_SOCKET. 00679 * \anchor OPTNAMES_SOCKET 00680 */ 00681 ///@{ 00682 /** Specify receive buffer size in payload bytes, as int32_t. 0 means traditional Nanostack behaviour - unread data dropped unless read in data callback */ 00683 #define SOCKET_SO_RCVBUF 1 00684 /** Specify send buffer size in payload bytes, as int32_t. Only currently used for stream sockets. */ 00685 #define SOCKET_SO_SNDBUF 2 00686 /** Specify receive low water mark in payload bytes, as int32_t. Not yet implemented. */ 00687 #define SOCKET_SO_RCVLOWAT 3 00688 /** Specify send low water mark in payload bytes, as int32_t. Queued sends will only be accepted if this many bytes of send queue space are available, else NS_EWOULDBLOCK is returned. */ 00689 #define SOCKET_SO_SNDLOWAT 4 00690 ///@} 00691 00692 /** \name IPv6 socket options 00693 * \anchor OPTNAMES_IPV6 00694 * 00695 * IPv6 socket options summary 00696 * 00697 * | opt_name / cmsg_type | Data type | set/getsockopt | sendmsg | recvmsg | 00698 * | :--------------------------: | :--------------: | :-------------: | :-----: | :-------------------------------: | 00699 * | SOCKET_IPV6_TCLASS | int16_t | Yes | Yes | If enabled with RECVTCLASS | 00700 * | SOCKET_IPV6_UNICAST_HOPS | int16_t | Yes | No | No | 00701 * | SOCKET_IPV6_MULTICAST_HOPS | int16_t | Yes | No | No | 00702 * | SOCKET_IPV6_ADDR_PREFERENCES | int | Yes | No | No | 00703 * | SOCKET_IPV6_USE_MIN_MTU | int8_t | Yes | Yes | No | 00704 * | SOCKET_IPV6_DONTFRAG | int8_t | Yes | Yes | No | 00705 * | SOCKET_IPV6_FLOW_LABEL | int32_t | Yes | No | No | 00706 * | SOCKET_IPV6_HOPLIMIT | int16_t | No | Yes | If enabled with RECVHOPLIMIT | 00707 * | SOCKET_IPV6_PKTINFO | ns_in6_pktinfo_t | No | Yes | If enabled with RECVPKTINFO | 00708 * | SOCKET_IPV6_RECVPKTINFO | bool | Yes | No | No | 00709 * | SOCKET_IPV6_RECVHOPLIMIT | bool | Yes | No | No | 00710 * | SOCKET_IPV6_RECVTCLASS | bool | Yes | No | No | 00711 * | SOCKET_IPV6_MULTICAST_IF | int8_t | Yes | No | No | 00712 * | SOCKET_IPV6_MULTICAST_LOOP | bool | Yes | Yes | No | 00713 * | SOCKET_IPV6_JOIN_GROUP | ns_ipv6_mreq_t | Set only | No | No | 00714 * | SOCKET_IPV6_LEAVE_GROUP | ns_ipv6_mreq_t | Set only | No | No | 00715 * | SOCKET_BROADCAST_PAN | int8_t | Yes | No | No | 00716 * | SOCKET_LINK_LAYER_SECURITY | int8_t | Yes | No | No | 00717 * | SOCKET_INTERFACE_SELECT | int8_t | Yes | No | No | 00718 * 00719 */ 00720 00721 ///@{ 00722 /** Specify traffic class for outgoing packets, as int16_t (RFC 3542 S6.5 says int); valid values 0-255, or -1 for system default. */ 00723 #define SOCKET_IPV6_TCLASS 1 00724 /** Specify hop limit for outgoing unicast packets, as int16_t (RFC 3493 S5.1 says int); valid values 0-255, or -1 for system default. */ 00725 #define SOCKET_IPV6_UNICAST_HOPS 2 00726 /** Specify hop limit for outgoing multicast packets, as int16_t (RFC 3493 S5.2 says int); valid values 0-255, or -1 for system default. */ 00727 #define SOCKET_IPV6_MULTICAST_HOPS 3 00728 /** Specify source address preferences, as uint32_t - see RFC 5014; valid values combinations of SOCKET_IPV6_PREFER_xxx flags. */ 00729 #define SOCKET_IPV6_ADDR_PREFERENCES 4 00730 /** Specify PMTU preference, as int8_t (RFC 3542 S11.1 says int); valid values -1 (PMTUD for unicast, default), 0 (PMTUD always), 1 (PMTUD off). */ 00731 #define SOCKET_IPV6_USE_MIN_MTU 5 00732 /** Specify not to fragment datagrams, as int8_t (RFC 3542 S11.2 says int); valid values 0 (fragment to path MTU, default), 1 (no fragmentation, TX fails if bigger than outgoing interface MTU). */ 00733 #define SOCKET_IPV6_DONTFRAG 6 00734 /** Specify flow label for outgoing packets, as int32_t; valid values 0-0xfffff, or -1 for system default, or -2 to always autogenerate */ 00735 #define SOCKET_IPV6_FLOW_LABEL 7 00736 /** Hop limit control for socket_sendmsg() and indicate for hop limit socket_recmsg(), as int16_t; valid values 0-255, -1 for default for outgoing packet */ 00737 #define SOCKET_IPV6_HOPLIMIT 8 00738 /** Specify control messages packet info for send and receive as ns_in6_pktinfo_t socket_sendmsg() it define source address and outgoing interface. socket_recvmsg() it define messages destination address and arrival interface. Reference at RFC3542*/ 00739 #define SOCKET_IPV6_PKTINFO 9 00740 00741 /** Specify socket_recvmsg() ancillary data request state for Packet info (destination address and interface id), as bool; valid values true write enabled, false write disabled */ 00742 #define SOCKET_IPV6_RECVPKTINFO 10 00743 /** Specify socket_recvmsg() ancillary data request state for receive messages hop-limit, as bool; valid values true write enabled, false information write disabled */ 00744 #define SOCKET_IPV6_RECVHOPLIMIT 11 00745 /** Specify socket_recvmsg() ancillary data request state for receive messages traffic class, as bool; valid values true write enabled, false information write disabled */ 00746 #define SOCKET_IPV6_RECVTCLASS 12 00747 00748 /** Set the interface to use for outgoing multicast packets, as int8_t (RFC3493 S5.2 says unsigned int); 0 means unspecified (use routing table) */ 00749 #define SOCKET_IPV6_MULTICAST_IF 13 00750 /** Specify whether outgoing multicast packets are looped back, as bool (RFC3493 S5.2 says unsigned int) */ 00751 #define SOCKET_IPV6_MULTICAST_LOOP 14 00752 /** Join a multicast group, using ns_ipv6_mreq_t */ 00753 #define SOCKET_IPV6_JOIN_GROUP 15 00754 /** Leave a multicast group, using ns_ipv6_mreq_t */ 00755 #define SOCKET_IPV6_LEAVE_GROUP 16 00756 00757 #define SOCKET_BROADCAST_PAN 0xfc /**< Internal use - transmit with IEEE 802.15.4 broadcast PAN ID */ 00758 #define SOCKET_LINK_LAYER_SECURITY 0xfd /**< Not standard enable or disable socket security at link layer (For 802.15.4). */ 00759 #define SOCKET_INTERFACE_SELECT 0xfe /**< Not standard socket interface ID. */ 00760 #define SOCKET_IPV6_ADDRESS_SELECT 0xff /**< Deprecated - use SOCKET_IPV6_ADDR_PREFERENCES instead. */ 00761 00762 /** Multicast group request used for setsockopt() */ 00763 typedef struct ns_ipv6_mreq { 00764 uint8_t ipv6mr_multiaddr[16]; /**< IPv6 multicast address */ 00765 int8_t ipv6mr_interface; /**< interface id */ 00766 } ns_ipv6_mreq_t; 00767 00768 /** 00769 * \brief Set an option for a socket 00770 * 00771 * Used to specify miscellaneous options for a socket. Supported levels and 00772 * names defined above. 00773 * 00774 * \param socket The socket ID. 00775 * \param level The protocol level. 00776 * \param opt_name The option name (interpretation depends on level). 00777 * See \ref OPTNAMES_SOCKET and \ref OPTNAMES_IPV6. 00778 * \param opt_value A pointer to value for the specified option. 00779 * \param opt_len Size of the data pointed to by the value. 00780 * 00781 * \return 0 on success. 00782 * \return -1 invalid socket ID. 00783 * \return -2 invalid/unsupported option. 00784 * \return -3 invalid option value. 00785 */ 00786 int8_t socket_setsockopt(int8_t socket, uint8_t level, uint8_t opt_name, const void *opt_value, uint16_t opt_len); 00787 00788 ///@} 00789 00790 /** 00791 * \brief Get an option for a socket. 00792 * 00793 * Used to read miscellaneous options for a socket. Supported levels and 00794 * names defined above. If the buffer is smaller than the option, the output 00795 * is silently truncated; otherwise opt_len is modified to indicate the actual 00796 * length. 00797 * 00798 * \param socket The socket ID. 00799 * \param level The protocol level. 00800 * \param opt_name The option name (interpretation depends on level). See \ref OPTNAMES_IPV6. 00801 * \param opt_value A pointer to output buffer. 00802 * \param opt_len A pointer to length of output buffer; updated on exit. 00803 * 00804 * \return 0 on success. 00805 * \return -1 invalid socket ID. 00806 * \return -2 invalid/unsupported option. 00807 */ 00808 int8_t socket_getsockopt(int8_t socket, uint8_t level, uint8_t opt_name, void *opt_value, uint16_t *opt_len); 00809 00810 #ifdef __cplusplus 00811 } 00812 #endif 00813 #endif /*_NS_SOCKET_H*/
Generated on Tue Jul 12 2022 13:54:51 by
