Lecture4 P3 Computer Networks Protocols (UDP TCP ICMP)
Lecture4 P3 Computer Networks Protocols (UDP TCP ICMP)
Outlines
2
(Sockets/Part 3_ Computer Networks Protocols(UDP_TCP_ICMP))
2
Module
2 UDP Broadcast
3
Module
2 Network Masks
A network mask, or ``netmask'', is used to divide an IP address into a network address and a host address. When you
set up a network, the network mask must be common to all network interfaces on that network. The default network
masks are 255.0.0.0 for class A, 255.255.0.0 for class B, and 255.255.255.0 for class C networks.
4
Module
2 How to Find the Broadcast Address?
A broadcast IP address is only assigned once in each network. It is always the last IP address
of the subnet.
The network address and the broadcast address are not used as computer IP addresses.
In a network, the first address field is reserved for the network and the last for the broadcast
address.
5
Module
2 UDP Broadcast Application
6
Module
2 Using TCP Streams Like Files
File objects can read() and write(), sockets can send() and recv(), and no kind of object can do
both.
Sometimes you will want to treat a socket like a normal Python file object—often because you
want to pass it into code like that of the many Python modules such as pickle, json, and zlib that
can read and write data directly from a file. For this purpose, Python provides a makefile() method
on every socket that returns a Python file object that is really calling recv() and send() behind the
scenes.
7
Module
Using TCP Streams Like Files
2
conn, addr = sock.accept()
print('socket accepted, got connection object')
sockFile = conn.makefile()
(Server side)
message = sockFile.readline()
print('received and read by socket: ' + str(message))
conn.close()
sock.connect((HOST, PORT))
print('socket connected')
sockFile = sock.makefile(mode='w')
(Client side)
message = 'Hi this is my msg Number = ' + str(myCounter) + ' ***' + '\r\n'
print('*** ' + message)
sockFile.write(message)
8
Module
2 Handling Errors
In Python, the socket code raises exceptions when network errors occur.
Virtually every function call that touches the network in any way can and does raise exceptions for various
reasons, for example: servers being down, connections dropped, and so on.
1. Socket Exceptions
1. socket. error for general I/O and communication problems
2. socket.gaierror for errors looking up address information
3. socket. herror for other addressing errors (This exception is thrown when there is a problem with gethostbyname ()
and gethostbyaddr())
4. socket. timeout for handling timeouts that occur after settimeout () has been called on a socket (Python
2.3 and higher)
9
Module
2 Handling Errors
2. Missed Errors
There are certain situations in which communication problems could occur but no exception would
be raised because no error was passed back from the operating system.
One such problem could occur if the remote server drops the connection between the time the
client connects and the time it writes out its request. In this case, the later call to recv() will receive
no data (since the server closed its connection) and the program will terminate successfully.
10
Module
2 Handling Errors with File-like Objects
fd = s.makefile('rw', 2048)
print('sleeping :') while 1:
time.sleep(5) try:
print('Continuing ...') buf = fd.read(2048)
except socket.error as e:
try: print('Error receiving data:', e)
fd.write("GET %s HTTP/1.1" % filename) sys.exit(1)
print('fd has this info: ', fd) if not len(buf):
except socket.error as e: break
print('Error sending data:', e) sys.stdout.write(buf)
sys.exit(1)
11
Module
2 ICMP Protocol
12
Module
Definition: ICMP Protocol
2
The Internet Control Message Protocol (ICMP) is a supporting protocol in the Internet protocol
suite.
It is used by network devices, including routers, to send error messages and operational information
indicating success or failure when communicating with another IP address, for example:
An error is indicated when a requested service is not available or that a host or router could not be reached.
ICMP is not typically used to exchange data between systems, nor is it regularly employed by end-
user network applications (with the exception of some diagnostic tools like ping and traceroute).
ICMP for IPv4 is defined in RFC 792.
ICMPv6, defined by RFC 4443, is used with IPv6.
13
Module
Definition: ICMP Protocol
2
ICMP is a network-layer protocol. There is no TCP or UDP port number associated with ICMP
packets as these numbers are associated with the transport layer above (It does not have source
and destination port numbers because it was designed to communicate network-layer information
between hosts and routers, not between application layer processes).
ICMP messages are typically used for diagnostic or control purposes or generated in response to
errors in IP operations (as specified in RFC 1122). ICMP errors are directed to the source IP address
of the originating packet.
14
Module
Definition: ICMP Protocol
2
ICMP uses the basic support of IP as if it were a higher-level protocol, however, ICMP is actually an
integral part of IP. Although ICMP messages are contained within standard IP packets, ICMP
messages are usually processed as a special case, distinguished from normal IP processing.
In many cases, it is necessary to inspect the contents of the ICMP message and deliver the
appropriate error message to the application responsible for transmitting the IP packet that
prompted the ICMP message to be sent.
15
Module An example of how an ICMPv4
2 redirect message works
16
Module
ICMP & T T L
2
For example, every device (such as an intermediate router) forwarding
an IP datagram first decrements the time to live (TTL) field in the IP
header by one. If the resulting TTL is 0, the packet is discarded and an
ICMP time exceeded in transit message is sent to the datagram's source
address.
17
Module
ICMP in Network
2
18
Module
ICMP in Network
2
ICMP messages:
Are encapsulated in IP packets
so most people would say that
it’s a layer 4 protocol like UDP
or TCP.
19
Module
ICMP Header
2
20
Module
ICMP Packet at Network Layer
2
1472 20 + 8 + 1472 =
20 bytes 8 bytes
bytes (maximum) 1500
According to MTU the size of the ICMP packet cannot be greater than 1500 bytes.
ICMP is actually a user of the IP protocol--in other words, ICMP messages must be encapsulated
within IP packets. However, ICMP is implemented as part of the IP layer. So ICMP processing can
be viewed as occurring parallel to, or as part of, IP processing.
21
Module
ICMP (Checksum)
2
22
Module
ICMP Messages
2
ICMP uses unicast routing for error detection and notification messages.
What is the difference between ICMP and ping?
Ping is a tool commonly used to find the status of a device on a network. Ping is based on
the ICMP protocol. When a Ping process request is sent out as an ICMP echo to the target
device, it replies with an ICMP echo reply if the device is available.
23
Module
ICMP Messages
2
Error-reporting messages
The error-reporting message means that the router encounters a
problem when it processes an IP packet then it reports a
message.
Query messages
The query messages are those messages that help the host to get
the specific information of another host. For example, suppose
there are a client and a server, and the client wants to know
whether the server is live or not, then it sends the ICMP message
to the server.
24
Module
ICMP Messages
2
Type Query/Error (Error Type) Code Description
0 Query 0 Echo reply
3 Error: Destination unreachable 0 Network unreachable
1 Host unreachable
2 Protocol unreachable
3 Port unreachable
4 Fragmentation needed, but the Don't Fragment bit has been set
25
Module
ICMP Message Format
2
26
Module
Error Message : Type 3
2
27
Module
ICMP Query Messages
2
28
Module ICMP Query Messages
2 (Timestamp) Type 13/14
29
Module ICMP Query Messages
2 (Timestamp) Type 13/14
30
Module
2 ICMP Messages (Notes)
31
Module
ICMP & Raw sockets
2
33
Module
ICMP & Raw sockets
2
The biggest problem with RAW-sockets (also the PACKET-sockets) is that there is no
uniform API for using RAW-sockets under different operating systems.
34
Module
ICMP & Raw sockets
2
Raw socket is a type of network socket which provides an express tunnel between an application and an
external source. Neither the operating system nor any other program on the computer has the ability to
interfere with a raw connection. (Need root privilege/ Administrator ).
The primary advantage to using raw socket network handling is that it cuts out the middle man. Since the
operating system doesn't handle the data specifically:
1. Reduce overhead on the network.
2. Save central processing unit (CPU) cycles.
3. Decrease stress on the system hardware.
35
Module
ICMP & Raw sockets
2
36
Module
ICMP Protocol in Python
2
icmplib 3.0.3
The power to form ICMP packets and do ping and traceroute. https://pypi.org/project/icmplib/
icmplib is a brand new and modern implementation of the ICMP protocol in Python.
Latest version : Released: Feb 6, 2022
37
Module
2 icmplib 3.0.3
38
Module
2 ICMP Structure
39
Module
2 ICMP Functions in Python
1. Ping
2. Broadcast Ping
3. Multi-Ping
4. Traceroute
5. Verbose Ping
6. Verbose traceroute
https://github.com/ValentinBELYN/icmplib
40