mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2024-12-22 09:41:54 +00:00
Explicit ip4_addr / ip6_addr printf support
This commit is contained in:
parent
74c838a870
commit
e691d16a63
@ -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);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
52
lib/printf.c
52
lib/printf.c
@ -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.
|
||||||
@ -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':
|
||||||
|
if (fmt[1] == '4') {
|
||||||
|
/* Explicit IPv4 address */
|
||||||
|
ip4_addr a = va_arg(args, ip4_addr);
|
||||||
|
ip4_ntop(a, ipbuf);
|
||||||
|
i = IP4_MAX_TEXT_LENGTH;
|
||||||
|
} 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);
|
ip_addr a = va_arg(args, ip_addr);
|
||||||
if (flags & SPECIAL)
|
|
||||||
ip6_ntox(ipa_to_ip6(a), ipbuf);
|
if (ipa_is_ip4(a)) {
|
||||||
else {
|
|
||||||
// XXXX better IPv4 / IPv6 distinction
|
|
||||||
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 *);
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user