Paz*_*aze 1 c++ networking struct network-programming data-structures
我是一个知道相当多的C++和其他编程语言的中级爱好程序员,现在我正在尝试使用原始套接字而不是c ++中的熟插槽,但是当我查看样本和其他人的源代码时(出于学习目的,我没看到)我可以看到他们都知道如何为网络层制作标题,例如他们写了一个像这样的ip标头结构:
struct IP_HEADER
{
BYTE ver_ihl; // Version (4 bits) and Internet Header Length (4 bits)
BYTE type; // Type of Service (8 bits)
WORD length; // Total size of packet (header + data)(16 bits)
WORD packet_id; // (16 bits)
WORD flags_foff; // Flags (3 bits) and Fragment Offset (13 bits)
BYTE time_to_live; // (8 bits)
BYTE protocol; // (8 bits)
WORD hdr_chksum; // Header check sum (16 bits)
DWORD source_ip; // Source Address (32 bits)
DWORD destination_ip; // Destination Address (32 bits)
} IPHEADER;
Run Code Online (Sandbox Code Playgroud)
但他们怎么知道他们应该在版本上使用BYTE?我知道它的ip头版本,它告诉你的ipv4或ipv6等.但我怎么知道我应该使用BYTE或WORD或任何其他变量?
使事情变得简单:我试图理解如何制作像我自己的结构,让我们说另一种头?
非常感谢!
IP头结构在RFC 791中指定:https://tools.ietf.org/html/rfc791#page-11
该结构将版本(4位)和ihl(4位)组合在一个BYTE(8位)和标志(3位)/碎片(13位)中的WORD(16位)中.所有其他部分一对一映射到BYTE(8位),WORD(16位)和DWORD(32位).
注意:BYTE/WORD/DWORD不是C++数据类型,这可能在代码中的某处定义如下:
typedef unsigned char BYTE; // 1byte
typedef unsigned short WORD; // 2bytes
typedef unsigned long DWORD; // 4bytes
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |