<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lead Thinking &#187; Machsend</title>
	<atom:link href="http://leadthinking.com/category/machsend/feed" rel="self" type="application/rss+xml" />
	<link>http://leadthinking.com</link>
	<description>The Business of Software Development</description>
	<lastBuildDate>Fri, 28 May 2010 14:50:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>P2P using TCP &amp; Ruby</title>
		<link>http://leadthinking.com/213-p2p-using-tcp-ruby</link>
		<comments>http://leadthinking.com/213-p2p-using-tcp-ruby#comments</comments>
		<pubDate>Mon, 31 Aug 2009 10:33:33 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Machsend]]></category>

		<guid isPermaLink="false">http://leadthinking.com/?p=213</guid>
		<description><![CDATA[This is going to be a fairly technical post about the technologies behind Machsend and using P2P over TCP rather than UDP. I&#8217;v written a lot of context to the problem, so if you want to skip to the juicy protocol details look for the set of bullet points.
We recently launched Machsend, which enables people to send [...]]]></description>
			<content:encoded><![CDATA[<p>This is going to be a fairly technical post about the technologies behind <a href="http://machsend.com">Machsend</a> and using P2P over TCP rather than UDP. I&#8217;v written a lot of context to the problem, so if you want to skip to the juicy protocol details look for the set of bullet points.</p>
<p>We recently launched <a href="http://machsend.com">Machsend</a>, which enables people to send unlimited amounts of data from inside the browser. You literally drag a file onto the site, send the recipient a link, and they can download it straight away. Machsend uses TCP P2P connections to transfer data between clients.<br />
<span id="more-213"></span></p>
<p>Most of the packets on the internet are controlled by <a href="http://en.wikipedia.org/wiki/Transmission_Control_Protocol">TCP</a> which provides a reliable, ordered delivery of a stream of bytes between two endpoints. The alternative is <a href="http://en.wikipedia.org/wiki/User_Datagram_Protocol">UDP</a> which is much lower level than TCP &#8211; information isn&#8217;t guaranteed to reach its destination. Most P2P implementations use UDP to communicate (for reasons we&#8217;ll come to later), but I&#8217;m going to show you how to get the flow control and reliability of TCP in P2P connections.</p>
<p>Routers (combined with firewalls) are both critical to the stability of the internet, and an enemy to P2P connections. Networks can have lots of computers behind a single IP using a router &#8211; and the router will make sure the right packets get to the right computers, AKA <a href="http://en.wikipedia.org/wiki/Network_address_translation">NAT</a>. However,  I guess they just weren&#8217;t designed with P2P in mind &#8211; so most require a bit of coaxing/trickery to set up P2P connections.</p>
<p>With UDP it&#8217;s fairly straight forward &#8211; that&#8217;s why it&#8217;s so commonly used in P2P software.<br />
Client A sends a packet to Client B, opening a hole in Client A&#8217;s firewall. That packet will be discarded by Client B&#8217;s firewall, but that doesn&#8217;t matter. Client B then does the reverse, sending a packet back Client A &#8211; now there&#8217;s a hole in both firewalls and a P2P connection. I&#8217;m glossing over some of the more technical details, such as <a href="http://en.wikipedia.org/wiki/STUN">STUN</a> and port prediction, but that&#8217;s the general gist. <a href="http://www.h-online.com/security/How-Skype-Co-get-round-firewalls--/features/82481/0">Here&#8217;s</a> a good article on how Skype uses UDP P2P techniques to traverse firewalls.</p>
<p style="text-align: center;"><img class="size-full wp-image-220 aligncenter" title="Machsend1" src="http://leadthinking.com/wp-content/uploads/2009/08/Machsend1.jpg" alt="Machsend1" width="453" height="146" /></p>
<p>UDP is perfect for Skype, for example, since you don&#8217;t care that much about reliability when delivering audio &amp; video &#8211; but rather responsiveness. The data is very time dependent and needs to get to its destination quickly. However, with most other data transfers we care that all the data reaches the destination properly. If you&#8217;re transferring a picture to someone, you want to be sure all of it reaches the recipient.</p>
<p>I have actually created a UDP protocol for data transfer that&#8217;s as reliable as TCP, yet is also faster. That, however, is the subject of another post.</p>
<p>While routers don&#8217;t meddle with UDP connections too much, that&#8217;s not the case for TCP ones. Routers will enforce TCP standards and prevent you from accepting random incoming TCP connections. On top of that, most operating systems require root access to create raw sockets, which you need to manipulate TCP streams at the packet level.</p>
<p>So, it&#8217;s a bit of a pickle &#8211; on hand you&#8217;ve got proper P2P connections with UDP, but you have to implement your own flow control and reliability algorithms. On the other hand, TCP P2P connections seem very tricky to setup and aren&#8217;t supported on many routers.</p>
<p>Most people presented with this problem, will either use <a href="http://en.wikipedia.org/wiki/Universal_Plug_and_Play">UPnP</a>, a protocol for programs to configure routers, or tell people to configure their routers manually.<br />
Obviously, this is far from ideal, both approaches have major caveats, but unfortunately it&#8217;s going to be the status quo for a while.</p>
<p>I was in the aforementioned situation, until I found a <a href="http://nutss.gforge.cis.cornell.edu/pub/imc05-tcpnat.pdf">paper</a> titled <em>Characterization and Measurement of TCP Traversal through NATs and Firewalls</em>. The students tested about 7 methods of TCP P2P traversals, and advocated a fairly simple one that works with about 80% of the firewalls they tested.</p>
<p>To save you reading the paper, I&#8217;ll elaborate. The TCP protocol starts with a 3 way handshake of SYN and ACK packets. However, although it&#8217;s an unlikely combination, most routers will let through incoming SYN packets, if an outgoing SYN packet has been sent to exactly the same ip and port combination. So, to sum up:</p>
<ul>
<li>Client A sends a SYN packet to Client B</li>
<li>Client A starts listening on exactly the same port the previous SYN packet was sent on</li>
<li>Client B then creates a normal TCP connection to Client A, to exactly the same local port Client A used to send the SYN, binding locally to exactly the same port that Client A sent the SYN packet too.</li>
</ul>
<p style="text-align: center;"><img class="size-full wp-image-221 aligncenter" title="Machsend2" src="http://leadthinking.com/wp-content/uploads/2009/08/Machsend2.jpg" alt="Machsend2" width="451" height="146" /></p>
<p>And that&#8217;s all there is to TCP P2P &#8211; those three steps.</p>
<p>To send a SYN packet without using raw sockets you just open a socket to an address, and then immediately close it.</p>
<p>To open a socket on a particular local port, since it&#8217;s usually chosen for you, you just need to set some options. If you enable SO_REUSEADDR and SO_REUSEPORT the operating system shouldn&#8217;t complain.</p>
<p>The document for Ruby&#8217;s Socket class is fairly sparse, so here&#8217;s an abstraction over it:</p>
<p><script src="http://gist.github.com/178025.js"></script> </p>
<p>To accept connections from Client B, Client A sends a SYN packet, and then listens:    </p>
<p><script src="http://gist.github.com/178028.js"></script></p>
<p>Client B&#8217;s connection to Client A looks like this:</p>
<p><script src="http://gist.github.com/178026.js"></script></p>
<p>If the connection fails, try reversing Client A and B, so Client B makes the connection to Client A &#8211; this usually works.</p>
<p>So, you might be asking yourself &#8211; how does Client A know Client B&#8217;s IP &amp; port, and vica versa? Who&#8217;s controlling it? Well, for that you need a third party server known to both endpoints &#8211; usually called a STUN server, or with TCP traversal, STUNT. For Machsend, we built a JSON RPC server using Event Machine .</p>
<p>At the moment we haven&#8217;t implemented any port  prediction since I&#8217;ve noticed most of the routers keep the same ports that the computers choose. However, to improve reliability, that could certainly be implemented.</p>
<p>As well as the previously linked paper, there&#8217;s a good article on the subject <a href="http://www.bford.info/pub/net/p2pnat/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://leadthinking.com/213-p2p-using-tcp-ruby/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Machsend release &#8211; P2P file transfers inside the browser</title>
		<link>http://leadthinking.com/186-machsend-release-p2p-file-transfers-inside-the-browser</link>
		<comments>http://leadthinking.com/186-machsend-release-p2p-file-transfers-inside-the-browser#comments</comments>
		<pubDate>Fri, 24 Jul 2009 13:42:20 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Machsend]]></category>

		<guid isPermaLink="false">http://leadthinking.com/?p=186</guid>
		<description><![CDATA[After a private beta of Machsend, it&#8217;s now being released publicly &#8211; check it out.
Machsend lets you send files to people without any size restrictions. Files are sent in a P2P fashion, straight from one client to another &#8211; which means transfers are faster, and you don&#8217;t have to trust a third party with your [...]]]></description>
			<content:encoded><![CDATA[<p>After a private beta of <a href="http://machsend.com">Machsend</a>, it&#8217;s now being released publicly &#8211; <a href="http://machsend.com">check it out</a>.</p>
<p>Machsend lets you send files to people without any size restrictions. Files are sent in a P2P fashion, straight from one client to another &#8211; which means transfers are faster, and you don&#8217;t have to trust a third party with your data.</p>
<p>If you&#8217;re on the same LAN, files are sent directly. Otherwise a type of firewall traversal  allows the two clients connect (which works on about 80% of firewalls). There are few good papers that explain more <a href="http://nutss.gforge.cis.cornell.edu/pub/imc05-tcpnat.pdf">here</a> and <a href="http://www.bford.info/pub/net/p2pnat/">here</a>.</p>
<p>Machsend uses Yahoo! <a href="http://browserplus.yahoo.com">BrowserPlus</a> to perform this TCP magic.</p>
<p>Data transfers are unlimited &#8211; I&#8217;ve sent gigabytes over the system without any trouble.</p>
<p>Machsend works on OSX and Windows. Unfortunately there isn&#8217;t a version of BrowserPlus for Linux yet, but one is being developed.</p>
<p>There are a few more details on the &#8216;<a href="http://machsend.com/about.html">about</a>&#8216; page.</p>
<p><a href="http://machsend.com"><img class="alignnone size-medium wp-image-187" title="Machsend" src="http://leadthinking.com/wp-content/uploads/2009/07/Picture-23-542x328.png" alt="Machsend" width="542" height="328" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://leadthinking.com/186-machsend-release-p2p-file-transfers-inside-the-browser/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Machsend &#8211; P2P file sharing in the browser</title>
		<link>http://leadthinking.com/89-machsend-p2p-in-the-browser</link>
		<comments>http://leadthinking.com/89-machsend-p2p-in-the-browser#comments</comments>
		<pubDate>Wed, 01 Jul 2009 04:00:06 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Machsend]]></category>

		<guid isPermaLink="false">http://leadthinking.com/?p=89</guid>
		<description><![CDATA[Recently I&#8217;ve been developing a project called Machsend which enables P2P file transfers from inside the browser.

The process goes:

Bob navigates to the site, and drags files onto it
Bob then sends his uniquely generated link to Alice
Alice opens the link, can see Bob&#8217;s shared files, and downloads them

The file transfer is direct between users &#8211; it [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been developing a project called Machsend which enables P2P file transfers from inside the browser.</p>
<p><img class="alignnone size-medium wp-image-120" title="Full" src="http://leadthinking.com/wp-content/uploads/2009/06/Picture-13-542x262.png" alt="Full" width="542" height="262" /></p>
<p>The process goes:</p>
<ul>
<li>Bob navigates to the site, and drags files onto it</li>
<li>Bob then sends his uniquely generated link to Alice</li>
<li>Alice opens the link, can see Bob&#8217;s shared files, and downloads them</li>
</ul>
<p>The file transfer is direct between users &#8211; it isn&#8217;t sent to any third party servers. This means it&#8217;s fast, and secure.</p>
<p>Files on the same network will transfer locally, greatly speeding up the process.</p>
<p>It also means there are no bandwidth bills to host the application, so it&#8217;ll be offered free, for unlimited size transfers (Machsend can transfer gigabytes without a problem).</p>
<p>Machsend is cross platform, and will work with about 85% of routers. It uses Yahoo BrowserPlus, which can be installed from within the browser, without a restart.</p>
<p><strong>I&#8217;m looking for Beta Testers &#8211; so if you&#8217;re interested, please <a href="http://groups.google.com/group/machsend-beta">join the Google Group</a>.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://leadthinking.com/89-machsend-p2p-in-the-browser/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
