• linux netlink套接字实现相似ss命令 ,统计套接字以及TCP信息


    參考了 ss的源码

    以及 netlink相关资料:http://blog.csdn.net/scdxmoe/article/details/27711205


    实现结果为:

    gcc netlink_dig_530_7.c -o netlink_dig_530_7
    ./netlink_dig_530_7

    state      family     l.addr     l.port       r.addr     r.rport   
    LISTEN     AF_INET   localhost      53         0.0.0.0        0         
    LISTEN     AF_INET   (null)         21         0.0.0.0        0         
    LISTEN     AF_INET   (null)         22         0.0.0.0        0         
    LISTEN     AF_INET   (null)         22         0.0.0.0        0         
    LISTEN     AF_INET   localhost      631        0.0.0.0        0         
    LISTEN     AF_INET   (null)         12865      0.0.0.0        0         
    ESTAB      AF_INET   ubuntu.local   59208      91.189.89.134  80        
    ESTAB      AF_INET   ubuntu.local   22         192.168.0.248  9689      
    ESTAB      AF_INET   ubuntu.local   22         192.168.0.248  9295      
    ESTAB      AF_INET   ubuntu.local   35531      91.189.94.25   80        
    ESTAB      AF_INET   ubuntu.local   22         192.168.0.248  9691  

    本文的实验 并没有实现怎么样获取TCP的窗体值cwnd和RTT值,在ss源代码中我看到了他利用了/proc

    文件来实现获取窗体和RTT值。怎么样用netlink套接字实现呢?还请教各位指点

    源码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <string.h>
    #include <asm/types.h>
    #include <sys/socket.h>
    #include <linux/netlink.h>
    #include <linux/inet_diag.h>
    #include <netinet/tcp.h>
    #include <netdb.h>
    #include<arpa/inet.h>
    struct sk_req {
        struct nlmsghdr nlh;
        struct inet_diag_req r;
    };
    
    
    typedef struct{
     __u8 family;
     __u8 bytelen;
     __s16 bitlen;
     __u32 flags;
     __u32 data[8];
    } inet_prefix;
    
    /*struct namerec
    {
            struct namerec *next;
            const char *name;
            inet_prefix addr;
    };*/
    
    struct tcpstat
    {
            inet_prefix
    
  • 相关阅读:
    TRichView文档结构2
    Qtablewidget 复杂表头
    Qwt 使用mingw编译安装过程(windows):
    [转]为什么有的人工作多年还是老样子?
    软件公司书籍推荐——按角色划分
    Qt error: one or more PCH files were found, but they were invalid
    qt5.5程序打包发布以及依赖【转】
    Qt5.5.1编译出来的程序出现libgcc_s_dw2-1.dll的解决方案
    值得推荐的C/C++框架和库
    开发资源汇总
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7146269.html
Copyright © 2020-2023  润新知