Monday, 1 October 2012

local-dropbox for home

The very idea of dropbox fascinates me. Local-dropbox uses the bandwidth of the home wireless router for transferring large files from one device to another. The idea is to save internet bandwidth by converting one of the spare laptops into a server and using it for auto-syncing files at different workstations(laptops, smartphones, ipod). For instance: Using a dropbox for transferring a movie is not efficient as dropbox will take hours for uploading and syncing(+ it will ask for money :P). A wireless router at home can serve the same purpose along with achieving the minimum possible latency, zero internet bandwidth and highly secure, since no external device is involved.

code - https://github.com/pragya1990/local-dropbox

Used Python IDE and SimpleHTTPServer.

The code is still very raw, but I would still highlight how I went about it:

1) Initial connection : The server remains open at standard HTTP port for receiving messages from the client. The client connects to the server using socket programming and initial control information is exchanged between them such as client_id, client_port_for_file_transfer, server_port_for_file_transfer etc. Standard OK messages are sent if the everything went fine otherwise appropriate error messages are shown.

2) File transfer: I used the SimpleHTTPServer's commands for opening two ports(one at the server and another at the client) for transferring files. The urllib library was used for viewing files at the other end and reading them.

3) Syncing: At the client end, the program continuously checks if the dropbox folder is updated or not. The log file of client and server should be exactly same if no changes has happened on either side. If it needs updation, the client modifies its log file and sends a control message to the server to update its log file as well. The server checks its log file with the client log and downloads/updates the required files. If it is a shared folder, control messages are further sent to the other clients who are sharing the modified folder.

4) Testing: Initially both the client and server are running on the same laptop to make testing easy. Syncing and file updation works perfectly. :)

HTTP forwarder


I like playing with network packets and I wanted a deeper understanding of how every layer works, not just theoretically, but practically. Small projects really help a lot :
My previous exercises of analysing network traffic exposed me to the network layer but to a very small extent. 
Making a proxy gave some idea about the working of application layer. 

This particular project was aimed for developing a deeper understanding of all layers of the TCP/IP protocol. The program mainly involves changing of physical and logical addresses, port checking and computing checksum at the ethernet, network and transport layer.

code - https://github.com/pragya1990/http-forwarder (written in C)


Consider a scenario in which one laptop(say server) is acting as a HTTP forwarder(it has access to the internet by WLAN) for another laptop(say client, which is not connected to the internet) and both laptops are connected to each other by LAN.

Steps while sending a packet :
1) Sending --> (in the child process)listen at eth1 and if destination port == 80 , then modify packet - (a) write destination ip and source port in router.txt (b) change source IP, source and destination mac addr, checksum (c) inject the modified packet in wlan1 interface using pcap_inject.
2) --> Receiving : (in the parent process)listen to wlan1 and if source ip & destination port number exist in router.txt & source port == 80,then modify packet - (a) change destination ip, source and destination mac addr, checksum. (b) inject the modified packet in eth0 interface using pcap_inject.


The response from the server includes tcp handshake packets(syn, syn-ack, ack) and one "http get" request.
Here's the wireshark dump of one such packet sent. (162.254.3.1 is the IP address of the client laptop and 125.252.226.160(some random site)
3 0.000370 162.254.3.1 125.252.226.160 TCP 51442 > http [SYN] Seq=0 Win=5840 Len=0 MSS=1460
5 0.063316 125.252.226.160 162.254.3.1 TCP http > 51442 [SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1460
6 0.063568 162.254.3.1 125.252.226.160 TCP 51442 > http [ACK] Seq=1 Ack=1 Win=5840 Len=0
7 0.063996 162.254.3.1 125.252.226.160 HTTP GET / HTTP/1.1
8 0.064114 125.252.226.160 162.254.3.1 TCP http > 51443 [SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1460
9 0.064381 162.254.3.1 125.252.226.160 TCP 51443 > http [ACK] Seq=1 Ack=1 Win=5840 Len=0