[Music] let's continue our study of the transport layer now by looking at UDP the user Datagram protocol and hey I've got great news for you this is gonna be relatively short because as we'll see UDP doesn't do too much more than frame and reframe multiplex and demultiplex data that's headed up to and down from the application layer so the topics we'll take a look at are what does what happens at a UDP sender and receiver what does the UDP Datagram format actually look like and then finally we'll take a look at the internet checksum so
let's get started by looking at actions that the UDP sender and receiver UDP is are really simple no frills bare-bones protocol and it can be so simple because of the service that it provides is just best-effort service UDP basically sends segments and hopes they get to the other side those segments can be lost they could be delivered out of order and so UDP doesn't really have to just do that much and because of this really simple service there's no need for handshaking between the UDP sender and receiver no need for the sender and receiver to
have shared state in that sense UDP is referred to as being connectionless also each UDP segment is going to be handled independently of all of the other arriving segments given how simple UDP is one might actually ask why is there even a UDP in the first place well there's actually a lot of good reasons for this first of all there's no connection establishment delay the time between when a UDP sender wants to talk to a UDP receiver and when data can actually flow will see that a UDP sender just sends a Datagram without waiting for
any connection establishment delay so there is no connection state shared between a sender and receiver as a result the UDP headers are also relatively simple so there's little overhead and then maybe most importantly UDP does not provide congestion control that is a UDP sender can blast away as fast as as it desires and should the network become congested can still function in the face of a congested Network something will see that TCP has more trouble with so these very characteristics of UDP make it useful for a certain set of applications for example streaming multimedia applications
are tolerant to a certain amount of segment loss but they're also rate sensitive so we can't congestion control them too strongly for DNS and SNMP it's important that they be able to operate when the network is in a compromised or some kind of congested state and then finally we know that well if you need reliable transfer it's possible to build that in the application level on top of UDP and that's exactly the approach taken by HTTP 3 as we'll see later on in this chapter UDP is defined and requests for comment number 768 and actually
I think it's always a good idea for everyone to read at least one RFC to see what an Internet Protocol specification looks like and UDP being simple means a chart I think it's only three pages so you might want to take a look if you're gonna read one Internet RFC this might be the easiest one to read let's next take a look at the actions of a UDP sender and receiver and let's start with the UDP sender everything begins when the an application passes an application layer message down to UDP UDP is then going to
form a UDP segment by filling out certain set of header field values and including the message from above in this example in SNMP message as payload in the UDP segment creating the UDP segment and then passing it down to IP IP in turn is then going to forward that IP Datagram on to the receiving IP host on the receiving side a UDP receiver will receive a segment from the network layer below it will perform a check of the UDP checksum extract the application layer message and then demultiplex that message up to the appropriate application layer
socket well it's admittedly boring pretty boring to take a at packet formats in this case the UDP segment format but really like eating vegetables like eating broccoli it's something that's actually good for you and when you think about it there's a lot of ideas and principles that go behind what's actually contained in each of those header fields so let's take a look at the UDP segment format we see here the structure of the UDP segment and as promised it's pretty simple the header only has four fields two of those fields are the source port number
field and the destination port number field used for multiplexing and demultiplexing as we just studied there's also a length field and we need that because the application data the payload part of the UDP segment can be a variable length and so UDP needs to know exactly how long that UDP segment is and then finally there's a checksum field one of the fields we just encountered in the UDP header was the internet checksum field let's take a look now at what actually goes into that field and how the internet checksum is actually computed and you should
pay close attention here because this is going to come back again when we look at TCP and also when we look at the IP protocol so pay attention here the role of the UDP checksum is to detect errors that is flipped bits in the transmitted segment between sender and receiver and here's the way to think about it imagine I want to send you two numbers I send you those two numbers but in addition I also send you the sum of those two numbers so you now receive three numbers any of those three might have been
changed during transmission you take the first number you received and the second number you received you add them together you check whether that sum equals the checksum the received checksum that I had sent to you if they're different you know there's a problem so imagine I send you the numbers five and six in the sum eleven you receive the numbers four so the five got changed to a four six and eleven you had four and six together you get ten you check 10 against the checksum 11 that I sent you and you say hey there's
a problem with this and so the UDP sender and receiver operate in a completely analogous way on the sending side the sender treats the contents of the UDP segment that includes the UDP header fields and also includes the source and destination IP addresses of the data gram as a sequence of 16-bit integers adds them together and takes the one compliment sum we'll take a look at that in just a second of the segment contents computes the checksum puts that value in the UDP checksum field and then drops the segment down to IP on the receiving
side the receiver computes the checksum of the received segment again that includes the header and the IP addresses it checks whether that computed checksum equals the checksum field value that was placed there by the sender if they're not equal as we just saw then there's an indication of an error what if they're equal well no error has been detected but might there be errors otherwise well more on that in just a second let's now take a look at how the internet checksum is actually computed and imagine we want to compute the checksum over two 16-bit
integers in reality we're going to be computing the checksum over a much more data but let's just imagine in this simple case we've got two 16-bit integers that we want to protect with an internet checksum so I take the two 16-bit integers that we see here I add them together starting at the right zero and one and base two arithmetic is 1 1 and 0 is 1 1 and 1 is 0 carrying a 1 0 0 plus the carry is 1 0 a 1 is 1 1 and 0 is 1 and so on after we
add the 16 bits together we see we have a 1 on the left hand side that's going to be a wrap around we're gonna add that back into the 16-bit sum that we created to get the final sum that's shown here and then finally to compute the checksum we're going to take the ones complement that's flip zeroes to ones and ones to zeroes and we get the internet checksum shown here and so we see that the computation of the internet checksum is relatively simple now here's something you might want to think about what kind of
protection does the internet checksum actually provide against flip bits let's take a look at an example that will shed some light into this and imagine that we've already computed the checksum of the two 16-bit integers that we saw before and imagine now during the transmission the first number and the second number are actually flipped during transmission so for the first number the rightmost two bits the one zero are flipped to zero one and for the second number the rightmost bits zero and one are flipped to one and zero so you should convince yourself that the
internet receiver having received now two incorrect numbers with the bit flip shown here will compute a checksum which is exactly the same as the checksum that was computed and transmitted by the sender and so in this case these errors go undetected now we'll see later when we look at the link layer and also when we look at security we'll see that there are actually much stronger ways to both detect and also correct errors in bits that are transmitted but I think the take-home message here the internet checksum is a form of protection but it's not
going to be foolproof let's wrap up our study of UDP by summarizing what we learned here we learned that UDP is a no frills protocol that with UDP segments can be lost they can be delivered out of order we've even seen that the internet checksum doesn't provide 100% guarantees against bit flips we also learned that UDP does have its uses it's valuable in the sense that there's no RTT delay incurred due to handshaking or set up and that UDP can function even when network service is compromised for instance when the network is congested or overloaded
because there is no built-in congestion control and we've seen that UDP can help with reliability through the use of the checksum now if this no-frills best-effort service isn't enough an application programmer can always use TCP or as we've seen earlier an application programmer can build the additional functionality on top of UDP up in the application layer and this is exactly what HTTP 3 for example does as we'll see shortly so that wraps up our study of the user Datagram protocol we took a look at the sender and receiver actions for UDP senders and receivers we
took a look at the UDP segment format and then we also took a look at the internet checksum now before we actually move on to TCP the other internet transport protocol we're going to one take a careful look at reliability how do we actually provide mechanisms for reliable data transfer over an unreliable medium because that's what TCP is going to have to do so that's coming up next [Music]