将目标地址与 C++ 中的转发表条目进行匹配
本质上,我有两个 4 字节 IP 地址:
u_int32_t daddr; // in the packet
u_int32_t entry; // in the forwarding table
我还有一个与转发表中的条目相匹配的前缀:
unsigned short prefix; // in forwarding table corresponding to entry
我需要根据前缀将 baddr 与条目进行匹配。我很确定这意味着什么:如果例如前缀是 23,那么我必须将条目的前 23 位与 baddr 相匹配。老实说,我什至不知道从哪里开始,因为我不知道如何匹配各个位。
我有一个包含许多条目的转发表,每个条目都有不同的前缀。我不知道如何将 da 与正确的条目相匹配。任何帮助将不胜感激。 我的daddr 存储在我从netinet ip.h 文件中获取的标准ip 标头中。
编辑:我找到了“最长”的匹配。因此,我不是比较条目只是为了检查它们是否相等,而是比较它们以确定有多少位是相同的。显然,最好的匹配是所有位都相同时。
Essentially, I have two 4 byte IP addresses:
u_int32_t daddr; // in the packet
u_int32_t entry; // in the forwarding table
I also have a prefix that goes with entry in the forwarding table:
unsigned short prefix; // in forwarding table corresponding to entry
I need to match the daddr to the entry based on the prefix. I am pretty sure what this means is: if e.g the prefix is 23, then I have to match the first 23 bits of the entry with the daddr. I honestly don't even know where to start because I don't know how to match individual bits.
I have a forwarding table with lots of entries which each have a different prefix. I am not sure how to match the da with the correct entry.. Any help would be much appreciated.
My daddr is stored in a standard ip header that I got from netinet ip.h file.
EDIT: I have find the "longest" match. So I am not comparing the entries to only check if they are equal, I am comparing them to determine how many bits are same. The best match is obviously when all of the bits are the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅比较某些无符号类型
UInt
的a
和b
的前n
位:比较底部 < code>m 位:
一些解释:
UInt
类型具有sizeof(UInt)
字节,因此具有NBITS
位。要比较前 n 位,我们只需将两个数字向右移动,以便仅保留n
位(新的最高位用零填充,因为类型是无符号的)。为了比较底部的m
位,我们将两个数字向左移动,直到除了m
位之外的所有数字都从左侧消失(并且在右侧填充零):To compare only the top
n
bits ofa
andb
of some unsigned typeUInt
:To compare the bottom
m
bits:Some explanation: The type
UInt
hassizeof(UInt)
bytes, and thusNBITS
bits. To compare the topn
bits, we simply shift both numbers to the right so that onlyn
bits remain (the new top bits are filled in with zeros because the type is unsigned). To compare the bottomm
bits, we shift both numbers to the left until all butm
bits have fallen off the left (and zeros are filled in on the right):也许是类似
daddr&(0xffffffff<<(32-prefix)) == Entry
的东西。Something like
daddr&(0xffffffff<<(32-prefix)) == entry
, perhaps.