From: Dmitry V. Levin Date: Fri, 5 Jun 2015 20:13:21 +0000 (+0000) Subject: net: factor out interface index printing code X-Git-Tag: v4.11~508 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ae28093c650f8ee4709c1a22ff365201e2fe809a;p=strace net: factor out interface index printing code * net.c (print_ifindex): New function. (printsock) [HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID]: Use it. --- diff --git a/net.c b/net.c index 0a3c601f..fc325df0 100644 --- a/net.c +++ b/net.c @@ -200,6 +200,22 @@ # include "xlat/af_packet_types.h" #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 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, addrbuf.sa6.sin6_flowinfo); #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID - { -#if defined(HAVE_IF_INDEXTONAME) && defined(IN6_IS_ADDR_LINKLOCAL) && defined(IN6_IS_ADDR_MC_LINKLOCAL) - int numericscope = 0; - if (IN6_IS_ADDR_LINKLOCAL(&addrbuf.sa6.sin6_addr) - || IN6_IS_ADDR_MC_LINKLOCAL(&addrbuf.sa6.sin6_addr)) { - char scopebuf[IFNAMSIZ + 1]; - - 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); - } + tprints(", sin6_scope_id="); +#if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL + if (IN6_IS_ADDR_LINKLOCAL(&addrbuf.sa6.sin6_addr) + || IN6_IS_ADDR_MC_LINKLOCAL(&addrbuf.sa6.sin6_addr)) + print_ifindex(addrbuf.sa6.sin6_scope_id); + else #endif + tprintf("%u", addrbuf.sa6.sin6_scope_id); +#endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */ break; #endif #if defined(AF_IPX)