博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux中网络通信中 使用的结构体
阅读量:6993 次
发布时间:2019-06-27

本文共 4651 字,大约阅读时间需要 15 分钟。

"+++++++++++++++++++++++++ Linux TCP/UDP通信中的结构体 +++++++++++++++++++++++++++++++++++++++"

struct udphdr

{
u_int16_t source;
u_int16_t dest;
u_int16_t len;
u_int16_t check;
};

struct iphdr

{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ihl:4;
unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int version:4;
unsigned int ihl:4;
#else
# error "Please fix <bits/endian.h>"
#endif
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
}

/*

* Interface request structure used for socket
* ioctl's. All interface ioctl's must have parameter
* definitions which begin with ifr_name. The
* remainder may be interface specific.
*/

struct ifreq

{
#define IFHWADDRLEN 6
union
{
char ifrn_name[IFNAMSIZ]; /* if 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 ifru_flags;
int ifru_ivalue;
int ifru_mtu;
struct ifmap ifru_map;
char ifru_slave[IFNAMSIZ]; /* Just fits the size */
char ifru_newname[IFNAMSIZ];
void __user * ifru_data;
struct if_settings ifru_settings;
} 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 ifr_settings ifr_ifru.ifru_settings /* Device/proto settings*/

基本介绍:

ifreq结构定义在/usr/include/net/if.h,用来配置ip地址,激活接口,配置MTU等接口信息的。其中包含了
一个接口的名字和具体内容——(是个共用体,有可能是IP地址,广播地址,子网掩码,MAC号,MTU或其他内容)。
ifreq包含在ifconf结构中。而ifconf结构通常是用来保存所有接口的信息的。

 

sockaddr_ll: 表示设备无关的物理层地址结构

struct sockaddr_ll {
unsigned short sll_family; //和sockaddr_in中的sa_family一样,地址族的意思
__be16 sll_protocol; //表示上层的协议类型
int sll_ifindex; // 表示接口类型
unsigned short sll_hatype; // ARP 硬件地址类型
unsigned char sll_pkttype; //包含分组类型
unsigned char sll_halen; //下面地址长度,精确的解释依赖于设备
unsigned char sll_addr[8]; //包括物理层(例如 IEEE 802.3)地址。 sll_addr[8] 为目的MAC地址
};

 

 

struct dhcpMessage    // DHCP信息结构体

{
u_int8_t op; //报文类型, 1 请求报文; 2 回应报文
u_int8_t htype; //硬件地址类型
u_int8_t hlen; //硬件地址长度 以太网中该值为 6
u_int8_t hops; //跳数。 客户端设置为0,也可被一个代理服务器设置
u_int32_t xid; //事物ID, 该值由客户端设置并由服务器返回,用来在client和server之间交流请求个响应
u_int16_t secs; //由client填充,表示从客户端开始获得IP地址或IP地址续借后所使用的秒数
u_int16_t flags; //只有最左边的一个比特有用,0:单播, 1:广播
u_int32_t ciaddr; //客户端的IP 地址。只有client是bound(链接),renew(更新), rebinding(重新绑定)状态,并且能响应ARP请求是,才能被填充
u_int32_t yiaddr; //client IP地址, 与上面的不太理解,可能使用时状态不同
u_int32_t siaddr; //DHCP协议流程下一个阶段要使用的server的IP 地址
u_int32_t giaddr; //DHCP中继器的IP 地址, //注意: 不是地址中定义的网关
u_int8_t chaddr[16];//硬件客户端地址。client 必须设置。
u_int8_t sname[64]; //可选的服务器主机名,以空结尾的字符串,由服务器填写
u_int8_t file[128]; //启动文件名,由空结尾的字符串,
u_int8_t options[308]; /* 312 - cookie */ //可选参数域,格式为"代码 + 长度 + 数据"
};

 

------------------------------------------------------------------------------------- 

typedef uint32_t in_addr_t;

struct in_addr {
  in_addr_t s_addr;
};

-------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------

 struct utsname

  { char sysname[_UTSNAME_SYSNAME_LENGTH];//当前操作系统名
   char nodename[_UTSNAME_NODENAME_LENGTH];//网络上的名称
   char release[_UTSNAME_RELEASE_LENGTH];//当前发布级别
   char version[_UTSNAME_VERSION_LENGTH];//当前发布版本
   char machine[_UTSNAME_MACHINE_LENGTH];//当前硬件体系类型
#if _UTSNAME_DOMAIN_LENGTH - 0
    /* Name of the domain of this node on the network.  */
# ifdef __USE_GNU
    char domainname[_UTSNAME_DOMAIN_LENGTH]; //当前域名
# else
    char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif
  };

例子:功能描述: 

获取当前内核名称和其它信息。
用法: 
#include <sys/utsname.h>
extern int uname (struct utsname *__name) __THROW;

返回说明: 

成功执行时,返回0。失败返回-1,errno被设为EFAULT,表示buf无效。

 

 

-------------------------------------------------------------------------------------

转载于:https://www.cnblogs.com/yyx1-1/p/6531282.html

你可能感兴趣的文章
[C# 基础知识系列]专题三:如何用委托包装多个方法——委托链
查看>>
Oracle DBA手记4:数据安全警示录
查看>>
HDU 4638 Group(分组)
查看>>
BZOJ 1037 生日聚会Party(DP)
查看>>
DG - 将physical standby置于read-only模式
查看>>
nginx的反向代理proxy_pass指令
查看>>
IIs工作原理
查看>>
用NSData玩转二进制文件的读写
查看>>
用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败。
查看>>
ZOJ 1871. Steps
查看>>
Win8使用技巧
查看>>
js+ajax获取文件大小
查看>>
Normalization VS Denormalization [转]
查看>>
perl 实现ascall 码转换
查看>>
KVO 进阶
查看>>
【转】plist文件的内容清空
查看>>
mysql数据库全备和全备还原(使用Xtrabackup)
查看>>
VS无法启动调试:“生成下面的模块时,启用了优化或没有调试信息“
查看>>
C#直接读取磁盘文件(类似linux的Direct IO模式)
查看>>
NHibernate使用之详细图解
查看>>