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 13299f2..1880448 100644
--- a/net.c
+++ b/net.c
@@ -132,34 +132,158 @@
 	tprintf("%u", ifindex);
 }
 
+typedef union {
+	char pad[128];
+	struct sockaddr sa;
+	struct sockaddr_in sin;
+	struct sockaddr_un sau;
+#ifdef HAVE_INET_NTOP
+	struct sockaddr_in6 sa6;
+#endif
+#if defined(AF_IPX)
+	struct sockaddr_ipx sipx;
+#endif
+#ifdef AF_PACKET
+	struct sockaddr_ll ll;
+#endif
+#ifdef AF_NETLINK
+	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;
+#endif
+} sockaddr_buf_t;
+
+static void
+print_sockaddr(struct tcb *tcp, const sockaddr_buf_t *addr, const int addrlen)
+{
+	tprints("{sa_family=");
+	printxval(addrfams, addr->sa.sa_family, "AF_???");
+	tprints(", ");
+
+	switch (addr->sa.sa_family) {
+	case AF_UNIX:
+		if (addrlen == 2) {
+			tprints("NULL");
+		} else if (addr->sau.sun_path[0]) {
+			tprints("sun_path=");
+			print_quoted_string(addr->sau.sun_path,
+					    sizeof(addr->sau.sun_path) + 1,
+					    QUOTE_0_TERMINATED);
+		} else {
+			tprints("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(addr->sin.sin_port), inet_ntoa(addr->sin.sin_addr));
+		break;
+#ifdef HAVE_INET_NTOP
+	case AF_INET6:
+		{
+			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)
+	case AF_IPX:
+		{
+			int i;
+			tprintf("sipx_port=htons(%u), ",
+					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(addr->sipx.sipx_network));
+			for (i = 0; i < IPX_NODE_LEN; i++)
+				tprintf("%02x", addr->sipx.sipx_node[i]);
+			tprintf("/[%02x]", addr->sipx.sipx_type);
+		}
+		break;
+#endif /* AF_IPX */
+#ifdef AF_PACKET
+	case AF_PACKET:
+		{
+			int i;
+			tprintf("proto=%#04x, if%d, pkttype=",
+					ntohs(addr->ll.sll_protocol),
+					addr->ll.sll_ifindex);
+			printxval(af_packet_types, addr->ll.sll_pkttype, "PACKET_???");
+			tprintf(", addr(%d)={%d, ",
+					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", addr->nl.nl_pid, addr->nl.nl_groups);
+		break;
+#endif /* AF_NETLINK */
+#if defined(AF_BLUETOOTH) && defined(HAVE_BLUETOOTH_BLUETOOTH_H)
+	case AF_BLUETOOTH:
+		tprintf("{sco_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X} or "
+			"{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)}",
+			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
+	AF_X25 AF_ROSE etc. still need to be done */
+
+	default:
+		tprints("sa_data=");
+		print_quoted_string(addr->sa.sa_data,
+				    sizeof(addr->sa.sa_data), 0);
+		break;
+	}
+	tprints("}");
+}
+
 void
 printsock(struct tcb *tcp, long addr, int addrlen)
 {
-	union {
-		char pad[128];
-		struct sockaddr sa;
-		struct sockaddr_in sin;
-		struct sockaddr_un sau;
-#ifdef HAVE_INET_NTOP
-		struct sockaddr_in6 sa6;
-#endif
-#if defined(AF_IPX)
-		struct sockaddr_ipx sipx;
-#endif
-#ifdef AF_PACKET
-		struct sockaddr_ll ll;
-#endif
-#ifdef AF_NETLINK
-		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;
-#endif
-	} addrbuf;
-	char string_addr[100];
+	sockaddr_buf_t addrbuf;
 
 	if (addrlen < 2) {
 		printaddr(addr);
@@ -174,119 +298,7 @@
 		return;
 	addrbuf.pad[sizeof(addrbuf.pad) - 1] = '\0';
 
-	tprints("{sa_family=");
-	printxval(addrfams, addrbuf.sa.sa_family, "AF_???");
-	tprints(", ");
-
-	switch (addrbuf.sa.sa_family) {
-	case AF_UNIX:
-		if (addrlen == 2) {
-			tprints("NULL");
-		} else if (addrbuf.sau.sun_path[0]) {
-			tprints("sun_path=");
-			print_quoted_string(addrbuf.sau.sun_path,
-					    sizeof(addrbuf.sau.sun_path) + 1,
-					    QUOTE_0_TERMINATED);
-		} else {
-			tprints("sun_path=@");
-			print_quoted_string(addrbuf.sau.sun_path + 1,
-					    sizeof(addrbuf.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));
-		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 */
-		break;
-#endif
-#if defined(AF_IPX)
-	case AF_IPX:
-		{
-			int i;
-			tprintf("sipx_port=htons(%u), ",
-					ntohs(addrbuf.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));
-			for (i = 0; i < IPX_NODE_LEN; i++)
-				tprintf("%02x", addrbuf.sipx.sipx_node[i]);
-			tprintf("/[%02x]", addrbuf.sipx.sipx_type);
-		}
-		break;
-#endif /* AF_IPX */
-#ifdef AF_PACKET
-	case AF_PACKET:
-		{
-			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_???");
-			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]);
-		}
-		break;
-
-#endif /* AF_PACKET */
-#ifdef AF_NETLINK
-	case AF_NETLINK:
-		tprintf("pid=%d, groups=%08x", addrbuf.nl.nl_pid, addrbuf.nl.nl_groups);
-		break;
-#endif /* AF_NETLINK */
-#if defined(AF_BLUETOOTH) && defined(HAVE_BLUETOOTH_BLUETOOTH_H)
-	case AF_BLUETOOTH:
-		tprintf("{sco_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X} or "
-			"{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));
-		break;
-#endif /* AF_BLUETOOTH && HAVE_BLUETOOTH_BLUETOOTH_H */
-	/* AF_AX25 AF_APPLETALK AF_NETROM AF_BRIDGE AF_AAL5
-	AF_X25 AF_ROSE etc. still need to be done */
-
-	default:
-		tprints("sa_data=");
-		print_quoted_string(addrbuf.sa.sa_data,
-				    sizeof(addrbuf.sa.sa_data), 0);
-		break;
-	}
-	tprints("}");
+	print_sockaddr(tcp, &addrbuf, addrlen);
 }
 
 #include "xlat/scmvals.h"
@@ -1266,40 +1278,10 @@
 		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 */