Browse code

core: ip addr - use POSIX INET6_ADDRSTRLEN and INET_ADDRSTRLEN to define sizes

- set the values of IP4_MAX_STR_SIZE and IP6_MAX_STR_SIZE based on them,
if defined, or to 15 and 45
- cope with IPv6 with IPv4 tunneling representation (RFC 4291 section 2.2)
- cover the requirements of using inet_ntop()
- GH #2823

(cherry picked from commit 22a72f3a7725d56f00e22bdc52d44bd53da7c091)
(cherry picked from commit 680d98caccc0216f43c2b5de83935dbc4853ab9d)

Daniel-Constantin Mierla authored on 13/08/2021 06:57:10
Showing 1 changed files
... ...
@@ -518,8 +518,20 @@ static inline int hostent2su( union sockaddr_union* su,
518 518
 
519 519
 
520 520
 /* maximum size of a str returned by ip_addr2str */
521
-#define IP6_MAX_STR_SIZE 39 /*1234:5678:9012:3456:7890:1234:5678:9012*/
522
-#define IP4_MAX_STR_SIZE 15 /*123.456.789.012*/
521
+/* POSIX INET6_ADDRSTRLEN (RFC 4291 section 2.2) - IPv6 with IPv4 tunneling
522
+ * (39): 1234:5678:9012:3456:7890:1234:5678:9012
523
+ * (45): ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255 */
524
+#ifdef INET6_ADDRSTRLEN
525
+#define IP6_MAX_STR_SIZE (INET6_ADDRSTRLEN-1)
526
+#else
527
+#define IP6_MAX_STR_SIZE 45
528
+#endif
529
+/*123.456.789.123*/
530
+#ifdef INET_ADDRSTRLEN
531
+#define IP4_MAX_STR_SIZE (INET_ADDRSTRLEN-1)
532
+#else
533
+#define IP4_MAX_STR_SIZE 15
534
+#endif
523 535
 
524 536
 /* converts a raw ipv6 addr (16 bytes) to ascii */
525 537
 static inline int ip6tosbuf(unsigned char* ip6, char* buff, int len)