ip结构体中各个字段含义

ip(struct)


ip is a struct (structure) in the C programming language. The ip struct is used as a template to form an IPv4 header in a raw socket. The structure can be found in the default include files of most Unix distributions. It is most commonly located in the <netinet/ip.h> header file.


Definition

struct ip {
   unsigned int   ip_hl:4; /* both fields are 4 bits */
   unsigned int   ip_v:4;
   uint8_t        ip_tos;
   uint16_t       ip_len;
   uint16_t       ip_id;
   uint16_t       ip_off;
   uint8_t        ip_ttl;
   uint8_t        ip_p;
   uint16_t       ip_sum;
   struct in_addr ip_src;
   struct in_addr ip_dst;
};

[edit]Fields

unsigned int ip_hl:4

IP header length expressed as a multiple of 32-bit octets or DWORDS (i.e. header length in bytes = value set in ip_hl x 4 [each # counts for 4 octets]). From the hex dump of an IP header this can be read off the value of an unsigned character at offset 0. Typically it will read 45 where 5 is a common default for ip_hl and 4 is ip_v.

Common Defaults: 5; sets header length to 20 bytes (header length without any routing options)
unsigned int ip_v:4

Internet Protocol version

Common Defaults: usually 4 ( IPv4) or 6 ( IPv6)
unsigned char ip_tos;

Type of Service controls the priority of the packet. The first 3 bits stand for routing priority, the next 4 bits for the type of service (delay, throughput, reliability and cost).

Common Defaults: 0x00 (normal)
unsigned short int ip_len;

Total length must contain the total length of the IP datagram. This includes IP, ICMPTCP or UDP header and payload size in bytes.

unsigned short int ip_id;

The ID sequence number is mainly used for reassembly of fragmented IP datagrams.

Common Defaults: Single datagrams - arbitrary ID, Multiple datagrams - sequential ID.
unsigned short int ip_off;

The fragment offset is used for reassembly of fragmented datagrams. The first 3 bits are the fragment flags, the first one always 0, the second the do-not-fragment bit (set by ip_off |= 0x4000) and the third the more-flag or more-fragments-following bit (ip_off |= 0x2000). The following 13 bits is the fragment offset, containing the number of 8-byte big packets already sent.

unsigned char ip_ttl;

Time to live is the amount of hops (routers to pass) before the packet is discarded, and an ICMP error message is returned. Can sometimes be used to reverse engineer the client distance from server (e.g. if ttl = 250 at server, client is probably 5 hops away)

Common Defaults: 64, 255 (max)
unsigned char ip_p;

The transport layer protocol. Can be tcp (6), udp(17), icmp(1), or whatever protocol follows the IP header. Look in /etc/protocols for more.

unsigned short int ip_sum;

The header checksum. Every time anything in the header changes, it needs to be recalculated, or the packet will be discarded by the next router.

struct     in_addr ip_src;

Source IP address - must be converted into binary format (suggested function is inet_pton())

struct     in_addr ip_dst;

Destination IP address - must be converted into binary format (suggested function is inet_pton())

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sockaddr_in 是一个用于表示 IPv4 地址的结构体,定义在 `<netinet/in.h>` 头文件。它的定义如下: ```c struct sockaddr_in { short int sin_family; // 地址族,通常为 AF_INET unsigned short int sin_port; // 端口号 struct in_addr sin_addr; // IPv4 地址 unsigned char sin_zero[8]; // 填充字段,通常为0 }; ``` sockaddr_in 结构体的各个成员参数的含义如下: - `sin_family`:地址族,它指定了地址类型。常用的取值是 `AF_INET`,表示 IPv4 地址族。 - `sin_port`:端口号,用于标识进程间通信的端口。以网络字节序存储,需要使用 `htons()` 函数进行转换。 - `sin_addr`:IPv4 地址,用于存储 IP 地址。它是一个结构体类型(struct in_addr),包含一个无符号 32 位整数(网络字节序),表示 IPv4 地址。 - `sin_zero`:填充字段,通常为0。用于保持与 sockaddr 结构体的大小相同。 示例用法: ```c #include <stdio.h> #include <netinet/in.h> int main() { struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8080); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); printf("Address family: %d\n", addr.sin_family); printf("Port: %d\n", ntohs(addr.sin_port)); printf("IP Address: %s\n", inet_ntoa(addr.sin_addr)); return 0; } ``` 上述示例,我们创建了一个 sockaddr_in 结构体对象 addr,并设置了其各个成员的值。然后,通过相应的函数(如 `htons()` 和 `inet_addr()`)进行格式转换和地址字符串的转换。最后,打印出了各个成员的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值