net: factor out interface index printing code

* net.c (print_ifindex): New function.
(printsock) [HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID]: Use it.
This commit is contained in:
Дмитрий Левин 2015-06-05 20:13:21 +00:00
parent 6c8ef051f7
commit ae28093c65

47
net.c
View File

@ -200,6 +200,22 @@
# include "xlat/af_packet_types.h" # include "xlat/af_packet_types.h"
#endif #endif
static void
print_ifindex(unsigned int ifindex)
{
#ifdef HAVE_IF_INDEXTONAME
char buf[IFNAMSIZ + 1];
if (if_indextoname(ifindex, buf)) {
tprints("if_nametoindex(");
print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
tprints(")");
return;
}
#endif
tprintf("%u", ifindex);
}
void void
printsock(struct tcb *tcp, long addr, int addrlen) printsock(struct tcb *tcp, long addr, int addrlen)
{ {
@ -279,30 +295,15 @@ printsock(struct tcb *tcp, long addr, int addrlen)
ntohs(addrbuf.sa6.sin6_port), string_addr, ntohs(addrbuf.sa6.sin6_port), string_addr,
addrbuf.sa6.sin6_flowinfo); addrbuf.sa6.sin6_flowinfo);
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
{ tprints(", sin6_scope_id=");
#if defined(HAVE_IF_INDEXTONAME) && defined(IN6_IS_ADDR_LINKLOCAL) && defined(IN6_IS_ADDR_MC_LINKLOCAL) #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
int numericscope = 0; if (IN6_IS_ADDR_LINKLOCAL(&addrbuf.sa6.sin6_addr)
if (IN6_IS_ADDR_LINKLOCAL(&addrbuf.sa6.sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&addrbuf.sa6.sin6_addr))
|| IN6_IS_ADDR_MC_LINKLOCAL(&addrbuf.sa6.sin6_addr)) { print_ifindex(addrbuf.sa6.sin6_scope_id);
char scopebuf[IFNAMSIZ + 1]; else
if (if_indextoname(addrbuf.sa6.sin6_scope_id, scopebuf) == NULL)
numericscope++;
else {
tprints(", sin6_scope_id=if_nametoindex(");
print_quoted_string(scopebuf,
sizeof(scopebuf),
QUOTE_0_TERMINATED);
tprints(")");
}
} else
numericscope++;
if (numericscope)
#endif
tprintf(", sin6_scope_id=%u", addrbuf.sa6.sin6_scope_id);
}
#endif #endif
tprintf("%u", addrbuf.sa6.sin6_scope_id);
#endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */
break; break;
#endif #endif
#if defined(AF_IPX) #if defined(AF_IPX)