@@ -37,11 +37,13 @@ IPAddress::IPAddress(const IPAddress& from)
37
37
}
38
38
39
39
IPAddress::IPAddress (uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) {
40
- setV4 ();
41
- (*this )[0 ] = first_octet;
42
- (*this )[1 ] = second_octet;
43
- (*this )[2 ] = third_octet;
44
- (*this )[3 ] = fourth_octet;
40
+ uint8_t addr[] {
41
+ first_octet,
42
+ second_octet,
43
+ third_octet,
44
+ fourth_octet,
45
+ };
46
+ *this = &addr[0 ];
45
47
}
46
48
47
49
IPAddress::IPAddress (uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16) {
@@ -65,12 +67,17 @@ IPAddress::IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5,
65
67
}
66
68
67
69
IPAddress::IPAddress (IPType type, const uint8_t *address) {
70
+ IPAddress (type, address, 0 );
71
+ }
72
+
73
+ IPAddress::IPAddress (IPType type, const uint8_t *address, uint8_t zone) {
68
74
if (type == IPv4) {
69
75
setV4 ();
70
76
memcpy (&this ->_ip .u_addr .ip4 , address, 4 );
71
77
} else if (type == IPv6) {
72
78
setV6 ();
73
79
memcpy (&this ->_ip .u_addr .ip6 .addr [0 ], address, 16 );
80
+ setZone (zone);
74
81
} else {
75
82
#if LWIP_IPV6
76
83
_ip = *IP6_ADDR_ANY;
@@ -81,19 +88,6 @@ IPAddress::IPAddress(IPType type, const uint8_t *address) {
81
88
82
89
}
83
90
84
- void IPAddress::ctor32 (uint32_t address) {
85
- setV4 ();
86
- v4 () = address;
87
- }
88
-
89
- IPAddress::IPAddress (const uint8_t *address) {
90
- setV4 ();
91
- (*this )[0 ] = address[0 ];
92
- (*this )[1 ] = address[1 ];
93
- (*this )[2 ] = address[2 ];
94
- (*this )[3 ] = address[3 ];
95
- }
96
-
97
91
bool IPAddress::fromString (const char *address) {
98
92
if (!fromString4 (address)) {
99
93
#if LWIP_IPV6
@@ -149,8 +143,9 @@ bool IPAddress::fromString4(const char *address) {
149
143
}
150
144
151
145
IPAddress& IPAddress::operator =(const uint8_t *address) {
152
- setV4 ();
153
- v4 () = *reinterpret_cast <const uint32_t *>(address);
146
+ uint32_t value;
147
+ memcpy_P (&value, address, sizeof (value));
148
+ *this = value;
154
149
return *this ;
155
150
}
156
151
@@ -161,15 +156,19 @@ IPAddress& IPAddress::operator=(uint32_t address) {
161
156
}
162
157
163
158
bool IPAddress::operator ==(const uint8_t * addr) const {
164
- return isV4 () && v4 () == *reinterpret_cast <const uint32_t *>(addr);
159
+ if (!isV4 ()) {
160
+ return false ;
161
+ }
162
+
163
+ uint32_t value;
164
+ memcpy_P (&value, addr, sizeof (value));
165
+
166
+ return v4 () == value;
165
167
}
166
168
167
169
size_t IPAddress::printTo (Print& p) const {
168
170
size_t n = 0 ;
169
171
170
- // if (!isSet())
171
- // return p.print(F("(IP unset)"));
172
-
173
172
#if LWIP_IPV6
174
173
if (isV6 ()) {
175
174
int count0 = 0 ;
@@ -185,6 +184,13 @@ size_t IPAddress::printTo(Print& p) const {
185
184
if ((i != 7 && count0 < 2 ) || count0 == 7 )
186
185
n += p.print (' :' );
187
186
}
187
+ // add a zone if zone-id si non-zero
188
+ if (_ip.u_addr .ip6 .zone ) {
189
+ n += p.print (' %' );
190
+ char if_name[NETIF_NAMESIZE];
191
+ netif_index_to_name (_ip.u_addr .ip6 .zone , if_name);
192
+ n += p.print (if_name);
193
+ }
188
194
return n;
189
195
}
190
196
#endif
@@ -202,7 +208,7 @@ String IPAddress::toString() const
202
208
StreamString sstr;
203
209
#if LWIP_IPV6
204
210
if (isV6 ())
205
- sstr.reserve (40 ); // 8 shorts x 4 chars each + 7 colons + nullterm
211
+ sstr.reserve (44 ); // 8 shorts x 4 chars each + 7 colons + nullterm + '%' + zone-id
206
212
else
207
213
#endif
208
214
sstr.reserve (16 ); // 4 bytes with 3 chars max + 3 dots + nullterm, or '(IP unset)'
@@ -230,56 +236,20 @@ void IPAddress::clear() {
230
236
#if LWIP_IPV6
231
237
232
238
bool IPAddress::fromString6 (const char *address) {
233
- // TODO: test test test
234
-
235
- uint32_t acc = 0 ; // Accumulator
236
- int dots = 0 , doubledots = -1 ;
237
-
238
- while (*address)
239
- {
240
- char c = tolower (*address++);
241
- if (isalnum (c)) {
242
- if (c >= ' a' )
243
- c -= ' a' - ' 0' - 10 ;
244
- acc = acc * 16 + (c - ' 0' );
245
- if (acc > 0xffff )
246
- // Value out of range
247
- return false ;
248
- }
249
- else if (c == ' :' ) {
250
- if (*address == ' :' ) {
251
- if (doubledots >= 0 )
252
- // :: allowed once
253
- return false ;
254
- // remember location
255
- doubledots = dots + !!acc;
256
- address++;
257
- }
258
- if (dots == 7 )
259
- // too many separators
260
- return false ;
261
- raw6 ()[dots++] = PP_HTONS (acc);
262
- acc = 0 ;
239
+ ip6_addr_t ip6;
240
+ if (ip6addr_aton (address, &ip6)) {
241
+ setV6 ();
242
+ memcpy (&this ->_ip .u_addr .ip6 .addr [0 ], ip6.addr , 16 );
243
+ // look for '%' in string
244
+ const char *s = address;
245
+ while (*s && *s != ' %' ) { s++; }
246
+ if (*s == ' %' ) {
247
+ // we have a zone id
248
+ setZone (netif_name_to_index (s + 1 ));
263
249
}
264
- else
265
- // Invalid char
266
- return false ;
250
+ return true ;
267
251
}
268
-
269
- if (doubledots == -1 && dots != 7 )
270
- // Too few separators
271
- return false ;
272
- raw6 ()[dots++] = PP_HTONS (acc);
273
-
274
- if (doubledots != -1 ) {
275
- for (int i = dots - doubledots - 1 ; i >= 0 ; i--)
276
- raw6 ()[8 - dots + doubledots + i] = raw6 ()[doubledots + i];
277
- for (int i = doubledots; i < 8 - dots + doubledots; i++)
278
- raw6 ()[i] = 0 ;
279
- }
280
-
281
- setV6 ();
282
- return true ;
252
+ return false ;
283
253
}
284
254
#endif // LWIP_IPV6
285
255
0 commit comments