From 141b245dfc4a95b706f6516e20227ee908c1d680 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 13 Mar 2016 17:25:05 -0500 Subject: [PATCH] Added better support for SocketAddress/string addresses/ports --- net/ESP8266Interface/ESP8266Interface.cpp | 2 +- net/ESP8266Interface/ESP8266Interface.h | 10 +++++----- net/LWIPInterface/LWIPInterface.cpp | 8 ++++---- net/LWIPInterface/LWIPInterface.h | 10 +++++----- net/NetworkSocketAPI/NetworkInterface.h | 10 +++++----- net/NetworkSocketAPI/TCPServer.cpp | 16 +++++++++++++-- net/NetworkSocketAPI/TCPServer.h | 19 +++++++++++++++--- net/NetworkSocketAPI/TCPSocket.cpp | 2 +- net/NetworkSocketAPI/UDPSocket.cpp | 24 ++++++++++++++++++++++- net/NetworkSocketAPI/UDPSocket.h | 23 ++++++++++++++++++++++ 10 files changed, 97 insertions(+), 27 deletions(-) diff --git a/net/ESP8266Interface/ESP8266Interface.cpp b/net/ESP8266Interface/ESP8266Interface.cpp index 4beee7ad53..60a1d17f75 100644 --- a/net/ESP8266Interface/ESP8266Interface.cpp +++ b/net/ESP8266Interface/ESP8266Interface.cpp @@ -138,7 +138,7 @@ int ESP8266Interface::socket_get_option(void *handle, int optname, void *optval, return NSAPI_ERROR_UNSUPPORTED; } -int ESP8266Interface::socket_bind(void *handle, int port) +int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address) { return NSAPI_ERROR_UNSUPPORTED; } diff --git a/net/ESP8266Interface/ESP8266Interface.h b/net/ESP8266Interface/ESP8266Interface.h index 21829a4d02..41d89687d5 100644 --- a/net/ESP8266Interface/ESP8266Interface.h +++ b/net/ESP8266Interface/ESP8266Interface.h @@ -96,11 +96,11 @@ protected: virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen); /** Bind a server socket to a specific port - \param handle Socket handle - \param port The port to listen for incoming connections on - \return 0 on success, negative on failure. - */ - virtual int socket_bind(void *handle, int port); + * @param handle Socket handle + * @param address Local address to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + virtual int socket_bind(void *handle, const SocketAddress &address); /** Start listening for incoming connections \param handle Socket handle diff --git a/net/LWIPInterface/LWIPInterface.cpp b/net/LWIPInterface/LWIPInterface.cpp index 2cc0c0873a..5e05daee46 100644 --- a/net/LWIPInterface/LWIPInterface.cpp +++ b/net/LWIPInterface/LWIPInterface.cpp @@ -223,20 +223,20 @@ int LWIPInterface::socket_get_option(void *handle, int optname, void *optval, un return NSAPI_ERROR_UNSUPPORTED; } -int LWIPInterface::socket_bind(void *handle, int port) +int LWIPInterface::socket_bind(void *handle, const SocketAddress &address) { struct lwip_socket *s = (struct lwip_socket *)handle; - ip_addr_t ip_addr = ip_addr_any; + ip_addr_t ip_addr = ip_addr_any; // TODO use address switch (s->proto) { case NSAPI_UDP: - if (udp_bind(s->udp, &ip_addr, port)) { + if (udp_bind(s->udp, &ip_addr, address.get_port())) { return NSAPI_ERROR_DEVICE_ERROR; } return 0; case NSAPI_TCP: - if (tcp_bind(s->tcp, &ip_addr, port)) { + if (tcp_bind(s->tcp, &ip_addr, address.get_port())) { return NSAPI_ERROR_DEVICE_ERROR; } return 0; diff --git a/net/LWIPInterface/LWIPInterface.h b/net/LWIPInterface/LWIPInterface.h index 1cdaafdc78..f2386c5d9d 100644 --- a/net/LWIPInterface/LWIPInterface.h +++ b/net/LWIPInterface/LWIPInterface.h @@ -83,11 +83,11 @@ protected: virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen); /** Bind a server socket to a specific port - \param handle Socket handle - \param port The port to listen for incoming connections on - \return 0 on success, negative on failure. - */ - virtual int socket_bind(void *handle, int port); + * @param handle Socket handle + * @param address Local address to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + virtual int socket_bind(void *handle, const SocketAddress &address); /** Start listening for incoming connections \param handle Socket handle diff --git a/net/NetworkSocketAPI/NetworkInterface.h b/net/NetworkSocketAPI/NetworkInterface.h index da002f6d7e..4f1c2cd139 100644 --- a/net/NetworkSocketAPI/NetworkInterface.h +++ b/net/NetworkSocketAPI/NetworkInterface.h @@ -22,7 +22,7 @@ /** Enum of standardized error codes - * @enum ns_error_t + * @enum nsapi_error_t */ enum nsapi_error_t { NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ @@ -38,9 +38,9 @@ enum nsapi_error_t { }; /** Enum of available options - * @enum ns_opt_t + * @enum nsapi_opt_t */ -enum ns_opt_t { +enum nsapi_opt_t { }; /** Enum of socket protocols @@ -134,10 +134,10 @@ protected: /** Bind a server socket to a specific port * @param handle Socket handle - * @param port The port to listen for incoming connections on + * @param address Local address to listen for incoming connections on * @return 0 on success, negative on failure. */ - virtual int socket_bind(void *handle, int port) = 0; + virtual int socket_bind(void *handle, const SocketAddress &address) = 0; /** Start listening for incoming connections * @param handle Socket handle diff --git a/net/NetworkSocketAPI/TCPServer.cpp b/net/NetworkSocketAPI/TCPServer.cpp index ccfd65056b..b2b7e57a6a 100644 --- a/net/NetworkSocketAPI/TCPServer.cpp +++ b/net/NetworkSocketAPI/TCPServer.cpp @@ -32,12 +32,24 @@ int TCPServer::open(NetworkInterface *iface) } int TCPServer::bind(uint16_t port) +{ + SocketAddress addr(0, port); + return bind(addr); +} + +int TCPServer::bind(const char *address, uint16_t port) +{ + SocketAddress addr(address, port); + return bind(addr); +} + +int TCPServer::bind(const SocketAddress &address) { if (!_socket) { - return NSAPI_ERROR_NO_SOCKET; + return NSAPI_ERROR_NO_SOCKET; } - return _iface->socket_bind(_socket, port); + return _iface->socket_bind(_socket, address); } int TCPServer::listen(int backlog) diff --git a/net/NetworkSocketAPI/TCPServer.h b/net/NetworkSocketAPI/TCPServer.h index 84fbe82c97..17b2a48c7b 100644 --- a/net/NetworkSocketAPI/TCPServer.h +++ b/net/NetworkSocketAPI/TCPServer.h @@ -36,11 +36,24 @@ public: */ virtual int open(NetworkInterface *iface); - /** Bind a socket to a specific port - * @param port The port to listen for incoming connections on - * @return 0 on success, negative on failure + /** Bind a TCP Server to a specific port + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure. */ int bind(uint16_t port); + + /** Bind a TCP Server to a local address + * @param address The null-terminated address to listen for incoming connections on + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const char *address, uint16_t port); + + /** Bind a TCP Server to a local address + * @param address The SocketAddress to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const SocketAddress &address); /** Start listening for incoming connections * @param backlog Number of pending connections that can be queued up at any diff --git a/net/NetworkSocketAPI/TCPSocket.cpp b/net/NetworkSocketAPI/TCPSocket.cpp index 6c19ddc2e1..a5225ce56c 100644 --- a/net/NetworkSocketAPI/TCPSocket.cpp +++ b/net/NetworkSocketAPI/TCPSocket.cpp @@ -43,7 +43,7 @@ int TCPSocket::connect(const SocketAddress &addr) int TCPSocket::connect(const char *host, uint16_t port) { SocketAddress addr(_iface, host, port); - if (!addr.get_ip_address()) { + if (!addr) { return NSAPI_ERROR_DNS_FAILURE; } diff --git a/net/NetworkSocketAPI/UDPSocket.cpp b/net/NetworkSocketAPI/UDPSocket.cpp index 5970d61def..6e210946e9 100644 --- a/net/NetworkSocketAPI/UDPSocket.cpp +++ b/net/NetworkSocketAPI/UDPSocket.cpp @@ -32,12 +32,34 @@ int UDPSocket::open(NetworkInterface *iface) } int UDPSocket::bind(uint16_t port) +{ + SocketAddress addr(0, port); + return bind(addr); +} + +int UDPSocket::bind(const char *address, uint16_t port) +{ + SocketAddress addr(address, port); + return bind(addr); +} + +int UDPSocket::bind(const SocketAddress &address) { if (!_socket) { return NSAPI_ERROR_NO_SOCKET; } - return _iface->socket_bind(_socket, port); + return _iface->socket_bind(_socket, address); +} + +int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigned size) +{ + SocketAddress addr(_iface, host, port); + if (!addr) { + return NSAPI_ERROR_DNS_FAILURE; + } + + return sendto(addr, data, size); } int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size) diff --git a/net/NetworkSocketAPI/UDPSocket.h b/net/NetworkSocketAPI/UDPSocket.h index e40d231287..f17ad6c8a3 100644 --- a/net/NetworkSocketAPI/UDPSocket.h +++ b/net/NetworkSocketAPI/UDPSocket.h @@ -40,6 +40,29 @@ public: */ int bind(uint16_t port); + /** Bind a UDP Server Socket to a local address + * @param address The null-terminated address to listen for incoming connections on + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const char *address, uint16_t port); + + /** Bind a UDP Server Socket to a local address + * @param address The SocketAddress to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + int bind(const SocketAddress &address); + + /** Send a packet to a remote endpoint + * @param host The host to connect to. It can either be an IP Address + * or a hostname that will be resolved with DNS + * @param port The remote port + * @param data The packet to be sent + * @param size The length of the packet to be sent + * @return The number of written bytes on success, negative on failure + */ + int sendto(const char *host, uint16_t port, const void *data, unsigned size); + /** Send a packet to a remote endpoint * @param address The remote SocketAddress * @param data The packet to be sent