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 <stddef.h> 6 #include <sys/socket.h> 7 #include <sys/types.h> 8 #include <sys/un.h> 9 #include <arpa/inet.h> 10 11 #include "trace/beauty/generated/sockaddr.c" 12 DEFINE_STRARRAY(socket_families, "PF_"); 13 14 static size_t af_inet__scnprintf(struct sockaddr *sa, size_t sa_size, char *bf, size_t size) 15 { 16 struct sockaddr_in *sin = (struct sockaddr_in *)sa; 17 char tmp[16]; 18 19 if (sa_size < sizeof(*sin)) 20 return 0; 21 22 return scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin->sin_port), 23 inet_ntop(sin->sin_family, &sin->sin_addr, tmp, sizeof(tmp))); 24 } 25 26 static size_t af_inet6__scnprintf(struct sockaddr *sa, size_t sa_size, char *bf, size_t size) 27 { 28 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; 29 u32 flowinfo; 30 char tmp[512]; 31 size_t printed; 32 33 /* RFC 2133's version, which the kernel accepts, lacks sin6_scope_id. */ 34 if (sa_size < offsetof(struct sockaddr_in6, sin6_scope_id)) 35 return 0; 36 37 flowinfo = ntohl(sin6->sin6_flowinfo); 38 printed = scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin6->sin6_port), 39 inet_ntop(sin6->sin6_family, &sin6->sin6_addr, tmp, sizeof(tmp))); 40 if (flowinfo != 0) 41 printed += scnprintf(bf + printed, size - printed, ", flowinfo: %lu", flowinfo); 42 if (sa_size >= sizeof(*sin6) && sin6->sin6_scope_id != 0) 43 printed += scnprintf(bf + printed, size - printed, ", scope_id: %lu", sin6->sin6_scope_id); 44 45 return printed; 46 } 47 48 static size_t af_local__scnprintf(struct sockaddr *sa, size_t sa_size, char *bf, size_t size) 49 { 50 struct sockaddr_un *sun = (struct sockaddr_un *)sa; 51 size_t path_size; 52 53 if (sa_size <= offsetof(struct sockaddr_un, sun_path)) 54 return 0; 55 56 /* The path needn't be NUL terminated. */ 57 path_size = sa_size - offsetof(struct sockaddr_un, sun_path); 58 if (path_size > sizeof(sun->sun_path)) 59 path_size = sizeof(sun->sun_path); 60 61 return scnprintf(bf, size, ", path: %.*s", (int)path_size, sun->sun_path); 62 } 63 64 static size_t (*af_scnprintfs[])(struct sockaddr *sa, size_t sa_size, char *bf, size_t size) = { 65 [AF_LOCAL] = af_local__scnprintf, 66 [AF_INET] = af_inet__scnprintf, 67 [AF_INET6] = af_inet6__scnprintf, 68 }; 69 70 static size_t syscall_arg__scnprintf_augmented_sockaddr(struct syscall_arg *arg, char *bf, size_t size) 71 { 72 const struct augmented_arg *augmented_arg = arg->augmented.args; 73 struct sockaddr *sa = (struct sockaddr *)&augmented_arg->value; 74 size_t sa_size = (size_t)augmented_arg->size; 75 char family[32]; 76 size_t printed; 77 78 strarray__scnprintf(&strarray__socket_families, family, sizeof(family), "%d", arg->show_string_prefix, sa->sa_family); 79 printed = scnprintf(bf, size, "{ .family: %s", family); 80 81 if (sa->sa_family < ARRAY_SIZE(af_scnprintfs) && af_scnprintfs[sa->sa_family]) 82 printed += af_scnprintfs[sa->sa_family](sa, sa_size, bf + printed, size - printed); 83 84 return printed + scnprintf(bf + printed, size - printed, " }"); 85 } 86 87 size_t syscall_arg__scnprintf_sockaddr(char *bf, size_t size, struct syscall_arg *arg) 88 { 89 /* The family printers check the rest of the payload. */ 90 if (syscall_arg__augmented_args_valid(arg, sizeof(sa_family_t))) 91 return syscall_arg__scnprintf_augmented_sockaddr(arg, bf, size); 92 93 return scnprintf(bf, size, "%#lx", arg->val); 94 } 95