2016-04-06 13:50:56 +00:00
|
|
|
/* 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 "SocketAddress.h"
|
2016-05-27 04:54:05 +00:00
|
|
|
#include "NetworkInterface.h"
|
2016-04-19 23:07:43 +00:00
|
|
|
#include "NetworkStack.h"
|
2016-04-06 13:50:56 +00:00
|
|
|
#include <string.h>
|
2018-05-17 20:36:15 +00:00
|
|
|
#include <stdio.h>
|
2018-08-27 17:39:02 +00:00
|
|
|
#include "ip4string.h"
|
2018-06-05 12:31:18 +00:00
|
|
|
#include "ip6string.h"
|
2018-05-17 20:36:15 +00:00
|
|
|
|
2016-04-06 13:50:56 +00:00
|
|
|
|
2016-03-14 00:30:30 +00:00
|
|
|
|
|
|
|
static bool ipv6_is_valid(const char *addr)
|
2016-03-13 21:49:18 +00:00
|
|
|
{
|
|
|
|
// Check each digit for [0-9a-fA-F:]
|
2016-09-23 06:46:29 +00:00
|
|
|
// Must also have at least 2 colons
|
|
|
|
int colons = 0;
|
2016-03-13 21:49:18 +00:00
|
|
|
for (int i = 0; addr[i]; i++) {
|
|
|
|
if (!(addr[i] >= '0' && addr[i] <= '9') &&
|
2018-08-03 12:32:29 +00:00
|
|
|
!(addr[i] >= 'a' && addr[i] <= 'f') &&
|
|
|
|
!(addr[i] >= 'A' && addr[i] <= 'F') &&
|
|
|
|
addr[i] != ':') {
|
2016-03-13 21:49:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-23 06:46:29 +00:00
|
|
|
if (addr[i] == ':') {
|
|
|
|
colons++;
|
|
|
|
}
|
2016-03-13 21:49:18 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 06:46:29 +00:00
|
|
|
return colons >= 2;
|
2016-03-13 21:49:18 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 00:00:33 +00:00
|
|
|
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
|
|
|
|
{
|
2018-06-12 14:34:52 +00:00
|
|
|
_ip_address = NULL;
|
2016-07-20 00:00:33 +00:00
|
|
|
set_addr(addr);
|
|
|
|
set_port(port);
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:50:56 +00:00
|
|
|
SocketAddress::SocketAddress(const char *addr, uint16_t port)
|
|
|
|
{
|
2018-06-12 14:34:52 +00:00
|
|
|
_ip_address = NULL;
|
2016-04-06 13:50:56 +00:00
|
|
|
set_ip_address(addr);
|
|
|
|
set_port(port);
|
|
|
|
}
|
|
|
|
|
2016-03-13 21:49:18 +00:00
|
|
|
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
|
|
|
|
{
|
2018-06-12 14:34:52 +00:00
|
|
|
_ip_address = NULL;
|
2016-03-13 21:49:18 +00:00
|
|
|
set_ip_bytes(bytes, version);
|
|
|
|
set_port(port);
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:50:56 +00:00
|
|
|
SocketAddress::SocketAddress(const SocketAddress &addr)
|
|
|
|
{
|
2018-06-12 14:34:52 +00:00
|
|
|
_ip_address = NULL;
|
2016-07-20 00:00:33 +00:00
|
|
|
set_addr(addr.get_addr());
|
2016-04-06 13:50:56 +00:00
|
|
|
set_port(addr.get_port());
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:46:29 +00:00
|
|
|
bool SocketAddress::set_ip_address(const char *addr)
|
2016-04-06 13:50:56 +00:00
|
|
|
{
|
2018-06-12 14:34:52 +00:00
|
|
|
delete[] _ip_address;
|
|
|
|
_ip_address = NULL;
|
2016-03-13 21:49:18 +00:00
|
|
|
|
2018-08-27 17:39:02 +00:00
|
|
|
if (addr && stoip4(addr, strlen(addr), _addr.bytes)) {
|
2016-07-20 00:00:33 +00:00
|
|
|
_addr.version = NSAPI_IPv4;
|
2016-09-23 06:46:29 +00:00
|
|
|
return true;
|
2016-03-14 00:30:30 +00:00
|
|
|
} else if (addr && ipv6_is_valid(addr)) {
|
2016-07-20 00:00:33 +00:00
|
|
|
_addr.version = NSAPI_IPv6;
|
2018-08-27 17:39:02 +00:00
|
|
|
stoip6(addr, strlen(addr), _addr.bytes);
|
2016-09-23 06:46:29 +00:00
|
|
|
return true;
|
2016-03-13 21:49:18 +00:00
|
|
|
} else {
|
2016-07-22 20:42:57 +00:00
|
|
|
_addr = nsapi_addr_t();
|
2016-09-23 06:46:29 +00:00
|
|
|
return false;
|
2016-03-13 21:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
|
|
|
|
{
|
2016-07-20 00:00:33 +00:00
|
|
|
nsapi_addr_t addr;
|
2016-11-03 10:16:17 +00:00
|
|
|
|
|
|
|
addr = nsapi_addr_t();
|
2016-07-20 00:00:33 +00:00
|
|
|
addr.version = version;
|
2016-11-03 10:16:17 +00:00
|
|
|
if (version == NSAPI_IPv6) {
|
|
|
|
memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES);
|
|
|
|
} else if (version == NSAPI_IPv4) {
|
|
|
|
memcpy(addr.bytes, bytes, NSAPI_IPv4_BYTES);
|
|
|
|
}
|
2016-07-20 00:00:33 +00:00
|
|
|
set_addr(addr);
|
|
|
|
}
|
2016-03-13 21:49:18 +00:00
|
|
|
|
2016-07-20 00:00:33 +00:00
|
|
|
void SocketAddress::set_addr(nsapi_addr_t addr)
|
|
|
|
{
|
2018-06-12 14:34:52 +00:00
|
|
|
delete[] _ip_address;
|
|
|
|
_ip_address = NULL;
|
2016-07-20 00:00:33 +00:00
|
|
|
_addr = addr;
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketAddress::set_port(uint16_t port)
|
|
|
|
{
|
|
|
|
_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *SocketAddress::get_ip_address() const
|
|
|
|
{
|
2016-10-03 21:57:27 +00:00
|
|
|
if (_addr.version == NSAPI_UNSPEC) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-13 21:49:18 +00:00
|
|
|
|
2018-06-12 14:34:52 +00:00
|
|
|
if (!_ip_address) {
|
|
|
|
_ip_address = new char[NSAPI_IP_SIZE];
|
2016-07-20 00:00:33 +00:00
|
|
|
if (_addr.version == NSAPI_IPv4) {
|
2018-08-27 17:39:02 +00:00
|
|
|
ip4tos(_addr.bytes, _ip_address);
|
2016-07-20 00:00:33 +00:00
|
|
|
} else if (_addr.version == NSAPI_IPv6) {
|
2018-08-27 17:39:02 +00:00
|
|
|
ip6tos(_addr.bytes, _ip_address);
|
2016-03-13 21:49:18 +00:00
|
|
|
}
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
2016-03-13 21:49:18 +00:00
|
|
|
|
2016-10-03 21:57:27 +00:00
|
|
|
return _ip_address;
|
2016-03-13 21:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const void *SocketAddress::get_ip_bytes() const
|
|
|
|
{
|
2016-07-20 00:00:33 +00:00
|
|
|
return _addr.bytes;
|
2016-03-13 21:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsapi_version_t SocketAddress::get_ip_version() const
|
|
|
|
{
|
2016-07-20 00:00:33 +00:00
|
|
|
return _addr.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsapi_addr_t SocketAddress::get_addr() const
|
|
|
|
{
|
|
|
|
return _addr;
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t SocketAddress::get_port() const
|
|
|
|
{
|
|
|
|
return _port;
|
|
|
|
}
|
2016-03-13 21:49:18 +00:00
|
|
|
|
|
|
|
SocketAddress::operator bool() const
|
|
|
|
{
|
2016-07-20 00:00:33 +00:00
|
|
|
if (_addr.version == NSAPI_IPv4) {
|
2016-10-03 21:57:27 +00:00
|
|
|
for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
|
|
|
|
if (_addr.bytes[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-03-13 21:49:18 +00:00
|
|
|
|
2016-10-03 21:57:27 +00:00
|
|
|
return false;
|
|
|
|
} else if (_addr.version == NSAPI_IPv6) {
|
|
|
|
for (int i = 0; i < NSAPI_IPv6_BYTES; i++) {
|
|
|
|
if (_addr.bytes[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-13 21:49:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 21:57:27 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-13 21:49:18 +00:00
|
|
|
}
|
2016-05-27 04:54:05 +00:00
|
|
|
|
2018-06-12 14:34:52 +00:00
|
|
|
SocketAddress &SocketAddress::operator=(const SocketAddress &addr)
|
|
|
|
{
|
|
|
|
delete[] _ip_address;
|
|
|
|
_ip_address = NULL;
|
|
|
|
set_addr(addr.get_addr());
|
|
|
|
set_port(addr.get_port());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-09-12 01:48:01 +00:00
|
|
|
bool operator==(const SocketAddress &a, const SocketAddress &b)
|
|
|
|
{
|
2016-10-03 21:57:27 +00:00
|
|
|
if (!a && !b) {
|
|
|
|
return true;
|
|
|
|
} else if (a._addr.version != b._addr.version) {
|
2016-09-12 01:48:01 +00:00
|
|
|
return false;
|
2016-10-03 21:57:27 +00:00
|
|
|
} 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;
|
2016-09-12 01:48:01 +00:00
|
|
|
}
|
2016-10-28 19:23:34 +00:00
|
|
|
|
|
|
|
MBED_UNREACHABLE;
|
2016-09-12 01:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const SocketAddress &a, const SocketAddress &b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:54:05 +00:00
|
|
|
void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
|
|
|
|
{
|
2018-06-12 14:34:52 +00:00
|
|
|
_ip_address = NULL;
|
2016-05-27 04:54:05 +00:00
|
|
|
|
2016-09-22 11:24:11 +00:00
|
|
|
// gethostbyname must check for literals, so can call it directly
|
|
|
|
int err = iface->gethostbyname(host, this);
|
|
|
|
_port = port;
|
|
|
|
if (err) {
|
|
|
|
_addr = nsapi_addr_t();
|
|
|
|
_port = 0;
|
2016-05-27 04:54:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-12 14:34:52 +00:00
|
|
|
|
|
|
|
SocketAddress::~SocketAddress()
|
|
|
|
{
|
|
|
|
delete[] _ip_address;
|
|
|
|
}
|