From 56e11d67098b9775c62245bb06b8a33a19500822 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 --- NetworkInterface.h | 10 +++++----- TCPServer.cpp | 16 ++++++++++++++-- TCPServer.h | 19 ++++++++++++++++--- TCPSocket.cpp | 2 +- UDPSocket.cpp | 24 +++++++++++++++++++++++- UDPSocket.h | 23 +++++++++++++++++++++++ 6 files changed, 82 insertions(+), 12 deletions(-) diff --git a/NetworkInterface.h b/NetworkInterface.h index da002f6d7e..4f1c2cd139 100644 --- a/NetworkInterface.h +++ b/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/TCPServer.cpp b/TCPServer.cpp index ccfd65056b..b2b7e57a6a 100644 --- a/TCPServer.cpp +++ b/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/TCPServer.h b/TCPServer.h index 84fbe82c97..17b2a48c7b 100644 --- a/TCPServer.h +++ b/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/TCPSocket.cpp b/TCPSocket.cpp index 6c19ddc2e1..a5225ce56c 100644 --- a/TCPSocket.cpp +++ b/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/UDPSocket.cpp b/UDPSocket.cpp index 5970d61def..6e210946e9 100644 --- a/UDPSocket.cpp +++ b/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/UDPSocket.h b/UDPSocket.h index e40d231287..f17ad6c8a3 100644 --- a/UDPSocket.h +++ b/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