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.
Fork of mbed-os by
NetworkStack.h
00001 00002 /** \addtogroup netsocket */ 00003 /** @{*/ 00004 /* NetworkStack 00005 * Copyright (c) 2015 ARM Limited 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 */ 00019 00020 #ifndef NETWORK_STACK_H 00021 #define NETWORK_STACK_H 00022 00023 #include "nsapi_types.h" 00024 #include "netsocket/SocketAddress.h" 00025 #include "netsocket/NetworkInterface.h" 00026 00027 00028 /** NetworkStack class 00029 * 00030 * Common interface that is shared between hardware that 00031 * can connect to a network over IP. By implementing the 00032 * NetworkStack, a network stack can be used as a target 00033 * for instantiating network sockets. 00034 */ 00035 class NetworkStack 00036 { 00037 public: 00038 virtual ~NetworkStack() {}; 00039 00040 /** Get the local IP address 00041 * 00042 * @return Null-terminated representation of the local IP address 00043 * or null if not yet connected 00044 */ 00045 virtual const char *get_ip_address() = 0; 00046 00047 /** Translates a hostname to an IP address 00048 * 00049 * The hostname may be either a domain name or an IP address. If the 00050 * hostname is an IP address, no network transactions will be performed. 00051 * 00052 * If no stack-specific DNS resolution is provided, the hostname 00053 * will be resolve using a UDP socket on the stack. 00054 * 00055 * @param host Hostname to resolve 00056 * @param address Destination for the host SocketAddress 00057 * @return 0 on success, negative error code on failure 00058 */ 00059 virtual int gethostbyname(const char *host, SocketAddress *address); 00060 00061 /** Translates a hostname to an IP address with specific version 00062 * 00063 * The hostname may be either a domain name or an IP address. If the 00064 * hostname is an IP address, no network transactions will be performed. 00065 * 00066 * If no stack-specific DNS resolution is provided, the hostname 00067 * will be resolve using a UDP socket on the stack. 00068 * 00069 * @param host Hostname to resolve 00070 * @param address Destination for the host SocketAddress 00071 * @param version IP version of address to resolve 00072 * @return 0 on success, negative error code on failure 00073 */ 00074 virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version); 00075 00076 /** Add a domain name server to list of servers to query 00077 * 00078 * @param addr Destination for the host address 00079 * @return 0 on success, negative error code on failure 00080 */ 00081 virtual int add_dns_server(const SocketAddress &address); 00082 00083 /* Set stack-specific stack options 00084 * 00085 * The setstackopt allow an application to pass stack-specific hints 00086 * to the underlying stack. For unsupported options, 00087 * NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified. 00088 * 00089 * @param level Stack-specific protocol level 00090 * @param optname Stack-specific option identifier 00091 * @param optval Option value 00092 * @param optlen Length of the option value 00093 * @return 0 on success, negative error code on failure 00094 */ 00095 virtual int setstackopt(int level, int optname, const void *optval, unsigned optlen); 00096 00097 /* Get stack-specific stack options 00098 * 00099 * The getstackopt allow an application to retrieve stack-specific hints 00100 * from the underlying stack. For unsupported options, 00101 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00102 * 00103 * @param level Stack-specific protocol level 00104 * @param optname Stack-specific option identifier 00105 * @param optval Destination for option value 00106 * @param optlen Length of the option value 00107 * @return 0 on success, negative error code on failure 00108 */ 00109 virtual int getstackopt(int level, int optname, void *optval, unsigned *optlen); 00110 00111 protected: 00112 friend class Socket; 00113 friend class UDPSocket; 00114 friend class TCPSocket; 00115 friend class TCPServer; 00116 00117 /** Opens a socket 00118 * 00119 * Creates a network socket and stores it in the specified handle. 00120 * The handle must be passed to following calls on the socket. 00121 * 00122 * A stack may have a finite number of sockets, in this case 00123 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00124 * 00125 * @param handle Destination for the handle to a newly created socket 00126 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP 00127 * @return 0 on success, negative error code on failure 00128 */ 00129 virtual int socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) = 0; 00130 00131 /** Close the socket 00132 * 00133 * Closes any open connection and deallocates any memory associated 00134 * with the socket. 00135 * 00136 * @param handle Socket handle 00137 * @return 0 on success, negative error code on failure 00138 */ 00139 virtual int socket_close(nsapi_socket_t handle) = 0; 00140 00141 /** Bind a specific address to a socket 00142 * 00143 * Binding a socket specifies the address and port on which to recieve 00144 * data. If the IP address is zeroed, only the port is bound. 00145 * 00146 * @param handle Socket handle 00147 * @param address Local address to bind 00148 * @return 0 on success, negative error code on failure. 00149 */ 00150 virtual int socket_bind(nsapi_socket_t handle, const SocketAddress &address) = 0; 00151 00152 /** Listen for connections on a TCP socket 00153 * 00154 * Marks the socket as a passive socket that can be used to accept 00155 * incoming connections. 00156 * 00157 * @param handle Socket handle 00158 * @param backlog Number of pending connections that can be queued 00159 * simultaneously 00160 * @return 0 on success, negative error code on failure 00161 */ 00162 virtual int socket_listen(nsapi_socket_t handle, int backlog) = 0; 00163 00164 /** Connects TCP socket to a remote host 00165 * 00166 * Initiates a connection to a remote server specified by the 00167 * indicated address. 00168 * 00169 * @param handle Socket handle 00170 * @param address The SocketAddress of the remote host 00171 * @return 0 on success, negative error code on failure 00172 */ 00173 virtual int socket_connect(nsapi_socket_t handle, const SocketAddress &address) = 0; 00174 00175 /** Accepts a connection on a TCP socket 00176 * 00177 * The server socket must be bound and set to listen for connections. 00178 * On a new connection, creates a network socket and stores it in the 00179 * specified handle. The handle must be passed to following calls on 00180 * the socket. 00181 * 00182 * A stack may have a finite number of sockets, in this case 00183 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00184 * 00185 * This call is non-blocking. If accept would block, 00186 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00187 * 00188 * @param server Socket handle to server to accept from 00189 * @param handle Destination for a handle to the newly created socket 00190 * @param address Destination for the remote address or NULL 00191 * @return 0 on success, negative error code on failure 00192 */ 00193 virtual int socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address=0) = 0; 00194 00195 /** Send data over a TCP socket 00196 * 00197 * The socket must be connected to a remote host. Returns the number of 00198 * bytes sent from the buffer. 00199 * 00200 * This call is non-blocking. If send would block, 00201 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00202 * 00203 * @param handle Socket handle 00204 * @param data Buffer of data to send to the host 00205 * @param size Size of the buffer in bytes 00206 * @return Number of sent bytes on success, negative error 00207 * code on failure 00208 */ 00209 virtual int socket_send(nsapi_socket_t handle, const void *data, unsigned size) = 0; 00210 00211 /** Receive data over a TCP socket 00212 * 00213 * The socket must be connected to a remote host. Returns the number of 00214 * bytes received into the buffer. 00215 * 00216 * This call is non-blocking. If recv would block, 00217 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00218 * 00219 * @param handle Socket handle 00220 * @param data Destination buffer for data received from the host 00221 * @param size Size of the buffer in bytes 00222 * @return Number of received bytes on success, negative error 00223 * code on failure 00224 */ 00225 virtual int socket_recv(nsapi_socket_t handle, void *data, unsigned size) = 0; 00226 00227 /** Send a packet over a UDP socket 00228 * 00229 * Sends data to the specified address. Returns the number of bytes 00230 * sent from the buffer. 00231 * 00232 * This call is non-blocking. If sendto would block, 00233 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00234 * 00235 * @param handle Socket handle 00236 * @param address The SocketAddress of the remote host 00237 * @param data Buffer of data to send to the host 00238 * @param size Size of the buffer in bytes 00239 * @return Number of sent bytes on success, negative error 00240 * code on failure 00241 */ 00242 virtual int socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, unsigned size) = 0; 00243 00244 /** Receive a packet over a UDP socket 00245 * 00246 * Receives data and stores the source address in address if address 00247 * is not NULL. Returns the number of bytes received into the buffer. 00248 * 00249 * This call is non-blocking. If recvfrom would block, 00250 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00251 * 00252 * @param handle Socket handle 00253 * @param address Destination for the source address or NULL 00254 * @param data Destination buffer for data received from the host 00255 * @param size Size of the buffer in bytes 00256 * @return Number of received bytes on success, negative error 00257 * code on failure 00258 */ 00259 virtual int socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, void *buffer, unsigned size) = 0; 00260 00261 /** Register a callback on state change of the socket 00262 * 00263 * The specified callback will be called on state changes such as when 00264 * the socket can recv/send/accept successfully and on when an error 00265 * occurs. The callback may also be called spuriously without reason. 00266 * 00267 * The callback may be called in an interrupt context and should not 00268 * perform expensive operations such as recv/send calls. 00269 * 00270 * @param handle Socket handle 00271 * @param callback Function to call on state change 00272 * @param data Argument to pass to callback 00273 */ 00274 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) = 0; 00275 00276 /* Set stack-specific socket options 00277 * 00278 * The setsockopt allow an application to pass stack-specific hints 00279 * to the underlying stack. For unsupported options, 00280 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. 00281 * 00282 * @param handle Socket handle 00283 * @param level Stack-specific protocol level 00284 * @param optname Stack-specific option identifier 00285 * @param optval Option value 00286 * @param optlen Length of the option value 00287 * @return 0 on success, negative error code on failure 00288 */ 00289 virtual int setsockopt(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen); 00290 00291 /* Get stack-specific socket options 00292 * 00293 * The getstackopt allow an application to retrieve stack-specific hints 00294 * from the underlying stack. For unsupported options, 00295 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00296 * 00297 * @param handle Socket handle 00298 * @param level Stack-specific protocol level 00299 * @param optname Stack-specific option identifier 00300 * @param optval Destination for option value 00301 * @param optlen Length of the option value 00302 * @return 0 on success, negative error code on failure 00303 */ 00304 virtual int getsockopt(nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen); 00305 }; 00306 00307 00308 /** Convert a raw nsapi_stack_t object into a C++ NetworkStack object 00309 * 00310 * @param stack Reference to an object that can be converted to a stack 00311 * - A raw nsapi_stack_t object 00312 * - A reference to a network stack 00313 * - A reference to a network interface 00314 * @return Reference to the underlying network stack 00315 */ 00316 NetworkStack *nsapi_create_stack(nsapi_stack_t *stack); 00317 NetworkStack *nsapi_create_stack(NetworkStack *stack); 00318 00319 template <typename IF> 00320 NetworkStack *nsapi_create_stack(IF *iface) 00321 { 00322 return nsapi_create_stack(static_cast<NetworkInterface *>(iface)->get_stack()); 00323 } 00324 00325 00326 #endif 00327 00328 /** @}*/
Generated on Tue Jul 12 2022 13:16:01 by
1.7.2
