You are on page 1of 136

What is an IP Address?

IP addresses are used to uniquely identify every host (also known as a network node) on a Transmission Control Protocol/Internet Protocol
(TCP/IP) network. They are virtual addresses assigned by routers. Each of the four 8-bit fields is represented by a decimal number ranging from
0 to 255. IP addresses are typically owned and controlled by a DHCP server running in the local network's router. Devices requesting to join a
local network could be assigned any available local IP address and the assigned IP address could change at any time.

As a side note, the IP address examples shown here are IPv4 addresses. IPv4 is still used for the vast majority of internet communications, but it
will eventually be replaced with IPv6 which uses eight 16-bit fields of addressing.

IPv4 allows 4.3 billion addresses (about one per person).


IPv6 allows 3.4 x 1038 addresses (5 x 1028 addresses per person).

1
Local Network Routers
Routers connect one network to another. They create local networks, control access to them, and route TCP/IP traffic on them. They also enable
local network TCP/IP traffic to move to and from the Internet using Network Address Translation (NAT). Routers also use a firewall to restrict
public Internet access to the local network.

Obtaining an IP Address
2
Before communicating with a device over a local network, we need to join the network. The router creates a local network and controls access to
it. It has the job of allocating and assigning the IP addresses used on its local network. When a device connects to a network, it will automatically
request an IP address from the router.

The following steps show a simplified TCP/IP transaction to get an IP address.

1. PC Generates Request for IP Address


2. IP Address Request Received in Router
3. Router offers an IP address to the PC
4. PC receives IP address from router
5. PC configures its IP address

1 PC Generates Request for IP Address


Note that the PC’s IP Address is 0.0.0.0 before it is assigned. Also note the PC and the router are physically connected together with an Ethernet
cable. The end of every Ethernet cable or WIFI antenna is connected to a network PHY. This PHY is a transceiver responsible for generating and
driving the signals that propagate on the wire. It will also receive and decode signals generated by the PHY at the other end of the connection.

3
 The MAC address has been intentionally left out of this slide to simplify it. We will discuss level 2 (MAC) addressing when switches are introduced.
 The broadcast IP address is 255.255.255.255.

 Dynamic Host Configuration Protocol (DHCP) is the specific application used to request and grant IP addresses.

2 IP Address Request Received in Router


The router receives the packet and finds it has been sent to everyone on the network. It, therefore, has to pay attention to the packet. The router
sees the sender of the packet needs an IP address assigned to it. That’s the router's job, so it creates a new IP address for it. All other hosts on the
local network will eventually discard the packet once they discover the content of the message and realize they cannot provide an IP address.

4
3 Router offers an IP address to the PC
The router allocates a new IP address for the PC and generates a broadcast message containing this IP address.

Question:
Why does the router use the broadcast IP address instead of sending it directly to the PC’s IP address?
Answer:
It can’t send it to the PC because the PC doesn’t know its IP address yet and therefore doesn’t have the ability to filter packets based on its IP
address.

As a side note, this frame sent by the router will include the destination MAC address of the PC. The IP address is a broadcast address, but the
frame also includes the specific destination MAC address of the PC. A switch will use the MAC address to forward this frame to the PC only.
The MAC address is the physical address of a network node. This will be covered in more detail when we describe how a switch works.
5
4 PC receives IP address from router
The PC receives a packet that has been broadcast to everyone on the network. It opens the packet and finds it contains a message for someone
requesting to have an IP address assigned to it. This is the message it has been waiting for!!

5 PC configures its IP address


The PC uses the IP address contained in the message to configure its network interface. It is now able to communicate on the network with it.

6
Switches in Local Networks
Now let’s learn what switches are and how to use them in a local network.

A switch enables the connection of multiple devices to the same network.

Note that each network interface to the switch has its own dedicated PHY responsible for driving the signals on each wire.

7
The uplink port on a switch is the same as the regular ports except that the transmit (Tx) and receive (Rx) signals are reversed. This means a
crossover cable is not required to connect one switch to another. Most new switches have Auto-MDIX interfaces which automatically switch the
Tx and Rx signals if needed.

Switches Inside Routers


Most routers for homes and small business have a built-in switch.

Switches use MAC Addresses


We’ve seen how routers use IP addresses to address hosts on the network. Switches don’t have the intelligence to use IP addresses. They instead
use something called a Media Access Controller (MAC) address.

8
A switch uses a Media Access Controller (MAC) to forward and filter data based on a host’s MAC address, not its IP address.

The MAC controls Layer 2 network functions. It forwards and filters frames based on their MAC addresses.

Every network host has two addresses:


1) IP Address (Layer 3 virtual address)
2) MAC Address (Layer 2 physical address)

A switch has a MAC, but no MAC address. It is transparent to the network. It is never the final destination for network traffic, so it doesn’t need
a MAC address. A router, on the other hand, has two MAC addresses, one used for the local area network (LAN), and one used for the Internet or
wide area network (WAN).

9
 Switches are referred to as Layer 2 devices because they work with Layer 2 (MAC) addresses only. routers are referred to as Layer 3
devices because they work with Layer 3 (IP) addresses only.

 The MAC is responsible for generating the frames that will be sent to the network and receiving frames that come in from the network.
Frames that are received on a network node that doesn't contain the node’s MAC address or MAC broadcast address
(FF:FF:FF:FF:FF:FF) are not forwarded up to Layer 3.

 The MAC allows multiple devices to access the same physical network using Carrier Sense Multiple Access with Collision Detection
(CSMA/CD (Ethernet)), or Carrier Sense Multiple Access with Collision Avoidance (CSMA/CA (WLAN)).

What is a MAC Address?


All hosts that have an IP address also have a MAC (Media Access Controller) address. Unlike IP addresses which are virtual, MAC addresses
are fixed hardware based addresses that never change. They are programmed into a device when it is manufactured and all MAC addresses are
globally unique. They are assigned and managed by the IEEE registration authority.

MAC addresses contain six eight-bit fields expressed as hex numbers.

10
A switch has a Media Access Controller, but no MAC address. It is transparent to the network. It is never the final destination for network traffic,
so it doesn’t need a MAC address. A router, however, has two MAC addresses: one used for the local network or LAN, and one used for the
Internet or WAN.

11
Example: Switch Operation on a Local Network
A switch uses a routing table to associate the switch’s port number with the MAC address connected at the other end of the wire. The following
steps will show how the switch’s routing table is populated.

1. PC Sends a Frame to the Switch


2. Switch Receives Frame
3. Switch Broadcasts Frame to all Nodes
4. Router Sends Reply to PC
5. Switch Forwards Frame to PC

1 PC Sends a Frame to the Switch


12
When a host first connects to a network, it attempts to communicate with a router to obtain an IP addresses.

It creates a packet with a broadcast IP address, then encapsulates the packet into a frame with a broadcast MAC address. This frame is then sent
to all hosts.

The MAC address used to broadcast a frame to all hosts is all ones (FF:FF:FF:FF:FF:FF).

13
2 Switch Receives Frame
When the switch receives this frame, it uses its routing table to associate the host's MAC address with the interface that received the frame.

Remember the frame sent to the Dynamic Host Configuration Protocol (DHCP) server in the router contains the source MAC address.
14
3 Switch Broadcasts Frame to all Nodes
The MAC in the switch sees this is a broadcast frame, so the switch forwards the frame to all hosts connected to it.

15
4 Router Sends Reply to PC
When the switch receives the reply from the router, it associates the router’s MAC address with the interface that received the frame.

5 Switch Forwards Frame to PC


16
The switch looks at this frame to find the destination MAC address and sees that it is already in its routing table. The frame is then forwarded to
the appropriate port. No other port will see this frame.

17
Address Resolution Protocol (ARP)
 ARP Request
 ARP Response
 ARP Table

When the DHCP server running in the local router assigned the PC its IP address, it also let the PC know what the default gateway’s IP address
is. So the PC knows the IP address of its default gateway, but it doesn’t know the MAC address of the default gateway. This is where ARP steps
in to save the day.

The default gateway for a local network is the host that connects the local network to another network. In this example, the default gateway is the
local router.

Address Resolution Protocol Request


The Address Resolution Protocol (ARP) enables a local host to discover another local host’s MAC address corresponding to its IP address.

ARP creates a message that says, “if this is your IP address, send me your MAC address”.

The Link layer creates the frame header by adding its MAC address as the source and the broadcast MAC address as the destination. The frame is
then sent to every host on the local network.

18
Address Resolution Protocol (ARP) Response
When the router and every other host on the local network receives the frame, they will look at the destination MAC address to determine if they
should pay attention to the frame. They will all see the broadcast MAC address as the destination so all will open the message and read it. Once
the other hosts see the message is an ARP request for an IP address other than their own, they will discard the frame and do nothing. The router
will read the message, compare the IP address (the default gateway) to its own, and discover someone has sent it an ARP request. It is now
required to send an ARP response.

It creates an ARP response message that includes its MAC address. The router’s Link layer then creates a frame header with the source and
destination MAC addresses, adds it to the message, then sends it to its physical layer to transmit the frames bits.

19
ARP Table
When the requesting host receives the ARP response, it adds the MAC and IP address to its ARP table. The next time the host needs the MAC
address for this IP address, it will find it in its ARP table and won’t need to generate another ARP request.

20
 Each node on a network maintains its own ARP table.

 ARP is a layer 2 protocol that references but does not use layer 3 IP addresses.

21
Example: Simplified Local Network TCP/IP
Communication
Now that we know how IP addresses are assigned and how switches and routers work, let's observe how two hosts communicate with each other
on a local network.

The following steps show a simplified TCP/IP transaction to get an IP address.

1. Open Web Browser and enter IP Address for Development Board


2. PC Generates and Transmits a Frame
3. Frame Forwarded through the Switch
4. Frame Arrives at Development Board and Forwarded to Web Server
5. Web Server Sends the Webpage to the PC

Assume we have an embedded network device we want to control and monitor. One way we could do this is to create a web page on the
embedded device that displays the status of the embedded device. This web page could also be used to control the device.

22
1 Open Web Browser and enter IP Address for Development Board
We will use a web browser on the PC to access a web page on the development board.

Note that the PC and development board in this example currently have an IP address. This also means the switch has seen a frame sent from
each node, so the switch’s routing table has been updated to show which MAC address is connected to each port.

23
We open a web browser on the PC and enter the IP address for the development board.

2 PC Generates and Transmits a Frame


The PC creates a frame of data then sends it to the PHY to transmit on the local network.

This frame was initiated by the web browser when it created a message requesting to download the web page found at IP address 192.168.1.102.

24
The source and destination IP addresses were added to the message to create a packet, then the source and destination MAC addresses were
added to the packet to create the frame.

This step has been simplified. It does not show the Transport Layer (TCP Protocol). It also doesn’t show the ARP (Address Resolution Protocol)
process of obtaining the destination MAC address.

25
3 Frame Forwarded through the Switch
The Frame arrives at the switch and it is opened to find the destination MAC address. This MAC address is found in the routing table so it sends
it out to Port 0.

26
Note that the switch only opens the frame to determine the MAC address. A switch is a layer 2 device and is only concerned with layer 2 (MAC)
addresses. It doesn’t touch the packet or care what the IP address is.

4 Frame Arrives at Development Board and Forwarded to Web


Server
The frame arrives at the development board and the message is delivered to the web server application. Let’s look at this process in more detail:

When the frame arrives at the development board, the destination MAC address is checked to determine if it needs to pay attention to the frame.
The MAC address matches its own, so it opens the frame to check the destination IP address in the packet. The development board finds the
destination IP address matches its own so it opens the packet to see what the message is. The development board finds the message is for a web
server. The board happens to have a web server running on it, so it sends the message to the web server application.

27
 If the MAC address in the Frame doesn’t match the device’s MAC address, the Frame will be discarded by the Data Link Layer (Layer
2).

 If the IP address in the Packet doesn’t match the device’s IP address, the Packet will be discarded by the Network Layer (Layer 3).

 If a Message is sent to an application (port number) that is not running in the device it will be discarded by the Transport Layer (Layer 4).

28
5 Web Server Sends the Webpage to the PC
Now it’s time for the development board to send the web page. The development board generates a message containing the web page then sends
the frame to the PC. Let’s look at this process in more detail:

The web server running on the development board generates a message containing the web page. The board then adds the source and destination
IP addresses to create a packet and adds the source and destination MAC addresses creating a frame. This frame is then sent to the PHY for
transmission on the local network.

29
Question: What happens next?
Answer:

1. The frame is received at the switch.


2. The switch finds the frame’s destination MAC address and uses its routing table to determine what port to forward the frame to.
3. The frame is sent to the PC
4. The PC opens the frame and checks the destination MAC address to determine if it needs to pay attention to it.
5. The PC opens the packet and checks the destination IP address
6. The PC opens the message and finds the web page (which is just an HTML file) it requested.

30
TCP/IP Five-Layer Software Model Overview
Basic Needs for TCP/IP Communication
Some of the applications we use require us to move data across a network from point A to point B. The Transmission Control Protocol/Internet
Protocol (TCP/IP) network provides a framework for transmitting this data, and it requires some basic information from us to move this data.

We need to specify if we want the most reliable or fastest transmissions and we need to specify where we want the data delivered. Sometimes our
data is routed based on its IP addresses and sometimes its routed based on its MAC address. The data we send needs both addressing capabilities.
This information needs to be sent along with all transmitted data. We also need to physically transmit the data from one location to another.

31
TCP/IP Five Layer Software Model Overview
We need to provide this basic information needed by TCP/IP in a standard format the network can understand. This format is provided by its
five-layer software model.

Each layer provides TCP/IP with the basic information it needs to move our data across the network. These layers group functions according to
the task that needs to be performed. Every function in this model is targeted to help a specific layer perform its job.

32
Each layer only communicates with adjacent layers. Software running in a higher layer does not have to know about or perform tasks delegated
to lower layer functions and vice versa. For example, the software you write for your application only needs to know how to request a connection
with a remote host using the Transport layer. It doesn’t need to know how bits are encoded before transmission. That’s the Physical layer’s job.

33
You are probably familiar with the seven-layer OSI model. TCP/IP simplifies this model to five layers. OSI stands for Open Systems
Interconnect which is a standard communication systems model. The top four layers of the seven layer OSI model have been condensed into the
top two TCP/IP layers.

TCP/IP Five
Layer Model Summary

Before we discuss each layer, let’s briefly summarize what each layer does.

Application Layer

As you might have guessed, the Application layer is where applications requiring network communications live. Examples of these applications
include email clients and web browsers. These applications use the Transport Layer to send requests to connect to remote hosts.

Transport Layer

The Transport layer establishes the connection between applications running on different hosts. It uses TCP for reliable connections and UDP for
fast connections. It keeps track of the processes running in the applications above it by assigning port numbers to them and uses the Network
layer to access the TCP/IP network.

Network Layer

The Network layer is responsible for creating the packets that move across the network. It uses IP addresses to identify the packet’s source and
destination.

34
Data Link Layer

The Data Link layer is responsible for creating the frames that move across the network. These frames encapsulate the packets and use MAC
addresses to identify the source and destination.

Physical Layer

The Physical layer encodes and decodes the bits found in a frame and includes the transceiver that drives and receives the signals on the network.

35
36
Transmit Data Using Network Layers
Now that we know the primary job of each layer, let’s see how they work together to send and receive data across a TCP/IP network.

This is a simplified view of how the network layers work together to generate frames. Higher layers pass information to lower layers. Each layer
adds information called a header to the data being passed to it. This header contains information the layer needs to perform its job. We will start
at the Application layer.

Application Layer

The Application layer generates a message. In this case, the specific application is a web browser requesting a webpage download. This message
is then sent to the Transport layer.

Transport Layer

The Transport layer adds the TCP or UDP header which includes the source and destination port addresses. Additional information like the
packet sequence number used for TCP will also be added to the header. The data generated by the transport layer is referred to as a Segment if
TCP is used, and is referred to as a Datagram if UDP is used. This segment is then sent to the Network layer.

Network Layer

The Network layer adds a header including the source and destination IP address to generate a packet. This packet is then sent to the Data Link
layer.

Data Link Layer

The Data Link layer adds a header containing the MAC address information to create a frame. The frame is then sent it to the Physical layer to
transmit the bits.

37
TCP/IP Five Layer Software Model Terminology Reference

38
39
TCP/IP Application Layer (Layer 5)
The top layer or layer 5 is called the Application layer. This is where most Transmission Control Protocol/Internet Protocol (TCP/IP)
applications live. The software you generate for your end application will typically interact with some of these applications. The most commonly
used TCP/IP application is HTTP (Hypertext Transport Protocol) which is used for surfing the internet.

40
TCP/IP Transport Layer (Layer 4)
Layer 4 is the Transport layer. The transport layer creates virtual Transfer Control Protocol (TCP) or User Datagram Protocol (UDP)
connections between network hosts.

This layer sends and receives data to and from the applications running on its host. The Transport layer assigns port numbers to the processes
running in applications on the host and adds a TCP or UDP header to the messages received from the applications detailing the source and
destination port numbers.

Note that some of the applications, specifically Telnet, SMTP, and HTTP require TCP as the transport protocol while the others use UDP.

41
Some applications require reliable ordered delivery of packets. The TCP protocol provides this capability. It uses error detection, retransmissions
and acknowledgements. This protocol cares about your data.

Other applications don’t care if every packet is received. These applications can take advantage UDP’s lower overhead to enable faster
transmissions.

Typical TCP applications include email and web browsing and typical UDP applications include VoIP and music streaming.

TCP is strictly used for point to point or unicast transmissions while UDP can also be used for multicast and broadcast transmissions.

42
43
TCP and UDP Headers
The header added to messages by the Transport layer includes more than just the source and destination port numbers. Here we are showing all
the information included in TCP and UDP headers.

Note how the TCP protocol requires more information and overhead to guarantee data delivery.

44
TCP/IP Network Layer (Layer 3)
Layer 3 is the Network or Internet layer.

When transmitting data, this layer adds a header containing the source and destination IP addresses to the to the data received from the Transport
layer. The packet it creates will then be forwarded to the MAC or Data Link layer.

When receiving data, this layer is used to determine if the packet received by the host contains the host’s IP address. If it does, the data is
forwarded up to the Transport layer.

45
Routers are referred to as “layer 3” devices because they route packets based on their IP addresses.

TCP/IP IPv4 Packet Header


The Network layer header includes more than just the source and destination IP addresses.

46
TCP/IP Data Link Layer (Layer 2)
Layer 2 is the Data Link layer. This layer uses a Media Access Controller (MAC) to generate the frames that will be transmitted. As the name
suggests, the MAC controls the physical transmission media. The wireless transmission media used for Wi-Fi® or 802.11 has different
requirements from the wired transmission media used for Ethernet or 802.3, and therefore needs a different MAC and PHY. Note the upper
layer software is not aware of or affected by the physical interface.

47
When transmitting data, this layer adds a header containing the source and destination MAC addresses to the packet received from the Network
layer (layer 3). The frame it creates will then be forwarded to the Physical layer.

When receiving data, this layer is used to determine if the frame received by the host contains the host’s MAC address. If it does, the data is
forwarded up to the Network layer.

Every host on the network has at least one MAC address. Laptops typically have two: one for the wired LAN and one for the wireless LAN.
Home routers also typically have two MACs: one for the local network and one for the Internet.

Most switches are referred to as “layer 2” devices because they route frames based on their MAC addresses.

Ethernet and WiFi Frame Format


As you probably guessed, the Data Link layer adds more than just the source and destination MAC addresses to the packet. Note that the MAC
for Ethernet and WiFi are different and generate different frames.

48
49
TCP/IP Physical Layer (Layer 1)
Layer 1 is the Physical layer. It sends and receives signals on the physical wire or antenna to transmit the bits found in frames.

There is a PHY found at the end of every network interface (e.g. end of wire or antenna).

50
Common TCP/IP Applications
 DHCP (Dynamic Host Configuration Protocol)
 DNS (Domain Name System)
 HTTP (HyperText Transfer Protocol)
 NBNS (NetBIOS Name Service)
 SMTP (Simple Mail Transfer Protocol)
 SNMP (Simple Network Managemant Protocol)
 SNTP (Simple Network Time Protocol)
 Telnet (Bi-directional serial text communication)
 TFTP (Trivial File Transfer Protocol)

51
Dynamic Host Configuration Protocol (DHCP)
The dynamic host configuration protocol (DHCP) is the application responsible for requesting and offering IP addresses. A DHCP client
automatically requests an IP address from a DHCP server when a network is detected. A DHCP server typically runs in a router and offers IP
addresses to DHCP clients.

52
DNS (Domain Name System)
A Domain Name System (DNS) enables us to browse to a website by providing the website or domain name instead of the website’s IP address.

It maps domain names to IP addresses. A network host needs the IP address (not the domain or host name) of the web server to generate a Packet.

Hypertext Transfer Protocol (HTTP)


The Hypertext Transfer Protocol (HTTP) is the most commonly used TCP/IP application as it transfers web pages from a web server to a web
browser.

Web pages are written using HTML, which stands for Hypertext Markup Language. In other words, the HTTP is used to transfer HTML files.

53
NetBIOS Name Service (NBNS)
NBNS stands for NetBIOS Name Service. It is used to translate host names on a local network to local IP addresses. This is similar to what DNS
does on the Internet.

In this example, the name “Board_A” has been assigned to a network host. NBNS allows us to access this host by typing its host name into a
browser instead of typing its IP address. It maps host names to IP addresses.

54
Simple Mail Transfer Protocol (SMTP)
Your embedded device can be configured to send emails. SMTP would be used for this.

55
Simple Network Management Protocol (SNMP)
SNMP stands for Simple Network Management Protocol which manages network devices. It is primarily used to monitor network devices for
conditions that may need a user's attention.

56
Simple Network Time Protocol (SNTP)
SNTP stands for Simple Network Time Protocol, which is a service that provides the time of day to network devices. Typical accuracy is in the
range of hundreds of milliseconds.

57
Telnet (Bi-directional serial text communication)
Telnet is an application that enables bi-directional text communication via a terminal application like HyperTerm or Tera Term.

58
Trivial File Transfer Protocol (TFTP)
TFTP is used to transfer files on a local network. It can be used to update firmware on an embedded device with a boot loader.

TFTP is a stripped down version of FTP. TFTP has no provisions for security, so it is only used on local networks. Files are transferred in blocks
of 512 bytes with a maximum file transfer size of 4 GB.

59
60
TCP/IP Ports
Ports are used to identify processes running in the applications on a host.

Let's assume we have two applications running on one PC that require TCP/IP communications. Assume one is a web browser and the other is an
email client.

Both applications send and receive packets with the same IP address, so how does the Transport layer differentiate a web browser packet from an
email packet?

The answer is port numbers.

61
TCP/IP "Well-Known" Ports
“Well-Known" ports are port numbers that have been reserved for common applications, typically server applications. The port numbers
assigned to these server applications have to be known by the client’s Transport layer, so they can add the correct destination port number to
messages. Clients know that servers will be listening for their requests at these reserved port numbers. This graphic shows some examples of
these Well-Known port numbers.

62
The well known port numbers are assigned by IANA which is the Internet Assigned Numbers Authority. IANA is the same group that manages
the DNS Root and IP addresses.

Client side port numbers are generated and assigned by the Transport layer. They could be any number from 1024 to 65535. These port numbers
are typically allocated for short term use and are referred to as “Ephemeral or Dynamic Ports”.

63
64
TCP/IP "Well-Known" Ports
“Well-Known" ports are port numbers that have been reserved for common applications, typically server applications. The port numbers
assigned to these server applications have to be known by the client’s Transport layer, so they can add the correct destination port number to
messages. Clients know that servers will be listening for their requests at these reserved port numbers. This graphic shows some examples of
these Well-Known port numbers.

65
"Ephemeral" (Dynamic) Ports
Client side port numbers are generated and assigned by the Transport layer. They could be any number from 1024 to 65535. These port numbers
are typically allocated for short term use and are referred to as “Ephemeral or Dynamic Ports”.

66
Sockets Defined
A socket is a software concept for a connection. Sockets enable applications to connect to a Transmission Control Protocol/Internet Protocol
(TCP/IP) network.

An application running on a host creates a socket or doorway to connect with an application on another host. Messages pass through this socket
or doorway.

Click image to enlarge.

67
Sockets Enable Virtual TCP or UDP Connections Between Hosts
Sockets enable virtual TCP or UDP communication channels between hosts.

When an application starts on a host, a port number is assigned to a process or a function running in it. When that application wants to
communicate with another host, (go to a website for example) a socket is created.

This example shows three applications requiring three TCP communication channels: Two channels for each of the two web browsers acting as
HTTP clients, and one for the email application acting as an SMTP client.

68
Sockets Implemented as TX & RX buffers
Sockets are physically implemented as transmit (TX) & receive (RX) memory buffers.

When an application wants to transmit a message, a process writes to the socket’s transmit buffer. This same process periodically checks the
socket’s receive buffer for messages being sent by the host on the other end of the virtual connection.

69
The Transport layer delivers messages to the application by writing them to the socket’s receive buffer. The Transport layer also periodically
polls the socket’s transmit buffer to determine if there are messages to send.

70
Example: Established Socket
A socket is created by an application running in a host. The application assigns a transport protocol (TCP or UDP) and source and destination
addresses to the socket. It identifies sockets by assigning numbers to them.

Note the web server has two sockets opened: one for each web page it is serving. These sockets are differentiated by the destination port
numbers.

One host does not assign the socket number on both sides of the communication channel. The socket numbers assigned to each socket are only
used by the host that assigned them. In other words, socket number 1 created on one host may be connected to socket number 5 on another host.

71
Based on the Well-Known source port numbers assigned to each socket, we can determine sockets 1 and 2 were created by an HTTP server
application and socket 3 was created by an SMTP or email server application.

This graphic shows a virtual TCP connection between a client and server. Note the socket numbers are not the same on both sides of the channel.
Hosts create, close and number their own sockets.

72
73
Example: Use Sockets to Create a TCP
Connection
The following steps describe a TCP connection process using sockets.

1. Server Creates Socket and Listens


2. Client Creates a Socket and Connects
3. Transport Layer Delivers Message to Server
4. Server Creates Socket & Process
5. Transport Layer Delivers Message to Client
6. Sockets Closed

1 Server Creates Socket and Listens


A web server creates a socket dedicated to listening for client requests. After the socket exists, the server goes into “listening” mode and waits for
a client's request. It periodically checks for messages received in this socket.

This type of socket is referred to as a connectionless socket. A connectionless socket is used to establish a TCP connection with the HTTP server.
There is no destination IP address or port number defined for this type of socket.

74
75
2 Client Creates a Socket and Connects
When a client wants to download a web page it creates a socket then sends the web page download request to the socket.

76
77
3 Transport Layer Delivers Message to Server
The client’s Transport layer periodically checks its transmit buffers to determine if a message needs to be sent. When a message is found it is
forwarded to the destination address.

78
4 Server Creates Socket & Process
When the server receives the client’s request, it creates a new dedicated socket and process. It then creates a message for the client and sends it to
the socket.

Note this socket uses the client’s destination IP address and port number. This virtual TCP connection is now referred to as “established”.

79
The message sent by the server is the HTML file for the requested webpage.

80
5 Transport Layer Delivers Message to Client
The server’s Transport layer periodically checks its transmit buffers to determine if a message needs to be sent.

When a message is found it is forwarded to the destination address.

6 Sockets Closed
After the client receives the web page it requested, it sends an acknowledge to the server then closes its socket.

81
The server receives the client’s acknowledge then closes its socket.

82
Berkeley Sockets
No class on sockets can be completed without mentioning Berkeley Sockets. Berkeley sockets is an industry standard Application Programming
Interface (API) to create and use sockets. It was initially used as an API for the Unix operating system and was later adopted by TCP/IP.

Berkeley defines 18 standard function names for this purpose. This graphic shows a few examples.

The socket() function creates a socket on the host.


83
The bind() function is typically used on the server side and assigns a socket to its local IP address and port number. Connect() is typically used
on the client side. It creates a socket and also attempts to establish a TCP or UDP connection with a server.

Send(),recv() and write(),read() are used to send and receive the messages to and from the socket.

Berkeley sockets are also sometimes referred to as BSD sockets (Berkeley Software Distribution), named for work done at the University of
California, Berkeley, in the 1980's.

84
Network Address Translation (NAT)
NAT translates between local network and Internet IP addresses.

All hosts that connect to the local network are assigned with local network IP addresses by the DHCP server running in the local network’s
router. When this router connects to the Internet, it is assigned with one IP address from the Internet service provider’s DHCP server. All local
network hosts will share this public IP address to access the Internet.

85
NAT Translation Table
How do all hosts on a local network share the same public IP address? Network Address Translation (NAT) re-assigns IP addresses and port
numbers and keeps track of these re-assignments using its NAT translation table.

86
When the router receives a packet from a local host containing a public IP address, it changes the source IP address to use its Internet IP address
and changes the source port number so it knows which local host process to deliver received packets to. This re-assignment is entered into the
translation table.

Each process requiring Internet access running on the local network is assigned a new IP address and port number by NAT. Each re-assignment
is then entered into the NAT translation table.

87
88
NAT Translation Table
How do all hosts on a local network share the same public IP address? Network Address Translation (NAT) re-assigns IP addresses and port
numbers and keeps track of these re-assignments using its NAT translation table.

When the router receives a packet from a local host containing a public IP address, it changes the source IP address to use its Internet IP address
and changes the source port number so it knows which local host process to deliver received packets to. This re-assignment is entered into the
translation table.

Each process requiring Internet access running on the local network is assigned a new IP address and port number by NAT. Each re-assignment
is then entered into the NAT translation table.

89
90
91
Client Server Defined
The client-server programming model is a distributed computing architecture that segregates information users (clients) from information
providers (servers).

 A client is an application that needs something like a web page or IP address from a server. Clients may contact a server for this
information at any time. Clients are information users.

 A server is an application that provides information or resources to clients. It needs to be always up and running, waiting for requests
from clients.

Client applications communicate only with server applications and vice versa. Clients do not communicate directly with other clients.

An alternative to the client-server architecture is the Peer to Peer (P2P) architecture. In these architectures two or more hosts communicate
directly with each other.

92
DHCP Client Server Example
Example: DHCP Client Server

Here is a very common example of the client-server programming model. The dynamic host configuration protocol (DHCP) is the application
responsible for requesting and offering IP addresses.

A DHCP client automatically requests an IP address from a DHCP server when a network is detected. A DHCP client could request a new IP
address at any time, so the DHCP server must always be active and ready to respond to client requests. The DHCP server application typically
exists in a router, but may also be found running on a network server for larger networks.

93
94
Client Server Programming Model
Client Server Model Defined
The client-server programming model is a distributed computing architecture that segregates information users (clients) from information
providers (servers).

 A client is an application that needs something like a web page or IP address from a server. Clients may contact a server for this information at any
time. Clients are information users.

 A server is an application that provides information or resources to clients. It needs to be always up and running, waiting for requests from clients.

Client applications communicate only with server applications and vice versa. Clients do not communicate directly with other clients.

An alternative to the client-server architecture is the Peer to Peer (P2P) architecture. In these architectures two or more hosts communicate
directly with each other.

Client Server Examples


Example: DHCP Client Server

Here is a very common example of the client-server programming model. The dynamic host configuration protocol (DHCP) is the application
responsible for requesting and offering IP addresses.

95
A DHCP client automatically requests an IP address from a DHCP server when a network is detected. A DHCP client could request a new IP
address at any time, so the DHCP server must always be active and ready to respond to client requests. The DHCP server application typically
exists in a router, but may also be found running on a network server for larger networks.

I can use an HTTP client running on a PC to control the lights at home. This example shows an HTTP client running on a home lighting control
board, which has been configured to monitor a lighting control website running on an Internet web server to determine if lights should be on or
off.
96
I browse to the same lighting control webpage being monitored by the lighting control board, enter my username and password, and now have
the ability to change the webpage. The next time the control board checks this webpage it will see the change and control the lights appropriately.

97
Example: HTTP Client and Server in the Same Local Host
A network host is usually either a client or a server but it is possible for a host to be both. Let’s see an example of this.

My control board may also have an HTTP Server running concurrently with the client. This could be used to serve a simple setup and
configuration web page, which would allow me to change the website and log-in information the HTTP client uses to check for lighting control
updates.

98
Example: Local Network HTTP Server
If you do have an HTTP server running on your embedded device, it could also be used to actually control the device.
This would allow you to eliminate the HTTP client application and Internet web server. At first, this may appear to be the best solution, but looks
can be deceiving.

99
This is probably the easiest solution if the HTTP client is running on the same local network as the lighting control board. Unfortunately, this is
not very common.

The ability to control the lights or anything else from a remote location over the internet is a more likely and useful scenario. Accessing a web
server on a local network from the Internet can be done, but it’s not a trivial task. Deciding where to locate a web server must be carefully
considered.

100
Example: HTTP Client Server

I can use an HTTP client running on a PC to control the lights at home. This example shows an HTTP client running on a home lighting control
board, which has been configured to monitor a lighting control website running on an Internet web server to determine if lights should be on or
off.

I browse to the same lighting control webpage being monitored by the lighting control board, enter my username and password, and now have
the ability to change the webpage. The next time the control board checks this webpage it will see the change and control the lights appropriately.

101
102
Example: HTTP Client and Server in the Same Local Host
A network host is usually either a client or a server but it is possible for a host to be both. Let’s see an example of this.

My control board may also have an HTTP Server running concurrently with the client. This could be used to serve a simple setup and
configuration web page, which would allow me to change the website and log-in information the HTTP client uses to check for lighting control
updates.

103
104
Example: Local Network HTTP Server
If you do have an HTTP server running on your embedded device, it could also be used to actually control the device.
This would allow you to eliminate the HTTP client application and Internet web server. At first, this may appear to be the best solution, but looks
can be deceiving.

This is probably the easiest solution if the HTTP client is running on the same local network as the lighting control board. Unfortunately, this is
not very common.

105
The ability to control the lights or anything else from a remote location over the internet is a more likely and useful scenario. Accessing a web
server on a local network from the Internet can be done, but it’s not a trivial task. Deciding where to locate a web server must be carefully
considered.

106
Internet Server vs. Local Network Server
Internet Server
Accessing an HTTP server on the Internet is effortless. You are doing so now as you read this webpage. You may need to enter a username or
password for some sites, but it really couldn't be easier.

In this example, the webpage that controls and displays the current state of the board runs on an HTTP server in the Internet. The embedded
HTTP client posts its current status to the server and polls for new commands.

A web browser on a PC or smartphone can monitor and control the embedded device via the webpage in the HTTP server. The server may be
implemented on a shared web hosting service from a company like godaddy.com or networksolutions.com, or may be implemented in the cloud,
which is a service of decentralized servers from companies like Amazon, Google, or ioBridge.

These service providers will allow you to choose a website name so your webpage can be easily accessed.

107
108
Local Network Server
Instead of locating our HTTP server on the Internet, we could instead locate it on a local network. In this case, the webpage that controls and
monitors the embedded device is actually running on the embedded device.

Just as in the previous case, a web browser on a PC or smartphone can monitor and control the embedded device via the webpage running in the
embedded HTTP server.

109
Servers on local networks have some challenges that internet servers do not have.
110
Local Network Server Obstacles and Solutions
 Obstacles
 Solutions
o Port Forwarding
o Local Server Website Name
o Dynamic IP Address

Local Network Server Obstacles


Locating an Hypertext Transfer Protocol (HTTP) server on a local network has some challenges.

The firewall in the local router restricts public access to the local network. You will need to allow remote HTTP clients on your local network.

A remote HTTP client will be able to access your webpage using the local network IP address, but you will probably want to provide a website
name for convenience.

Also, your local network may not have a static IP address. If your internet service provider changes the IP address to your local network (if you
have a dynamic IP address), how will a remote HTTP client access your webpage?

111
112
Solutions: Port Forwarding
Generally speaking, we want to prevent public Internet traffic from accessing our private local network. Nevertheless, you can allow Internet
hosts to access your local network through a mechanism in the router called port forwarding.

Port forwarding allows Internet packets destined for a particular port on your local network to be forwarded to a specific local network IP
address. Care must be used when allowing this access.

For our application, we need to configure the local router to forward incoming HTTP server packets to the local IP address assigned to the host
running the HTTP server. Unauthorized access to the webpage can be prevented by requiring a username and password. You should also use
some kind of encryption like SSL to prevent a packet sniffer from stealing your credentials.

113
Properly implemented and secure port forwarding is also referred to as a “firewall pinhole.”

114
Solutions: Local Server Website Name
Your web server is not required to have a website name, but it provides significant advantages.

Website names are easier to remember than IP addresses and if the IP address changes, DNS servers can be automatically updated to handle the
change.

115
You can acquire domain names from domain name service companies like GoDaddy.com or DynDNS.com.

116
Solutions: Dynamic IP Address
Before we discuss how dynamic IP addresses affect the ability to access a local server, we first need to know what a dynamic IP address is.

Dynamic IP addresses are IP addresses that can change at any time. Internet service providers typically assign dynamic IP addresses to their
customers. You can usually obtain a static IP address from an ISP, but they will charge you more for it.

Static IP addresses, as their name implies, are fixed and never change. Internet routers and servers typically use static IP addresses.

117
118
If your local router has been assigned a dynamic IP address, how does a remote host access your web server if the IP address changes?

If your remote HTTP client is using your website name instead of an IP address to access your HTTP server, all you need to do is update the
DNS server with the new IP address. Some routers have this capability built into them. If yours doesn’t, you can have your embedded device
periodically check its local network IP address and have it update its DNS server if it changes.

DynDNS and ZoneEdit are examples of two DNS service companies that can provide you with a domain name and the ability to update their
DNS servers if your IP address changes.

119
Detailed TCP/IP Communication Example
Example: Download a Webpage from the Internet
Let’s observe the first TCP/IP transactions that enable a local host to download a webpage from the Internet. Before continuing through this
example, you should be familiar with local network TCP/IP communication, and how DNS servers and NAT work.

1. Enter website in browser


2. DNS client creates a message
3. Create a datagram
4. Create a packet
5. Determine destination MAC address
6. Create and transmit a frame
7. NAT and forward frame to Internet
8. Forward frame to DNS server
9. DNS server receives frame
10. DNS translates and generates reply
11. Forward frame to local router
12. NAT translation in local router
13. Frame forwarded to PC
14. DNS client delivers IP address
15. HTTP client creates message

1 Enter website in browser


Open a web browser on the PC and enter the website name.

120
2 DNS client creates a message
The HTTP client in the PC tries to generate a message requesting the HTML file from www.website.com. It needs to supply the destination IP
address in the message it generates but doesn’t have it, so it employs the PC’s DNS client to get it.

The DNS client sends a message to the Transport layer requesting the IP address for website.com.

121
3 Create a datagram
The Transport layer adds the UDP header containing the source and destination port to the message.

It creates the source port number and assigns it to the DNS process requesting the IP address. The "Well-Known" DNS server port number is
used as the destination port. The resulting datagram is then sent to the Network layer.

122
4 Create a packet
The Network layer adds the IP header containing the source and destination IP address to the datagram.

It adds its own IP address as the source and the DNS server IP address as the destination. The PC knows the DNS server’s IP address because the
router provided the PC with it when it assigned its local IP address. The resulting packet is sent to the Link layer.

123
The router provided the PC with the default gateway (gateway between the local network and Internet which is the router in this case) and DNS
IP addresses when it assigned its local IP address.

5 Determine destination MAC address


The Link layer that determines the destination IP address for this packet is not on the local network. It, therefore, needs to send the packet to its
default gateway which in this example is the router.

Now we have a problem. The Link Layer can’t create the frame because it doesn’t know the MAC address of the default gateway. The Address
Resolution Protocol (ARP) was created to solve this problem.

124
6 Create and transmit a frame
After using ARP to get the MAC address for its default gateway, the Link Layer can complete the frame for the DNS transaction.

It adds the source and destination MAC addresses to the packet to create a frame. The source MAC address is its own and the destination MAC
address is that of the default gateway.

This frame is then sent to the Physical layer to transmit the frame’s bits.

125
7 NAT and forward frame to Internet
The router receives the frame from the PC, opens the packet and determines the destination IP address is not on the local network, so it must
forward this frame to the Internet.

The router creates a new Network Address Translation (NAT) entry and changes the local network IP address to its public Internet IP address. It
also uses the same NAT table entry to change the source port so the router can determine which host and host process to deliver in-coming
packets to.

126
The router has to change the source MAC from its local network MAC to its Wide Area Network (WAN) or Internet MAC. The destination
MAC address also needs to be changed to that of the router's default gateway, which in this example is the ISP's router. The local router
references its ARP table to get the Internet Service Provider (ISP) router's MAC address.

The local router forwards this new frame to the ISP's router.

8 Forward frame to DNS server

127
The router at the ISP receives the frame and opens the packet to find the destination IP address. It determines the destination IP address belongs
to its DNS server. The router’s Network layer sends the packet back to its Link layer to generate a new frame header.

The source MAC address is its own and the destination MAC address is the DNS server’s. Note that the source and destination IP addresses
remain the same.

The new frame is sent to the DNS server.

9 DNS server receives frame


The DNS server’s PHY receives the bits and forwards the frame to the Link layer.

The Link layer finds its MAC address as the destination so it forwards the packet to the Network layer.

128
The Network layer opens the packet and finds its IP address as the destination. It still needs to pay attention to the message, so it forwards it up to
the Transport layer.

The Transport layer opens the datagram and finds it is being sent to port 53. The DNS server has a process running and listening for traffic on
this port, so the message is forwarded to it.

10 DNS translates and generates reply


The DNS Server determines the IP address for website.com and generates a reply message containing it. The message is sent back to the
Transport layer.

The Transport layer adds the port information and creates the datagram.

129
The Network layer adds the IP information and creates the packet.

The Link layer adds the MAC information and creates the frame.

The Physical layer transmits the frame’s bits to the router.

11 Forward frame to local router


The ISP’s router receives the frame, finds its MAC address, and sends it to its Network layer to determine the destination IP address. It finds that
the packet belongs to our local router’s IP address, so it sends the packet back down to its Link layer.

The Link layer attaches new source and destination MAC addresses to the packet then sends the frame to our local router.

130
12 NAT translation in local router
When the local router receives the packet from the ISP’s router, it consults its NAT table to determine which local host to forward it to. It finds
an entry with an internet destination port number of 1 in the translation table.

The corresponding local IP address and port number are substituted into the packet that will be sent to the local network.

131
13 Frame forwarded to PC
The new packet is sent to the router’s Link layer where the source and destination MAC addresses are added creating the frame.

The router knows the destination MAC address corresponding to the destination IP address by consulting its ARP table.

This frame is then sent to the physical layer to be transmitted on the network.

132
14 DNS client delivers IP address
When the Frame arrives at the PC the message is sent to the DNS client.

The DNS client then provides the IP address for website.com to the HTTP client.

133
15 HTTP client creates message
Now that the HTTP client has the IP address for website.com (130:27:45:69) it can generate the message to download the HTML file for this
website.

134
At this point in the process, we’ve demonstrated NAT and DNS server functionality. We’ve also shown how the MAC source and destination
addresses change from node to node and that the IP source and destination addresses stay the same from the source to the destination (other than
across the network boundary). The rest of the TCP/IP communications to download this web page should be understood at this point.

135
Reference:
https://microchipdeveloper.com/tcpip:start

136

You might also like