【问题标题】:PHP Sockets - socket_sendto() - UDP Header MismatchPHP 套接字 - socket_sendto() - UDP 标头不匹配
【发布时间】:2013-06-16 14:55:36
【问题描述】:

遇到一个我无法完全解释的问题。

我正在为发送 36 字节“通知”数据包的系统构建一个接口,该系统需要一个 36 字节“确认”数据包。

36 字节的有效负载是一个十六进制字符串(72 个字符),我用bin2hex()“解包”,解析出来,找出需要更改的值,然后使用pack() 重新打包。

当我使用socket_sendto() 时,tcpdump 显示我的传出数据包的 UDP 校验和错误。

这是我正在使用的代码示例。这只是将第一个数据包的有效负载转换为十六进制字符串,

<?php

    $socket      = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    $local_host  = '<source ip>';
    $local_port  = '5858';
    $remote_host = '';
    $remote_port = '';

    socket_bind($socket, $local_host, $local_port) or die('Could not bind to address');

    echo "running...\n";

    while(true)
    {
        socket_recvfrom($socket, $input_buffer, 64, 0, $remote_host, $remote_port);

        $hex_string = bin2hex($input_buffer);

        // assume that I'd parse and modify $hex_string here.
        // for this post, I'm just going to repack the same string.

        $new_packet = pack("H*", $hex_string);
        if($input_buffer === $new_packet) { echo "inbound/outbound contents match\n"; }

        $socket2 = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
        socket_sendto($socket2, $new_packet, strlen($new_packet), 0, $remote_host, $local_port);
        echo "sent\n\n";
    }

我在 tcpdump 输出中得到的是……

(remote ip).1144 > (local ip).5858: [udp sum ok] UDP, length 36
(local ip).35572 > (remote ip).5858: [bad udp cksum 0x37a1 -> 0xaf02!] UDP, length 36

谁能解释一下为什么会这样?

【问题讨论】:

  • 您的系统可能已将校验和卸载到 NIC。

标签: sockets udp php


【解决方案1】:

这是因为关闭 TCP/UDP 校验和卸载到 NIC。请参阅this page 了解更多详细信息,以及可用于禁用此选项的命令,这将抑制错误。

【讨论】:

  • 谢谢。我已经用尽了我的资源来解决这个问题。多谢。 :-)
  • 当我在谷歌上搜索“tcpdump bad udp cksum”时,该页面是第一个点击。 Google 不是您的资源之一?
  • 你有权对此嗤之以鼻。我正在搜索所有内容,因为这是我使用 php 的问题。也许最好说它已经耗尽了我的理智。
猜你喜欢
  • 2012-04-11
  • 2020-07-04
  • 1970-01-01
  • 2012-07-22
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多