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

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