将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

PHP Sockets - socket_sendto() - UDP Header Mismatch
PHP套接字 socket_sendto() header 不匹配

running into a problem i can't quite explain.

I'm building an interface to a system that sends a 36-byte 'notification 'packet, and expects a 36-byte 'acknowledgement 'packet.

The 36-byte payload is a hexadecimal string (72 characters) that i 'unpack 'with bin2hex(), parse out, figure out what values need to be changed, and then repack using pack().

When i use socket_sendto(), tcpdump shows that my outgoing packet has a bad UDP checksum.

Here's a sample of the code i'm using.this is just converting the payload of the first packet into a hex string,


<?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 matchn"; }

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

And what i'm getting in the tcpdump output is...


(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

Can anyone shed some light as to why this is happening?

时间: 作者:

This happens because off TCP/UDP checksum offloading to the NIC.see this page for more details, and commands you can use to disable this option, which will suppress the errors.

原作者:
...