Base class for IP Based Networking Libraries
Dependencies: DnsQuery
Dependents: TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more
For a complete getting started guide see the wiki...
Network Socket API
The Network Socket API provides a common interface for using sockets on network devices. The API provides a simple class-based interface that should be familiar to users experienced with other socket APIs. Additionally, the API provides a simple interface for implementing network devices, making it easy to connect hardware agnostic programs to new devices.
Network Interfaces
The NetworkInterface provides an abstract class for network devices that support sockets. Devices should provide a DeviceInterface class that inherits this interface and adds implementation specific methods for using the device. A NetworkInterface must be provided to a Socket constructor to open a socket on the interface. Currently two subclasses are defined for common devices, EthernetInterface
and WiFiInterface.
Sockets
The Socket class is used for managing network sockets. Once opened, the socket provides a pipe through which data can sent and recieved to a specific endpoint. The socket class can be instantiated as either a TCPSocket
or a UDPSocket
which defines the protocol used for the connection.
Revision 80:9c6673c93082, committed 2016-04-05
- Comitter:
- Christopher Haster
- Date:
- Tue Apr 05 12:02:56 2016 -0500
- Parent:
- 79:43a7e8c0d6cc
- Child:
- 81:1600369a29dd
- Commit message:
- Added support for DNS resolution
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DnsQuery.lib Tue Apr 05 12:02:56 2016 -0500 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/NetworkSocketAPI/code/DnsQuery/#248f32a9c48d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetworkInterface.cpp Tue Apr 05 12:02:56 2016 -0500 @@ -0,0 +1,22 @@ +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DnsQuery.h" + +int NetworkInterface::gethostbyname(const char *name, char *dest) +{ + return dnsQuery(this, name, dest); +}
--- a/NetworkInterface.h Tue Apr 05 10:40:34 2016 -0500 +++ b/NetworkInterface.h Tue Apr 05 12:02:56 2016 -0500 @@ -18,6 +18,7 @@ #define NETWORK_INTERFACE_H #include "FunctionPointer.h" +#include "SocketAddress.h" /** * @enum ns_error_t @@ -71,10 +72,10 @@ /** Looks up the specified host's IP address /param name Hostname to lookup - /param port Optional port to pass to SocketAddress - /return Resolved IP address, SocketAddress with null IP address on failure + /param dest Destination for IP address, must have space for SocketAddress::IP_SIZE + /return 0 on success, negative on failure */ - virtual SocketAddress gethostbyname(const char *name, uint16_t port=0); + virtual int gethostbyname(const char *name, char *dest); protected: friend class Socket; @@ -132,7 +133,7 @@ one time [Default: 1] \return 0 on success, negative on failure */ - virtual int socket_listen(void *handle, int backlog=1) = 0; + virtual int socket_listen(void *handle, int backlog) = 0; /** Accept a new connection. \param handle Socket handle @@ -146,13 +147,13 @@ \param address SocketAddress to connect to \return 0 on success, negative on failure */ - virtual int socket_connect(void *handle, SocketAddress address) = 0; + virtual int socket_connect(void *handle, const SocketAddress &address) = 0; /** Check if the socket is connected \param handle Socket handle \return true if connected, false otherwise */ - virtual bool socket_is_connected(void *handle); + virtual bool socket_is_connected(void *handle) = 0; /** Send data to the remote host \param handle Socket handle @@ -177,7 +178,7 @@ \param size The length of the packet to be sent \return the number of written bytes on success, negative on failure */ - virtual int socket_sendto(void *handle, SocketAddress address, const void *data, unsigned size) = 0; + virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size) = 0; /** Receive a packet from a remote endpoint \param handle Socket handle
--- a/SocketAddress.cpp Tue Apr 05 10:40:34 2016 -0500 +++ b/SocketAddress.cpp Tue Apr 05 12:02:56 2016 -0500 @@ -15,8 +15,20 @@ */ #include "SocketAddress.h" +#include "NetworkInterface.h" #include <string.h> +SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t port) +{ + int err = iface->gethostbyname(host, _ip_address); + set_port(port); + + if (err) { + _ip_address[0] = '\0'; + _port = 0; + } +} + SocketAddress::SocketAddress(const char *addr, uint16_t port) { set_ip_address(addr); @@ -42,6 +54,9 @@ const char *SocketAddress::get_ip_address() const { + if (!_ip_address[0]) { + return 0; + } return _ip_address; }
--- a/SocketAddress.h Tue Apr 05 10:40:34 2016 -0500 +++ b/SocketAddress.h Tue Apr 05 12:02:56 2016 -0500 @@ -19,40 +19,60 @@ #include <stdint.h> +// Predeclared classes +class NetworkInterface; + /** * A general socket address composed of the IP address and port */ class SocketAddress { public: + /** Maximum size of IP address + */ + static const int IP_SIZE = 16; + + /** SocketAddress construction using DNS resolution + /param iface NetworkInterface to use for DNS resolution + /param addr Null-terminated hostname that will be resolved + /param port 16-bit port + /note on failure, IP address and port will be set to null + */ + SocketAddress(NetworkInterface *iface, const char *addr, uint16_t port = 0); + /** SocketAddress construction - /param addr Null-terminated string representing the IP address - /param port 16-bit port - */ + /param addr Null-terminated IP address + /param port 16-bit port + /note on failure, IP address and port will be set to null + */ SocketAddress(const char *addr = 0, uint16_t port = 0); - SocketAddress(const SocketAddress &); + + /** SocketAddress construction + /param addr SocketAddress to copy + */ + SocketAddress(const SocketAddress &addr); /** Set the IP address - \param addr Null-terminated string representing the IP address + \param addr Null-terminated string representing the IP address */ void set_ip_address(const char *addr); /** Set the port - \param port 16-bit port + \param port 16-bit port */ void set_port(uint16_t port); /** Get the IP address - \return The string representation of the IP Address + \return The string representation of the IP Address */ const char *get_ip_address() const; /** Get the port - \return The 16-bit port + \return The 16-bit port */ uint16_t get_port(void) const; private: - char _ip_address[16]; + char _ip_address[IP_SIZE]; uint16_t _port; };
--- a/TCPServer.h Tue Apr 05 10:40:34 2016 -0500 +++ b/TCPServer.h Tue Apr 05 12:02:56 2016 -0500 @@ -28,7 +28,6 @@ /** TCP Server lifetime */ TCPServer(NetworkInterface *iface); - ~TCPServer(); /** Bind a socket to a specific port \param port The port to listen for incoming connections on
--- a/TCPSocket.cpp Tue Apr 05 10:40:34 2016 -0500 +++ b/TCPSocket.cpp Tue Apr 05 12:02:56 2016 -0500 @@ -21,7 +21,7 @@ { } -int TCPSocket::connect(SocketAddress addr) +int TCPSocket::connect(const SocketAddress &addr) { if (!_socket) { return NSAPI_ERROR_NO_SOCKET; @@ -32,7 +32,7 @@ int TCPSocket::connect(const char *host, uint16_t port) { - SocketAddress addr = _iface->gethostbyname(host, port); + SocketAddress addr(_iface, host, port); if (!addr.get_ip_address()) { return NSAPI_ERROR_DNS_FAILURE; }
--- a/TCPSocket.h Tue Apr 05 10:40:34 2016 -0500 +++ b/TCPSocket.h Tue Apr 05 12:02:56 2016 -0500 @@ -28,7 +28,6 @@ /** TCP socket lifetime */ TCPSocket(NetworkInterface *iface); - ~TCPSocket(); /** Connects this TCP socket to the server \param host The host to connect to. It can either be an IP Address @@ -42,7 +41,7 @@ \param address SocketAddress to connect to \return 0 on success, negative on failure */ - int connect(SocketAddress address); + int connect(const SocketAddress &address); /** Check if the socket is connected \return true if connected, false otherwise
--- a/UDPSocket.cpp Tue Apr 05 10:40:34 2016 -0500 +++ b/UDPSocket.cpp Tue Apr 05 12:02:56 2016 -0500 @@ -30,7 +30,7 @@ return _iface->socket_bind(_socket, port); } -int UDPSocket::sendto(SocketAddress address, const void *data, unsigned size) +int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size) { if (!_socket) { return NSAPI_ERROR_NO_SOCKET; @@ -47,4 +47,3 @@ return _iface->socket_recvfrom(_socket, address, buffer, size); } -
--- a/UDPSocket.h Tue Apr 05 10:40:34 2016 -0500 +++ b/UDPSocket.h Tue Apr 05 12:02:56 2016 -0500 @@ -28,7 +28,6 @@ /** UDPSocket lifetime */ UDPSocket(NetworkInterface *iface); - ~UDPSocket(); /** Bind a UDP Server Socket to a specific port \param port The port to listen for incoming connections on @@ -42,7 +41,7 @@ \param size The length of the packet to be sent \return the number of written bytes on success, negative on failure */ - int sendto(SocketAddress address, const void *data, unsigned size); + int sendto(const SocketAddress &address, const void *data, unsigned size); /** Receive a packet from a remote endpoint \param address Destination for the remote SocketAddress or null