Skip to content

Commit 8054b8e

Browse files
author
Ari Parkkila
committed
Cellular: Fix resolving of DNS server IPv4/6 address
1 parent d91ed5f commit 8054b8e

File tree

4 files changed

+39
-63
lines changed

4 files changed

+39
-63
lines changed

UNITTESTS/features/cellular/framework/common/util/utiltest.cpp

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -184,60 +184,54 @@ TEST_F(Testutil, separate_ip_addresses)
184184
char ip[64] = {0};
185185
char subnet[64] = {0};
186186

187-
strncpy(s, "32.1.20.187.1.112.139.245.251.136.232.110.123.51.230.138.0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15\0", 95);
187+
// IP address representations formats are for IPv6 or IPv4 address, with and without mask, in dotted or colon (IPv6 only) notation
188+
189+
// IPv6 with a mask in dotted notation
190+
strncpy(s, "32.1.20.187.1.112.139.245.251.136.232.110.123.51.230.138.0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15\0",
191+
sizeof("32.1.20.187.1.112.139.245.251.136.232.110.123.51.230.138.0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15\0"));
188192
separate_ip_addresses(NULL, ip, sizeof(ip), subnet, sizeof(subnet));
189193

190194
separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
191195
EXPECT_STREQ("2001:14BB:170:8BF5:FB88:E86E:7B33:E68A", ip);
192196
EXPECT_STREQ("001:203:405:607:809:A0B:C0D:E0F", subnet);
193197

194-
strncpy(s, "32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138 0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15\0", 95);
198+
// IPv6 with mask in colon notation
199+
strncpy(s, "32:1:20:187:1:112:139:1245 0:1:2:3:4:5:6:7\0", sizeof("32:1:20:187:1:112:139:1245 0:1:2:3:4:5:6:7\0"));
195200
separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
196-
EXPECT_STREQ("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", ip);
197-
EXPECT_STREQ("0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15", subnet);
201+
EXPECT_STREQ("32:1:20:187:1:112:139:1245", ip);
202+
EXPECT_STREQ("0:1:2:3:4:5:6:7", subnet);
198203

199204
ip[0] = '\0';
200205
subnet[0] = '\0';
201-
strncpy(s, "1.2.3.4\0", 8);
206+
// IPv6 without mask in dotted notation
207+
strncpy(s, "0.2.3.4.5.6.7.8.90.100.11.12.13.14.15.16\0", sizeof("0.2.3.4.5.6.7.8.90.100.11.12.13.14.15.16\0"));
202208
separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
203-
EXPECT_STREQ("1.2.3.4", ip);
209+
EXPECT_STREQ("002:304:506:708:5A64:B0C:D0E:F10", ip);
204210
EXPECT_STREQ("", subnet);
205211

206212
ip[0] = '\0';
207213
subnet[0] = '\0';
208-
strncpy(s, "1.2.3.4.5.6.7.8\0", 16);
209-
separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
210-
EXPECT_STREQ("1.2.3.4", ip);
211-
EXPECT_STREQ("5.6.7.8", subnet);
212-
213-
ip[0] = '\0';
214-
subnet[0] = '\0';
215-
strncpy(s, "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16\0", 39);
214+
// IPv6 without mask in colon notation
215+
strncpy(s, "0032:1:20:187:0:112:139:245f\0", sizeof("0032:1:20:187:0:112:139:245f\0"));
216216
separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
217-
EXPECT_STREQ("102:304:506:708:90A:B0C:D0E:F10", ip);
217+
EXPECT_STREQ("0032:1:20:187:0:112:139:245f", ip);
218218
EXPECT_STREQ("", subnet);
219219

220220
ip[0] = '\0';
221221
subnet[0] = '\0';
222-
strncpy(s, "32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138\0", 57);
222+
// IPv4 with mask
223+
strncpy(s, "100.0.3.40.255.6.7.80\0", sizeof("100.0.3.40.255.6.7.80\0"));
223224
separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
224-
EXPECT_STREQ("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", ip);
225-
EXPECT_STREQ("", subnet);
225+
EXPECT_STREQ("100.0.3.40", ip);
226+
EXPECT_STREQ("255.6.7.80", subnet);
226227

227228
ip[0] = '\0';
228229
subnet[0] = '\0';
229-
strncpy(s, "1.2.3.4 32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138\0", 65);
230+
// IPv4 without mask
231+
strncpy(s, "1.255.3.0\0", sizeof("1.255.3.0\0"));
230232
separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
231-
EXPECT_STREQ("1.2.3.4", ip);
232-
EXPECT_STREQ("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", subnet);
233-
234-
ip[0] = '\0';
235-
subnet[0] = '\0';
236-
strncpy(s, "1.2.3.4 5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20\0", 51);
237-
separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
238-
EXPECT_STREQ("1.2.3.4", ip);
239-
EXPECT_STREQ("506:708:90A:B0C:D0E:F10:1112:1314", subnet);
240-
EXPECT_STREQ("1.2.3.4 5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20", s);
233+
EXPECT_STREQ("1.255.3.0", ip);
234+
EXPECT_STREQ("", subnet);
241235
}
242236

243237
TEST_F(Testutil, get_dynamic_ip_port)

UNITTESTS/stubs/CellularUtil_stub.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ void separate_ip4like_addresses(char *orig, char *ip, size_t ip_size, char *ip2,
6969
strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx]));
7070
ip[strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx])] = '\0';
7171
CellularUtil_stub::table_idx--;
72-
memcpy(ip2, CellularUtil_stub::char_table[CellularUtil_stub::table_idx],
73-
strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx]));
74-
ip2[strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx])] = '\0';
72+
if (ip2) {
73+
memcpy(ip2, CellularUtil_stub::char_table[CellularUtil_stub::table_idx],
74+
strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx]));
75+
ip2[strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx])] = '\0';
76+
}
7577
}
7678
}
7779

@@ -83,9 +85,11 @@ void separate_ip_addresses(char *orig, char *ip, size_t ip_size, char *ip2, size
8385
strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx]));
8486
ip[strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx])] = '\0';
8587
CellularUtil_stub::table_idx--;
86-
memcpy(ip2, CellularUtil_stub::char_table[CellularUtil_stub::table_idx],
87-
strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx]));
88-
ip2[strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx])] = '\0';
88+
if (ip2) {
89+
memcpy(ip2, CellularUtil_stub::char_table[CellularUtil_stub::table_idx],
90+
strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx]));
91+
ip2[strlen(CellularUtil_stub::char_table[CellularUtil_stub::table_idx])] = '\0';
92+
}
8993
}
9094
}
9195

features/cellular/framework/AT/AT_CellularContext.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,7 @@ nsapi_error_t AT_CellularContext::get_rate_control(
816816
nsapi_error_t AT_CellularContext::get_pdpcontext_params(pdpContextList_t &params_list)
817817
{
818818
const int ipv6_subnet_size = 128;
819-
const int max_ipv6_size = 64;
820819
char *ipv6_and_subnetmask = new char[ipv6_subnet_size];
821-
char *temp = new char[max_ipv6_size];
822820

823821
_at.lock();
824822

@@ -834,38 +832,28 @@ nsapi_error_t AT_CellularContext::get_pdpcontext_params(pdpContextList_t &params
834832

835833
// rest are optional params
836834
ipv6_and_subnetmask[0] = '\0';
837-
temp[0] = '\0';
838835
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
839836
separate_ip_addresses(ipv6_and_subnetmask, params->local_addr, sizeof(params->local_addr), params->local_subnet_mask, sizeof(params->local_subnet_mask));
840837
ipv6_and_subnetmask[0] = '\0';
841838

842839
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
843-
separate_ip_addresses(ipv6_and_subnetmask, params->gateway_addr, sizeof(params->gateway_addr), temp, max_ipv6_size);
844-
prefer_ipv6(params->gateway_addr, sizeof(params->gateway_addr), temp, max_ipv6_size);
840+
separate_ip_addresses(ipv6_and_subnetmask, params->gateway_addr, sizeof(params->gateway_addr), NULL, 0);
845841
ipv6_and_subnetmask[0] = '\0';
846-
temp[0] = '\0';
847842

848843
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
849-
separate_ip_addresses(ipv6_and_subnetmask, params->dns_primary_addr, sizeof(params->dns_primary_addr), temp, max_ipv6_size);
850-
prefer_ipv6(params->dns_primary_addr, sizeof(params->dns_primary_addr), temp, max_ipv6_size);
844+
separate_ip_addresses(ipv6_and_subnetmask, params->dns_primary_addr, sizeof(params->dns_primary_addr), NULL, 0);
851845
ipv6_and_subnetmask[0] = '\0';
852-
temp[0] = '\0';
853846

854847
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
855-
separate_ip_addresses(ipv6_and_subnetmask, params->dns_secondary_addr, sizeof(params->dns_secondary_addr), temp, max_ipv6_size);
856-
prefer_ipv6(params->dns_secondary_addr, sizeof(params->dns_secondary_addr), temp, max_ipv6_size);
848+
separate_ip_addresses(ipv6_and_subnetmask, params->dns_secondary_addr, sizeof(params->dns_secondary_addr), NULL, 0);
857849
ipv6_and_subnetmask[0] = '\0';
858-
temp[0] = '\0';
859850

860851
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
861-
separate_ip_addresses(ipv6_and_subnetmask, params->p_cscf_prim_addr, sizeof(params->p_cscf_prim_addr), temp, max_ipv6_size);
862-
prefer_ipv6(params->p_cscf_prim_addr, sizeof(params->p_cscf_prim_addr), temp, max_ipv6_size);
852+
separate_ip_addresses(ipv6_and_subnetmask, params->p_cscf_prim_addr, sizeof(params->p_cscf_prim_addr), NULL, 0);
863853
ipv6_and_subnetmask[0] = '\0';
864-
temp[0] = '\0';
865854

866855
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
867-
separate_ip_addresses(ipv6_and_subnetmask, params->p_cscf_sec_addr, sizeof(params->p_cscf_sec_addr), temp, max_ipv6_size);
868-
prefer_ipv6(params->p_cscf_sec_addr, sizeof(params->p_cscf_sec_addr), temp, max_ipv6_size);
856+
separate_ip_addresses(ipv6_and_subnetmask, params->p_cscf_sec_addr, sizeof(params->p_cscf_sec_addr), NULL, 0);
869857

870858
params->im_signalling_flag = _at.read_int();
871859
params->lipa_indication = _at.read_int();
@@ -877,7 +865,6 @@ nsapi_error_t AT_CellularContext::get_pdpcontext_params(pdpContextList_t &params
877865
}
878866
_at.resp_stop();
879867

880-
delete [] temp;
881868
delete [] ipv6_and_subnetmask;
882869

883870
return _at.unlock_return_error();

features/cellular/framework/common/CellularUtil.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,7 @@ void separate_ip_addresses(char *orig, char *ip, size_t ip_size, char *ip2, size
205205
}
206206
}
207207
} else {
208-
temp = strstr(orig, " ");
209-
// found space as separator and it wasn't in beginning --> contains 2 ip addresses
210-
if (temp && temp != orig) {
211-
separate_ip4like_addresses(temp++, ip2, ip2_size, NULL, 0);
212-
orig[temp - orig - 1] = '\0';
213-
separate_ip4like_addresses(orig, ip, ip_size, NULL, 0);
214-
orig[temp - orig - 1] = ' '; // put space back to keep orig as original
215-
} else {
216-
separate_ip4like_addresses(orig, ip, ip_size, ip2, ip2_size);
217-
}
208+
separate_ip4like_addresses(orig, ip, ip_size, ip2, ip2_size);
218209
}
219210
}
220211

0 commit comments

Comments
 (0)