Skip to content

Commit d7c02a1

Browse files
authored
Merge pull request #2953 from geky/nsapi-fix-ipv6-parsing
nsapi - Fix leftover bytes from suffix during ipv6 parsing
2 parents ecbfaa7 + cce82b1 commit d7c02a1

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

TESTS/netsocket/ip_parsing/main.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
6+
using namespace utest::v1;
7+
8+
9+
// IP parsing verification
10+
void test_ip_accept(const char *string, nsapi_addr_t addr) {
11+
SocketAddress address;
12+
TEST_ASSERT(address.set_ip_address(string));
13+
TEST_ASSERT(address == SocketAddress(addr));
14+
}
15+
16+
template <const char *string>
17+
void test_ip_reject() {
18+
SocketAddress address;
19+
TEST_ASSERT(!address.set_ip_address(string));
20+
TEST_ASSERT(!address);
21+
}
22+
23+
#define TEST_IP_ACCEPT(name, string, ...) \
24+
void name() { \
25+
nsapi_addr_t addr = __VA_ARGS__; \
26+
test_ip_accept(string, addr); \
27+
}
28+
29+
#define TEST_IP_REJECT(name, string) \
30+
void name() { \
31+
test_ip_reject(string); \
32+
}
33+
34+
35+
// Test cases
36+
TEST_IP_ACCEPT(test_simple_ipv4_address,
37+
"12.34.56.78",
38+
{NSAPI_IPv4,{12,34,56,78}})
39+
TEST_IP_ACCEPT(test_left_weighted_ipv4_address,
40+
"255.0.0.0",
41+
{NSAPI_IPv4,{255,0,0,0}})
42+
TEST_IP_ACCEPT(test_right_weighted_ipv4_address,
43+
"0.0.0.255",
44+
{NSAPI_IPv4,{0,0,0,255}})
45+
TEST_IP_ACCEPT(test_null_ipv4_address,
46+
"0.0.0.0",
47+
{NSAPI_IPv4,{0,0,0,0}})
48+
49+
TEST_IP_ACCEPT(test_simple_ipv6_address,
50+
"1234:5678:9abc:def0:1234:5678:9abc:def0",
51+
{NSAPI_IPv6,{0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
52+
0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0}})
53+
TEST_IP_ACCEPT(test_left_weighted_ipv6_address,
54+
"1234:5678::",
55+
{NSAPI_IPv6,{0x12,0x34,0x56,0x78,0x00,0x00,0x00,0x00,
56+
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}})
57+
TEST_IP_ACCEPT(test_right_weighted_ipv6_address,
58+
"::1234:5678",
59+
{NSAPI_IPv6,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
60+
0x00,0x00,0x00,0x00,0x12,0x34,0x56,0x78}})
61+
TEST_IP_ACCEPT(test_hollowed_ipv6_address,
62+
"1234:5678::9abc:def8",
63+
{NSAPI_IPv6,{0x12,0x34,0x56,0x78,0x00,0x00,0x00,0x00,
64+
0x00,0x00,0x00,0x00,0x9a,0xbc,0xde,0xf8}})
65+
TEST_IP_ACCEPT(test_null_ipv6_address,
66+
"::",
67+
{NSAPI_IPv6,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
68+
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}})
69+
70+
71+
// Test setup
72+
utest::v1::status_t test_setup(const size_t number_of_cases) {
73+
GREENTEA_SETUP(10, "default_auto");
74+
return verbose_test_setup_handler(number_of_cases);
75+
}
76+
77+
Case cases[] = {
78+
Case("Simple IPv4 address", test_simple_ipv4_address),
79+
Case("Left-weighted IPv4 address", test_left_weighted_ipv4_address),
80+
Case("Right-weighted IPv4 address", test_right_weighted_ipv4_address),
81+
Case("Null IPv4 address", test_null_ipv4_address),
82+
83+
Case("Simple IPv6 address", test_simple_ipv6_address),
84+
Case("Left-weighted IPv6 address", test_left_weighted_ipv6_address),
85+
Case("Right-weighted IPv6 address", test_right_weighted_ipv6_address),
86+
Case("Hollowed IPv6 address", test_hollowed_ipv6_address),
87+
Case("Null IPv6 address", test_null_ipv6_address),
88+
};
89+
90+
Specification specification(test_setup, cases);
91+
92+
int main() {
93+
return !Harness::run(specification);
94+
}

features/netsocket/SocketAddress.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ static void ipv4_from_address(uint8_t *bytes, const char *addr)
6666
int i = 0;
6767

6868
for (; count < NSAPI_IPv4_BYTES; count++) {
69-
int scanned = sscanf(&addr[i], "%hhu", &bytes[count]);
69+
unsigned char b;
70+
int scanned = sscanf(&addr[i], "%hhu", &b);
7071
if (scanned < 1) {
7172
return;
7273
}
7374

75+
bytes[count] = b;
76+
7477
for (; addr[i] != '.'; i++) {
7578
if (!addr[i]) {
7679
return;
@@ -86,11 +89,14 @@ static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk) {
8689
int i = 0;
8790

8891
for (; count < NSAPI_IPv6_BYTES/2; count++) {
89-
int scanned = sscanf(&chunk[i], "%hx", &shorts[count]);
92+
unsigned short s;
93+
int scanned = sscanf(&chunk[i], "%hx", &s);
9094
if (scanned < 1) {
9195
return count;
9296
}
9397

98+
shorts[count] = s;
99+
94100
for (; chunk[i] != ':'; i++) {
95101
if (!chunk[i]) {
96102
return count+1;
@@ -107,8 +113,6 @@ static void ipv6_from_address(uint8_t *bytes, const char *addr)
107113
{
108114
// Start with zeroed address
109115
uint16_t shorts[NSAPI_IPv6_BYTES/2];
110-
memset(shorts, 0, sizeof shorts);
111-
112116
int suffix = 0;
113117

114118
// Find double colons and scan suffix
@@ -122,6 +126,8 @@ static void ipv6_from_address(uint8_t *bytes, const char *addr)
122126
// Move suffix to end
123127
memmove(&shorts[NSAPI_IPv6_BYTES/2-suffix], &shorts[0],
124128
suffix*sizeof(uint16_t));
129+
memset(&shorts[0], 0,
130+
(NSAPI_IPv6_BYTES/2-suffix)*sizeof(uint16_t));
125131

126132
// Scan prefix
127133
ipv6_scan_chunk(shorts, &addr[0]);

0 commit comments

Comments
 (0)