Developers.GnutellaHandshake
The Handshake
When two computers running Gnutella software connect with a TCP socket, they each can send bytes to the other. The first bytes they send are ASCII text characters, like 'G', followed by 'N', 'U', 'T', 'E', and so on. Each line of text ends with the two bytes "\r\n". A blank line ends a group of lines. The first thing a Gnutella computer might tell a remote computer it's connected to is:
<source lang="c"> GNUTELLA CONNECT/0.6\r\n Listen-IP: 67.176.34.172:6346\r\n Remote-IP: 81.103.192.245\r\n User-Agent: Shareaza 1.8.2.0\r\n X-Ultrapeer: False\r\n \r\n </source>
Each line of ASCII text that ends with "\r\n" is called a header. A blank line ends a group of headers. The receiving computer reads the group of headers, and responds with its own.
<source lang="c"> Initiator Receiver
GNUTELLA CONNECT/0.6 Listen-IP: 1.2.3.4:6346 Remote-IP: 6.7.8.9 User-Agent: Shareaza 1.8.2.0 Accept: application/x-gnutella2 X-Ultrapeer: False
GNUTELLA/0.6 200 OK Listen-IP: 6.7.8.9:6346 Remote-IP: 1.2.3.4 User-Agent: Shareaza 1.8.2.0 Content-Type: application/x-gnutella2 Accept: application/x-gnutella2 X-Ultrapeer: True X-Ultrapeer-Needed: False
GNUTELLA/0.6 200 OK Content-Type: application/x-gnutella2 X-Ultrapeer: False </source>
Then, the original computer sends a third group of headers. This initial part of the communication is called the Gnutella handshake. In the handshake:
- Each letter is an ASCII character that takes up a single byte.
- The computer reads the letters as a line of text until it gets to the two bytes "\r\n". This is one header.
- A blank line ends a group of headers.
- There are always three groups of headers. The computer that made the connection makes the first, the receiving computer responds with the second, and then the initiating computer sends the third.
To learn more about the Gnutella handshake, read Gnutella Handshake Proposal, and TCP Stream Connection and Handshaking at gnutella2.com.
After the handshake, both computers begin sending the binary data of Gnutella packets.