# Application Layer

The application layer is the seventh level of the seven-layer OSI model. It interfaces directly to and performs common application services for the application processes; it also issues requests to the presentation layer.
The common application layer services provide semantic conversion between associated application processes. Note: Examples of common application services of general interest include the virtual file, virtual terminal, and job transfer and manipulation protocols.
The application layer of the four layer and five layer TCP/IP models corresponds to the application layer, the presentation layer and session layer in the seven layer OSI model.

## Dynamic Host Configuration Protocol

DHCP is a protocol used by networked host (clients) to obtain IP addresses and other parameters such as the default gateway, subnet mask, and IP address of DNS servers from a DHCP server. It facilitates access to a network because these settings would otherwise have to be made manually for the client to participate in the network.
The DHCP server ensures that all IP addresses are unique, e.g., no IP address is assigned to a second client while the first client's assignment is valid (its lease has not expired). Thus IP address pool management is done by the server and not by a human network administrator.
DHCP functionally became a successor to the older BOOTP protocol, whose leases were given for infinite time and did not support options. Due to the backward-compatibility of DHCP, very few networks continue to use pure BOOTP.

### IP address allocation

Depending on implementation, the DHCP server has three methods of allocating IP addresses:
* manual allocation, where the DHCP server performs the allocation based on a table with MAC address - IP address pairs manually filled by the server administrator. Only requesting clients with a MAC address listed in this table get the IP address according to the table.
* automatic allocation, where the DHCP server permanently assigns to a requesting client a free IP address from a range given by the administrator.
* dynamic allocation, the only method which provides dynamic re-use of IP addresses. A network administrator assigns a range of IP addresses to DHCP, and each client computer on the LAN has its TCP/IP software configured to request an IP address from the DHCP server when that client computer's network interface card starts up. The request-and-grant process uses a lease concept with a controllable time period. This eases the network installation procedure on the client computer side considerably.
This decision is generally unapparent to clients.
Some DHCP server implementations can update the DNS name associated with the client hosts to reflect the new IP address. They make use of the DNS update protocol established with RFC 2136.

### Configuring the IOS DHCP Server

* Setup a DHCP Database Agent
Using the IP DHCP database command, configure a DHCP database store. Alternately, use the no IP DHCP conflict logging command to disable logging of DHCP conflicts.
* Setup Excluded IP addresses
Use the ip dhcp excluded-address command to configure any IP addresses which fall within the DHCP pool address range but which should not be allocated to clients.
* Configure the address pool
Firstly, create a DHCP Poll with the IP DHCP pool name command. Next, when in the DHCP pool mode, use the network command to add network ranges to the DHCP pool.
* Configure the default DNS server
Use the dns-server command to set the IP addresses of the default DNS servers.
* Configure the default route
Use the default-router command to set the default route of DHCP clients.
* Enable the DHCP Server
Use the service DHCP command to enable the DHCP Server.

### DHCP and firewalls

Firewalls usually have to permit DHCP traffic explicitly. Specification of the DHCP client-server protocol describes several cases when packets must have the source address of 0x00000000 or the destination address of 0xffffffff. Anti-spoofing policy rules and tight inclusive firewalls often stop such packets. Multi-homed DHCP servers require special consideration and further complicate configuration.
To allow DHCP, network administrators need to allow several types of packets through the server-side firewall. All DHCP packets travel as UDP datagrams; all client-sent packets have source port 68 and destination port 67; all server-sent packets have source port 67 and destination port 68. For example, a server-side firewall should allow the following types of packets:
* Incoming packets from 0.0.0.0 or dhcp-pool to dhcp-ip
* Incoming packets from any address to 255.255.255.255
* Outgoing packets from dhcp-ip to dhcp-pool or 255.255.255.255
where dhcp-ip represents any address configured on a DHCP server host and dhcp-pool stands for the pool from which a DHCP server assigns addresses to clients

### Example in Cisco IOS Extended ACL

The following entries are valid on a Cisco 3560 switch with enabled DHCP service. The ACL is applied to a routed interface, 10.32.73.129, on input. The subnet is 10.32.73.128/26.

### Technical details

DHCP uses the same two IANA assigned ports as BOOTP: 67/udp for the server side, and 68/udp for the client side.
DHCP operations fall into four basic phases. These phases are IP lease request, IP lease offer, IP lease selection, and IP lease acknowledgement.

#### DHCP discovery

The client broadcasts on the local physical subnet to find available servers. Network administrators can configure a local router to forward DHCP packets to a DHCP server on a different subnet. This client-implementation creates a UDP packet with the broadcast destination of 255.255.255.255 or subnet broadcast address.
A client can also request its last-known IP address (in the example below, 192.168.1.100). If the client is still in a network where this IP is valid, the server might grant the request. Otherwise, it depends whether the server is set up as authoritative or not. An authoritative server will deny the request, making the client ask for a new IP immediately. A non-authoritative server simply ignores the request, leading to an implementation dependent time out for the client to give up on the request and ask for a new IP.
DHCPDISCOVER :
| UDP Src=0.0.0.0 sPort=68 Dest=255.255.255.255 dPort=67 ||||
| OP | HTYPE | HLEN | HOPS |
| 0x01 | 0x01 | 0x06 | 0x00 |
| XID ||||
| 0x3903F326 ||||
| SECS | SECS | FLAGS | FLAGS |
| 0x0000 | 0x0000 | 0x0000 | 0x0000 |
| CIADDR ||||
| 0x00000000 ||||
| YIADDR ||||
| 0x00000000 ||||
| SIADDR ||||
| 0x00000000 ||||
| GIADDR ||||
| 0x00000000 ||||
| CHADDR ||||
| 0x00053C04 ||||
| 0x8D590000 ||||
| 0x00000000 ||||
| 0x00000000 ||||
| 192 octets of 0's. BOOTP legacy ||||
| DHCP option 53: DHCP Discover ||||
| DHCP option 50: 192.168.1.100 requested ||||

#### DHCP offers

When a DHCP server receives an IP lease request from a client, it extends an IP lease offer. This is done by reserving an IP address for the client and sending a DHCPOFFER message across the network to the client. This message contains the client's MAC address, followed by the IP address that the server is offering, the subnet mask, the lease duration, and the IP address of the DHCP server making the offer.
The server determines the configuration, based on the client's hardware address as specified in the CHADDR field. Here the server, 192.168.1.1, specifies the IP address in the YIADDR field.
DHCPOFFER :
| UDP Src=192.168.1.1 sPort=67 Dest=255.255.255.255 dPort=68 ||||
| OP | HTYPE | HLEN | HOPS |
| 0x02 | 0x01 | 0x06 | 0x00 |
| XID ||||
| 0x3903F326 ||||
| SECS | SECS | FLAGS | FLAGS |
| 0x0000 | 0x0000 | 0x0000 | 0x0000 |
| CIADDR ||||
| 0x00000000 ||||
| YIADDR ||||
| 0xC0A80164 ||||
| SIADDR ||||
| 0x00000000 ||||
| GIADDR ||||
| 0x00000000 ||||
| CHADDR ||||
| 0x00053C04 ||||
| 0x8D590000 ||||
| 0x00000000 ||||
| 0x00000000 ||||
| 192 octets of 0's. BOOTP legacy ||||
| DHCP option 53: DHCP Offer ||||
| DHCP option 1: 255.255.255.0 subnet mask ||||
| DHCP option 3: 192.168.1.1 router ||||
| DHCP option 51: 1 day IP lease time ||||
| DHCP option 54: 192.168.1.1 DHCP server ||||

#### DHCP requests

The client selects a configuration out of the DHCP "Offer" packets it has received and broadcasts it on the local subnet. Again, this client requests the 192.168.1.100 address that the server specified. In case the client has received multiple offers it specifies the server from which it has accepted the offer.
DHCPREQUEST :
| UDP Src=0.0.0.0 sPort=68 Dest=255.255.255.255 dPort=67 ||||
| OP | HTYPE | HLEN | HOPS |
| 0x01 | 0x01 | 0x06 | 0x00 |
| XID ||||
| 0x3903F326 ||||
| SECS | SECS | FLAGS | FLAGS |
| 0x0000 | 0x0000 | 0x0000 | 0x0000 |
| CIADDR ||||
| 0x00000000 ||||
| YIADDR ||||
| 0x00000000 ||||
| SIADDR ||||
| 0x00000000 ||||
| GIADDR ||||
| 0x00000000 ||||
| CHADDR ||||
| 0x00053C04 ||||
| 0x8D590000 ||||
| 0x00000000 ||||
| 0x00000000 ||||
| 192 octets of 0's. BOOTP legacy ||||
| DHCP option 53: DHCP Request ||||
| DHCP option 50: 192.168.1.100 requested ||||
| DHCP option 54: 192.168.1.1 DHCP server. ||||

#### DHCP acknowledgement

When the DHCP server receives the DHCPREQUEST message from the client, it initiates the final phase of the configuration process. This acknowledgement phase involves sending a DHCPACK packet to the client. This packet includes the lease duration and any other configuration information that the client might have requested. At this point, the TCP/IP configuration process is complete.
The server acknowledges the request and sends the acknowledgement to the client. The system as a whole expects the client to configure its network interface with the supplied options.
DHCPACK :
| UDP Src=192.168.1.1 sPort=67 Dest=255.255.255.255 dPort=68 ||||
| OP | HTYPE | HLEN | HOPS |
| 0x02 | 0x01 | 0x06 | 0x00 |
| XID ||||
| 0x3903F326 ||||
| SECS | SECS | FLAGS | FLAGS |
| 0x0000 | 0x0000 | 0x0000 | 0x0000 |
| CIADDR (Client IP Address) ||||
| 0x00000000 ||||
| YIADDR (Your IP Address) ||||
| 0xC0A80164 ||||
| SIADDR (Server IP Address) ||||
| 0x00000000 ||||
| GIADDR (Relay IP Address) ||||
| 0x00000000 ||||
| CHADDR (Client Hardware Address) ||||
| 0x00053C04 ||||
| 0x8D590000 ||||
| 0x00000000 ||||
| 0x00000000 ||||
| 192 octets of 0's. BOOTP legacy ||||
| DHCP option 53: DHCP ACK ||||
| DHCP option 1: 255.255.255.0 subnet mask ||||
| DHCP option 3: 192.168.1.1 router ||||
| DHCP option 51: 1 day IP lease time ||||
| DHCP option 54: 192.168.1.1 DHCP server ||||

#### DHCP selection

When the client PC receives an IP lease offer, it must tell all the other DHCP servers that it has accepted an offer. To do this, the client broadcasts a DHCPREQUEST message containing the IP address of the server that made the offer. When the other DHCP servers receive this message, they withdraw any offers that they might have made to the client. They then return the address that they had reserved for the client back to the pool of valid addresses that they can offer to another computer. Any number of DHCP servers can respond to an IP lease request, but the client can only accept one offer per network interface card.

#### DHCP information

The client sends a request to the DHCP server: either to request more information than the server sent with the original DHCPACK; or to repeat data for a particular application - for example, browsers use DHCP Inform to obtain web proxy settings via WPAD. Such queries do not cause the DHCP server to refresh the IP expiry time in its database.

#### DHCP releasing

The client sends a request to the DHCP server to release the DHCP and the client unconfigures its IP address. As clients usually do not know when users may unplug them from the network, the protocol does not define the sending of DHCP Release as mandatory.

## e-mail

Email is composed of two types of protocols: Server to Server and Client to Server.

### Server to Server

Simple Mail Transfer Protocol (SMTP) is the de facto standard for e-mail transmissions across the Internet. Formally SMTP is defined in RFC 821 (STD 10) as amended by RFC 1123 (STD 3) chapter 5. The protocol used today is also known as ESMTP and defined in RFC 2821.
SMTP is a relatively simple, text-based protocol, where one or more recipients of a message are specified (and in most cases verified to exist) and then the message text is transferred. It is quite easy to test an SMTP server using the telnet program (see below).
SMTP uses TCP port 25. To determine the SMTP server for a given domain name, the MX (Mail eXchange) DNS record is typically used, falling back to a simple A record in the case of no MX (not all MTAs (Mail Transfer Agents) support fallback). Some current mail transfer agents will also use SRV records, a more general form of MX, though these are not widely adopted.

### Client to Server

SMTP is a "push" protocol that does not allow one to "pull" messages from a remote server on demand. To do this a mail client must use POP3 or IMAP. Another SMTP server can trigger a delivery in SMTP using ETRN.
POP3 has made earlier versions of the protocol obsolete, POP (informally called POP1) and POP2. In contemporary usage, the less precise term POP almost always means POP3 in the context of e-mail protocols.
The design of POP3 and its procedures supports end-users with intermittent connections (such as dial-up connections), allowing these users to retrieve e-mail when connected and then to view and manipulate the retrieved messages without needing to stay connected. Although most clients have an option to leave mail on server, e-mail clients using POP3 generally connect, retrieve all messages, store them on the user's PC as new messages, delete them from the server, and then disconnect. In contrast, the newer, more capable Internet Message Access Protocol (IMAP) supports both connected and disconnected modes of operation. E-mail clients using IMAP generally leave messages on the server until the user explicitly deletes them. This and other facets of IMAP operation allow multiple clients to access the same mailbox. Most e-mail clients support either POP3 or IMAP to retrieve messages; however, fewer Internet Service Providers (ISPs) support IMAP. The fundamental difference between POP3 and IMAP4 is that POP3 offers access to a mail drop; the mail exists on the server until it is collected by the client. Even if the client leaves some or all messages on the server, the client's message store is considered authoritative. In contrast, IMAP4 offers access to the mail store; the client may store local copies of the messages, but these are considered to be a temporary cache; the server's store is authoritative.

## Web

Hypertext Transfer Protocol (HTTP) is a method used to transfer or convey information on the World Wide Web. Its original purpose was to provide a way to publish and retrieve HTML pages.
Development of HTTP was coordinated by the W3C and the IETF, culminating in the publication of a series of RFCs, most notably RFC 2616 (1999), which defines HTTP/1.1, the version of HTTP in common use today.
HTTP is a request/response protocol between clients and servers. The originating client, such as a web browser, spider, or other end-user tool, is referred to as the user agent. The destination server, which stores or creates resources such as HTML files and images, is called the origin server. In between the user agent and origin server may be several intermediaries, such as proxies, gateways, and tunnels.
An HTTP client initiates a request by establishing a Transmission Control Protocol (TCP) connection to a particular port on a remote host (port 80 by default; see Wikipedia's list of port numbers). An HTTP server listening on that port waits for the client to send a request message.
Upon receiving the request, the server sends back a status line, such as "HTTP/1.1 200 OK", and a message of its own, the body of which is perhaps the requested file, an error message, or some other information.
Resources to be accessed by HTTP are identified using URIs (or, more specifically, those using the http: or https URI schemes.

### Request message

The request message consists of the following:
* Request line, such as GET /images/logo.gif HTTP/1.1, which requests the file logo.gif from the /images directory
* Headers, such as Accept-Language: en
* An empty line
* An optional message body
The request line and headers must all end with CRLF (i.e. a carriage return followed by a line feed). The empty line must consist of only CRLF and no other whitespace.
In the HTTP/1.1 protocol, all headers except Host are optional.

### Request methods

HTTP defines eight methods (sometimes referred to as "verbs") indicating the desired action to be performed on the identified resource.

#### HEAD

    Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

#### GET

    Requests a representation of the specified resource. By far the most common method used on the Web today. Should not be used for operations that cause side-effects (using it for actions in web applications is a common misuse). See 'safe methods' below.

#### POST

    Submits data to be processed (e.g. from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

#### PUT

    Uploads a representation of the specified resource.

#### DELETE

    Deletes the specified resource.

#### TRACE

    Echoes back the received request, so that a client can see what intermediate servers are adding or changing in the request.

#### OPTIONS

    Returns the HTTP methods that the server supports. This can be used to check the functionality of a web server.

#### CONNECT

    For use with a proxy that can change to being an SSL tunnel.
HTTP servers are supposed to implement at least the GET and HEAD methods and, whenever possible, also the OPTIONS method.