ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers socket_api.h Source File

socket_api.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
00003  *
00004  * SPDX-License-Identifier: LicenseRef-PBL
00005  *
00006  * Licensed under the Permissive Binary License, Version 1.0 (the "License"); you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * https://www.mbed.com/licenses/PBL-1.0
00010  *
00011  * See the License for the specific language governing permissions and limitations under the License.
00012  *
00013  */
00014 #ifndef _NS_SOCKET_API_H
00015 #define _NS_SOCKET_API_H
00016 
00017 #ifdef __cplusplus
00018 extern "C" {
00019 #endif
00020 /**
00021  * \file socket_api.h
00022  * \brief 6LoWPAN Library Socket API
00023  *
00024  * \section socket-com Common socket API
00025  *  - socket_open(), A function to open a socket.
00026  *  - socket_close(), A function to close a socket.
00027  *
00028  * \section socket-read Socket read API at callback
00029  *  - socket_read(), A function to read received data buffer from a socket.
00030  *  - socket_recvmsg(), A function to read received data buffer from a socket to Posix defined message structure
00031  *  - socket_read_session_address(), A function to read session info for a TCP event.
00032  *
00033  * \section socket-tx Socket TX API
00034  * - socket_send(), A function to write data buffer to a socket.
00035  * - socket_sendto(), A function to write data to a specific destination in the socket.
00036  * - socket_senmsg(), A function which support socket_send and socket_sendto functionality which supports ancillary data
00037  *
00038  * \section sock-connect TCP socket connection handle
00039  *  - socket_listen(), A function to set the socket to listening mode.
00040  *  - socket_connect(), A function to connect to a remote peer.
00041  *  - socket_shutdown(), A function to shut down a connection.
00042  *
00043  * Sockets are a common abstraction model for network communication and are used in most operating systems.
00044  * 6LoWPAN Library API follows BSD socket API conventions closely with some extensions necessitated
00045  * by the event-based scheduling model.
00046  *
00047  * Library supports the following socket types:
00048  * | Socket name    |   Socket description           |
00049  * | :------------: | :----------------------------: |
00050  * | SOCKET_UDP     |   UDP socket type              |
00051  * | SOCKET_TCP     |   TCP socket type              |
00052  * | SOCKET_ICMP    |   ICMP raw socket type         |
00053  *
00054  * \section socket-raw ICMP RAW socket instruction
00055  * An ICMP raw socket can be created with the function socket_open() and the
00056  * identifier 0xffff. When using ICMP sockets, the minimum packet length is 4
00057  * bytes which is the 4 bytes of ICMP header. The first byte is for type, second
00058  * for code and last two are for the checksum that must always be set to zero.
00059  * The stack will calculate the checksum automatically before transmitting the packet.
00060  * THe header is followed by the payload if there is any.
00061  * NOTE: While it is perfectly legal to send an ICMP packet without payload,
00062  * some packet sniffing softwares might regard such packets as malformed.
00063  * In such a case, a dummy payload can be attached by providing a socket_sendto()
00064  * function with a pointer to your desired data buffer.
00065  * Moreover, the current implementation of the stack allows only certain ICMP types, for example
00066  * ICMP echo, reply, destination unreachable, router advertisement, router solicitation.
00067  * It will drop any other unimplemented types. For example, Private experimentation type (200) will
00068  * be dropped by default.
00069 
00070  * | ICMP Type |    ICMP Code | Checksum  | Payload    | Notes           |
00071  * | :-------: | :----------: | :-------: | :--------: | :--------------:|
00072  * | 1         | 1            | 2         | n bytes    | Length in bytes |
00073  * | 0xXX      | 0xXX         | 0x00 0x00 | n bytes    | Transmit        |
00074  * | 0xXX      | 0xXX         | 0xXX 0xXX | n bytes    | Receive         |
00075  *
00076  * ICMP echo request with 4 bytes of payload (ping6).
00077  *
00078  * | ICMP Type |    ICMP Code | Checksum  | Payload             |
00079  * | :-------: | :----------: | :-------: | :-----------------: |
00080  * | 0x80      | 0x00         | 0x00 0x00 | 0x00 0x01 0x02 0x03 |
00081  *
00082  * ICMP echo response for the message above.
00083  *
00084  * | ICMP Type |    ICMP Code | Checksum  | Payload             |
00085  * | :-------: | :----------: | :-------: | :-----------------: |
00086  * | 0x81      | 0x00         | 0xXX 0xXX | 0x00 0x01 0x02 0x03 |
00087  *
00088  * \section socket-receive Socket receiving
00089  * When there is data to read from the socket, a receive callback function is called with the event type parameter set to SOCKET_DATA.
00090  * The application must read the received data with socket_read() or socket_recvmsg() during the callback because the stack will release the data
00091  * when returning from the receive callback.
00092  *
00093  * Socket event has event_type and socket_id fields. The receive callback function must be defined when socket is opened using socket_open() API.
00094  *
00095  * All supported socket event types are listed here:
00096  *
00097  * | Event Type                 | Value | Description                                                         |
00098  * | :------------------------: | :---: | :-----------------------------------------------------------------: |
00099  * | SOCKET_EVENT_MASK          | 0xF0  | NC Socket event mask.                                               |
00100  * | SOCKET_DATA                | 0x00  | Data received, read data length available in d_len field.           |
00101  * | SOCKET_BIND_DONE           | 0x10  | TCP connection ready.                                               |
00102  * | SOCKET_TX_FAIL             | 0x50  | Socket data send failed.                                            |
00103  * | SOCKET_CONNECT_CLOSED      | 0x60  | TCP connection closed.                                              |
00104  * | SOCKET_CONNECTION_RESET    | 0x70  | TCP connection reset.                                               |
00105  * | SOCKET_NO_ROUTER           | 0x80  | No route available to destination.                                  |
00106  * | SOCKET_TX_DONE             | 0x90  | Last socket TX process done, in TCP, whole TCP process is ready.    |
00107  * | SOCKET_NO_RAM              | 0xA0  | No RAM available.                                                   |
00108  *
00109  *
00110  * \section socket-tcp How to use TCP sockets:
00111  *
00112  * | API                           | Socket Type   | Description                                                      |
00113  * | :---------------------------: | :-----------: | :------------------------------------------------------------:   |
00114  * | socket_open()                 | Server/Client | Open socket to specific or dynamic port number.                  |
00115  * | socket_shutdown()             | Client        | Shut down opened TCP connection.                                 |
00116  * | socket_listen()               | Server        | Set server port to listen state.                                 |
00117  * | socket_connect()              | Client        | Connect client socket to specific destination.                   |
00118  * | socket_close()                | Server/Client | Closes the TCP Socket.                   |
00119  * | socket_send()                 | Client        | Send data to session based destination.                          |
00120  * | socket_sendto()               | Server/Client | Send data to specific destination.                               |
00121  * | socket_read_session_address() | Server/Client | Read socket TCP session address and port information.            |
00122  *
00123  * 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.
00124  *
00125  * 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.
00126  * The listen mode closes the connection automatically after server timeout or when the client or application closes the connection manually by socket_shutdown() function.
00127  *
00128  * 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.
00129  * After the successful state change, data can be sent using socket_send() by client and socket_send() by server.
00130  * The connection can be shut down with socket_shutdown() function or by server timeout.
00131  *
00132  * \section socket-udpicmp How to use UDP and RAW socket:
00133  *
00134  * 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.
00135  * Data can be transmitted with the socket_sendto() function. An ICMP socket works with same function call.
00136  *
00137  */
00138 
00139 #include "ns_address.h"
00140 
00141 /** \name Protocol IDs used with sockets. */
00142 ///@{
00143 /** UDP socket type. */
00144 #define SOCKET_UDP      17
00145 /** Normal TCP socket type. */
00146 #define SOCKET_TCP      6
00147 /** ICMPv6 raw socket type */
00148 #define SOCKET_ICMP     32
00149 /** Raw IPv6 socket type (socket_open identifier is IPv6 protocol number) */
00150 #define SOCKET_RAW      2
00151 /** REMOVED Feature in this release socket open return error Local Sockets for Tun interface functionality to APP-APP trough any BUS */
00152 #define SOCKET_LOCAL    1
00153 ///@}
00154 
00155 /** ICMP socket instruction
00156  *
00157  * ICMP header is comprised of 4 bytes. The first byte is for type, second for code and
00158  * the last two are for checksum that must always be zero.
00159  * The stack will calculate the checksum automatically when sending the packet.
00160  * The payload comes after the header and it can be of any length. It can also be set to 0.
00161  *
00162  * This applies for reading and sending.
00163  *
00164  *      ICMP TYPE   ICMP Code   ICMP Checksum (2 bytes)     Payload n bytes
00165  *      --------    --------    -------- --------           -------- -------- ....... -------
00166  *      0xXX        0xXX        0x00    0x00                0xXX    0xXX    ......  0xXX
00167  *
00168  * Example data flow for ping:
00169  *
00170  * ICMP echo request with 4-bytes payload (Ping6).
00171  *      0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03
00172  *
00173  * ICMP echo response for the message above.
00174  *      0x81, 0x00, 0xXX, 0xXX, 0x00, 0x01, 0x02, 0x03
00175  */
00176 
00177 /*!
00178  * \struct socket_callback_t
00179  * \brief Socket Callback function structure type.
00180  */
00181 
00182 typedef struct socket_callback_t {
00183     uint8_t event_type;     /**< Socket Callback Event check list below */
00184     int8_t socket_id;       /**< Socket id queue which socket cause call back */
00185     int8_t interface_id;    /**< Network Interface ID where Packet came */
00186     uint16_t  d_len;        /**< Data length if event type is SOCKET_DATA */
00187     uint8_t LINK_LQI;       /**< LINK LQI info if interface cuold give */
00188 } socket_callback_t;
00189 
00190 /*!
00191  * \struct ns_msghdr_t
00192  * \brief Normal IP socket message structure for socket_recvmsg() and socket_sendmsg().
00193  */
00194 
00195 typedef struct ns_msghdr {
00196     void *msg_name;                 /**< Optional address send use for destination and receive it will be source. MUST be ns_address_t */
00197     uint_fast16_t msg_namelen;      /**< Length of optional address use sizeof(ns_address_t) when msg_name is defined */
00198     ns_iovec_t *msg_iov;            /**< Message data vector list */
00199     uint_fast16_t  msg_iovlen;      /**< Data vector count in msg_iov */
00200     void *msg_control;              /**< Ancillary data list of ns_cmsghdr_t pointer */
00201     uint_fast16_t  msg_controllen;  /**< Ancillary data length */
00202     int flags;                      /**< Flags for received messages */
00203 } ns_msghdr_t;
00204 
00205 /*!
00206  * \struct ns_cmsghdr_t
00207  * \brief Control messages.
00208  */
00209 typedef struct ns_cmsghdr {
00210     uint16_t cmsg_len;      /**< Ancillary data control messages length including cmsghdr */
00211     uint8_t cmsg_level;     /**< Originating protocol level should be SOCKET_IPPROTO_IPV6 */
00212     uint8_t cmsg_type;      /**< Protocol Specific types for example SOCKET_IPV6_PKTINFO,  */
00213 } ns_cmsghdr_t;
00214 
00215 
00216 /** \name socket_recvmsg() message error flags.
00217  * \anchor MSG_HEADER_FLAGS
00218  */
00219 ///@{
00220 /** In msg_flags, indicates that given data buffer was smaller than received datagram. Can also be passed as an flag parameter to recvmsg. */
00221 #define NS_MSG_TRUNC    1
00222 /** Indicates that given ancillary data buffer was smaller than enabled at socket msg->msg_controllen define proper writed data lengths. */
00223 #define NS_MSG_CTRUNC   2
00224 ///@}
00225 /*!
00226  * \struct ns_in6_pktinfo_t
00227  * \brief IPv6 packet info which is used for socket_recvmsg() socket_sendmsg().
00228  */
00229 typedef struct ns_in6_pktinfo {
00230     uint8_t ipi6_addr[16];    /**< src/dst IPv6 address */
00231     int8_t  ipi6_ifindex;    /**< send/recv interface index */
00232 } ns_in6_pktinfo_t;
00233 
00234 #define CMSG_HEADER_ALIGN sizeof(long)
00235 
00236 #define CMSG_DATA_ALIGN CMSG_HEADER_ALIGN
00237 
00238 #ifndef NS_ALIGN_SIZE
00239 #define NS_ALIGN_SIZE(length, aligment_base) \
00240     ((length + (aligment_base -1 )) & ~(aligment_base -1))
00241 #endif
00242 
00243 /**
00244  * \brief Parse first control message header from message ancillary data.
00245  *
00246  * \param msgh Pointer for socket message.
00247  *
00248  * \return Pointer to first control message header , Could be NULL when control_message is undefined.
00249  */
00250 #define NS_CMSG_FIRSTHDR(msgh) \
00251     ((msgh)->msg_controllen >= sizeof(struct ns_cmsghdr) ? \
00252             (struct ns_cmsghdr *)(msgh)->msg_control : \
00253             (struct ns_cmsghdr *)NULL )
00254 /**
00255  * \brief Parse next control message from message by current control message header.
00256  *
00257  * \param msgh Pointer for socket message.
00258  * \param cmsg Pointer for last parsed control message
00259  *
00260  * \return Pointer to Next control message header , Could be NULL when no more control messages data.
00261  */
00262 
00263 ns_cmsghdr_t *NS_CMSG_NXTHDR(const ns_msghdr_t *msgh, const ns_cmsghdr_t *cmsg);
00264 
00265 /**
00266  * \brief Get Data pointer from control message header.
00267  *
00268  * \param cmsg Pointer (ns_cmsghdr_t) for last parsed control message
00269  *
00270  * \return Data pointer.
00271  */
00272 #define NS_CMSG_DATA(cmsg) \
00273     ((uint8_t *)(cmsg) + NS_ALIGN_SIZE(sizeof(ns_cmsghdr_t), CMSG_DATA_ALIGN))
00274 
00275 /**
00276  * \brief Returns the total space required for a cmsg header plus the specified data, allowing for all padding
00277  *
00278  * \param length  size of the ancillary data
00279  *
00280  * For example, the space required for a SOCKET_IPV6_PKTINFO is NS_CMSG_SPACE(sizeof(ns_in6_pktinfo))
00281  *
00282  * \return Total size of CMSG header, data and trailing padding.
00283  */
00284 #define NS_CMSG_SPACE(length) \
00285     (NS_ALIGN_SIZE(sizeof(ns_cmsghdr_t), CMSG_DATA_ALIGN) + NS_ALIGN_SIZE(length, CMSG_HEADER_ALIGN))
00286 
00287 /**
00288  * \brief Calculate length to store in cmsg_len with taking into account any necessary alignment.
00289  *
00290  * \param length  size of the ancillary data
00291  *
00292  * For example, cmsg_len for a SOCKET_IPV6_PKTINFO is NS_CMSG_LEN(sizeof(ns_in6_pktinfo))
00293  *
00294  * \return Size of CMSG header plus data, without trailing padding.
00295  */
00296 #define NS_CMSG_LEN(length) \
00297     (NS_ALIGN_SIZE(sizeof(ns_cmsghdr_t), CMSG_DATA_ALIGN) + length)
00298 
00299 
00300 /** IPv6 wildcard address IN_ANY */
00301 extern const uint8_t ns_in6addr_any[16];
00302 
00303 /**
00304  * \brief Create and initialize a socket for communication.
00305  *
00306  * \param protocol Defines the protocol to use.
00307  * \param identifier The socket port. 0 indicates that port is not specified and it will be selected automatically when using the socket.
00308  * \param passed_fptr A function pointer to a function that is called whenever a data frame is received to this socket.
00309  *
00310  * \return 0 or greater on success; Return value is the socket ID.
00311  * \return -1 on failure.
00312  * \return -2 on port reserved.
00313  */
00314 int8_t socket_open(uint8_t protocol, uint16_t identifier, void (*passed_fptr)(void *));
00315 
00316 /**
00317  * \brief A function to close a socket.
00318  *
00319  * \param socket ID of the socket to be closed.
00320  *
00321  * \return 0 socket closed.
00322  * \return -1 socket not closed.
00323  */
00324 int8_t socket_close(int8_t socket);
00325 
00326 /**
00327  * \brief A function to set a socket to listening mode.
00328  *
00329  * \param socket The socket ID.
00330  * \param backlog The pending connections queue size. (Not yet implemented).
00331  * \return 0 on success.
00332  * \return -1 on failure.
00333  */
00334 int8_t socket_listen(int8_t socket, uint8_t backlog);
00335 
00336 /**
00337  * \brief A function to accept a new connection on an socket.
00338  *
00339  * NOT YET IMPLEMENTED - PLACEHOLDER FOR FUTURE TCP CHANGES
00340  *
00341  * \param socket_id The socket ID of the listening socket.
00342  * \param addr Either NULL pointer or pointer to structure where the remote address of the connecting host is copied.
00343  * \param passed_fptr A function pointer to a function that is called whenever a data frame is received to the new socket.
00344  * \return 0 or greater on success; return value is the new socket ID.
00345  * \return -1 on failure.
00346  */
00347 int8_t socket_accept(int8_t socket_id, ns_address_t *addr, void (*passed_fptr)(void *));
00348 
00349 /**
00350  * \brief A function to connect to remote peer (TCP).
00351  *
00352  * \param socket The socket ID.
00353  * \param address The address of a remote peer.
00354  * \deprecated \param randomly_take_src_number Ignored - random local port is always chosen if not yet bound
00355  *
00356  * \return 0 on success.
00357  * \return -1 in case of an invalid socket ID or parameter.
00358  * \return -2 if memory allocation fails.
00359  * \return -3 if the socket is in listening state.
00360  * \return -4 if the socket is already connected.
00361  * \return -5 connect is not supported on this type of socket.
00362  * \return -6 if the TCP session state is wrong.
00363  * \return -7 if the source address selection fails.
00364  */
00365 int8_t socket_connect(int8_t socket, ns_address_t *address, uint8_t randomly_take_src_number);
00366 
00367 /**
00368  * \brief Bind socket to address.
00369  *
00370  * Used by the application to bind a socket to a port and/or an address. Binding can
00371  * be done only once. The port or address cannot be changed after binding.
00372  *
00373  * \param socket Socket ID of the socket to bind.
00374  * \param address Address structure containing the port and address to bind.
00375  *
00376  * \return 0 on success.
00377  * \return -1 if the given address is NULL.
00378  * \return -2 if the port is already bound to another socket.
00379  * \return -3 if address is not us.
00380  * \return -4 if the socket is already bound.
00381  * \return -5 bind is not supported on this type of socket.
00382  *
00383  */
00384 int8_t socket_bind(int8_t socket, const ns_address_t *address);
00385 
00386 /**
00387  * \brief Bind a local address to a socket based on the destination address and
00388  *  the address selection preferences.
00389  *  Binding happens to the same address that socket_connect() would bind to.
00390  *  Reference: RFC5014 IPv6 Socket API for Source Address Selection.
00391  *
00392  * \param socket The socket ID.
00393  * \param dst_address The destination address to which the socket wants to communicate.
00394  *
00395  * \return 0 on success.
00396  * \return -1 if the given address is NULL or socket ID is invalid.
00397  * \return -2 if the memory allocation failed.
00398  * \return -3 if the socket is already bound to address.
00399  * \return -4 if the interface cannot be found.
00400  * \return -5 if the source address selection fails.
00401  * \return -6 bind2addrsel is not supported on this type of socket.
00402  */
00403 int8_t socket_bind2addrsel(int8_t socket, const ns_address_t *dst_address);
00404 
00405 /**
00406  * Options for the socket_shutdown() parameter 'how'.
00407  */
00408 #define SOCKET_SHUT_RD      0   ///< Disables further receive operations.
00409 #define SOCKET_SHUT_WR      1   ///< Disables further send operations.
00410 #define SOCKET_SHUT_RDWR    2   ///< Disables further send and receive operations.
00411 
00412 /**
00413  * \brief A function to shut down a connection.
00414  *
00415  * \param socket The ID of the socket to be shut down.
00416  * \param how How socket is to be shut down, one of SOCKET_SHUT_XX.
00417  *
00418  * \return 0 on success.
00419  * \return -1 if the given socket ID is not found, if the socket type is wrong or TCP layer returns a failure.
00420  * \return -2 if no active TCP session was found.
00421  */
00422 int8_t socket_shutdown(int8_t socket, uint8_t how);
00423 
00424 /**
00425  * \brief Send data via a connected TCP socket by client.
00426  *
00427  * Note: The socket connection must be ready before using this function.
00428  * The stack uses automatically the address of the remote connected host as the destination address for the packet.
00429  *
00430  * \param socket The socket ID.
00431  * \param buffer A pointer to data.
00432  * \param length Data length.
00433  *
00434  * \return 0 done
00435  * \return -1 Invalid socket ID.
00436  * \return -2 Socket memory allocation fail.
00437  * \return -3 TCP state not established or address scope not defined .
00438  * \return -4 Socket TX process busy or unknown interface.
00439  * \return -5 Socket not connected
00440  * \return -6 Packet too short (ICMP raw socket error).
00441  */
00442 int8_t socket_send(int8_t socket, uint8_t *buffer, uint16_t length);
00443 
00444 /**
00445  * \brief A function to read received data buffer from a socket.
00446  *
00447  * Used by the application to get data from a socket. This method must be called once
00448  * from a socket callback when handling event SOCKET_DATA. If the received data does not fit
00449  * in the buffer provided the excess data bytes are discarded.
00450  *
00451  * \param socket The socket ID.
00452  * \param src_addr A pointer to a structure where the sender's address is stored.
00453  * \param buffer A pointer to an array where the read data is written to.
00454  * \param length The maximum length of the allocated buffer.
00455  *
00456  * \return greater than 0 indicates the length of the data copied to buffer.
00457  * \return 0 if no data is available to read.
00458  * \return -1 invalid input parameters.
00459  */
00460 int16_t socket_read(int8_t socket, ns_address_t *src_addr, uint8_t *buffer, uint16_t length);
00461 
00462 /**
00463  * \brief A function to read received message with ancillary data from a socket.
00464  *
00465  * Used by the application to get data from a socket. This method must be called once
00466  * from a socket callback when handling event SOCKET_DATA. If the received data does not fit
00467  * in the buffer provided the excess data bytes are discarded.
00468  *
00469  * Ancillary data must request by socket_setsockopt().
00470  *
00471  * msg->msg_controllen is updated to indicate actual length of ancillary data output
00472  *
00473  * The returned length is normally the length of data actually written to the buffer; if
00474  * NS_MSG_TRUNC is set in flags, then for non-stream sockets, the actual datagram length is
00475  * returned instead, which may be larger than the buffer size.
00476  *
00477  * \param socket The socket ID.
00478  * \param msg A pointer to a structure where messages is stored with or without ancillary data
00479  * \param flags A flags for message read.
00480  *
00481  * \return greater than 0 indicates the length of the data.
00482  * \return 0 if no data is available to read.
00483  * \return -1 invalid input parameters.
00484  */
00485 int16_t socket_recvmsg(int8_t socket, ns_msghdr_t *msg, int flags);
00486 
00487 /**
00488  * \brief A function to send UDP, TCP or raw ICMP data via the socket.
00489  *
00490  * Used by the application to send data.
00491  *
00492  * \param socket The socket ID.
00493  * \param address A pointer to the destination address information.
00494  * \param buffer A pointer to data to be sent.
00495  * \param length Length of the data to be sent.
00496  *
00497  * \return 0 on success.
00498  * \return -1 Invalid socket ID.
00499  * \return -2 Socket memory allocation fail.
00500  * \return -3 TCP state not established or address scope not defined .
00501  * \return -4 Socket TX process busy or unknown interface.
00502  * \return -5 Socket not connected
00503  * \return -6 Packet too short (ICMP raw socket error).
00504  */
00505 int8_t socket_sendto(int8_t socket, ns_address_t *address, uint8_t *buffer, uint16_t length);
00506 
00507 /**
00508  * \brief A function to send UDP, TCP or raw ICMP data via the socket with or without ancillary data or destination address.
00509  *
00510  * Used by the application to send data message header support also vector list socket_send() and socket_sendto() use this functionality internally.
00511  *
00512  * \param socket The socket ID.
00513  * \param msg A pointer to the Message header which include address, payload and ancillary data.
00514  * \param flags A flags for message send for future usage (not supported yet)
00515  *
00516  * 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
00517  *
00518  * Messages payload and length is defined msg->msg_iov and msg->msg_iovlen. API support to send multiple data vector.
00519  *
00520  * Supported ancillary data for send defined by msg->msg_control and msg->msg_controllen.
00521  *
00522  * msg->flags and flags is ignored
00523  *
00524  * \return 0 on success.
00525  * \return -1 Invalid socket ID or message structure.
00526  * \return -2 Socket memory allocation fail.
00527  * \return -3 TCP state not established or address scope not defined .
00528  * \return -4 Socket TX process busy or unknown interface.
00529  * \return -5 Socket not connected
00530  * \return -6 Packet too short (ICMP raw socket error).
00531  */
00532 int8_t socket_sendmsg(int8_t socket, const ns_msghdr_t *msg, int flags);
00533 
00534 /**
00535  * \brief A function to read session info for TCP event.
00536  *
00537  *
00538  * \param socket The socket ID.
00539  * \param address A pointer to the address structure where the session address information is read to.
00540  *
00541  * \return 0 on success.
00542  * \return -1 if no socket is found or TCP is not compiled into this project.
00543  * \return -2 if no session information is found.
00544  *
00545  * Note: This function should be called only at socket callback when the socket event is SOCKET_BIND_DONE or SOCKET_TX_DONE.
00546  * The following sections introduce those functions.
00547  */
00548 int8_t socket_read_session_address(int8_t socket, ns_address_t *address);
00549 
00550 
00551 /** \name Flags for SOCKET_IPV6_ADDR_PREFERENCES - opposites 16 bits apart. */
00552 ///@{
00553 #define SOCKET_IPV6_PREFER_SRC_TMP              0x00000001 /**< Prefer temporary address (RFC 4941); default. */
00554 #define SOCKET_IPV6_PREFER_SRC_PUBLIC           0x00010000 /**< Prefer public address (RFC 4941). */
00555 #define SOCKET_IPV6_PREFER_SRC_6LOWPAN_SHORT    0x00000100 /**< Prefer 6LoWPAN short address. */
00556 #define SOCKET_IPV6_PREFER_SRC_6LOWPAN_LONG     0x01000000 /**< Prefer 6LoWPAN long address. */
00557 ///@}
00558 
00559 /** \name Options for SOCKET_IPV6_ADDRESS_SELECT. */
00560 ///@{
00561 #define SOCKET_SRC_ADDRESS_MODE_PRIMARY     0 /**< Default value always. */
00562 #define SOCKET_SRC_ADDRESS_MODE_SECONDARY   1 /**< This mode typically selects the secondary address. */
00563 ///@}
00564 
00565 /** \name Protocol levels used for socket_setsockopt. */
00566 ///@{
00567 #define SOCKET_IPPROTO_IPV6         41  /**< IPv6. */
00568 ///@}
00569 
00570 /** \name Option names for protocol level SOCKET_IPPROTO_IPV6.
00571  * \anchor OPTNAMES_IPV6
00572  */
00573 ///@{
00574 /** Specify traffic class for outgoing packets, as int16_t (RFC 3542 S6.5 says int); valid values 0-255, or -1 for system default. */
00575 #define SOCKET_IPV6_TCLASS                  1
00576 /** 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. */
00577 #define SOCKET_IPV6_UNICAST_HOPS            2
00578 /** 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. */
00579 #define SOCKET_IPV6_MULTICAST_HOPS          3
00580 /** Specify source address preferences, as uint32_t - see RFC 5014; valid values combinations of SOCKET_IPV6_PREFER_xxx flags. */
00581 #define SOCKET_IPV6_ADDR_PREFERENCES        4
00582 /** 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). */
00583 #define SOCKET_IPV6_USE_MIN_MTU             5
00584 /** 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). */
00585 #define SOCKET_IPV6_DONTFRAG                6
00586 /** Specify flow label for outgoing packets, as int32_t; valid values 0-0xfffff, or -1 for system default, or -2 to always autogenerate */
00587 #define SOCKET_IPV6_FLOW_LABEL              7
00588 /** 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 */
00589 #define SOCKET_IPV6_HOPLIMIT                8
00590 /** 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*/
00591 #define SOCKET_IPV6_PKTINFO                 9
00592 
00593 /** 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 */
00594 #define SOCKET_IPV6_RECVPKTINFO             10
00595 /** Specify socket_recvmsg() ancillary data request state for receive messages hop-limit, as bool; valid values true  write enabled, false information write disabled */
00596 #define SOCKET_IPV6_RECVHOPLIMIT           11
00597 /** Specify socket_recvmsg() ancillary data request state for receive messages traffic class, as bool; valid values true  write enabled, false information write disabled */
00598 #define SOCKET_IPV6_RECVTCLASS             12
00599 
00600 #define SOCKET_BROADCAST_PAN                0xfc /**< Internal use - transmit with IEEE 802.15.4 broadcast PAN ID */
00601 #define SOCKET_LINK_LAYER_SECURITY          0xfd /**< Not standard enable or disable socket security at link layer (For 802.15.4). */
00602 #define SOCKET_INTERFACE_SELECT             0xfe /**< Not standard socket interface ID. */
00603 #define SOCKET_IPV6_ADDRESS_SELECT          0xff /**< Deprecated - use SOCKET_IPV6_ADDR_PREFERENCES instead. */
00604 
00605 /** Socket options summary
00606  *
00607  * | opt_name / cmsg_type         | Data type        | set/getsockopt  | sendmsg | recvmsg                           |
00608  * | :--------------------------: | :--------------: | :-------------: | :-----: | :-------------------------------: |
00609  * | SOCKET_IPV6_TCLASS           | int16_t          |     Yes         |   Yes   | If enabled with RECVTCLASS        |
00610  * | SOCKET_IPV6_UNICAST_HOPS     | int16_t          |     Yes         |   No    | No                                |
00611  * | SOCKET_IPV6_MULTICAST_HOPS   | int16_t          |     Yes         |   No    | No                                |
00612  * | SOCKET_IPV6_ADDR_PREFERENCES | int              |     Yes         |   No    | No                                |
00613  * | SOCKET_IPV6_USE_MIN_MTU      | int8_t           |     Yes         |   Yes   | No                                |
00614  * | SOCKET_IPV6_DONTFRAG         | int8_t           |     Yes         |   Yes   | No                                |
00615  * | SOCKET_IPV6_FLOW_LABEL       | int32_t          |     Yes         |   No    | No                                |
00616  * | SOCKET_IPV6_HOPLIMIT         | int16_t          |     No          |   Yes   | If enabled with RECVHOPLIMIT      |
00617  * | SOCKET_IPV6_PKTINFO          | ns_in6_pktinfo_t |     No          |   Yes   | If enabled with RECVPKTINFO       |
00618  * | SOCKET_IPV6_RECVPKTINFO      | bool             |     Yes         |   No    | No                                |
00619  * | SOCKET_IPV6_RECVHOPLIMIT     | bool             |     Yes         |   No    | No                                |
00620  * | SOCKET_IPV6_RECVTCLASS       | bool             |     Yes         |   No    | No                                |
00621  * | SOCKET_BROADCAST_PAN         | int8_t           |     Yes         |   No    | No                                |
00622  * | SOCKET_LINK_LAYER_SECURITY   | int8_t           |     Yes         |   No    | No                                |
00623  * | SOCKET_INTERFACE_SELECT      | int8_t           |     Yes         |   No    | No                                |
00624  *
00625  *
00626  */
00627 
00628 ///@}
00629 
00630 /**
00631  * \brief Set an option for a socket
00632  *
00633  * Used to specify miscellaneous options for a socket. Supported levels and
00634  * names defined above.
00635  *
00636  * \param socket The socket ID.
00637  * \param level The protocol level.
00638  * \param opt_name The option name (interpretation depends on level). See \ref OPTNAMES_IPV6.
00639  * \param opt_value A pointer to value for the specified option.
00640  * \param opt_len Size of the data pointed to by the value.
00641  *
00642  * \return 0 on success.
00643  * \return -1 invalid socket ID.
00644  * \return -2 invalid/unsupported option.
00645  * \return -3 invalid option value.
00646  */
00647 int8_t socket_setsockopt(int8_t socket, uint8_t level, uint8_t opt_name, const void *opt_value, uint16_t opt_len);
00648 
00649 /**
00650  * \brief Get an option for a socket.
00651  *
00652  * Used to read miscellaneous options for a socket. Supported levels and
00653  * names defined above. If the buffer is smaller than the option, the output
00654  * is silently truncated; otherwise opt_len is modified to indicate the actual
00655  * length.
00656  *
00657  * \param socket The socket ID.
00658  * \param level The protocol level.
00659  * \param opt_name The option name (interpretation depends on level). See \ref OPTNAMES_IPV6.
00660  * \param opt_value A pointer to output buffer.
00661  * \param opt_len A pointer to length of output buffer; updated on exit.
00662  *
00663  * \return 0 on success.
00664  * \return -1 invalid socket ID.
00665  * \return -2 invalid/unsupported option.
00666  */
00667 int8_t socket_getsockopt(int8_t socket, uint8_t level, uint8_t opt_name, void *opt_value, uint16_t *opt_len);
00668 
00669 #ifdef __cplusplus
00670 }
00671 #endif
00672 #endif /*_NS_SOCKET_H*/