09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 2 ■ UDP<br />

UDP Fragmentation<br />

I have been claiming so far in this chapter that UDP lets you, as a user, send raw network packets to<br />

which just a little bit <strong>of</strong> information (an IP address and port for both the sender and receiver) has been<br />

added. But you might already have become suspicious, because the foregoing program listings have<br />

suggested that a UDP packet can be up to 64kB in size, whereas you probably already know that your<br />

Ethernet or wireless card can only handle packets <strong>of</strong> around 1,500 bytes instead.<br />

The actual truth is that IP sends small UDP packets as single packets on the wire, but splits up larger UDP<br />

packets into several small physical packets, as was briefly discussed in Chapter 1. This means that large<br />

packets are more likely to be dropped, since if any one <strong>of</strong> their pieces fails to make its way to the destination,<br />

then the whole packet can never be reassembled and delivered to the listening operating system.<br />

But aside from the higher chance <strong>of</strong> failure, this process <strong>of</strong> fragmenting large UDP packets so that<br />

they will fit on the wire should be invisible to your application. There are three ways, however, in which<br />

it might be relevant:<br />

• If you are thinking about efficiency, you might want to limit your protocol to small<br />

packets, to make retransmission less likely and to limit how long it takes the<br />

remote IP stack to reassemble your UDP packet and give it to the waiting<br />

application.<br />

• If the ICMP packets are wrongfully blocked by a firewall that would normally allow<br />

your host to auto-detect the MTU between you and the remote host, then your<br />

larger UDP packets might disappear into oblivion without your ever knowing. The<br />

MTU is the “maximum transmission unit” or “largest packet size” that all <strong>of</strong> the<br />

network devices between two hosts will support.<br />

• If your protocol can make its own choices about how it splits up data between<br />

different packets, and you want to be able to auto-adjust this size based on the<br />

actual MTU between two hosts, then some operating systems let you turn <strong>of</strong>f<br />

fragmentation and receive an error if a UDP packet is too big. This lets you<br />

regroup and split it into several packets if that is possible.<br />

Linux is one operating system that supports this last option. Take a look at Listing 2–3, which sends<br />

a very large message to one <strong>of</strong> the servers that we have just designed.<br />

Listing 2–3. Sending a Very Large UDP Packet<br />

#!/usr/bin/env python<br />

# <strong>Foundations</strong> <strong>of</strong> <strong>Python</strong> <strong>Network</strong> <strong>Programming</strong> - Chapter 2 - big_sender.py<br />

# Send a big UDP packet to our server.<br />

import IN, socket, sys<br />

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<br />

MAX = 65535<br />

PORT = 1060<br />

if len(sys.argv) != 2:<br />

» print >>sys.stderr, 'usage: big_sender.py host'<br />

» sys.exit(2)<br />

hostname = sys.argv[1]<br />

30

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!