将目标地址与 C++ 中的转发表条目进行匹配

发布于 2024-12-12 15:33:23 字数 557 浏览 0 评论 0原文

本质上,我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-12-19 15:33:23

仅比较某些无符号类型 UIntab 的前 n 位:

const unsigned int NBITS = sizeof(UInt) * CHAR_BIT;

UInt a, b;

if ((a >> (NBITS - n)) == (b >> (NBITS - n))) { /*...*/ }

比较底部 < code>m 位:

if ((a << (NBITS - m)) == (b << (NBITS - m))) { /*...*/ }

一些解释:UInt 类型具有 sizeof(UInt) 字节,因此具有 NBITS 位。要比较前 n 位,我们只需将两个数字向右移动,以便仅保留 n 位(新的最高位用零填充,因为类型是无符号的)。为了比较底部的 m 位,我们将两个数字向左移动,直到除了 m 位之外的所有数字都从左侧消失(并且在右侧填充零):

NBITS = 12, n = 4, m = 7:

     a:  1 2 3 4 x A B C D E F G
     b:  1 3 2 5 x A B C D E F G

a >> 8:  0 0 0 0 0 0 0 0 1 2 3 4
b >> 8:  0 0 0 0 0 0 0 0 1 3 2 5

a << 5:  A B C D E F G 0 0 0 0 0
b << 5:  A B C D E F G 0 0 0 0 0

To compare only the top n bits of a and b of some unsigned type UInt:

const unsigned int NBITS = sizeof(UInt) * CHAR_BIT;

UInt a, b;

if ((a >> (NBITS - n)) == (b >> (NBITS - n))) { /*...*/ }

To compare the bottom m bits:

if ((a << (NBITS - m)) == (b << (NBITS - m))) { /*...*/ }

Some explanation: The type UInt has sizeof(UInt) bytes, and thus NBITS bits. To compare the top n bits, we simply shift both numbers to the right so that only n bits remain (the new top bits are filled in with zeros because the type is unsigned). To compare the bottom m bits, we shift both numbers to the left until all but m bits have fallen off the left (and zeros are filled in on the right):

NBITS = 12, n = 4, m = 7:

     a:  1 2 3 4 x A B C D E F G
     b:  1 3 2 5 x A B C D E F G

a >> 8:  0 0 0 0 0 0 0 0 1 2 3 4
b >> 8:  0 0 0 0 0 0 0 0 1 3 2 5

a << 5:  A B C D E F G 0 0 0 0 0
b << 5:  A B C D E F G 0 0 0 0 0
雨轻弹 2024-12-19 15:33:23

也许是类似 daddr&(0xffffffff<<(32-prefix)) == Entry 的东西。

Something like daddr&(0xffffffff<<(32-prefix)) == entry, perhaps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文