User Datagram Protocol (UDP) UDP [RFC 768] UDP Socket

5
Computer Networks Lecture 25: UDP Socket User Datagram Protocol (UDP) UDP service: “no frills,” “bare bones” extension of best-effort IP “best effort” service, UDP segments may be: lost delivered out of order to app connectionless: no handshaking between UDP sender and receiver each UDP segment handled independently of others UDP [RFC 768] src port # dst port # 32 bits Application data (message) length checksum length, in bytes, of UDP segment, including header Checksum: checksum computed including pseudo IP header (containing src and dst IP addresses, protocol, segment length) computed with all 0’s in the checksum field Internet checksum: 1’s complement of 1’s complement sum when checksum is computed at receiver, result is 0 UDP segment format UDP Socket UDP socket identified by the tuple: <dest IP address, dest port number> (contrast with TCP’s four-tuple!) When a host receives a UDP segment, it: checks the destination port number and directs the UDP segment to the socket with that port number IP datagrams with different source IP addresses and/or source port numbers are directed to the same socket

Transcript of User Datagram Protocol (UDP) UDP [RFC 768] UDP Socket

Computer Networks

Lecture25:UDPSocket

UserDatagramProtocol(UDP)

UDPservice:• “nofrills,”“barebones”extensionofbest-effortIP• “besteffort”service,UDPsegmentsmaybe:•  lost•  deliveredoutofordertoapp• connectionless:•  nohandshakingbetweenUDPsenderandreceiver

•  eachUDPsegmenthandledindependentlyofothers

UDP[RFC768]

srcport# dstport#

32bits

Applicationdata

(message)

length checksumlength,inbytes,of

UDPsegment,includingheader

Checksum:•  checksumcomputedincludingpseudoIPheader(containingsrcanddstIPaddresses,protocol,segmentlength)

•  computedwithall0’sinthechecksumfield

•  Internetchecksum:1’scomplementof1’scomplementsum

�  whenchecksumiscomputedatreceiver,resultis0

UDPsegmentformat

UDPSocketUDPsocketidentifiedbythetuple:<dest IP address, dest port number>

(contrastwithTCP’sfour-tuple!)

WhenahostreceivesaUDPsegment,it:•  checksthedestinationportnumberand•  directstheUDPsegmenttothesocketwiththatportnumber⇒  IPdatagramswithdifferentsourceIPaddressesand/orsource

portnumbersaredirectedtothesamesocket

ConnectionlessDemultiplexing

clientIP:B

P2

clientIP:A

P1P1P3

serverIP:C

SP:6428DP:9157

SP:9157DP:6428

SP:6428DP:5775

SP:5775DP:6428

SPprovides“returnaddress”

UDPSocketSimilartoTCP’sstreamsockets,except:•  socketscreatedusingSOCK_DGRAMinsteadofSOCK_STREAM•  noneedforconnectionestablishmentandtermination

•  noconnect-bind,listen,accepthandshaking,butservermuststillalwayscallbind()•  clientdoesn’tneedtocallconnect()thoughclientmayuseconnect()totellkernelto“remember”theserver’saddressandport#

DataTransmissionNo“connection”betweensenderandreceiver

•  senderexplicitlyattachesIPaddressandport#ofdestinationtoeachpacket,byusingsendto()insteadofsend() •  send()canstillbeusedifserver’saddressandport#havebeen“registered”withkernelusingconnect()

•  ifreceiverusesrecvfrom()itcanextractIPaddressandport#ofsenderfromreceivedpacket•  ifthesearenotneeded,recv()maybeusedinstead

Transmitteddatamaybedeliveredoutoforder,ornotdeliveredatall

NoConnection

Stevens

Somewhereinthesocketstructure:TCPServer:TCPClient:

SocketAddresses

UDPServer:

UDPClient:

DataTransmission

Stevens

UDPpacketshaveboundaries,notformingabyte-streamasinTCP,sorecv()retrievesonemessageatatime,i.e.,noneedtocallrecv()inaloop•  calltorecv()returnsthewholepacket

•  cannotretrieveonlypartsofapacket

•  toinspectapacketcallrecv()withflags=MSG_PEEK

•  sameforrecvfrom()andrecvmsg()

SocketBuffers

Whenreceiver’ssocketreceivebufferisfull,incomingUDPpacketswillsimplybedroppedIfsender’ssocketsendbufferissmallerthanthesizeofUDPdatapassedtosend(),send()returns-1andthesystemglobalvariableerrnoissettoEMSGSIZE TheAPIsgetsockopt()andsetsockopt()areusedtoqueryandsetsocketoptions,includingtheSO_RCVBUFandSO_SNDBUFoptions

Lab5DemoBest-effortnetimgwithnoflow-controlandnoerror-control

Learnhowtosetsendandreceivebuffersizes

Playwithdifferentreceivebuffersizesandobserveeffectoflackofflowcontrol

Playwithdifferentdropratesandobserveeffectoflackoferrorcontrol

Learnhowtosendandreceivelargedatabufferusinggatherwrite(sendmsg())andscatterread(recvmsg())

FILEINMEMORY

HowtoSendaLargeFileErrorrecoveryandcorrectionbothrequireassociatingdatawithsequencenumber(sqn)

Howtoattachsequencenumberstochunksofdatatobesent(assuming1Ksegment)?

sqn0

sqn1K

sqn2Ksqn3Ksqn4K

sqn5K

sqn6Ksqn7K

sqn8K

0 1K 2K 3K

4K 5K 6K 7K

8K

sqn

FILEINMEMORY

GatherWrite

i/ovectorsendthis�sqn0

sqn0

sqn1K

sqn2Ksqn3Ksqn4K

sqn5K

sqn6Ksqn7K

sqn8K

sqn1Ksqn2Ksqn3Ksqn4Ksqn5Ksqn6Ksqn7Ksqn8K

tobesentonnetwork:

Outputdataisgatheredfrommultiplebuffers

reused!

sqn

MEMORYBUFFER

ScatterRead

receiveintohere�sqn0

sqn0

sqn1K

sqn2Ksqn3Ksqn4K

sqn5K

sqn6Ksqn7K

sqn8K

sqn1Ksqn2Ksqn3Ksqn4Ksqn5Ksqn6Ksqn7Ksqn8K

receivedfromnetwork:

i/ovector

Inputdataisscatteredintomultiplebuffers

reused! sqn

MEMORYBUFFER

Out-of-OrderPackets

receiveintohere�sqn0

sqn0

sqn2Ksqn3Ksqn4K

sqn5K

sqn6Ksqn7K

sqn8K

sqn2Ksqn3Ksqn4Ksqn5Ksqn6Ksqn7Ksqn8K

receivedfromnetwork:

i/ovectorreused!

DataScatter/GatherThesocketAPIsforscatter-gatherI/Oarerecvmsg(sd, msg, flags)andsendmsg(sd, msg, flags)

usedtoidentifydestinationwhensending

pointertoavectorof

pointerstodatabuffers

wheresenderinformationisstoredwhenreceiving

TCPcanalsousesendto(),recvfrom(),sendmsg(),andrecvmsg()

[Stevens]

sqn

Lab5:SequenceNumberSequencenumberisperbyte,notperpacket

Thesequencenumberattachedtoapacketisthesequencenumberofitsfirstbyte

Thesequencenumberofabyteisitsbyteoffsetfromthestartofimagebuffer

Thisenablesout-of-orderdatatobeplacedinitsrightpositionintheimagebuffer

Lab5:InteroperabilityTesting

HomefirewallmayblockUDPpacketsUse“ssh -Y”totestclientonCAENeecs489hostsifUDPblockedbyhomefirewall•  onWindows,useMobaXtermorsimilartoolsthatsupportsXforwarding

VNCmayhaveOpenGLcompatibilityissuesandisslower