Showing posts with label HTTP. Show all posts
Showing posts with label HTTP. Show all posts

Wednesday, December 17, 2014

How Trace Route Works: TTLs

TTL stands for Time To Live. When a TCP packet is sent, its TTL is set, which is the number of routers (hops) it can pass through before the packet is discarded. As the packet passes through a router the TTL is decremented until, when the TTL reaches zero, the packet is destroyed and an ICMP "time exceeded" message is returned. The return message's TTL is set by the terminating router when it creates the packet, and decremented normally.

Trace Route works by setting the TTL for a packet to 1, sending it towards the requested destination host, and listening for the reply. When the initiating machine receives a "time exceeded" response, it examines the packet to determine where the packet came from - this identifies the machine one hop away. Then the tracing machine generates a new packet with TTL 2, and uses the response to determine the machine 2 hops away, and so on.

Unfortunately not all TCP stacks behave correctly. Some TCP stacks set the TTL for the ICMP "time exceeded" message to that of the message being killed. So if the TTL is 0, the packet will be killed by the next machine to which it is passed. This can have two effects on a trace . If the computer is an intermediate machine in the trace, the entry will remain blank. No information is returned to the machine conducting the trace because the "time exceeded" message never makes it back. If the machine you are doing a trace to has this bug in its TCP stack, return packets won't reach the originating machine unless the TTL is high enough to cover the round trip. So Trace Route will show a number of failed connections equal to n (the number of hops to the destination machine) minus 1.
Machines running MacTCP display this bug . Open Transport handles TTLs correctly. The original distributions of 4.3 BSD had this error, so some UNIX machines display the problem.

The times in Trace Route are total round trip times in seconds. The Min/Avg/Max should increase from machine n to machine n+1, but they may not, for a variety of reasons. To start with, the times are based on the number of tests listed in the results column (generally 3 for a completed trace). Response times vary depending on network usage. Additionally, returning a "time exceeded" message requires more computational time than routing a packet onwards. Finally, packet paths may not be the same coming and going.

HTTP - HYPER TEXT TRANSFER PROTOCOL

request-response protocol, stateless protocol, persistent connection, keep-alive, streaming support.

HTTP functions as a request-response protocol in the client-server computing model. A web browser, for example, may be the client and an application running on a computer hosting a web site may be the server.

An HTTP session is a sequence of network request-response transactions. An HTTP client initiates a request by establishing a Transmission Control Protocol (TCP) connection to a particular port on a server (typically port 80, occasionally port 8080; see List of TCP and UDP port numbers). An HTTP server listening on that port waits for a client's 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 this message is typically the requested resource, although an error message or other information may also be returned.

HTTP is a stateless protocol. A stateless protocol does not require the HTTP server to retain information or status about each user for the duration of multiple requests. However, some web applications implement states or server side sessions using for instance HTTP cookies or Hidden variables within web forms.

In HTTP/0.9 and 1.0, the connection is closed after a single request/response pair. In HTTP/1.1 a keep-alive-mechanism was introduced, where a connection could be reused for more than one request. Such persistent connections reduce request latency perceptibly, because the client does not need to re-negotiate the TCP 3-Way-Handshake connection after the first request has been sent. Another positive side effect is that in general the connection becomes faster with time due to TCP's slow-start-mechanism.

Version 1.1 of the protocol also made bandwidth optimization improvements to HTTP/1.0. For example, HTTP/1.1 introduced chunked transfer encoding to allow content on persistent connections to be streamed rather than buffered. HTTP pipelining further reduces lag time, allowing clients to send multiple requests before waiting for each response. Another improvement to the protocol was byte serving, where a server transmits just the portion of a resource explicitly requested by a client.

HTTP is an application layer protocol designed within the framework of the Internet Protocol Suite. Its definition presumes an underlying and reliable transport layer protocol,[3]and Transmission Control Protocol (TCP) is commonly used. However HTTP can use unreliable protocols such as the User Datagram Protocol (UDP), for example in Simple Service Discovery Protocol (SSDP).

RTMP Handshake


After establishing a TCP connection, an RTMP connection is established first performing a handshake through the exchange of 3 packets from each side (also referred as Chunks in the official documentation). These are referred in the official spec as C0-2 for the client sent packets and S0-2 for the server side respectively and are not to be confused with RTMP packets that can be exchanged only after the handshake is complete. These packets have a structure of their own and C1 contains a field setting the "epoch" timestamp but since this can be set to zero, as is done in third party implementations, the packet can be simplified. The client initialises the connection by sending the C0 packet with a constant value of 0x03 representing the current protocol version. It follows straight with C1 without waiting for S0 to be received first which contains 1536 bytes, with the first 4 representing the epoch timestamp, the second 4 all being 0, and the rest being random (and which can be set to 0 in third party implementations). C2 and S2 are an echo of S1 and C1 respectively, except with the second 4 bytes being the time the respective message was received (instead of 0). After C2 and S2 are received the handshake is considered complete.


Tuesday, December 16, 2014

Networking Concept

A very important diagram to remember that will certainly help you in a lot of interview questions



























Networks consist of hosts i.e. members, usually computers but possibly other participating devices such as printers. A collection of hosts on a network is sometimes referred to as a site. All hosts on a network, as well as being connected by some medium either wired or wireless, must also agree on a set of protocols for communicating with each other. The OSI model is an abstract reference model for communications and computer network protocol design. 

The internet protocol suite commonly used on the internet roughly maps to the OSI model as shown in the diagram.

The everyday protocols you use are at the top (application layer). Examples include ssh (for
connecting to machines), ftp (for transferring files), http (for browsing the web) and smtp (for
sending email).

Underneath these applications, either TCP or UDP is used to actually transport data packets. Transmissions Control Protocol (TCP) is a connection-oriented protocol that offers guaranteed delivery of packets (with an overhead in network handshaking that this requires) while User Datagram Protocol (UDP) is a connectionless protocol that transmits packets on a best effort basis without any guarantees about delivery.

TCP and UDP run on the Internet Protocol which describes the protocol used to encapsulate and transport data around the network. It handles details such as addressing and routing. Dynamic routing of IP packets around the network is handled by protocols including RIP, BGP and OSPF. 

Internet Control Message Protocol (ICMP) is used to send status and error messages around IP networks (this is the protocol used by tools such as ping).

At the lowest level, the Address Resolution Protocol (ARP) is used to map IP addresses back to the physical addresses used by the underlying hardware. In the case of Ethernet networks for instance, each network device has a unique physical address (the MAC address). ARP is used to map these addresses to the IP addresses used at the logical network level.

The internet protocol suite can run on a variety of physical networks and media including Ethernet, Token Ring and Wireless..

Network Address Translation

Have you ever wondered how the request originating from your PC or laptop returns back only to you via your corporate office firewall, reason being for the external world outside of your firewall the client IP address of all the machines are same in your LAN?

The answer lies in the NAT - Network Address Translation

NAT is often used in conjunction with a firewall. NAT, also known as network masquerading or IP masquerading, is a technique where the source and/or destination addresses on packets are rewritten as they pass through a firewall. This allows many computers on a private network to access the internet using a single public IP address.


  • The firewall translates each private network address to the public IP address when the packet leaves the private network.
  • The internet destination for the packet processes the request and responds to the public IP address
  • The request is sent back to the firewall which recognises that the packet is intended for a machine on the private network
  • The firewall rewrites the destination address of the packet for the private network and sends it on
  • The iptables mechanism works well with NAT.

Monday, December 15, 2014

Difference between PUT and POST

HTTP PUT:
PUT puts a file or resource at a specific URI, and exactly at that URI. If there's already a file or resource at that URI, PUT replaces that file or resource. If there is no file or resource there, PUT creates one.
HTTP POST:
POST sends data to a specific URI and expects the resource at that URI to handle the request. The web server at this point can determine what to do with the data in the context of the specified resource.
The official HTTP RFC specifies POST to be:
  • Annotation of existing resources;
  • Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;
  • Providing a block of data, such as the result of submitting a form, to a data-handling process;
  • Extending a database through an append operation.