summaryrefslogtreecommitdiff
path: root/socketutils.c
diff options
context:
space:
mode:
authorFabien Siron <fabien.siron@epita.fr>2016-05-08 10:45:52 +0000
committerDmitry V. Levin <ldv@altlinux.org>2016-05-08 12:37:03 +0000
commit071b9274f4c867c213a92cae587d0ea541618389 (patch)
treeb3be9b979a67c286542f31bdfff0d8d8054127d3 /socketutils.c
parentb6d4b1167b208574797f17b8838b726d9eebb57e (diff)
downloadstrace-071b9274f4c867c213a92cae587d0ea541618389.tar.gz
Factorize send_query functions
* socketutils.c (send_query): New function. (inet_send_query, unix_send_query): Use it.
Diffstat (limited to 'socketutils.c')
-rw-r--r--socketutils.c65
1 files changed, 25 insertions, 40 deletions
diff --git a/socketutils.c b/socketutils.c
index 640c67321..885119848 100644
--- a/socketutils.c
+++ b/socketutils.c
@@ -78,29 +78,14 @@ print_sockaddr_by_inode_cached(const unsigned long inode)
}
static bool
-inet_send_query(const int fd, const int family, const int proto)
+send_query(const int fd, void *req, size_t req_size)
{
struct sockaddr_nl nladdr = {
.nl_family = AF_NETLINK
};
- struct {
- const struct nlmsghdr nlh;
- const 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)
+ .iov_base = req,
+ .iov_len = req_size
};
const struct msghdr msg = {
.msg_name = &nladdr,
@@ -119,6 +104,27 @@ inet_send_query(const int fd, const int family, const int proto)
}
}
+static bool
+inet_send_query(const int fd, const int family, const int proto)
+{
+ struct {
+ const struct nlmsghdr nlh;
+ const 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
+ }
+ };
+ return send_query(fd, &req, sizeof(req));
+}
+
static int
inet_parse_response(const char *const proto_name, const void *const data,
const int data_len, const unsigned long inode)
@@ -232,9 +238,6 @@ inet_print(const int fd, const int family, const int protocol,
static bool
unix_send_query(const int fd, const unsigned long inode)
{
- struct sockaddr_nl nladdr = {
- .nl_family = AF_NETLINK
- };
struct {
const struct nlmsghdr nlh;
const struct unix_diag_req udr;
@@ -251,25 +254,7 @@ unix_send_query(const int fd, const unsigned long inode)
.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
}
};
- struct iovec iov = {
- .iov_base = &req,
- .iov_len = sizeof(req)
- };
- const struct msghdr msg = {
- .msg_name = &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;
- }
+ return send_query(fd, &req, sizeof(req));
}
static int