Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce .text footprint of the network stack #6293

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions features/cellular/UNITTESTS/stubs/SocketAddress_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,16 @@
#include "mbed.h"


static bool ipv4_is_valid(const char *addr)
{
return false;
}

static bool ipv6_is_valid(const char *addr)
{
return false;
}

static void ipv4_from_address(uint8_t *bytes, const char *addr)
{

}

static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk)
{
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)
{
Expand Down
71 changes: 5 additions & 66 deletions features/netsocket/SocketAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,11 @@
#include "NetworkStack.h"
#include <string.h>
#include <stdio.h>
#include "ip4string.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)
{
// Check each digit for [0-9a-fA-F:]
Expand All @@ -62,48 +44,6 @@ static bool ipv6_is_valid(const char *addr)
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)
{
_ip_address = NULL;
Expand Down Expand Up @@ -137,13 +77,12 @@ bool SocketAddress::set_ip_address(const char *addr)
delete[] _ip_address;
_ip_address = NULL;

if (addr && ipv4_is_valid(addr)) {
if (addr && stoip4(addr, strlen(addr), _addr.bytes)) {
_addr.version = NSAPI_IPv4;
ipv4_from_address(_addr.bytes, addr);
return true;
} else if (addr && ipv6_is_valid(addr)) {
_addr.version = NSAPI_IPv6;
ipv6_from_address(_addr.bytes, addr);
stoip6(addr, strlen(addr), _addr.bytes);
return true;
} else {
_addr = nsapi_addr_t();
Expand Down Expand Up @@ -186,9 +125,9 @@ const char *SocketAddress::get_ip_address() const
if (!_ip_address) {
_ip_address = new char[NSAPI_IP_SIZE];
if (_addr.version == NSAPI_IPv4) {
ipv4_to_address(_ip_address, _addr.bytes);
ip4tos(_addr.bytes, _ip_address);
} else if (_addr.version == NSAPI_IPv6) {
ipv6_to_address(_ip_address, _addr.bytes);
ip6tos(_addr.bytes, _ip_address);
}
}

Expand Down