Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in dhcp pack method #675

Open
giovanni-bellini-argo opened this issue Jan 28, 2025 · 0 comments
Open

bug in dhcp pack method #675

giovanni-bellini-argo opened this issue Jan 28, 2025 · 0 comments

Comments

@giovanni-bellini-argo
Copy link

when building a dhcp packet

from dpkt.dhcp import (
    DHCP,
    DHCP_OPT_MSGTYPE,
    DHCPDISCOVER
)

packet = DHCP(chaddr=b'ffffffffffff').pack()

the pack method fails with:
struct.error: argument for 's' must be a bytes object.

the opts values are expected to be stored as bytes but the default values are strings.

the fix is really easy, either modify the default value to byte objects

opts = (
        (DHCP_OPT_MSGTYPE, chr(DHCPDISCOVER).encode()),
        (DHCP_OPT_PARAM_REQ, ''.join(map(chr, (DHCP_OPT_REQ_IP,
                                               DHCP_OPT_ROUTER,
                                               DHCP_OPT_NETMASK,
                                               DHCP_OPT_DNS_SVRS))).encode())
    )  # list of (type, data) tuples

or add a check in the pack_opts func and transform data there

def pack_opts(self):
        """Return packed options string."""
        if not self.opts:
            return b''
        l_ = []
        for t, data in self.opts:
            if isinstance(data, str): data = data.encode()
            l_.append(struct.pack("BB%is" % len(data), t, len(data), data))
        l_.append(b'\xff')
        return b''.join(l_)

Details:

  • Python 3.10.12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant