]> granicus.if.org Git - strace/commitdiff
net: factor out interface index printing code
authorDmitry V. Levin <ldv@altlinux.org>
Fri, 5 Jun 2015 20:13:21 +0000 (20:13 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Fri, 5 Jun 2015 20:13:21 +0000 (20:13 +0000)
* net.c (print_ifindex): New function.
(printsock) [HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID]: Use it.

net.c

diff --git a/net.c b/net.c
index 0a3c601fb0c9f68f28a8be5bf67adff3f0fd260a..fc325df013c4b77f5bb8d682f5e546fe7f2b28ba 100644 (file)
--- a/net.c
+++ b/net.c
 # 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)