@@ -29,8 +29,8 @@ namespace eprosima {
29
29
namespace fastrtps {
30
30
namespace rtps {
31
31
32
- static const std::regex IPv4_REGEX (" ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\ .){3}"
33
- " (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" );
32
+ static const std::regex IPv4_REGEX (" ^(?:(?:0* 25[0-5]|0* 2[0-4][0-9]|0* [01]?[0-9][0-9]?)\\ .){3}"
33
+ " (?:0* 25[0-5]|0* 2[0-4][0-9]|0* [01]?[0-9][0-9]?)$" );
34
34
static const std::regex IPv6_QUARTET_REGEX (" ^(?:[A-Fa-f0-9]){0,4}$" );
35
35
36
36
// Factory
@@ -106,7 +106,31 @@ bool IPLocator::setIPv4(
106
106
// This function do not set address to 0 in case it fails
107
107
// Be careful, do not set all IP to 0 because WAN and LAN could be set beforehand
108
108
109
- std::stringstream ss (ipv4);
109
+ std::string s (ipv4);
110
+ if (!IPLocator::isIPv4 (s))
111
+ {
112
+ // Attempt DNS resolution
113
+ auto response = IPLocator::resolveNameDNS (s);
114
+
115
+ // Use the first valid IPv4 address that we can find
116
+ if (response.first .size () > 0 )
117
+ {
118
+ s = response.first .begin ()->data ();
119
+ // Redundant check for extra security (here a custom regex is used instead of asio's verification)
120
+ if (!IPLocator::isIPv4 (s))
121
+ {
122
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " DNS name [" << ipv4 << " ] resolved into wrong IPv4 format: " << s);
123
+ return false ;
124
+ }
125
+ }
126
+ else
127
+ {
128
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv4 " << s << " error format. Expected X.X.X.X or valid DNS name" );
129
+ return false ;
130
+ }
131
+ }
132
+
133
+ std::stringstream ss (s);
110
134
uint32_t a;
111
135
uint32_t b;
112
136
uint32_t c;
@@ -127,7 +151,7 @@ bool IPLocator::setIPv4(
127
151
// If there are more info to read, it fails
128
152
return ss.rdbuf ()->in_avail () == 0 ;
129
153
}
130
- EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv4 " << ipv4 << " error format. Expected X.X.X.X" );
154
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv4 " << s << " error format. Expected X.X.X.X or valid DNS name " );
131
155
return false ;
132
156
}
133
157
@@ -261,14 +285,33 @@ bool IPLocator::setIPv6(
261
285
return false ;
262
286
}
263
287
264
- if (!IPv6isCorrect (ipv6))
288
+ std::string s (ipv6);
289
+ if (!IPLocator::isIPv6 (s))
265
290
{
266
- EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << ipv6 << " is not well defined" );
267
- return false ;
291
+ // Attempt DNS resolution
292
+ auto response = IPLocator::resolveNameDNS (s);
293
+
294
+ // Use the first valid IPv6 address that we can find
295
+ if (response.second .size () > 0 )
296
+ {
297
+ s = response.second .begin ()->data ();
298
+ // Redundant check for extra security (here a custom regex is used instead of asio's verification)
299
+ if (!IPLocator::isIPv6 (s))
300
+ {
301
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " DNS name [" << ipv6 << " ] resolved into wrong IPv6 format: " << s);
302
+ return false ;
303
+ }
304
+ }
305
+ else
306
+ {
307
+ EPROSIMA_LOG_WARNING (IP_LOCATOR,
308
+ " IPv6 " << s << " error format. Expected well defined address or valid DNS name" );
309
+ return false ;
310
+ }
268
311
}
269
312
270
313
LOCATOR_ADDRESS_INVALID (locator.address );
271
- uint16_t count = (uint16_t ) std::count_if ( ipv6 .begin (), ipv6 .end (), []( char c )
314
+ uint16_t count = (uint16_t ) std::count_if ( s .begin (), s .end (), []( char c )
272
315
{
273
316
return c == ' :' ;
274
317
}); // C type cast to avoid Windows warnings
@@ -281,10 +324,10 @@ bool IPLocator::setIPv6(
281
324
size_t aux_prev; // This must be size_t as string::npos could change value depending on size_t size
282
325
283
326
// Check whether is a zero block and where
284
- if (ipv6 .front () == ' :' )
327
+ if (s .front () == ' :' )
285
328
{
286
329
// First element equal : -> starts with zeros
287
- if (ipv6 .back () == ' :' )
330
+ if (s .back () == ' :' )
288
331
{
289
332
// Empty string (correct ipv6 format)
290
333
initial_zeros = 16 ;
@@ -298,7 +341,7 @@ bool IPLocator::setIPv6(
298
341
initial_zeros = (7 - (count - 2 )) * 2 ;
299
342
}
300
343
}
301
- else if (ipv6 .back () == ' :' )
344
+ else if (s .back () == ' :' )
302
345
{
303
346
// Last element equal : -> ends with zeros
304
347
// It does not start with :: (previous if)
@@ -307,8 +350,8 @@ bool IPLocator::setIPv6(
307
350
else
308
351
{
309
352
// It does not starts or ends with zeros, but it could have :: in the middle or not have it
310
- aux_prev = ipv6 .size (); // Aux could be 1 so this number must be unreacheable
311
- aux = ipv6 .find (' :' ); // Index of first ':'
353
+ aux_prev = s .size (); // Aux could be 1 so this number must be unreacheable
354
+ aux = s .find (' :' ); // Index of first ':'
312
355
313
356
// Look for "::" will loop string twice
314
357
// Therefore, we use this loop that will go over less or equal once
@@ -324,13 +367,13 @@ bool IPLocator::setIPv6(
324
367
// Not "::" found, keep searching in next ':'
325
368
position_zeros += 2 ; // It stores the point where the 0 block is
326
369
aux_prev = aux;
327
- aux = ipv6 .find (' :' , aux + 1 );
370
+ aux = s .find (' :' , aux + 1 );
328
371
}
329
372
}
330
373
331
374
char punct;
332
375
std::stringstream ss;
333
- ss << std::hex << ipv6 ;
376
+ ss << std::hex << s ;
334
377
uint16_t i;
335
378
uint32_t input_aux; // It cannot be uint16_t or we could not find whether the input number is bigger than allowed
336
379
@@ -350,7 +393,7 @@ bool IPLocator::setIPv6(
350
393
ss >> punct >> input_aux;
351
394
if (input_aux >= 65536 )
352
395
{
353
- EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << ipv6 << " has values higher than expected (65536)" );
396
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << s << " has values higher than expected (65536)" );
354
397
return false ;
355
398
}
356
399
locator.address [i++] = octet (input_aux >> 8 );
@@ -371,7 +414,7 @@ bool IPLocator::setIPv6(
371
414
ss >> input_aux >> punct;
372
415
if (input_aux >= 65536 )
373
416
{
374
- EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << ipv6 << " has values higher than expected (65536)" );
417
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << s << " has values higher than expected (65536)" );
375
418
return false ;
376
419
}
377
420
locator.address [i++] = octet (input_aux >> 8 );
@@ -393,7 +436,7 @@ bool IPLocator::setIPv6(
393
436
ss >> input_aux >> punct;
394
437
if (input_aux >= 65536 )
395
438
{
396
- EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << ipv6 << " has values higher than expected (65536)" );
439
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << s << " has values higher than expected (65536)" );
397
440
return false ;
398
441
}
399
442
locator.address [i++] = octet (input_aux >> 8 );
@@ -410,7 +453,7 @@ bool IPLocator::setIPv6(
410
453
ss >> punct >> input_aux;
411
454
if (input_aux >= 65536 )
412
455
{
413
- EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << ipv6 << " has values higher than expected (65536)" );
456
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << s << " has values higher than expected (65536)" );
414
457
return false ;
415
458
}
416
459
locator.address [i++] = octet (input_aux >> 8 );
@@ -428,7 +471,7 @@ bool IPLocator::setIPv6(
428
471
ss >> punct >> input_aux;
429
472
if (input_aux >= 65536 )
430
473
{
431
- EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << ipv6 << " has values higher than expected (65536)" );
474
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv6 " << s << " has values higher than expected (65536)" );
432
475
return false ;
433
476
}
434
477
locator.address [i++] = octet (input_aux >> 8 );
@@ -678,7 +721,37 @@ bool IPLocator::setWan(
678
721
Locator_t& locator,
679
722
const std::string& wan)
680
723
{
681
- std::stringstream ss (wan);
724
+ if (locator.kind != LOCATOR_KIND_TCPv4)
725
+ {
726
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " Trying to set WAN address in a non TCP-IPv4 Locator" );
727
+ return false ;
728
+ }
729
+
730
+ std::string s (wan);
731
+ if (!IPLocator::isIPv4 (s))
732
+ {
733
+ // Attempt DNS resolution
734
+ auto response = IPLocator::resolveNameDNS (s);
735
+
736
+ // Use the first valid IPv4 address that we can find
737
+ if (response.first .size () > 0 )
738
+ {
739
+ s = response.first .begin ()->data ();
740
+ // Redundant check for extra security (here a custom regex is used instead of asio's verification)
741
+ if (!IPLocator::isIPv4 (s))
742
+ {
743
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " DNS name [" << wan << " ] resolved into wrong IPv4 format: " << s);
744
+ return false ;
745
+ }
746
+ }
747
+ else
748
+ {
749
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv4 " << s << " error format. Expected X.X.X.X or valid DNS name" );
750
+ return false ;
751
+ }
752
+ }
753
+
754
+ std::stringstream ss (s);
682
755
int a, b, c, d; // to store the 4 ints
683
756
char ch; // to temporarily store the '.'
684
757
@@ -690,6 +763,7 @@ bool IPLocator::setWan(
690
763
locator.address [11 ] = (octet)d;
691
764
return true ;
692
765
}
766
+ EPROSIMA_LOG_WARNING (IP_LOCATOR, " IPv4 " << s << " error format. Expected X.X.X.X or valid DNS name" );
693
767
return false ;
694
768
}
695
769
0 commit comments