From: Dmitry V. Levin Date: Sat, 21 Nov 2015 02:38:59 +0000 (+0000) Subject: net: factor out address printing code to a separate function X-Git-Tag: v4.11~131 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1019f002c11a075abbbc195b62617775d5c5e0d0;p=strace net: factor out address printing code to a separate function * net.c (sockaddr_buf_t): New type. (print_sockaddr): New function. (printsock, print_group_req): Use it. --- diff --git a/net.c b/net.c index 13299f2b..18804481 100644 --- a/net.c +++ b/net.c @@ -132,88 +132,79 @@ print_ifindex(unsigned int ifindex) tprintf("%u", ifindex); } -void -printsock(struct tcb *tcp, long addr, int addrlen) -{ - union { - char pad[128]; - struct sockaddr sa; - struct sockaddr_in sin; - struct sockaddr_un sau; +typedef union { + char pad[128]; + struct sockaddr sa; + struct sockaddr_in sin; + struct sockaddr_un sau; #ifdef HAVE_INET_NTOP - struct sockaddr_in6 sa6; + struct sockaddr_in6 sa6; #endif #if defined(AF_IPX) - struct sockaddr_ipx sipx; + struct sockaddr_ipx sipx; #endif #ifdef AF_PACKET - struct sockaddr_ll ll; + struct sockaddr_ll ll; #endif #ifdef AF_NETLINK - struct sockaddr_nl nl; + struct sockaddr_nl nl; #endif #ifdef HAVE_BLUETOOTH_BLUETOOTH_H - struct sockaddr_hci hci; - struct sockaddr_l2 l2; - struct sockaddr_rc rc; - struct sockaddr_sco sco; + struct sockaddr_hci hci; + struct sockaddr_l2 l2; + struct sockaddr_rc rc; + struct sockaddr_sco sco; #endif - } addrbuf; - char string_addr[100]; - - if (addrlen < 2) { - printaddr(addr); - return; - } - - if (addrlen > (int) sizeof(addrbuf)) - addrlen = sizeof(addrbuf); - - memset(&addrbuf, 0, sizeof(addrbuf)); - if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad)) - return; - addrbuf.pad[sizeof(addrbuf.pad) - 1] = '\0'; +} sockaddr_buf_t; +static void +print_sockaddr(struct tcb *tcp, const sockaddr_buf_t *addr, const int addrlen) +{ tprints("{sa_family="); - printxval(addrfams, addrbuf.sa.sa_family, "AF_???"); + printxval(addrfams, addr->sa.sa_family, "AF_???"); tprints(", "); - switch (addrbuf.sa.sa_family) { + switch (addr->sa.sa_family) { case AF_UNIX: if (addrlen == 2) { tprints("NULL"); - } else if (addrbuf.sau.sun_path[0]) { + } else if (addr->sau.sun_path[0]) { tprints("sun_path="); - print_quoted_string(addrbuf.sau.sun_path, - sizeof(addrbuf.sau.sun_path) + 1, + print_quoted_string(addr->sau.sun_path, + sizeof(addr->sau.sun_path) + 1, QUOTE_0_TERMINATED); } else { tprints("sun_path=@"); - print_quoted_string(addrbuf.sau.sun_path + 1, - sizeof(addrbuf.sau.sun_path), + print_quoted_string(addr->sau.sun_path + 1, + sizeof(addr->sau.sun_path), QUOTE_0_TERMINATED); } break; case AF_INET: tprintf("sin_port=htons(%u), sin_addr=inet_addr(\"%s\")", - ntohs(addrbuf.sin.sin_port), inet_ntoa(addrbuf.sin.sin_addr)); + ntohs(addr->sin.sin_port), inet_ntoa(addr->sin.sin_addr)); break; #ifdef HAVE_INET_NTOP case AF_INET6: - inet_ntop(AF_INET6, &addrbuf.sa6.sin6_addr, string_addr, sizeof(string_addr)); - tprintf("sin6_port=htons(%u), inet_pton(AF_INET6, \"%s\", &sin6_addr), sin6_flowinfo=%u", - ntohs(addrbuf.sa6.sin6_port), string_addr, - addrbuf.sa6.sin6_flowinfo); -#ifdef HAVE_STRUCT_SOCKADDR_IN6_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 */ + { + char string_addr[100]; + inet_ntop(AF_INET6, &addr->sa6.sin6_addr, + string_addr, sizeof(string_addr)); + tprintf("sin6_port=htons(%u), inet_pton(AF_INET6" + ", \"%s\", &sin6_addr), sin6_flowinfo=%u", + ntohs(addr->sa6.sin6_port), string_addr, + addr->sa6.sin6_flowinfo); +# ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID + tprints(", sin6_scope_id="); +# if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL + if (IN6_IS_ADDR_LINKLOCAL(&addr->sa6.sin6_addr) + || IN6_IS_ADDR_MC_LINKLOCAL(&addr->sa6.sin6_addr)) + print_ifindex(addr->sa6.sin6_scope_id); + else +# endif + tprintf("%u", addr->sa6.sin6_scope_id); +# endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */ + } break; #endif #if defined(AF_IPX) @@ -221,17 +212,17 @@ printsock(struct tcb *tcp, long addr, int addrlen) { int i; tprintf("sipx_port=htons(%u), ", - ntohs(addrbuf.sipx.sipx_port)); + ntohs(addr->sipx.sipx_port)); /* Yes, I know, this does not look too * strace-ish, but otherwise the IPX * addresses just look monstrous... * Anyways, feel free if you don't like * this way.. :) */ - tprintf("%08lx:", (unsigned long)ntohl(addrbuf.sipx.sipx_network)); + tprintf("%08lx:", (unsigned long)ntohl(addr->sipx.sipx_network)); for (i = 0; i < IPX_NODE_LEN; i++) - tprintf("%02x", addrbuf.sipx.sipx_node[i]); - tprintf("/[%02x]", addrbuf.sipx.sipx_type); + tprintf("%02x", addr->sipx.sipx_node[i]); + tprintf("/[%02x]", addr->sipx.sipx_type); } break; #endif /* AF_IPX */ @@ -240,21 +231,21 @@ printsock(struct tcb *tcp, long addr, int addrlen) { int i; tprintf("proto=%#04x, if%d, pkttype=", - ntohs(addrbuf.ll.sll_protocol), - addrbuf.ll.sll_ifindex); - printxval(af_packet_types, addrbuf.ll.sll_pkttype, "PACKET_???"); + ntohs(addr->ll.sll_protocol), + addr->ll.sll_ifindex); + printxval(af_packet_types, addr->ll.sll_pkttype, "PACKET_???"); tprintf(", addr(%d)={%d, ", - addrbuf.ll.sll_halen, - addrbuf.ll.sll_hatype); - for (i = 0; i < addrbuf.ll.sll_halen; i++) - tprintf("%02x", addrbuf.ll.sll_addr[i]); + addr->ll.sll_halen, + addr->ll.sll_hatype); + for (i = 0; i < addr->ll.sll_halen; i++) + tprintf("%02x", addr->ll.sll_addr[i]); } break; #endif /* AF_PACKET */ #ifdef AF_NETLINK case AF_NETLINK: - tprintf("pid=%d, groups=%08x", addrbuf.nl.nl_pid, addrbuf.nl.nl_groups); + tprintf("pid=%d, groups=%08x", addr->nl.nl_pid, addr->nl.nl_groups); break; #endif /* AF_NETLINK */ #if defined(AF_BLUETOOTH) && defined(HAVE_BLUETOOTH_BLUETOOTH_H) @@ -263,18 +254,18 @@ printsock(struct tcb *tcp, long addr, int addrlen) "{rc_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X, rc_channel=%d} or " "{l2_psm=htobs(%d), l2_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X, l2_cid=htobs(%d)} or " "{hci_dev=htobs(%d)}", - addrbuf.sco.sco_bdaddr.b[0], addrbuf.sco.sco_bdaddr.b[1], - addrbuf.sco.sco_bdaddr.b[2], addrbuf.sco.sco_bdaddr.b[3], - addrbuf.sco.sco_bdaddr.b[4], addrbuf.sco.sco_bdaddr.b[5], - addrbuf.rc.rc_bdaddr.b[0], addrbuf.rc.rc_bdaddr.b[1], - addrbuf.rc.rc_bdaddr.b[2], addrbuf.rc.rc_bdaddr.b[3], - addrbuf.rc.rc_bdaddr.b[4], addrbuf.rc.rc_bdaddr.b[5], - addrbuf.rc.rc_channel, - btohs(addrbuf.l2.l2_psm), addrbuf.l2.l2_bdaddr.b[0], - addrbuf.l2.l2_bdaddr.b[1], addrbuf.l2.l2_bdaddr.b[2], - addrbuf.l2.l2_bdaddr.b[3], addrbuf.l2.l2_bdaddr.b[4], - addrbuf.l2.l2_bdaddr.b[5], btohs(addrbuf.l2.l2_cid), - btohs(addrbuf.hci.hci_dev)); + addr->sco.sco_bdaddr.b[0], addr->sco.sco_bdaddr.b[1], + addr->sco.sco_bdaddr.b[2], addr->sco.sco_bdaddr.b[3], + addr->sco.sco_bdaddr.b[4], addr->sco.sco_bdaddr.b[5], + addr->rc.rc_bdaddr.b[0], addr->rc.rc_bdaddr.b[1], + addr->rc.rc_bdaddr.b[2], addr->rc.rc_bdaddr.b[3], + addr->rc.rc_bdaddr.b[4], addr->rc.rc_bdaddr.b[5], + addr->rc.rc_channel, + btohs(addr->l2.l2_psm), addr->l2.l2_bdaddr.b[0], + addr->l2.l2_bdaddr.b[1], addr->l2.l2_bdaddr.b[2], + addr->l2.l2_bdaddr.b[3], addr->l2.l2_bdaddr.b[4], + addr->l2.l2_bdaddr.b[5], btohs(addr->l2.l2_cid), + btohs(addr->hci.hci_dev)); break; #endif /* AF_BLUETOOTH && HAVE_BLUETOOTH_BLUETOOTH_H */ /* AF_AX25 AF_APPLETALK AF_NETROM AF_BRIDGE AF_AAL5 @@ -282,13 +273,34 @@ printsock(struct tcb *tcp, long addr, int addrlen) default: tprints("sa_data="); - print_quoted_string(addrbuf.sa.sa_data, - sizeof(addrbuf.sa.sa_data), 0); + print_quoted_string(addr->sa.sa_data, + sizeof(addr->sa.sa_data), 0); break; } tprints("}"); } +void +printsock(struct tcb *tcp, long addr, int addrlen) +{ + sockaddr_buf_t addrbuf; + + if (addrlen < 2) { + printaddr(addr); + return; + } + + if (addrlen > (int) sizeof(addrbuf)) + addrlen = sizeof(addrbuf); + + memset(&addrbuf, 0, sizeof(addrbuf)); + if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad)) + return; + addrbuf.pad[sizeof(addrbuf.pad) - 1] = '\0'; + + print_sockaddr(tcp, &addrbuf, addrlen); +} + #include "xlat/scmvals.h" #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 @@ -1266,40 +1278,10 @@ print_group_req(struct tcb *tcp, long addr, int len) return; } - union { - struct sockaddr *sa; - struct sockaddr_in *sin; -#ifdef HAVE_INET_NTOP - struct sockaddr_in6 *sin6; -#endif - } a = { .sa = (struct sockaddr *) &greq.gr_group }; -#ifdef HAVE_INET_NTOP - char str[INET6_ADDRSTRLEN]; -#endif - - tprintf("{gr_interface=%u, gr_group={sa_family=", greq.gr_interface); - printxval(addrfams, a.sa->sa_family, "AF_???"); - - switch (a.sa->sa_family) { - case AF_INET: - tprintf(", sin_port=htons(%u), sin_addr=inet_addr(\"%s\")}}", - ntohs(a.sin->sin_port), - inet_ntoa(a.sin->sin_addr)); - return; -#ifdef HAVE_INET_NTOP - case AF_INET6: - if (!inet_ntop(AF_INET6, &a.sin6->sin6_addr, str, sizeof(str))) - break; - tprintf(", sin6_port=htons(%u)" - ", inet_pton(AF_INET6, \"%s\", &sin6_addr)}}", - ntohs(a.sin6->sin6_port), str); - return; -#endif /* HAVE_INET_NTOP */ - } - - tprints(", sa_data="); - print_quoted_string(a.sa->sa_data, sizeof(a.sa->sa_data), 0); - tprintf("}}"); + tprintf("{gr_interface=%u, gr_group=", greq.gr_interface); + print_sockaddr(tcp, (const void *) &greq.gr_group, + sizeof(greq.gr_group)); + tprintf("}"); } #endif /* MCAST_JOIN_GROUP */