UDP
User Datagram Protocol (UDP) Unreliable and unordered datagram service Adds multiplexing No flow control Endpoints identified by ports servers have well-known ports see /etc/services on Unix Optional checksum pseudo header + udp header + data Header format
Initiating a Session Client initiates the connection and sends the clients port in the message header Server port is contained in /etc/services DNS=53 talk=517 Connectionless Primary purpose demux (demultiplex)
Demux Process Application process Application process Application process UDP Packets arrive Ports Queues Packets demultiplexed Port 2000 Port 3000 Port 3100
Using it TCP s=socket(AF_INET, SOC_STREAM,0) bind listen accept connect read/write UDP S=socket(AF_INET, SOCK_DGRAM, 0) bind (receiver) sendto, recvfrom
Broadcasting with UDP You have to enable broadcasting on the socket Set up the address you will be passing to “sendto” to refer to (0xffffffff) Anyone in the broadcast domain will be able to read the packet. A broadcast domain will generally be machines on one port of a router
What is a broadcast domain? Networks are built from hubs (not used much anymore), ethernet switches and routers Packets from machines on different ports of a hub will collide (A hub is like tying the wires together) The collision domain for a network built from hubs is all of the ports on the hub Broadcast packets from machines on different ports of a ethernet switch will be seen on all ports of the switch The broadcast domain for a network built from switches is all of the ports on the switch
How does a ethernet switch work? When you try to connect to mail.cs.byu.edu, your ethernet card doesn’t know which ethernet address to put into the destination So it sends an Address Resolution Protocol (ARP) packet to the broadcast ethernet address asking if anyone knows about mail.cs.byu.edu Everyone else will ignore it, mail.cs.byu.edu should respond with a packet indicating what his ethernet address is Your machine then puts this address into the destination field
Ethernet switches An ethernet switch learns where certain ethernet addresses are by looking at source addresses It starts by behaving like a hub, and forwards all packets out every port When it sees a new source ethernet address on a port, it adds that address to a cache of known addresses When this ethernet address appears in the destination address of a packet, the switch sends it only to the port where this address was seen as a source address
Broadcast Addresses When you send a packet to the broadcast IP address, the broadcast ethernet address is placed in the destination address for the packet. Switches forward packets that are sent to the broadcast ethernet address to all of their ports. Routers ignore these packets, and only forward packets that are sent directly to their address That is why you have to configure a gateway
Where do I send packets? Check to see if the destination IP address is in your broadcast domain Look at the network mask and see if you both resolve to the same network If the destination is not in the same network, send it to the gateway If the destination is in the same network, then ARP to find the ethernet address
ARP Tables You can use /sbin/arp to see current arp table $ /sbin/arp -a cs-private-2.cs.byu.edu ( ) at 00:E0:B1:62:C8:2C [ether] on eth0 You can look at your network mask with ifconfig $ /sbin/ifconfig eth0 Link encap:Ethernet HWaddr 00:08:74:30:AD:2F inet addr: Bcast: Mask: inet6 addr: fe80::208:74ff:fe30:ad2f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets: errors:0 dropped:0 overruns:0 frame:0 TX packets: errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes: (1.5 GiB) TX bytes: (2.6 GiB)
Example (router R2) Network MaskNetwork NumberNext Hop R3 ( ) R1 ( ) (R3)Interface R1 ( ) (R1)Interface 1 Dst= Dst= Dst=
Routers? Hubs? Switches H1 H2H3 H4 H5H6 H7 H8H9 H10 H11H12 Hub X X X X Packet from H1 to H2 Packet from H1 to H10 Packet from H4 to broadcast Seen 171,172,173,174
Routers? Hubs? Switches/Bridges H1 H2H3 H4 H5H6 H7 H8H9 H10 H11H12 Switch X X X X Packet from H1 to H2 Packet from H1 to H10 Packet from H4 to broadcast Seen 171 Seen 171,174 Seen 171,172,173,174 Learn from source Ethernet addresses (layer 2)
Routers? Hubs? Switches H1 H2H3 H4 H5H6 H7 H8H9 H10 H11H12 Router X X X X Packet from H1 to H2 Packet from H1 to H10 Packet from H4 to broadcast Seen 171 Seen 171,174 Seen 172 Use routing protocols and static configurations (layer 3)
Routers? Hubs? Switches H1 H2H3 H4 H5H6 H7 H8H9 H10 H11H12 Hub D D 44.fe d5 S S fe d H10= IP , Ethernet 44.fe d5 D D 44.fe d5 S S fe d5.29 D D 44.fe d5 S S fe d5.29 D D 44.fe d5 S S fe d5.29
Routers? Hubs? Switches H1 H2H3 H4 H5H6 H7 H8H9 H10 H11H12 Switch D D 44.fe d5 S S fe d H10= IP , Ethernet 44.fe d5 D D 44.fe d5 S S fe d5.29 Normally uses only Ethernet addresses
Routers? Hubs? Switches H1 H2H3 H4 H5H6 H7 H8H9 H10 H11H12 Router D D ef.c S S fe d H10= IP , Ethernet 44.fe d5 D D 44.fe d5 S S 55.7e.c ef.c e.c Default Gateway
Our Department We have ethernet switches throughout the labs We have a router on the edge between us and the university Broadcast packets will be seen in most of the labs They wont go outside of the department Some administrators may block UDP broadcasts, because they fill every network link in the broadcast domain If you have a really bad bug in your program, you may bring down the network
Perl Code Sender use IO::Socket::INET; $MySocket=new IO::Socket::INET->new( PeerPort=>1234,Proto=>'udp',PeerAddr=>'localhost'); $msg="This is the message"; $MySocket->send($msg); Receiver $MySocket=new IO::Socket::INET->new( LocalPort=>1234, Proto=>'udp') $MySocket->recv($text,128); print "\nReceived message '", $text,"'\n";
# Client Program use IO::Socket::INET; print ">> Client Program <<"; # Create a new socket $MySocket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'udp',Broadcast=>1, PeerAddr=>inet_ntoa(INADDR_BROADCAST)); # Send messages $def_msg="Enter message to send to server : "; print "\n",$def_msg; while($msg= ) { chomp $msg; if($msg ne '') { print "\nSending message '",$msg,"'"; if($MySocket->send($msg)) { print "..... ","\n"; print $def_msg; } else { # Send an empty message to server and exit $MySocket->send(''); exit 1; } # Client Program use IO::Socket::INET; print ">> Client Program <<"; # Create a new socket $MySocket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'udp',Broadcast=>1, PeerAddr=>inet_ntoa(INADDR_BROADCAST)); # Send messages $def_msg="Enter message to send to server : "; print "\n",$def_msg; while($msg= ) { chomp $msg; if($msg ne '') { print "\nSending message '",$msg,"'"; if($MySocket->send($msg)) { print "..... ","\n"; print $def_msg; } else { # Send an empty message to server and exit $MySocket->send(''); exit 1; } # Server Program use IO::Socket::INET; print ">> Server Program <<\n"; # Create a new socket $MySocket=new IO::Socket::INET->new(LocalPort=>1234,Proto=>'udp'); # Keep receiving messages from client $def_msg="\nReceiving message from client.....\n"; while(1) { $MySocket->recv($text,128); if($text ne '') { print "\nReceived message '", $text,"'\n"; } # If client message is empty exit else { print "Cilent has exited!"; exit 1; } # Server Program use IO::Socket::INET; print ">> Server Program <<\n"; # Create a new socket $MySocket=new IO::Socket::INET->new(LocalPort=>1234,Proto=>'udp'); # Keep receiving messages from client $def_msg="\nReceiving message from client.....\n"; while(1) { $MySocket->recv($text,128); if($text ne '') { print "\nReceived message '", $text,"'\n"; } # If client message is empty exit else { print "Cilent has exited!"; exit 1; }
C Code Create a dgram socket sock = socket( AF_INET, SOCK_DGRAM, 0 ) Set the broadcast bit setsockopt(sock, SOL_SOCKET, SO_BROADCAST,(char *) &one, sizeof(one)); Bind to the port you want to receive on Recvfrom provides sender information in caller recvfrom( sock, buf, sizeof buf,0, (struct sockaddr *) &caller,&caller_len) Sendto parameter contains destination in socketname sendto(socket, buff,sendflags,(struct sockaddr *)&socketname, sizeof socketname ); Can be broadcast address
Local URL Resolution Protocol Peer-to-Peer web-cache Bootstrapping & Peer Discovery UDP broadcast Content Location UDP broadcast for content Content Delivery Direct Download from single peer