mirror of https://github.com/ARMmbed/mbed-os.git
Switch the IPv4 parser to the one recently introduced in libservice. Return a bool value on wether parsing of the IPv4 address string was successful
parent
1b051c7687
commit
758f6d2e83
|
|
@ -22,40 +22,16 @@
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
|
|
||||||
|
|
||||||
static bool ipv4_is_valid(const char *addr)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool ipv6_is_valid(const char *addr)
|
static bool ipv6_is_valid(const char *addr)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ipv4_from_address(uint8_t *bytes, const char *addr)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk)
|
static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ipv6_from_address(uint8_t *bytes, const char *addr)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ipv4_to_address(char *addr, const uint8_t *bytes)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ipv6_to_address(char *addr, const uint8_t *bytes)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
|
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -19,29 +19,11 @@
|
||||||
#include "NetworkStack.h"
|
#include "NetworkStack.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "ip4string.h"
|
||||||
#include "ip6string.h"
|
#include "ip6string.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static bool ipv4_is_valid(const char *addr)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
// Check each digit for [0-9.]
|
|
||||||
for (; addr[i]; i++) {
|
|
||||||
if (!(addr[i] >= '0' && addr[i] <= '9') && addr[i] != '.') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ending with '.' garuntees host
|
|
||||||
if (i > 0 && addr[i - 1] == '.') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool ipv6_is_valid(const char *addr)
|
static bool ipv6_is_valid(const char *addr)
|
||||||
{
|
{
|
||||||
// Check each digit for [0-9a-fA-F:]
|
// Check each digit for [0-9a-fA-F:]
|
||||||
|
|
@ -62,48 +44,6 @@ static bool ipv6_is_valid(const char *addr)
|
||||||
return colons >= 2;
|
return colons >= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ipv4_from_address(uint8_t *bytes, const char *addr)
|
|
||||||
{
|
|
||||||
int count = 0;
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
for (; count < NSAPI_IPv4_BYTES; count++) {
|
|
||||||
unsigned d;
|
|
||||||
// Not using %hh, since it might be missing in newlib-based toolchains.
|
|
||||||
// See also: https://git.io/vxiw5
|
|
||||||
int scanned = sscanf(&addr[i], "%u", &d);
|
|
||||||
if (scanned < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes[count] = static_cast<uint8_t>(d);
|
|
||||||
|
|
||||||
for (; addr[i] != '.'; i++) {
|
|
||||||
if (!addr[i]) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ipv6_from_address(uint8_t *bytes, const char *addr)
|
|
||||||
{
|
|
||||||
stoip6(addr, strlen(addr), bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ipv4_to_address(char *addr, const uint8_t *bytes)
|
|
||||||
{
|
|
||||||
sprintf(addr, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ipv6_to_address(char *addr, const uint8_t *bytes)
|
|
||||||
{
|
|
||||||
ip6tos(bytes, addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
|
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
|
||||||
{
|
{
|
||||||
_ip_address = NULL;
|
_ip_address = NULL;
|
||||||
|
|
@ -137,13 +77,12 @@ bool SocketAddress::set_ip_address(const char *addr)
|
||||||
delete[] _ip_address;
|
delete[] _ip_address;
|
||||||
_ip_address = NULL;
|
_ip_address = NULL;
|
||||||
|
|
||||||
if (addr && ipv4_is_valid(addr)) {
|
if (addr && stoip4(addr, strlen(addr), _addr.bytes)) {
|
||||||
_addr.version = NSAPI_IPv4;
|
_addr.version = NSAPI_IPv4;
|
||||||
ipv4_from_address(_addr.bytes, addr);
|
|
||||||
return true;
|
return true;
|
||||||
} else if (addr && ipv6_is_valid(addr)) {
|
} else if (addr && ipv6_is_valid(addr)) {
|
||||||
_addr.version = NSAPI_IPv6;
|
_addr.version = NSAPI_IPv6;
|
||||||
ipv6_from_address(_addr.bytes, addr);
|
stoip6(addr, strlen(addr), _addr.bytes);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
_addr = nsapi_addr_t();
|
_addr = nsapi_addr_t();
|
||||||
|
|
@ -186,9 +125,9 @@ const char *SocketAddress::get_ip_address() const
|
||||||
if (!_ip_address) {
|
if (!_ip_address) {
|
||||||
_ip_address = new char[NSAPI_IP_SIZE];
|
_ip_address = new char[NSAPI_IP_SIZE];
|
||||||
if (_addr.version == NSAPI_IPv4) {
|
if (_addr.version == NSAPI_IPv4) {
|
||||||
ipv4_to_address(_ip_address, _addr.bytes);
|
ip4tos(_addr.bytes, _ip_address);
|
||||||
} else if (_addr.version == NSAPI_IPv6) {
|
} else if (_addr.version == NSAPI_IPv6) {
|
||||||
ipv6_to_address(_ip_address, _addr.bytes);
|
ip6tos(_addr.bytes, _ip_address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue