ini*_*_js 6 binary networking packet-capture rfc packet
我正在我的家庭有线网络上解析 ICMPv6 数据报,但在特定的 RFC 中找不到对位排序约定的明确提及。
多字节字段是网络顺序,但是字节中的位呢?
机器是字节寻址的,但网络硬件序列化位。在图表上,一个 8 位字段的“左侧”位在无符号字节的哪一位(最重要或最不重要)中结束?这是每个 RFC,还是所有 Internet RFC 都一样?
假设我将数据包数据存储在一个名为 的变量中data:
data, remote_peer = sock.recvfrom(1024) #pseudocode
Run Code Online (Sandbox Code Playgroud)
并且我找到了包含标志的特定字节(不是位):
flag_byte = data[some_offset] #pseudocode
Run Code Online (Sandbox Code Playgroud)
尝试解析此消息,RFC4161 第 2.3 节,指定路由信息选项有一个 2 位标志称为Prf。
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Length | Prefix Length |Resvd|Prf|Resvd|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Route Lifetime |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Prefix (Variable Length) |
. .
. .
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
...
Prf (Route Preference)
2-bit signed integer. The Route Preference indicates
whether to prefer the router associated with this prefix
over others, when multiple identical prefixes (for
different routers) have been received. If the Reserved
(10) value is received, the Route Information Option MUST
be ignored.
Run Code Online (Sandbox Code Playgroud)
用这个例子来表达我的问题,(flag_byte & 0x18) >> 3会让我得到两个位。会b & 0x10是符号位吗?我也有兴趣找出指定这是应该的方式的标准。
正如之前的评论中所指出的(感谢ron-maupin),RFC1700指定消息(涵盖互联网协议)在左侧使用最高有效位进行描述。
Whenever an octet represents a numeric quantity the left most bit in
the diagram is the high order or most significant bit. That is, the
bit labeled 0 is the most significant bit. For example, the following
diagram represents the value 170 (decimal).
0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+
|1 0 1 0 1 0 1 0|
+-+-+-+-+-+-+-+-+
Significance of Bits
Similarly, whenever a multi-octet field represents a numeric quantity
the left most bit of the whole field is the most significant bit.
Run Code Online (Sandbox Code Playgroud)
RFC1700被取代RFC3232,这在放了最新的协议定义在线iana.org/protocols。他们似乎保留了那个符号(例如RouterAdvertisementFlags)。
我假设这个关于重要性的约定也适用于 n 位位字段 (1 < n < 8),因此 2 位字段中最左边的位(例如Prf)将是符号位。
应该由硬件来对物理介质上的位进行反序列化,并将它们放置在字节可寻址计算机上一个字节内的正确位置。不同的物理层(物理以太网、wifi、同轴电缆、infiniband、光纤通道)可能会在“线路”上以不同的顺序序列化位,但无论如何,数据包级别的相应字节位置都是相同的。