C获取网络信息1

阅读: 评论:0

C获取网络信息1

C获取网络信息1

其实在开发中经常遇到获取系统网络的情况,一种是通过命令获取,这种获取方式显然不专业;那如何通过c的接口获取呢?

先看看相关的结构体:
//以下为头文件中,对结构体的定义或重命名。简化了结构体也方便阅读

其有三个结构体:

  

具本如下所示:

1、socket地址结构


struct sockaddr
{unsigned short sa_family;char sa_data[14];
}
struct sockaddr_in
{   short int sin_family;     //* Address family    其实sockaddr_in与sockaddr意义一致,表达不同而已,都占16字节unsigned short int sin_port; //* Port number   且在调用的时候,我们用此变量,但函数中类型规定要用(struct sockaddr *)struct in_addr sin_addr;     //* Internet address   unsigned char sin_zero[8];     //* Same size as struct sockaddr   
}; 
struct in_addr 
{   unsigned long S_addr;   
}


 

2、网络信息结构体 

 

struct ifreq{
# define IFHWADDRLEN 6
# define IFNAMSIZ IF_NAMESIZEunion{char ifrn_name[IFNAMSIZ];     // Interface name, e.g. "en0". } ifr_ifrn;union{struct sockaddr ifru_addr;struct sockaddr ifru_dstaddr;struct sockaddr ifru_broadaddr;struct sockaddr ifru_netmask;struct sockaddr ifru_hwaddr;short int ifru_flags;int ifru_ivalue;int ifru_mtu;struct ifmap ifru_map;char ifru_slave[IFNAMSIZ]; // Just fits the size char ifru_newname[IFNAMSIZ];__caddr_t ifru_data;} ifr_ifru;};
# define ifr_name ifr_ifrn.ifrn_name     //* interface name 
# define ifr_hwaddr ifr_ifru.ifru_hwaddr     //* MAC address 
# define ifr_addr ifr_ifru.ifru_addr     //* address 
# define ifr_dstaddr ifr_ifru.ifru_dstaddr     //* other end of p-p lnk 
# define ifr_broadaddr ifr_ifru.ifru_broadaddr     //* broadcast address 
# define ifr_netmask ifr_ifru.ifru_netmask     //* interface net mask 
# define ifr_flags ifr_ifru.ifru_flags     //* flags 
# define ifr_metric ifr_ifru.ifru_ivalue     //* metric 
# define ifr_mtu ifr_ifru.ifru_mtu     //* mtu 
# define ifr_map ifr_ifru.ifru_map     //* device map 
# define ifr_slave ifr_ifru.ifru_slave     //* slave device 
# define ifr_data ifr_ifru.ifru_data     //* for use by interface 
# define ifr_ifindex ifr_ifru.ifru_ivalue     //* interface index 
# define ifr_bandwidth ifr_ifru.ifru_ivalue     /* link bandwidth 
# define ifr_qlen ifr_ifru.ifru_ivalue     //* queue length 
# define ifr_newname ifr_ifru.ifru_newname     //* New name 
# define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
# define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
# define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0) */
/* Structure used in SIOCGIFCONF request. Used to retrieve interfaceconfiguration for machine (useful for programs which must know allnetworks accessible). */
/*


 

3、网络整体结构体

struct ifconf{int ifc_len; // Size of buffer. union{__caddr_t ifcu_buf;struct ifreq *ifcu_req;} ifc_ifcu;};
# define ifc_buf ifc_ifcu.ifcu_buf // Buffer address. 
# define ifc_req ifc_ifcu.ifcu_req // Array of structures. 
# define _IOT_ifconf _IOT(_IOTS(struct ifconf),1,0,0,0,0) // not right 
几种结构体的级别:struct ifconf > struct ifreq > struct sockaddr  前者嵌套后者



 

本文发布于:2024-02-04 12:03:57,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170706774955395.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:网络   信息
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23