xref: /openbmc/linux/tools/perf/trace/beauty/sockaddr.c (revision f519f0be)
1 // SPDX-License-Identifier: LGPL-2.1
2 // Copyright (C) 2018, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 
4 #include "trace/beauty/beauty.h"
5 #include <sys/socket.h>
6 #include <sys/types.h>
7 #include <sys/un.h>
8 #include <arpa/inet.h>
9 
10 static const char *socket_families[] = {
11 	"UNSPEC", "LOCAL", "INET", "AX25", "IPX", "APPLETALK", "NETROM",
12 	"BRIDGE", "ATMPVC", "X25", "INET6", "ROSE", "DECnet", "NETBEUI",
13 	"SECURITY", "KEY", "NETLINK", "PACKET", "ASH", "ECONET", "ATMSVC",
14 	"RDS", "SNA", "IRDA", "PPPOX", "WANPIPE", "LLC", "IB", "CAN", "TIPC",
15 	"BLUETOOTH", "IUCV", "RXRPC", "ISDN", "PHONET", "IEEE802154", "CAIF",
16 	"ALG", "NFC", "VSOCK",
17 };
18 DEFINE_STRARRAY(socket_families, "PF_");
19 
20 static size_t af_inet__scnprintf(struct sockaddr *sa, char *bf, size_t size)
21 {
22 	struct sockaddr_in *sin = (struct sockaddr_in *)sa;
23 	char tmp[16];
24 	return scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin->sin_port),
25 			 inet_ntop(sin->sin_family, &sin->sin_addr, tmp, sizeof(tmp)));
26 }
27 
28 static size_t af_inet6__scnprintf(struct sockaddr *sa, char *bf, size_t size)
29 {
30 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
31 	u32 flowinfo = ntohl(sin6->sin6_flowinfo);
32 	char tmp[512];
33 	size_t printed = scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin6->sin6_port),
34 				   inet_ntop(sin6->sin6_family, &sin6->sin6_addr, tmp, sizeof(tmp)));
35 	if (flowinfo != 0)
36 		printed += scnprintf(bf + printed, size - printed, ", flowinfo: %lu", flowinfo);
37 	if (sin6->sin6_scope_id != 0)
38 		printed += scnprintf(bf + printed, size - printed, ", scope_id: %lu", sin6->sin6_scope_id);
39 
40 	return printed;
41 }
42 
43 static size_t af_local__scnprintf(struct sockaddr *sa, char *bf, size_t size)
44 {
45 	struct sockaddr_un *sun = (struct sockaddr_un *)sa;
46 	return scnprintf(bf, size, ", path: %s", sun->sun_path);
47 }
48 
49 static size_t (*af_scnprintfs[])(struct sockaddr *sa, char *bf, size_t size) = {
50 	[AF_LOCAL] = af_local__scnprintf,
51 	[AF_INET]  = af_inet__scnprintf,
52 	[AF_INET6] = af_inet6__scnprintf,
53 };
54 
55 static size_t syscall_arg__scnprintf_augmented_sockaddr(struct syscall_arg *arg, char *bf, size_t size)
56 {
57 	struct sockaddr *sa = (struct sockaddr *)arg->augmented.args;
58 	char family[32];
59 	size_t printed;
60 
61 	strarray__scnprintf(&strarray__socket_families, family, sizeof(family), "%d", arg->show_string_prefix, sa->sa_family);
62 	printed = scnprintf(bf, size, "{ .family: %s", family);
63 
64 	if (sa->sa_family < ARRAY_SIZE(af_scnprintfs) && af_scnprintfs[sa->sa_family])
65 		printed += af_scnprintfs[sa->sa_family](sa, bf + printed, size - printed);
66 
67 	return printed + scnprintf(bf + printed, size - printed, " }");
68 }
69 
70 size_t syscall_arg__scnprintf_sockaddr(char *bf, size_t size, struct syscall_arg *arg)
71 {
72 	if (arg->augmented.args)
73 		return syscall_arg__scnprintf_augmented_sockaddr(arg, bf, size);
74 
75 	return scnprintf(bf, size, "%#x", arg->val);
76 }
77