【问题标题】:Parsing dhcp-parameter-request-list解析 dhcp-parameter-request-list
【发布时间】:2021-09-16 08:10:17
【问题描述】:

我实际上是在尝试解析 dhcp-parameter-request-list 并查看它是否在列表中包含选项 143。如果它包含 143 选项,那么我必须使用引导服务器列表回复 143 消息。

但是基于当前的评估选项,我只能看到请求消息中是否存在选项 55。 substring() 也不起作用,因为我必须提到要匹配的偏移量和长度,但是 143 消息可以在参数请求列表消息中的任何位置。

例如 - 选项 dhcp-parameter-request-list 1、3、43、53、54、51、58、59、143、121;

在不知道偏移量的情况下,如果 143 选项是选项 dhcp-parameter-request-list 的一部分,我如何解析此消息并匹配?

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。
  • 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 以了解如何提高问题的质量。然后edit你的问题将你的源代码包含为minimal reproducible example,可以被其他人测试。

标签: dhcp


【解决方案1】:

DHCP 选项是一系列标签长度数据。为了检查选项是否是 DHCP 消息的一部分,您必须遍历选项序列,直到找到相应的标签或 255(结束)标签。请注意,len 是数据部分的长度,不包括 taglen 本身。这是一个用于在选项序列中查找标签的 C 函数:

unsigned char *find_option(unsigned char *option, unsigned char tag)
{
    // option points to the beginning of option sequence
    // tag is what we want to find

    while (*option != 255) {
        if (*option == tag)
            return option;

        if (*option != 0)
            option += option[1] + 2; // calc next option 
        else
            option += 1; // option 0 has no len field
    }
    return NULL; // tag not found
}

参数请求列表具有选项标记 55,它被简单地组织为请求的标记号数组。要检查是否请求了 143,请执行以下操作:

unsigned char *opt55 = find_option(option, 55);

for (i=0; opt55 != NULL && i < opt55[1]; ++i) {
    if (opt55[i+2] == 143) {
        // do something if 143 is requested
    }
}

错误检查(例如虚假请求中的缓冲区溢出)被省略以专注于回答您的问题,但需要在您的实际服务器程序中考虑。如果你不是用 C 编程,那么上面的代码更像是伪代码,但应该有助于理解这个想法。这一切都在 RFC2131 中进行了描述,我强烈建议您仔细阅读。

【讨论】:

    猜你喜欢
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2022-01-25
    • 2017-02-20
    • 2017-04-13
    • 2019-04-23
    相关资源
    最近更新 更多