nsapi - Standardized support of NSAPI_UNSPEC

- Reordered nsapi_version_t to make defaule nsapi_addr_t NSAPI_UNSPEC
- Added support to NSAPI_UNSPEC in SocketAddress
pull/2897/head
Christopher Haster 2016-10-03 16:57:27 -05:00
parent 5a55b39b2b
commit 1f4eb0aaa1
8 changed files with 45 additions and 90 deletions

View File

@ -55,11 +55,6 @@ int NetworkInterface::set_dhcp(bool dhcp)
}
// DNS operations go through the underlying stack by default
int NetworkInterface::gethostbyname(const char *name, SocketAddress *address)
{
return get_stack()->gethostbyname(name, address);
}
int NetworkInterface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
{
return get_stack()->gethostbyname(name, address, version);

View File

@ -100,20 +100,6 @@ public:
*/
virtual int disconnect() = 0;
/** Translates a hostname to an IP address
*
* The hostname may be either a domain name or an IP address. If the
* hostname is an IP address, no network transactions will be performed.
*
* If no stack-specific DNS resolution is provided, the hostname
* will be resolve using a UDP socket on the stack.
*
* @param address Destination for the host SocketAddress
* @param host Hostname to resolve
* @return 0 on success, negative error code on failure
*/
virtual int gethostbyname(const char *host, SocketAddress *address);
/** Translates a hostname to an IP address with specific version
*
* The hostname may be either a domain name or an IP address. If the
@ -124,10 +110,11 @@ public:
*
* @param address Destination for the host SocketAddress
* @param host Hostname to resolve
* @param version IP version of address to resolve
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
* @return 0 on success, negative error code on failure
*/
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version);
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
/** Add a domain name server to list of servers to query
*

View File

@ -22,16 +22,6 @@
// Default NetworkStack operations
int NetworkStack::gethostbyname(const char *name, SocketAddress *address)
{
// check for simple ip addresses
if (address->set_ip_address(name)) {
return 0;
}
return nsapi_dns_query(this, name, address);
}
int NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
{
// check for simple ip addresses
@ -100,25 +90,13 @@ public:
return address->get_ip_address();
}
virtual int gethostbyname(const char *name, SocketAddress *address)
{
if (!_stack_api()->gethostbyname) {
return NetworkStack::gethostbyname(name, address);
}
nsapi_addr_t addr = {NSAPI_IPv4, 0};
int err = _stack_api()->gethostbyname(_stack(), name, &addr, NSAPI_UNSPEC);
address->set_addr(addr);
return err;
}
virtual int gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
{
if (!_stack_api()->gethostbyname) {
return NetworkStack::gethostbyname(name, address, version);
}
nsapi_addr_t addr = {NSAPI_IPv4, 0};
nsapi_addr_t addr = {NSAPI_UNSPEC, 0};
int err = _stack_api()->gethostbyname(_stack(), name, &addr, version);
address->set_addr(addr);
return err;

View File

@ -41,20 +41,6 @@ public:
*/
virtual const char *get_ip_address() = 0;
/** Translates a hostname to an IP address
*
* The hostname may be either a domain name or an IP address. If the
* hostname is an IP address, no network transactions will be performed.
*
* If no stack-specific DNS resolution is provided, the hostname
* will be resolve using a UDP socket on the stack.
*
* @param host Hostname to resolve
* @param address Destination for the host SocketAddress
* @return 0 on success, negative error code on failure
*/
virtual int gethostbyname(const char *host, SocketAddress *address);
/** Translates a hostname to an IP address with specific version
*
* The hostname may be either a domain name or an IP address. If the
@ -65,10 +51,11 @@ public:
*
* @param host Hostname to resolve
* @param address Destination for the host SocketAddress
* @param version IP version of address to resolve
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
* @return 0 on success, negative error code on failure
*/
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version);
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
/** Add a domain name server to list of servers to query
*

View File

@ -215,17 +215,19 @@ void SocketAddress::set_port(uint16_t port)
const char *SocketAddress::get_ip_address() const
{
char *ip_address = (char *)_ip_address;
if (_addr.version == NSAPI_UNSPEC) {
return NULL;
}
if (!ip_address[0]) {
if (!_ip_address[0]) {
if (_addr.version == NSAPI_IPv4) {
ipv4_to_address(ip_address, _addr.bytes);
ipv4_to_address(_ip_address, _addr.bytes);
} else if (_addr.version == NSAPI_IPv6) {
ipv6_to_address(ip_address, _addr.bytes);
ipv6_to_address(_ip_address, _addr.bytes);
}
}
return ip_address;
return _ip_address;
}
const void *SocketAddress::get_ip_bytes() const
@ -250,34 +252,38 @@ uint16_t SocketAddress::get_port() const
SocketAddress::operator bool() const
{
int count = 0;
if (_addr.version == NSAPI_IPv4) {
count = NSAPI_IPv4_BYTES;
} else if (_addr.version == NSAPI_IPv6) {
count = NSAPI_IPv6_BYTES;
}
for (int i = 0; i < count; i++) {
if (_addr.bytes[i]) {
return true;
for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
if (_addr.bytes[i]) {
return true;
}
}
}
return false;
return false;
} else if (_addr.version == NSAPI_IPv6) {
for (int i = 0; i < NSAPI_IPv6_BYTES; i++) {
if (_addr.bytes[i]) {
return true;
}
}
return false;
} else {
return false;
}
}
bool operator==(const SocketAddress &a, const SocketAddress &b)
{
int count = 0;
if (a._addr.version == NSAPI_IPv4 && b._addr.version == NSAPI_IPv4) {
count = NSAPI_IPv4_BYTES;
} else if (a._addr.version == NSAPI_IPv6 && b._addr.version == NSAPI_IPv6) {
count = NSAPI_IPv6_BYTES;
} else {
if (!a && !b) {
return true;
} else if (a._addr.version != b._addr.version) {
return false;
} else if (a._addr.version == NSAPI_IPv4) {
return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv4_BYTES) == 0;
} else if (a._addr.version == NSAPI_IPv6) {
return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv6_BYTES) == 0;
}
return (memcmp(a._addr.bytes, b._addr.bytes, count) == 0);
}
bool operator!=(const SocketAddress &a, const SocketAddress &b)

View File

@ -160,7 +160,7 @@ public:
private:
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
char _ip_address[NSAPI_IP_SIZE];
mutable char _ip_address[NSAPI_IP_SIZE];
nsapi_addr_t _addr;
uint16_t _port;
};

View File

@ -102,7 +102,7 @@ static void dns_append_question(uint8_t **p, const char *host, nsapi_version_t v
dns_append_byte(p, 0);
// fill out question footer
if (version == NSAPI_IPv4) {
if (version != NSAPI_IPv6) {
dns_append_word(p, RR_A); // qtype = ipv4
} else {
dns_append_word(p, RR_AAAA); // qtype = ipv6

View File

@ -85,16 +85,18 @@ typedef enum nsapi_error {
* @enum nsapi_version_t
*/
typedef enum nsapi_version {
NSAPI_IPv4, /*!< Address is IPv4 */
NSAPI_IPv6, /*!< Address is IPv6 */
NSAPI_UNSPEC /*!< Address is unspecified */
NSAPI_UNSPEC, /*!< Address is unspecified */
NSAPI_IPv4, /*!< Address is IPv4 */
NSAPI_IPv6, /*!< Address is IPv6 */
} nsapi_version_t;
/** IP address structure for passing IP addresses by value
*/
typedef struct nsapi_addr {
/** IP version
* NSAPI_IPv4 or NSAPI_IPv6 (NSAPI_UNSPEC not currently supported)
* - NSAPI_IPv4
* - NSAPI_IPv6
* - NSAPI_UNSPEC
*/
nsapi_version_t version;