0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 01:31:55 +00:00

Explicit ip4_addr / ip6_addr printf support

This commit is contained in:
Ondrej Zajicek (work) 2015-12-29 17:12:47 +01:00
parent 74c838a870
commit e691d16a63
4 changed files with 42 additions and 29 deletions

View File

@ -178,7 +178,7 @@ pxlen4:
} }
| ':' IP4 { | ':' IP4 {
$$ = ip4_masklen($2); $$ = ip4_masklen($2);
if ($$ == 255) cf_error("Invalid netmask %I", $2); /* XXXX */ if ($$ == 255) cf_error("Invalid netmask %I4", $2);
} }
; ;

View File

@ -31,17 +31,16 @@ net_format(const net_addr *N, char *buf, int buflen)
{ {
net_addr_union *n = (void *) N; net_addr_union *n = (void *) N;
/* XXXX fix %I vs %R */
switch (n->n.type) switch (n->n.type)
{ {
case NET_IP4: case NET_IP4:
return bsnprintf(buf, buflen, "%R/%d", n->ip4.prefix, n->ip4.pxlen); return bsnprintf(buf, buflen, "%I4/%d", n->ip4.prefix, n->ip4.pxlen);
case NET_IP6: case NET_IP6:
return bsnprintf(buf, buflen, "%I/%d", n->ip6.prefix, n->ip6.pxlen); return bsnprintf(buf, buflen, "%I6/%d", n->ip6.prefix, n->ip6.pxlen);
case NET_VPN4: case NET_VPN4:
return bsnprintf(buf, buflen, "%u:%u %R/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen); return bsnprintf(buf, buflen, "%u:%u %I4/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
case NET_VPN6: case NET_VPN6:
return bsnprintf(buf, buflen, "%u:%u %I/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen); return bsnprintf(buf, buflen, "%u:%u %I6/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
} }
return 0; return 0;

View File

@ -118,15 +118,15 @@ static char * number(char * str, long num, int base, int size, int precision,
* @fmt: format string * @fmt: format string
* @args: a list of arguments to be formatted * @args: a list of arguments to be formatted
* *
* This functions acts like ordinary sprintf() except that it checks * This functions acts like ordinary sprintf() except that it checks available
* available space to avoid buffer overflows and it allows some more * space to avoid buffer overflows and it allows some more format specifiers:
* format specifiers: |%I| for formatting of IP addresses (any non-zero * |%I| for formatting of IP addresses (width of 1 is automatically replaced by
* width is automatically replaced by standard IP address width which * standard IP address width which depends on whether we use IPv4 or IPv6; |%I4|
* depends on whether we use IPv4 or IPv6; |%#I| gives hexadecimal format), * or |%I6| can be used for explicit ip4_addr / ip6_addr arguments, |%N| for
* |%R| for Router / Network ID (u32 value printed as IPv4 address) * generic network addresses (net_addr *), |%R| for Router / Network ID (u32
* and |%m| resp. |%M| for error messages (uses strerror() to translate @errno code to * value printed as IPv4 address) and |%m| resp. |%M| for error messages (uses
* message text). On the other hand, it doesn't support floating * strerror() to translate @errno code to message text). On the other hand, it
* point numbers. * doesn't support floating point numbers.
* *
* Result: number of characters of the output string or -1 if * Result: number of characters of the output string or -1 if
* the buffer space was insufficient. * the buffer space was insufficient.
@ -168,7 +168,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
case '#': flags |= SPECIAL; goto repeat; case '#': flags |= SPECIAL; goto repeat;
case '0': flags |= ZEROPAD; goto repeat; case '0': flags |= ZEROPAD; goto repeat;
} }
/* get field width */ /* get field width */
field_width = -1; field_width = -1;
if (is_digit(*fmt)) if (is_digit(*fmt))
@ -186,7 +186,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
/* get the precision */ /* get the precision */
precision = -1; precision = -1;
if (*fmt == '.') { if (*fmt == '.') {
++fmt; ++fmt;
if (is_digit(*fmt)) if (is_digit(*fmt))
precision = skip_atoi(&fmt); precision = skip_atoi(&fmt);
else if (*fmt == '*') { else if (*fmt == '*') {
@ -289,22 +289,36 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
continue; continue;
/* IP address */ /* IP address */
case 'I': { case 'I':
ip_addr a = va_arg(args, ip_addr); if (fmt[1] == '4') {
if (flags & SPECIAL) /* Explicit IPv4 address */
ip6_ntox(ipa_to_ip6(a), ipbuf); ip4_addr a = va_arg(args, ip4_addr);
else { ip4_ntop(a, ipbuf);
// XXXX better IPv4 / IPv6 distinction i = IP4_MAX_TEXT_LENGTH;
if (ipa_is_ip4(a)) } else if (fmt[1] == '6') {
/* Explicit IPv6 address */
ip6_addr a = va_arg(args, ip6_addr);
ip6_ntop(a, ipbuf);
i = IP6_MAX_TEXT_LENGTH;
} else {
/* Just IP address */
ip_addr a = va_arg(args, ip_addr);
if (ipa_is_ip4(a)) {
ip4_ntop(ipa_to_ip4(a), ipbuf); ip4_ntop(ipa_to_ip4(a), ipbuf);
else i = IP4_MAX_TEXT_LENGTH;
} else {
ip6_ntop(ipa_to_ip6(a), ipbuf); ip6_ntop(ipa_to_ip6(a), ipbuf);
if (field_width == 1) i = IP6_MAX_TEXT_LENGTH;
field_width = (ipa_is_ip4(a) ? IP4_MAX_TEXT_LENGTH : IP6_MAX_TEXT_LENGTH); }
} }
s = ipbuf; s = ipbuf;
if (field_width == 1)
field_width = i;
goto str; goto str;
}
/* Interface scope after link-local IP address */ /* Interface scope after link-local IP address */
case 'J': case 'J':
iface = va_arg(args, struct iface *); iface = va_arg(args, struct iface *);

View File

@ -691,7 +691,7 @@ nl_parse_addr4(struct ifaddrmsg *i, int scan, int new)
ifa.brd = ipa_from_ip4(xbrd); ifa.brd = ipa_from_ip4(xbrd);
else if (ifi->flags & IF_TMP_DOWN) /* Complain only during the first scan */ else if (ifi->flags & IF_TMP_DOWN) /* Complain only during the first scan */
{ {
log(L_ERR "KIF: Invalid broadcast address %I for %s", xbrd, ifi->name); log(L_ERR "KIF: Invalid broadcast address %I4 for %s", xbrd, ifi->name);
ifa.brd = ipa_from_ip4(ybrd); ifa.brd = ipa_from_ip4(ybrd);
} }
} }