Mbed OS Reference
Loading...
Searching...
No Matches
Socket.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** @file Socket.h Abstract Socket interface */
19/** @addtogroup NetSocket
20 * Mbed OS Socket API
21 * @{ */
22
23#ifndef SOCKET_H
24#define SOCKET_H
25
27#include "Callback.h"
28
29/** Socket interface.
30 *
31 * This class defines the Mbed OS Socket API.
32 * Socket is an abstract interface for communicating with remote endpoints.
33 *
34 * This API is intended for applications and libraries instead of
35 * protocol-specific implementations. For example, TCPSocket
36 * and UDPSocket are implementations of the Socket interface.
37 * Socket API is intentionally not protocol-specific, and allows all protocol
38 * to provide the same API regardless of the underlying transport mechanism.
39 */
40class Socket {
41public:
42 /** Destroy a socket.
43 *
44 * Closes socket if the socket is still open.
45 */
46 virtual ~Socket() = default;
47
48 /** Closes the socket.
49 *
50 * Closes any open connection and deallocates any memory associated
51 * with the socket. Called from destructor if socket is not closed.
52 *
53 * @return NSAPI_ERROR_OK on success.
54 * Negative subclass-dependent error code on failure.
55 */
56 virtual nsapi_error_t close() = 0;
57
58 /** Connects socket to a remote address.
59 *
60 * Attempts to make connection on connection-mode protocol or set or reset
61 * the peer address on connectionless protocol.
62 *
63 * Connectionless protocols also use the connected address to filter
64 * incoming packets for recv() and recvfrom() calls.
65 *
66 * To reset the peer address, there must be zero initialized(default constructor) SocketAddress
67 * objects in the address parameter.
68 *
69 * @note If connect() fails it is recommended to close the Socket and create
70 * a new one before attempting to reconnect.
71 *
72 * @param address The SocketAddress of the remote peer.
73 * @return NSAPI_ERROR_OK on success.
74 * Negative subclass-dependent error code on failure.
75 */
76 virtual nsapi_error_t connect(const SocketAddress &address) = 0;
77
78 /** Send data on a socket
79 *
80 * The socket must be connected to a remote host before send() call.
81 * Returns the number of bytes sent from the buffer.
82 * In case of connectionless socket, sends data to pre-specified remote.
83 *
84 * By default, send blocks until all data is sent. If socket is set to
85 * non-blocking or times out, a partial amount can be written.
86 * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
87 *
88 * @param data Buffer of data to send to the host.
89 * @param size Size of the buffer in bytes.
90 * @return NSAPI_ERROR_OK on success.
91 * Negative subclass-dependent error code on failure.
92 */
93 virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size) = 0;
94
95 /** Receive data from a socket.
96 *
97 * Receive data from connected socket, or in the case of connectionless socket,
98 * equivalent to calling recvfrom(NULL, data, size).
99 *
100 * If socket is connected, only packets coming from connected peer address
101 * are accepted.
102 *
103 * @note recv() is allowed write to data buffer even if an error occurs.
104 *
105 * By default, recv blocks until some data is received. If socket is set to
106 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
107 * indicate no data.
108 *
109 * @param data Destination buffer for data received from the host.
110 * @param size Size of the buffer in bytes.
111 * @return Number of received bytes on success, negative, subclass-dependent
112 * error code on failure. If no data is available to be received
113 * and the peer has performed an orderly shutdown,
114 * recv() returns 0.
115 */
116 virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size) = 0;
117
118 /** Send a message on a socket.
119 *
120 * The sendto() function sends a message through a connection-mode or connectionless-mode socket.
121 * If the socket is a connectionless-mode socket, the message is sent to the address specified.
122 * If the socket is a connected-mode socket, address is ignored.
123 *
124 * By default, sendto blocks until data is sent. If socket is set to
125 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
126 * immediately.
127 *
128 * @param address Remote address
129 * @param data Buffer of data to send to the host
130 * @param size Size of the buffer in bytes
131 * @return Number of sent bytes on success, negative subclass-dependent error
132 * code on failure
133 */
135 const void *data, nsapi_size_t size) = 0;
136
137 /** Receive a data from a socket
138 *
139 * Receives a data and stores the source address in address if address
140 * is not NULL. Returns the number of bytes written into the buffer.
141 *
142 * If socket is connected, only packets coming from connected peer address
143 * are accepted.
144 *
145 * @note recvfrom() is allowed write to address and data buffers even if error occurs.
146 *
147 * By default, recvfrom blocks until a datagram is received. If socket is set to
148 * non-blocking or times out with no data, NSAPI_ERROR_WOULD_BLOCK
149 * is returned.
150 *
151 * @param address Destination for the source address or NULL
152 * @param data Destination buffer for datagram received from the host
153 * @param size Size of the buffer in bytes
154 * @return Number of received bytes on success, negative subclass-dependent
155 * error code on failure
156 */
158 void *data, nsapi_size_t size) = 0;
159
160 /** Send a message on a socket.
161 *
162 * The sendto_control() function sends a message through a connection-mode or connectionless-mode socket.
163 * If the socket is a connectionless-mode socket, the message is sent to the address specified.
164 * If the socket is a connected-mode socket, address is ignored.
165 *
166 * Additional control information can be passed to the stack for specific operations.
167 *
168 * By default, sendto blocks until data is sent. If socket is set to
169 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
170 * immediately.
171 *
172 * @param address Remote address
173 * @param data Buffer of data to send to the host
174 * @param size Size of the buffer in bytes
175 * @param control Control data, for instance a populated #nsapi_pktinfo structure.
176 * @param control_size Size of \c control in bytes.
177 * @return Number of sent bytes on success, negative subclass-dependent error
178 * code on failure
179 */
181 const void *data, nsapi_size_t size,
182 nsapi_msghdr_t *control, nsapi_size_t control_size) = 0;
183
184
185 /** Receive a data from a socket
186 *
187 * Receives a data and stores the source address in address if address
188 * is not NULL. Returns the number of bytes written into the buffer.
189 *
190 * If socket is connected, only packets coming from connected peer address
191 * are accepted.
192 *
193 * Ancillary data is stored into \c control. The caller needs to allocate a buffer
194 * that is large enough to contain the data they want to receive, then pass the pointer in
195 * through the \c control member. The data will be filled into \c control, beginning with a header
196 * specifying what data was received. See #MsgHeaderIterator for how to parse this data.
197 *
198 * @note recvfrom_control() is allowed write to address and data buffers even if error occurs.
199 *
200 * By default, recvfrom blocks until a datagram is received. If socket is set to
201 * non-blocking or times out with no data, NSAPI_ERROR_WOULD_BLOCK
202 * is returned.
203 *
204 * @param address Destination for the source address or NULL
205 * @param data Destination buffer for datagram received from the host
206 * @param size Size of the buffer in bytes
207 * @param control Caller-allocated buffer to store ancillary data.
208 * @param control_size Size of the \c control buffer that the caller has allocated.
209 * @return Number of received bytes on success, negative subclass-dependent
210 * error code on failure
211 */
213 void *data, nsapi_size_t size,
214 nsapi_msghdr_t *control, nsapi_size_t control_size) = 0;
215
216 /** Bind a specific address to a socket.
217 *
218 * Binding a socket specifies the address and port on which to receive
219 * data. If the IP address is zeroed, only the port is bound.
220 *
221 * @param address Local address to bind.
222 * @return NSAPI_ERROR_OK on success, negative subclass-dependent error
223 * code on failure.
224 */
225 virtual nsapi_error_t bind(const SocketAddress &address) = 0;
226
227 /** Set blocking or non-blocking mode of the socket.
228 *
229 * Initially all sockets are in blocking mode. In non-blocking mode
230 * blocking operations such as send/recv/accept return
231 * NSAPI_ERROR_WOULD_BLOCK if they cannot continue.
232 *
233 * set_blocking(false) is equivalent to set_timeout(0)
234 * set_blocking(true) is equivalent to set_timeout(-1)
235 *
236 * @param blocking true for blocking mode, false for non-blocking mode.
237 */
238 virtual void set_blocking(bool blocking) = 0;
239
240 /** Set timeout on blocking socket operations.
241 *
242 * Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
243 * is returned if a blocking operation takes longer than the specified
244 * timeout. A timeout of 0 removes the timeout from the socket. A negative
245 * value gives the socket an unbounded timeout.
246 *
247 * set_timeout(0) is equivalent to set_blocking(false)
248 * set_timeout(-1) is equivalent to set_blocking(true)
249 *
250 * @param timeout Timeout in milliseconds
251 */
252 virtual void set_timeout(int timeout) = 0;
253
254 /** Register a callback on state change of the socket.
255 *
256 * The specified callback is called on state changes, such as when
257 * the socket can receive/send/accept successfully and when an error
258 * occurs. The callback may also be called spuriously without reason.
259 *
260 * The callback may be called in an interrupt context and should not
261 * perform expensive operations such as receive/send calls.
262 *
263 * Note! This is not intended as a replacement for a poll or attach-like
264 * asynchronous API, but rather as a building block for constructing
265 * such functionality. The exact timing of the registered function
266 * is not guaranteed and susceptible to change.
267 *
268 * @param func Function to call on state change.
269 */
270 virtual void sigio(mbed::Callback<void()> func) = 0;
271
272 /** Set socket options.
273 *
274 * setsockopt() allows an application to pass stack-specific options
275 * to the underlying stack using stack-specific level and option names,
276 * or to request generic options using levels from nsapi_socket_level_t.
277 *
278 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
279 * and the socket is unmodified.
280 *
281 * @param level Stack-specific protocol level or nsapi_socket_level_t.
282 * @param optname Level-specific option name.
283 * @param optval Option value.
284 * @param optlen Length of the option value.
285 * @retval NSAPI_ERROR_OK on success.
286 * @retval NSAPI_ERROR_NO_SOCKET if socket is not open.
287 * @retval Negative error code on failure, see #NetworkStack::setsockopt
288 */
289 virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen) = 0;
290
291 /** Get socket options.
292 *
293 * getsockopt() allows an application to retrieve stack-specific options
294 * from the underlying stack using stack-specific level and option names,
295 * or to request generic options using levels from nsapi_socket_level_t.
296 *
297 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
298 * and the socket is unmodified.
299 *
300 * @param level Stack-specific protocol level or nsapi_socket_level_t.
301 * @param optname Level-specific option name.
302 * @param optval Destination for option value.
303 * @param optlen Length of the option value.
304 * @retval NSAPI_ERROR_OK on success.
305 * @retval NSAPI_ERROR_NO_SOCKET if socket is not open.
306 * @retval Negative error code on failure, see #NetworkStack::getsockopt
307 */
308 virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen) = 0;
309
310 /** Accepts a connection on a socket.
311 *
312 * The server socket must be bound and set to listen for connections.
313 * On a new connection, returns connected network socket to call close()
314 * that deallocates the resources. Referencing a returned pointer after a close()
315 * call is not allowed and leads to undefined behavior.
316 *
317 * By default, accept blocks until incoming connection occurs. If socket is set to
318 * non-blocking or times out, error is set to NSAPI_ERROR_WOULD_BLOCK.
319 *
320 * @param error Pointer to storage of the error value or NULL.
321 * @return Pointer to a socket.
322 */
323 virtual Socket *accept(nsapi_error_t *error = NULL) = 0;
324
325 /** Listen for incoming connections.
326 *
327 * Marks the socket as a passive socket that can be used to accept
328 * incoming connections.
329 *
330 * @param backlog Number of pending connections that can be queued
331 * simultaneously, defaults to 1.
332 * @return NSAPI_ERROR_OK on success, negative error code on failure.
333 */
334 virtual nsapi_error_t listen(int backlog = 1) = 0;
335
336 /** Get the remote-end peer associated with this socket.
337 *
338 * Copy the remote peer address to a SocketAddress structure pointed by
339 * address parameter. Socket must be connected to have a peer address
340 * associated.
341 *
342 * @param address Pointer to SocketAddress structure.
343 * @retval NSAPI_ERROR_OK on success.
344 * @retval NSAPI_ERROR_NO_SOCKET if socket is not connected.
345 * @retval NSAPI_ERROR_NO_CONNECTION if the remote peer was not set.
346 */
348};
349
350
351#endif
352
353/** @}*/
SocketAddress class.
SocketAddress class.
Definition: SocketAddress.h:37
Socket interface.
Definition: Socket.h:40
virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen)=0
Set socket options.
virtual nsapi_size_or_error_t recvfrom_control(SocketAddress *address, void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size)=0
Receive a data from a socket.
virtual 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)=0
Send a message on a socket.
virtual nsapi_error_t close()=0
Closes the socket.
virtual nsapi_error_t bind(const SocketAddress &address)=0
Bind a specific address to a socket.
virtual Socket * accept(nsapi_error_t *error=NULL)=0
Accepts a connection on a socket.
virtual void sigio(mbed::Callback< void()> func)=0
Register a callback on state change of the socket.
virtual ~Socket()=default
Destroy a socket.
virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, void *data, nsapi_size_t size)=0
Receive a data from a socket.
virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen)=0
Get socket options.
virtual nsapi_error_t getpeername(SocketAddress *address)=0
Get the remote-end peer associated with this socket.
virtual nsapi_error_t connect(const SocketAddress &address)=0
Connects socket to a remote address.
virtual nsapi_size_or_error_t sendto(const SocketAddress &address, const void *data, nsapi_size_t size)=0
Send a message on a socket.
virtual nsapi_error_t listen(int backlog=1)=0
Listen for incoming connections.
virtual void set_timeout(int timeout)=0
Set timeout on blocking socket operations.
virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size)=0
Send data on a socket.
virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size)=0
Receive data from a socket.
virtual void set_blocking(bool blocking)=0
Set blocking or non-blocking mode of the socket.
Callback class based on template specialization.
Definition: Callback.h:53
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
Definition: nsapi_types.h:153
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
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.
Definition: nsapi_types.h:416