strace/socketutils.c

366 lines
8.9 KiB
C
Raw Normal View History

/*
* Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com>
* Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "defs.h"
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/sock_diag.h>
#include <linux/inet_diag.h>
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
#include <linux/unix_diag.h>
#include <linux/rtnetlink.h>
#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
#endif
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
#include <sys/un.h>
#ifndef UNIX_PATH_MAX
# define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
#endif
static bool
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
inet_send_query(const int fd, const int family, const int proto)
{
struct sockaddr_nl nladdr = {
.nl_family = AF_NETLINK
};
struct {
struct nlmsghdr nlh;
struct inet_diag_req_v2 idr;
} req = {
.nlh = {
.nlmsg_len = sizeof(req),
.nlmsg_type = SOCK_DIAG_BY_FAMILY,
.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
},
.idr = {
.sdiag_family = family,
.sdiag_protocol = proto,
.idiag_states = -1
}
};
struct iovec iov = {
.iov_base = &req,
.iov_len = sizeof(req)
};
struct msghdr msg = {
.msg_name = (void *) &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1
};
for (;;) {
if (sendmsg(fd, &msg, 0) < 0) {
if (errno == EINTR)
continue;
return false;
}
return true;
}
}
static int
inet_parse_response(const char *proto_name, const void *data,
const int data_len, const unsigned long inode)
{
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
const struct inet_diag_msg *diag_msg = data;
static const char zero_addr[sizeof(struct in6_addr)];
socklen_t addr_size, text_size;
if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
return -1;
if (diag_msg->idiag_inode != inode)
return 0;
switch(diag_msg->idiag_family) {
case AF_INET:
addr_size = sizeof(struct in_addr);
text_size = INET_ADDRSTRLEN;
break;
case AF_INET6:
addr_size = sizeof(struct in6_addr);
text_size = INET6_ADDRSTRLEN;
break;
default:
return -1;
}
char src_buf[text_size];
if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
src_buf, text_size))
return -1;
if (diag_msg->id.idiag_dport ||
memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
char dst_buf[text_size];
if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
dst_buf, text_size))
return -1;
tprintf("%s:[%s:%u->%s:%u]",
proto_name,
src_buf, ntohs(diag_msg->id.idiag_sport),
dst_buf, ntohs(diag_msg->id.idiag_dport));
} else {
tprintf("%s:[%s:%u]", proto_name, src_buf,
ntohs(diag_msg->id.idiag_sport));
}
return 1;
}
static bool
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
receive_responses(const int fd, const unsigned long inode,
const char *proto_name,
int (* parser) (const char *, const void *,
int, unsigned long))
{
static long buf[8192 / sizeof(long)];
struct sockaddr_nl nladdr = {
.nl_family = AF_NETLINK
};
struct iovec iov = {
.iov_base = buf,
.iov_len = sizeof(buf)
};
int flags = 0;
for (;;) {
ssize_t ret;
struct msghdr msg = {
.msg_name = (void *) &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1
};
ret = recvmsg(fd, &msg, flags);
if (ret < 0) {
if (errno == EINTR)
continue;
return false;
}
struct nlmsghdr *h = (struct nlmsghdr *) buf;
if (!NLMSG_OK(h, ret))
return false;
for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
return false;
int rc = parser(proto_name, NLMSG_DATA(h),
h->nlmsg_len, inode);
if (rc > 0)
return true;
if (rc < 0)
return false;
}
flags = MSG_DONTWAIT;
}
}
static bool
inet_print(const int fd, const int family, const int protocol,
const unsigned long inode, const char *proto_name)
{
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
return inet_send_query(fd, family, protocol)
&& receive_responses(fd, inode, proto_name, inet_parse_response);
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
}
static bool
unix_send_query(const int fd, const unsigned long inode)
{
struct sockaddr_nl nladdr = {
.nl_family = AF_NETLINK
};
struct {
struct nlmsghdr nlh;
struct unix_diag_req udr;
} req = {
.nlh = {
.nlmsg_len = sizeof(req),
.nlmsg_type = SOCK_DIAG_BY_FAMILY,
.nlmsg_flags = NLM_F_REQUEST
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
},
.udr = {
.sdiag_family = AF_UNIX,
.udiag_ino = inode,
.udiag_states = -1,
.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER,
.udiag_cookie = { ~0U, ~0U }
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
}
};
struct iovec iov = {
.iov_base = &req,
.iov_len = sizeof(req)
};
struct msghdr msg = {
.msg_name = (void *) &nladdr,
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1
};
for (;;) {
if (sendmsg(fd, &msg, 0) < 0) {
if (errno == EINTR)
continue;
return false;
}
return true;
}
}
static int
unix_parse_response(const char *proto_name, const void *data,
const int data_len, const unsigned long inode)
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
{
const struct unix_diag_msg *diag_msg = data;
struct rtattr *attr;
int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
uint32_t peer = 0;
size_t path_len = 0;
char path[UNIX_PATH_MAX + 1];
if (rta_len < 0)
return -1;
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
if (diag_msg->udiag_ino != inode)
return 0;
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
if (diag_msg->udiag_family != AF_UNIX)
return -1;
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
for (attr = (struct rtattr *) (diag_msg + 1);
RTA_OK(attr, rta_len);
attr = RTA_NEXT(attr, rta_len)) {
switch (attr->rta_type) {
case UNIX_DIAG_NAME:
if (!path_len) {
path_len = RTA_PAYLOAD(attr);
if (path_len > UNIX_PATH_MAX)
path_len = UNIX_PATH_MAX;
memcpy(path, RTA_DATA(attr), path_len);
path[path_len] = '\0';
}
break;
case UNIX_DIAG_PEER:
if (RTA_PAYLOAD(attr) >= 4)
peer = *(uint32_t *) RTA_DATA(attr);
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
break;
}
}
/*
* print obtained information in the following format:
* "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
*/
if (peer || path_len) {
tprintf("%s:[%lu", proto_name, inode);
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
if (peer)
tprintf("->%u", peer);
if (path_len) {
if (path[0] == '\0') {
tprints(",@");
print_quoted_string(path + 1, path_len,
QUOTE_0_TERMINATED);
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
} else {
tprints(",");
print_quoted_string(path, path_len + 1,
QUOTE_0_TERMINATED);
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
}
}
tprints("]");
return 1;
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
}
else
return -1;
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
}
static bool
unix_print(int fd, const unsigned long inode)
{
return unix_send_query(fd, inode)
&& receive_responses(fd, inode, "UNIX", unix_parse_response);
}
/* 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;
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
if (fd < 0)
return false;
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");
Support unix domain sockets in -yy option This change extends -yy option to handle unix domain sockets: their peer addresses will be printed, similar to inet sockets. For a listening socket, its socket inode and socket path are printed. For an accepted socket, its socket inode, the peer inode, and the socket path are printed. For a client socket, its socket inode and the peer inode are printed. An example of a server side communication using netcat: $ ./strace -yy -e network nc -l -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 setsockopt(3<UNIX:[14728348]>, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3<UNIX:[14728348]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 listen(3<UNIX:[14728348,"/tmp/example.sock"]>, 10) = 0 accept(3<UNIX:[14728348,"/tmp/example.sock"]>, {sa_family=AF_LOCAL, NULL}, [2]) = 4<UNIX:[14727246->14727245,"/tmp/example.sock"]> recvfrom(4<UNIX:[14727246->14727245,"/tmp/example.sock"]>, "INPUT\n", 8192, 0, NULL, NULL) = 6 INPUT An example of a client side communication using netcat: $ ./strace -yy -e network nc -U /tmp/example.sock socket(PF_LOCAL, SOCK_STREAM, 0) = 3 connect(3<UNIX:[14727245]>, {sa_family=AF_LOCAL, sun_path="/tmp/example.sock"}, 19) = 0 getsockopt(3<UNIX:[14727245]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 INPUT ... sendto(3<UNIX:[14727245->14727246]>, "INPUT\n", 6, 0, NULL, 0) = 6 * linux/unix_diag.h: New file. * socketutils.c (send_query): Rename to inet_send_query. (parse_response): Rename to inet_parse_response. (unix_print, unix_send_query, unix_parse_response): New functions. (receive_responses): Add a new argument named parser: a function for handling protocol specific data parts of diag messages. (print_sockaddr_by_inode): Call unix_print. Replace NETLINK_INET_DIAG with NETLINK_SOCK_DIAG, they are equal but NETLINK_SOCK_DIAG looks more generic. Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2014-12-24 14:59:31 +03:00
else if (strcmp(proto_name, "UNIX") == 0)
r = unix_print(fd, inode);
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)))
break;
}
}
close(fd);
return r;
}