diff options
author | Jan Moskyto Matejka <mq@ucw.cz> | 2015-12-18 11:57:38 +0100 |
---|---|---|
committer | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2015-12-20 13:47:39 +0100 |
commit | 7fd4143eadd5af6e1ad7825d7d7506ad021bf1ad (patch) | |
tree | b16f93737a5bda12b6677a5ad3f45cf5b1c13c30 /lib | |
parent | 9656dce72eead158e6da3ad560720bb0addfe7e2 (diff) |
Integrated address print lengths
Minor changes by Ondrej Santiago Zajicek
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ip.h | 10 | ||||
-rw-r--r-- | lib/net.c | 7 | ||||
-rw-r--r-- | lib/net.h | 10 | ||||
-rw-r--r-- | lib/printf.c | 23 |
4 files changed, 30 insertions, 20 deletions
@@ -33,6 +33,10 @@ #define IP4_MAX_PREFIX_LENGTH 32 #define IP6_MAX_PREFIX_LENGTH 128 +#define IP4_MAX_TEXT_LENGTH 15 /* "255.255.255.255" */ +#define IP6_MAX_TEXT_LENGTH 39 /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" */ +#define IPA_MAX_TEXT_LENGTH 39 + #define IP4_MIN_MTU 576 #define IP6_MIN_MTU 1280 @@ -42,12 +46,6 @@ #define IP6_HEADER_LENGTH 40 #define UDP_HEADER_LENGTH 8 -#ifdef IPV6 -#define STD_ADDRESS_P_LENGTH 39 -#else -#define STD_ADDRESS_P_LENGTH 15 -#endif - #ifdef DEBUGGING @@ -18,6 +18,13 @@ const u8 net_max_prefix_length[] = { [NET_VPN6] = IP4_MAX_PREFIX_LENGTH }; +const u16 net_max_text_length[] = { + [NET_IP4] = 18, /* "255.255.255.255/32" */ + [NET_IP6] = 43, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */ + [NET_VPN4] = 40, /* "4294967296:4294967296 255.255.255.255/32" */ + [NET_VPN6] = 65, /* "4294967296:4294967296 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */ +}; + int net_format(const net_addr *N, char *buf, int buflen) @@ -68,7 +68,11 @@ typedef union net_addr_union { extern const u16 net_addr_length[]; -extern const u8 net_max_prefix_length[]; +extern const u8 net_max_prefix_length[]; +extern const u16 net_max_text_length[]; + +#define NET_MAX_TEXT_LENGTH 65 + #define NET_ADDR_IP4(prefix,pxlen) \ ((net_addr_ip4) { NET_IP4, pxlen, sizeof(net_addr_ip4), prefix }) @@ -103,6 +107,7 @@ static inline void net_fill_ipa(net_addr *a, ip_addr prefix, uint pxlen) net_fill_ip6(a, ipa_to_ip6(prefix), pxlen); } + static inline ip4_addr net4_prefix(const net_addr *a) { return ((net_addr_ip4 *) a)->prefix; } @@ -243,6 +248,3 @@ int net_in_netX(const net_addr *A, const net_addr *N); #endif - - - diff --git a/lib/printf.c b/lib/printf.c index 1a8c2a90..9db5363b 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -139,7 +139,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args) u32 x; char *str, *start; const char *s; - char ipbuf[STD_ADDRESS_P_LENGTH+1]; + char ipbuf[NET_MAX_TEXT_LENGTH+1]; struct iface *iface; int flags; /* flags to number() */ @@ -156,7 +156,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args) *str++ = *fmt; continue; } - + /* process flags */ flags = 0; repeat: @@ -236,12 +236,14 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args) case 'M': s = strerror(va_arg(args, int)); goto str; - case 'N': + case 'N': { + net_addr *n = va_arg(args, net_addr *); if (field_width == 1) - field_width = STD_ADDRESS_P_LENGTH; /* XXXX */ - net_format(va_arg(args, net_addr *), ipbuf, sizeof(ipbuf)); + field_width = net_max_text_length[n->type]; + net_format(n, ipbuf, sizeof(ipbuf)); s = ipbuf; goto str; + } case 's': s = va_arg(args, char *); if (!s) @@ -287,17 +289,18 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args) continue; /* IP address */ - case 'I': + case 'I': { + ip_addr a = va_arg(args, ip_addr); if (flags & SPECIAL) - ipa_ntox(va_arg(args, ip_addr), ipbuf); + ipa_ntox(a, ipbuf); else { - ipa_ntop(va_arg(args, ip_addr), ipbuf); + ipa_ntop(a, ipbuf); if (field_width == 1) - field_width = STD_ADDRESS_P_LENGTH; + field_width = (ipa_is_ip4(a) ? IP4_MAX_TEXT_LENGTH : IP6_MAX_TEXT_LENGTH); } s = ipbuf; goto str; - + } /* Interface scope after link-local IP address */ case 'J': iface = va_arg(args, struct iface *); |