From: Dmitry V. Levin Date: Wed, 10 Feb 2016 17:39:57 +0000 (+0000) Subject: print_sockaddr_by_inode: cleanup protocol lookup X-Git-Tag: v4.12~571 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bcf5975a3e0b9a3c72e4aced11cdc376cc970302;p=strace print_sockaddr_by_inode: cleanup protocol lookup * socketutils.c (tcp_v4_print, udp_v4_print, tcp_v6_print, udp_v6_print): New functions. (print_sockaddr_by_inode): Make "protocols" array static, replace "family" and "protocol" members with "print" in its underlying structure. Iterate through protocols array when the protocol name is specified. --- diff --git a/socketutils.c b/socketutils.c index 6df50fd7..6e56228b 100644 --- a/socketutils.c +++ b/socketutils.c @@ -358,51 +358,67 @@ unix_print(int fd, const unsigned long inode) && receive_responses(fd, inode, "UNIX", unix_parse_response); } +static bool +tcp_v4_print(const int fd, const unsigned long inode) +{ + return inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP"); +} + +static bool +udp_v4_print(const int fd, const unsigned long inode) +{ + return inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP"); +} + +static bool +tcp_v6_print(const int fd, const unsigned long inode) +{ + return inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6"); +} + +static bool +udp_v6_print(const int fd, const unsigned long inode) +{ + return inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6"); +} + /* Given an inode number of a socket, print out the details * of the ip address and port. */ bool print_sockaddr_by_inode(const unsigned long inode, const char *proto_name) { - int fd; - bool r = false; + static const struct { + const char *const name; + bool (*const print)(int, unsigned long); + } protocols[] = { + { "TCP", tcp_v4_print }, + { "UDP", udp_v4_print }, + { "TCPv6", tcp_v6_print }, + { "UDPv6", udp_v6_print }, + { "UNIX", unix_print } + }; - fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); + const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); if (fd < 0) return false; + bool r = false; + unsigned int i; if (proto_name) { - if (strcmp(proto_name, "TCP") == 0) - r = inet_print(fd, AF_INET, IPPROTO_TCP, inode, "TCP"); - else if (strcmp(proto_name, "UDP") == 0) - r = inet_print(fd, AF_INET, IPPROTO_UDP, inode, "UDP"); - else if (strcmp(proto_name, "TCPv6") == 0) - r = inet_print(fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6"); - else if (strcmp(proto_name, "UDPv6") == 0) - r = inet_print(fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6"); - else if (strcmp(proto_name, "UNIX") == 0) - r = unix_print(fd, inode); + for (i = 0; i < ARRAY_SIZE(protocols); ++i) { + if (strcmp(proto_name, protocols[i].name) == 0) { + r = protocols[i].print(fd, inode); + break; + } + } if (!r) { tprintf("%s:[%lu]", proto_name, inode); r = true; } } else { - const struct { - const int family; - const int protocol; - const char *name; - } protocols[] = { - { AF_INET, IPPROTO_TCP, "TCP" }, - { AF_INET, IPPROTO_UDP, "UDP" }, - { AF_INET6, IPPROTO_TCP, "TCPv6" }, - { AF_INET6, IPPROTO_UDP, "UDPv6" } - }; - size_t i; - for (i = 0; i < ARRAY_SIZE(protocols); ++i) { - if ((r = inet_print(fd, protocols[i].family, - protocols[i].protocol, inode, - protocols[i].name))) + if ((r = protocols[i].print(fd, inode))) break; } }