Skip to content

Commit 0610629

Browse files
author
Cruz Monrreal
authored
Merge pull request #6293 from Taiki-San/patch-1
Reduce .text footprint of the network stack
2 parents dc45990 + 758f6d2 commit 0610629

File tree

2 files changed

+5
-90
lines changed

2 files changed

+5
-90
lines changed

features/cellular/UNITTESTS/stubs/SocketAddress_stub.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,16 @@
2222
#include "mbed.h"
2323

2424

25-
static bool ipv4_is_valid(const char *addr)
26-
{
27-
return false;
28-
}
29-
3025
static bool ipv6_is_valid(const char *addr)
3126
{
3227
return false;
3328
}
3429

35-
static void ipv4_from_address(uint8_t *bytes, const char *addr)
36-
{
37-
38-
}
39-
4030
static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk)
4131
{
4232
return 0;
4333
}
4434

45-
static void ipv6_from_address(uint8_t *bytes, const char *addr)
46-
{
47-
48-
}
49-
50-
static void ipv4_to_address(char *addr, const uint8_t *bytes)
51-
{
52-
53-
}
54-
55-
static void ipv6_to_address(char *addr, const uint8_t *bytes)
56-
{
57-
}
58-
5935

6036
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
6137
{

features/netsocket/SocketAddress.cpp

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,11 @@
1919
#include "NetworkStack.h"
2020
#include <string.h>
2121
#include <stdio.h>
22+
#include "ip4string.h"
2223
#include "ip6string.h"
2324

2425

2526

26-
static bool ipv4_is_valid(const char *addr)
27-
{
28-
int i = 0;
29-
30-
// Check each digit for [0-9.]
31-
for (; addr[i]; i++) {
32-
if (!(addr[i] >= '0' && addr[i] <= '9') && addr[i] != '.') {
33-
return false;
34-
}
35-
}
36-
37-
// Ending with '.' garuntees host
38-
if (i > 0 && addr[i - 1] == '.') {
39-
return false;
40-
}
41-
42-
return true;
43-
}
44-
4527
static bool ipv6_is_valid(const char *addr)
4628
{
4729
// Check each digit for [0-9a-fA-F:]
@@ -62,48 +44,6 @@ static bool ipv6_is_valid(const char *addr)
6244
return colons >= 2;
6345
}
6446

65-
static void ipv4_from_address(uint8_t *bytes, const char *addr)
66-
{
67-
int count = 0;
68-
int i = 0;
69-
70-
for (; count < NSAPI_IPv4_BYTES; count++) {
71-
unsigned d;
72-
// Not using %hh, since it might be missing in newlib-based toolchains.
73-
// See also: https://git.io/vxiw5
74-
int scanned = sscanf(&addr[i], "%u", &d);
75-
if (scanned < 1) {
76-
return;
77-
}
78-
79-
bytes[count] = static_cast<uint8_t>(d);
80-
81-
for (; addr[i] != '.'; i++) {
82-
if (!addr[i]) {
83-
return;
84-
}
85-
}
86-
87-
i++;
88-
}
89-
}
90-
91-
static void ipv6_from_address(uint8_t *bytes, const char *addr)
92-
{
93-
stoip6(addr, strlen(addr), bytes);
94-
}
95-
96-
static void ipv4_to_address(char *addr, const uint8_t *bytes)
97-
{
98-
sprintf(addr, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
99-
}
100-
101-
static void ipv6_to_address(char *addr, const uint8_t *bytes)
102-
{
103-
ip6tos(bytes, addr);
104-
}
105-
106-
10747
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
10848
{
10949
_ip_address = NULL;
@@ -137,13 +77,12 @@ bool SocketAddress::set_ip_address(const char *addr)
13777
delete[] _ip_address;
13878
_ip_address = NULL;
13979

140-
if (addr && ipv4_is_valid(addr)) {
80+
if (addr && stoip4(addr, strlen(addr), _addr.bytes)) {
14181
_addr.version = NSAPI_IPv4;
142-
ipv4_from_address(_addr.bytes, addr);
14382
return true;
14483
} else if (addr && ipv6_is_valid(addr)) {
14584
_addr.version = NSAPI_IPv6;
146-
ipv6_from_address(_addr.bytes, addr);
85+
stoip6(addr, strlen(addr), _addr.bytes);
14786
return true;
14887
} else {
14988
_addr = nsapi_addr_t();
@@ -186,9 +125,9 @@ const char *SocketAddress::get_ip_address() const
186125
if (!_ip_address) {
187126
_ip_address = new char[NSAPI_IP_SIZE];
188127
if (_addr.version == NSAPI_IPv4) {
189-
ipv4_to_address(_ip_address, _addr.bytes);
128+
ip4tos(_addr.bytes, _ip_address);
190129
} else if (_addr.version == NSAPI_IPv6) {
191-
ipv6_to_address(_ip_address, _addr.bytes);
130+
ip6tos(_addr.bytes, _ip_address);
192131
}
193132
}
194133

0 commit comments

Comments
 (0)