Marc
Marc

Reputation: 33

PHP Sockets - socket_sendto() - UDP Header Mismatch

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

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?

Upvotes: 2

Views: 1406

Answers (1)

Barmar
Barmar

Reputation: 781235

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.

Upvotes: 0

Related Questions