是否可以创建没有校验和的 UDP 套接字?

发布于 2025-01-10 19:50:02 字数 1711 浏览 0 评论 0原文

我在为大学布置作业时遇到了一些麻烦。作业背后的想法是通过在 Python (3.9.10) 中实现一个名为 bTCP 的基本变体来了解 TCP 的工作原理。超级棒,但就是行不通。整个问题归结为这个函数:

class LossyLayer:

    def __init__(self, btcp_socket, local_ip, local_port, remote_ip, remote_port):

        self._bTCP_socket   = btcp_socket
        self._remote_ip     = remote_ip
        self._remote_port   = remote_port

        self._udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        # Disable UDP checksum generation (and by extension, checking) s.t.
        # corrupt packets actually make it to the bTCP layer.
        # socket.SO_NO_CHECK is not defined in Python, so hardcode the value
        # from /usr/include/asm-generic/socket.h:#define SO_NO_CHECK = 11.
        self._udp_socket.setsockopt(socket.SOL_SOCKET, 11, 1)
        self._udp_socket.bind((local_ip, local_port))

        self._event     = threading.Event()
        self._thread    = threading.Thread(
            target      = handle_incoming_segments,
            args        = (self._bTCP_socket, self._event, self._udp_socket))

        self._thread.start()

    # some other less relevant functions
    # ...

这个想法是创建一个所谓的“有损层”,与 bTCP 下面的层交互,但我们通过 UDP 模拟实际网络。因此,我将类的 socketremote_ipremote_port 设置为某个值。之后,我创建实际的 UDP 套接字并启用地址的重用。然后我想禁用标准校验和验证,但出现以下错误:

OSError: [Errno 42] Protocol not available

上面提供的代码在作业中准确说明。我不必更改代码就能让一切正常工作,所以我想知道我的 MacBook(带有 macOS Monterey 12.2.1)是否支持此设置。这是正确的,还是我忽略了什么? macOS(因为操作系统限制)是否有可能在 SO_NO_CHECK 工作的情况下获得 UDP 套接字?

我真的很好奇为什么它不起作用。希望有人能尽快帮助解答这个谜题!

I’m having some trouble with an assignment for my university. The idea behind the assignment is to learn how TCP works by implementing a basic variant called bTCP in Python (3.9.10). Super awesome, but it just won’t work. The whole problem comes down to this function:

class LossyLayer:

    def __init__(self, btcp_socket, local_ip, local_port, remote_ip, remote_port):

        self._bTCP_socket   = btcp_socket
        self._remote_ip     = remote_ip
        self._remote_port   = remote_port

        self._udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        # Disable UDP checksum generation (and by extension, checking) s.t.
        # corrupt packets actually make it to the bTCP layer.
        # socket.SO_NO_CHECK is not defined in Python, so hardcode the value
        # from /usr/include/asm-generic/socket.h:#define SO_NO_CHECK = 11.
        self._udp_socket.setsockopt(socket.SOL_SOCKET, 11, 1)
        self._udp_socket.bind((local_ip, local_port))

        self._event     = threading.Event()
        self._thread    = threading.Thread(
            target      = handle_incoming_segments,
            args        = (self._bTCP_socket, self._event, self._udp_socket))

        self._thread.start()

    # some other less relevant functions
    # ...

The idea is to create a so called “lossy layer” that interacts with the layer below bTCP, but we emulate the actual network over UDP. So, I set the class' socket, remote_ip and remote_port to some value. Right after that, I create the actual UDP socket and enable reuse of the address. Then I want to disable the standard checksum validation, but I get the following error:

OSError: [Errno 42] Protocol not available

The code provided above is exactly stated in the assignment. I do not have to change the code to be able to get everything working, so I was wondering if my MacBook (with macOS Monterey 12.2.1) does (not) support this setting. Is this correct, or am I overlooking something? Is it even possible for macOS (because operating system restrictions) to get a UDP socket with the SO_NO_CHECK working?

I’m really curious why it doesn’t work. Hopefully someone can help solving the riddle soon!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文