How to send big chunk of data in one UDP packet?

2019-05-18 23:05发布

问题:

trying to send UDP packets using Netcat

nc -u 127.0.0.1 1234

And using tcpdump to see actual packets

tcpdump -i any -vv -n udp dst port 1234

In theory the UDP packet size can be about 64K, however when I'm sending a message with size bigger than 2048 the Netcat splits the data and sends in 2 separate UDP packets. For example if I send the following long string

012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789

the tcpdump will show 2 packets:

08:13:09.171828 IP (tos 0x0, ttl 64, id 25262, offset 0, flags [DF], proto UDP (17), length 2076)
    127.0.0.1.33279 > 127.0.0.1.1234: [bad udp cksum 0x061c -> 0x7eb9!] UDP, length 2048
08:13:09.171842 IP (tos 0x0, ttl 64, id 25263, offset 0, flags [DF], proto UDP (17), length 981)
    127.0.0.1.33279 > 127.0.0.1.1234: [bad udp cksum 0x01d5 -> 0x3723!] UDP, length 953

Is it possible to send long messages in one UDP packet via Netcat or some other tool? Couldn't find any options for Netcat on internet.

P.S. I'm using Ubuntu 14.04

回答1:

The problem is because netcat reads in chunks of a specific size and writes in these chunk sizes. And since it is reading from a stream (i.e. stdin) there is no way to define something like message boundary anyway. I cannot see any options to change the read size with nc but if you need some command line to send larger packets you could use Perl or similar like this:

$ dd if=/dev/zero bs=1024 count=32 | perl -MIO::Socket::INET -e \
   'IO::Socket::INET->new(PeerAddr => q[127.0.0.1:1234], Proto => q[udp])->send(do { local $/; <STDIN> })'

$ tcpdump -i lo -n port 1234
... IP 127.0.0.1.42061 > 127.0.0.1.1234: UDP, length 32768