据说 ss 比 netstat 快,想研究一下原理,从这边看到代码:http://bbs.chinaunix.net/thread-3766684-1-1.html,但很不靠谱……整理了一下,供大家参考。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int fd;
struct sockaddr_nl src_addr, dest_addr;
struct
{
struct nlmsghdr nlh;
struct inet_diag_req r;
} req;
struct inet_diag_msg *pkg;
struct msghdr msg;
char buf[8192];
char src_ip[40];
char dest_ip[40];
struct iovec iov;
if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
return -1;
int ret;
ret = fcntl(fd, F_SETFL, O_NONBLOCK);
if (ret <