Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 1 | #include "defs.h" |
| 2 | #include <netinet/in.h> |
| 3 | #include <sys/socket.h> |
| 4 | #include <arpa/inet.h> |
| 5 | #include <linux/netlink.h> |
| 6 | #include <linux/sock_diag.h> |
| 7 | #include <linux/inet_diag.h> |
| 8 | |
| 9 | static bool |
| 10 | send_query(const int fd, const int family, const int proto) |
| 11 | { |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 12 | struct sockaddr_nl nladdr = { |
| 13 | .nl_family = AF_NETLINK |
| 14 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 15 | struct { |
| 16 | struct nlmsghdr nlh; |
| 17 | struct inet_diag_req_v2 idr; |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 18 | } req = { |
| 19 | .nlh = { |
| 20 | .nlmsg_len = sizeof(req), |
| 21 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
| 22 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST |
| 23 | }, |
| 24 | .idr = { |
| 25 | .sdiag_family = family, |
| 26 | .sdiag_protocol = proto, |
| 27 | .idiag_states = -1 |
| 28 | } |
| 29 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 30 | struct iovec iov = { |
| 31 | .iov_base = &req, |
| 32 | .iov_len = sizeof(req) |
| 33 | }; |
| 34 | struct msghdr msg = { |
| 35 | .msg_name = (void*)&nladdr, |
| 36 | .msg_namelen = sizeof(nladdr), |
| 37 | .msg_iov = &iov, |
| 38 | .msg_iovlen = 1 |
| 39 | }; |
| 40 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 41 | for (;;) { |
| 42 | if (sendmsg(fd, &msg, 0) < 0) { |
| 43 | if (errno == EINTR) |
| 44 | continue; |
| 45 | return false; |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static bool |
| 52 | parse_response(const struct inet_diag_msg *diag_msg, const unsigned long inode) |
| 53 | { |
| 54 | static const char zero_addr[sizeof(struct in6_addr)]; |
| 55 | socklen_t addr_size, text_size; |
| 56 | |
| 57 | if (diag_msg->idiag_inode != inode) |
| 58 | return false; |
| 59 | |
| 60 | switch(diag_msg->idiag_family) { |
| 61 | case AF_INET: |
| 62 | addr_size = sizeof(struct in_addr); |
| 63 | text_size = INET_ADDRSTRLEN; |
| 64 | break; |
| 65 | case AF_INET6: |
| 66 | addr_size = sizeof(struct in6_addr); |
| 67 | text_size = INET6_ADDRSTRLEN; |
| 68 | break; |
| 69 | default: |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | char src_buf[text_size]; |
| 74 | |
| 75 | if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src, |
| 76 | src_buf, text_size)) |
| 77 | return false; |
| 78 | |
| 79 | if (diag_msg->id.idiag_dport || |
| 80 | memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) { |
| 81 | char dst_buf[text_size]; |
| 82 | |
| 83 | if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst, |
| 84 | dst_buf, text_size)) |
| 85 | return false; |
| 86 | |
| 87 | tprintf("%s:%u->%s:%u", |
| 88 | src_buf, ntohs(diag_msg->id.idiag_sport), |
| 89 | dst_buf, ntohs(diag_msg->id.idiag_dport)); |
| 90 | } else { |
| 91 | tprintf("%s:%u", src_buf, ntohs(diag_msg->id.idiag_sport)); |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | static bool |
| 98 | receive_responses(const int fd, const unsigned long inode) |
| 99 | { |
| 100 | static char buf[8192]; |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 101 | struct sockaddr_nl nladdr = { |
| 102 | .nl_family = AF_NETLINK |
| 103 | }; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 104 | struct iovec iov = { |
| 105 | .iov_base = buf, |
| 106 | .iov_len = sizeof(buf) |
| 107 | }; |
| 108 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 109 | for (;;) { |
| 110 | ssize_t ret; |
| 111 | struct nlmsghdr *h; |
| 112 | struct msghdr msg = { |
| 113 | .msg_name = (void*)&nladdr, |
| 114 | .msg_namelen = sizeof(nladdr), |
| 115 | .msg_iov = &iov, |
Dmitry V. Levin | af534b8 | 2014-11-21 19:59:16 +0000 | [diff] [blame] | 116 | .msg_iovlen = 1 |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | ret = recvmsg(fd, &msg, 0); |
| 120 | if (ret < 0) { |
| 121 | if (errno == EINTR) |
| 122 | continue; |
| 123 | return false; |
| 124 | } |
| 125 | if (!ret) |
| 126 | return false; |
| 127 | for (h = (struct nlmsghdr*)buf; |
| 128 | NLMSG_OK(h, ret); |
| 129 | h = NLMSG_NEXT(h, ret)) { |
| 130 | switch (h->nlmsg_type) { |
| 131 | case NLMSG_DONE: |
| 132 | case NLMSG_ERROR: |
| 133 | return false; |
| 134 | } |
| 135 | if (parse_response(NLMSG_DATA(h), inode)) |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame^] | 141 | static bool |
| 142 | inet_print(int fd, int family, int protocol, const unsigned long inode) |
| 143 | { |
| 144 | return send_query(fd, family, protocol) |
| 145 | && receive_responses(fd, inode); |
| 146 | } |
| 147 | |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 148 | /* Given an inode number of a socket, print out the details |
| 149 | * of the ip address and port. */ |
| 150 | bool |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame^] | 151 | print_sockaddr_by_inode(const unsigned long inode, const char *proto_name) |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 152 | { |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 153 | int fd; |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame^] | 154 | bool r = false; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 155 | |
| 156 | fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG); |
| 157 | if (fd < 0) |
| 158 | return false; |
| 159 | |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame^] | 160 | if (proto_name) { |
| 161 | if (strcmp(proto_name, "TCP") == 0) |
| 162 | r = inet_print(fd, AF_INET, IPPROTO_TCP, inode); |
| 163 | else if (strcmp(proto_name, "UDP") == 0) |
| 164 | r = inet_print(fd, AF_INET, IPPROTO_UDP, inode); |
| 165 | else if (strcmp(proto_name, "TCPv6") == 0) |
| 166 | r = inet_print(fd, AF_INET6, IPPROTO_TCP, inode); |
| 167 | else if (strcmp(proto_name, "UDPv6") == 0) |
| 168 | r = inet_print(fd, AF_INET6, IPPROTO_UDP, inode); |
| 169 | } else { |
| 170 | const int families[] = {AF_INET, AF_INET6}; |
| 171 | const int protocols[] = {IPPROTO_TCP, IPPROTO_UDP}; |
| 172 | const size_t flen = ARRAY_SIZE(families); |
| 173 | const size_t plen = ARRAY_SIZE(protocols); |
| 174 | size_t fi, pi; |
| 175 | |
| 176 | for (fi = 0; fi < flen; ++fi) { |
| 177 | for (pi = 0; pi < plen; ++pi) { |
| 178 | if ((r = inet_print(fd, families[fi], protocols[pi], inode))) |
| 179 | goto out; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame^] | 183 | out: |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 184 | close(fd); |
Masatake YAMATO | f605e92 | 2014-12-10 12:55:06 +0900 | [diff] [blame^] | 185 | return r; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 186 | } |