Mbed OS Reference
Loading...
Searching...
No Matches
InternetDatagramSocket.h
1/** \addtogroup NetSocket */
2/** @{*/
3/* InternetDatagramSocket
4 * Copyright (c) 2015 ARM Limited
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#ifndef INTERNETDATAGRAMSOCKET_H
21#define INTERNETDATAGRAMSOCKET_H
22
23#include "netsocket/InternetSocket.h"
26#include "rtos/EventFlags.h"
27
28
29/** InternetDatagramSocket socket implementation.
30 */
32public:
33 /** Send data to the specified address.
34 *
35 * By default, sendto blocks until data is sent. If socket is set to
36 * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
37 * immediately.
38 *
39 * It uses sendto_control with zero ancillary data
40 * @param address The SocketAddress of the remote host.
41 * @param data Buffer of data to send to the host.
42 * @param size Size of the buffer in bytes.
43 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
44 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
45 * and send cannot be performed immediately.
46 * @retval int Other negative error codes for stack-related failures.
47 * See \ref NetworkStack::socket_send.
48 */
50 const void *data, nsapi_size_t size) override;
51
52 /** Receive a datagram and store the source address in address if it's not NULL.
53 *
54 * By default, recvfrom blocks until a datagram is received. If socket is set to
55 * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
56 * is returned.
57 *
58 * @note If the datagram is larger than the buffer, the excess data is silently discarded.
59 *
60 * @note If socket is connected, only packets coming from connected peer address
61 * are accepted.
62 *
63 * @note recvfrom() is allowed write to address and data buffers even if error occurs.
64 * It uses recvfrom_control with zero ancillary data
65 * @param address Destination for the source address or NULL.
66 * @param data Destination buffer for RAW data to be received from the host.
67 * @param size Size of the buffer in bytes.
68 * @retval Number of received bytes on success.
69 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
70 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
71 * and send cannot be performed immediately.
72 * @retval Other negative error codes for stack-related failures.
73 * See \ref NetworkStack::socket_recv.
74 */
76 void *data, nsapi_size_t size) override;
77
78 /** Send datagram and ancillary data to the specified address.
79 *
80 * By default, sendto blocks until data is sent. If socket is set to
81 * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
82 * immediately.
83 *
84 * @param address The SocketAddress of the remote host.
85 * @param data Buffer of data to send to the host.
86 * @param size Size of the buffer in bytes.
87 * @param control Control data, for instance a populated #nsapi_pktinfo structure.
88 * @param control_size Size of \c control in bytes.
89 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
90 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
91 * and send cannot be performed immediately.
92 * @retval int Other negative error codes for stack-related failures.
93 * See \ref NetworkStack::socket_send.
94 */
96 const void *data, nsapi_size_t size,
97 nsapi_msghdr_t *control, nsapi_size_t control_size) override;
98
99
100 /** Receive a datagram with ancillary data and store the source address in address if it's not NULL.
101 *
102 * By default, recvfrom blocks until a datagram is received. If socket is set to
103 * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
104 * is returned.
105 *
106 * Ancillary data is stored into \c control. The caller needs to allocate a buffer
107 * that is large enough to contain the data they want to receive, then pass the pointer in
108 * through the \c control member. The data will be filled into \c control, beginning with a header
109 * specifying what data was received. See #MsgHeaderIterator for how to parse this data.
110 *
111 * @note If the datagram is larger than the buffer, the excess data is silently discarded.
112 *
113 * @note If socket is connected, only packets coming from connected peer address
114 * are accepted.
115 *
116 * @note recvfrom_control() is allowed write to address and data buffers even if error occurs.
117 *
118 * @param address Destination for the source address or NULL.
119 * @param data Destination buffer for RAW data to be received from the host.
120 * @param size Size of the buffer in bytes.
121 * @param control Caller-allocated buffer to store ancillary data.
122 * @param control_size Size of the \c control buffer that the caller has allocated.
123 * @retval Number of received bytes on success.
124 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
125 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
126 * and send cannot be performed immediately.
127 * @retval Other negative error codes for stack-related failures.
128 * See \ref NetworkStack::socket_recv.
129 */
131 void *data, nsapi_size_t size,
132 nsapi_msghdr_t *control, nsapi_size_t control_size) override;
133
134 /** Set the remote address for next send() call and filtering
135 * of incoming packets. To reset the address, zero initialized
136 * SocketAddress must be in the address parameter.
137 *
138 * @param address The SocketAddress of the remote host.
139 * @return NSAPI_ERROR_OK on success.
140 */
141 nsapi_error_t connect(const SocketAddress &address) override;
142
143 /** Send a raw data to connected remote address.
144 *
145 * By default, send blocks until all data is sent. If socket is set to
146 * nonblocking or times out, a partial amount can be written.
147 * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
148 *
149 * @note The socket must be connected to a remote host before send() call.
150 *
151 * @param data Buffer of data to send to the host.
152 * @param size Size of the buffer in bytes.
153 * @retval Number of sent bytes on success.
154 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
155 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
156 * and send cannot be performed immediately.
157 * @retval NSAPI_ERROR_NO_ADDRESS if the address was not set with connect().
158 * @retval Other negative error codes for stack-related failures.
159 * See \ref NetworkStack::socket_send.
160 */
161 nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override;
162
163 /** Receive data from a socket.
164 *
165 * This is equivalent to calling recvfrom(NULL, data, size).
166 *
167 * By default, recv blocks until some data is received. If socket is set to
168 * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
169 * indicate no data.
170 *
171 * @note recv() is allowed write to data buffer even if error occurs.
172 *
173 * @param data Pointer to buffer for data received from the host.
174 * @param size Size of the buffer in bytes.
175 * @retval Number of received bytes on success.
176 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
177 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
178 * and send cannot be performed immediately.
179 * @retval Other negative error codes for stack-related failures.
180 * See \ref NetworkStack::socket_recv.
181 */
182 nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override;
183
184 /** Not implemented for InternetDatagramSocket.
185 *
186 * @param error Not used.
187 * @return NSAPI_ERROR_UNSUPPORTED
188 */
189 Socket *accept(nsapi_error_t *error = nullptr) override;
190
191 /** Not implemented for InternetDatagramSocket.
192 *
193 * @param backlog Not used.
194 * @return NSAPI_ERROR_UNSUPPORTED
195 */
196 nsapi_error_t listen(int backlog = 1) override;
197#if !defined(DOXYGEN_ONLY)
198
199protected:
200
201 /** Create an uninitialized socket.
202 *
203 * @note Must call open to initialize the socket on a network stack.
204 */
205 InternetDatagramSocket() = default;
206
207#endif //!defined(DOXYGEN_ONLY)
208};
209
210
211#endif
212
213/** @}*/
Network Interface base class.
NetworkStack class.
InternetDatagramSocket socket implementation.
nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override
Send a raw data to connected remote address.
nsapi_size_or_error_t sendto_control(const SocketAddress &address, const void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size) override
Send datagram and ancillary data to the specified address.
nsapi_error_t connect(const SocketAddress &address) override
Set the remote address for next send() call and filtering of incoming packets.
nsapi_error_t listen(int backlog=1) override
Not implemented for InternetDatagramSocket.
nsapi_size_or_error_t sendto(const SocketAddress &address, const void *data, nsapi_size_t size) override
Send data to the specified address.
Socket * accept(nsapi_error_t *error=nullptr) override
Not implemented for InternetDatagramSocket.
nsapi_size_or_error_t recvfrom_control(SocketAddress *address, void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size) override
Receive a datagram with ancillary data and store the source address in address if it's not NULL.
nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override
Receive data from a socket.
nsapi_size_or_error_t recvfrom(SocketAddress *address, void *data, nsapi_size_t size) override
Receive a datagram and store the source address in address if it's not NULL.
Socket implementation that uses IP network stack.
Representation of an IP (v4 or v6) address and port pair.
Socket interface.
Definition Socket.h:41
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
signed int nsapi_error_t
Type used to represent error codes.
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
MBED_NORETURN void error(const char *format,...) MBED_PRINTF(1
To generate a fatal compile-time error, you can use the pre-processor error directive.
Header structure for control info.