<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Making Life Easier</title>
	
	<link>http://rlc.vlinder.ca</link>
	<description>One Byte at a time</description>
	<lastBuildDate>Wed, 25 Jan 2012 01:38:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/making-life-easier" /><feedburner:info uri="making-life-easier" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Changing an API in subtle, unpredictable ways</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/3suwnW0c4Mg/</link>
		<comments>http://rlc.vlinder.ca/blog/2012/01/changing-an-api-in-subtle-unpredictable-ways/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 22:14:34 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[C & C++]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[anecdotes]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[rants]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/?p=1785</guid>
		<description><![CDATA[Many seasoned Windows systems programmers will know that you can wait for the death of a thread with WaitForSingleObject and for the deaths of multiple threads with its bigger brother, WaitForMultipleObjects. Big brother changes its behavior on some platforms, though &#8230; <a href="http://rlc.vlinder.ca/blog/2012/01/changing-an-api-in-subtle-unpredictable-ways/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>Many seasoned Windows systems programmers will know that you can wait for the death of a thread with <code>WaitForSingleObject</code> and for the deaths of multiple threads with its bigger brother, <code>WaitForMultipleObjects</code>. Big brother changes its behavior on some platforms, though &#8212; as I just found out myself, the hard way.<br />
<span id="more-1785"></span><br />
<code>WaitForMultipleObjects</code> takes four parameters: the number of objects to wait for, the handles of the objects to wait for, whether or not it should wait for all of the objects to be signalled before returning, and a time-out. One common way to wait for a bunch of threads to die looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">HANDLE threads<span style="color: #008000;">&#91;</span>thread_count__<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> the threads <span style="color: #008000;">&#125;</span>
DWORD wfmo_result<span style="color: #008000;">&#40;</span>WaitForMultipleObjects<span style="color: #008000;">&#40;</span>thread_count__, threads, TRUE, INFINITE<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// handle errors here</span></pre></div></div>

<p>The snag is in the third parameter. On some platforms, it <em>has to be</em> <code>FALSE</code> &#8212; otherwise <code>WaitForMultipleObjects</code> will (may?) return immediately.</p>
<p>Personally, I find this kind of thing really annoying: when you have an API that works perfectly well, dropping a feature like this &#8212; even if you can&#8217;t implement it as efficiently as you can on other platforms, just isn&#8217;t an acceptable (or user-friendly) practice. Companies smaller than Microsoft <em>would not</em> get away with it.</p>
<p>I can understand that with the Embedded Compact scheduler, it may not be feasible to implement a wait-for-all option very efficiently but, knowing your users need the option and will likely port applications between platforms, would it really be too much to ask to do something along these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">DWORD WaitForMultipleObjects<span style="color: #008000;">&#40;</span>DWORD nObjects, HANDLE <span style="color: #000040;">*</span>lpHandles, BOOL bWaitForAll, DWORD dwTimeOut<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>bWaitForAll<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		HANDLE handles<span style="color: #008000;">&#91;</span>MAXIMUM_WAIT_OBJECTS<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span>handles, lpHandles, nObjects <span style="color: #000040;">*</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>HANDLE<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		DWORD dwTickCount <span style="color: #000080;">=</span> GetTickCount<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>nObjects <span style="color: #000040;">&amp;&amp;</span> dwTimeOut<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			DWORD result <span style="color: #000080;">=</span> WaitForMultipleObjects<span style="color: #008000;">&#40;</span>nObjects, handles, FALSE, dwTimeOut<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			DWORD dwNewTickCount <span style="color: #000080;">=</span> GetTickCount<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			DWORD elapsed <span style="color: #000080;">=</span> dwNewTickCount <span style="color: #000040;">-</span> dwTickCount<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>dwTimeOut <span style="color: #000040;">!</span><span style="color: #000080;">=</span> INFINITE<span style="color: #008000;">&#41;</span> dwTimeOut <span style="color: #000040;">-</span><span style="color: #000080;">=</span> elapsed <span style="color: #000080;">&lt;</span> dwTimeOut <span style="color: #008080;">?</span> elapsed <span style="color: #008080;">:</span> dwTimeOut<span style="color: #008080;">;</span>
			dwTickCount <span style="color: #000080;">=</span> dwNewTickCount<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>result <span style="color: #000080;">&gt;=</span> WAIT_OBJECT_0 <span style="color: #000040;">&amp;&amp;</span> result <span style="color: #000080;">&lt;</span> WAIT_OBJECT_0 <span style="color: #000040;">+</span> nObjects<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #000040;">--</span>nObjects<span style="color: #008080;">;</span>
				handles<span style="color: #008000;">&#91;</span>result <span style="color: #000040;">-</span> WAIT_OBJECT_0<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> handles<span style="color: #008000;">&#91;</span>nObjects<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0000ff;">else</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">return</span> WAIT_OBJECT_0<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// current implementation</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I&#8217;ve even written this code in Microsoft style &#8212; hungarian wartHogs and all. If anyone at Microsoft is reading this: a QFE for Windows Embedded Compact 7 with this code (or something similar) in it would be appreciated (but I won&#8217;t be holding my breath).</p>
<p>Changing APIs from one version of an OS to another (even if one is embedded and the other isn&#8217;t) in subtle, unpredictable ways is a bad idea &#8212; even if you&#8217;re doing while working for a huge company like Microsoft, which can get away with it.</p>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/3suwnW0c4Mg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2012/01/changing-an-api-in-subtle-unpredictable-ways/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2012/01/changing-an-api-in-subtle-unpredictable-ways/</feedburner:origLink></item>
		<item>
		<title>Opening a support ticket with Microsoft (or: how not to support your customers)</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/3qzM8YAXxCE/</link>
		<comments>http://rlc.vlinder.ca/blog/2012/01/opening-a-support-ticket-with-microsoft-or-how-not-to-support-your-customers/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 22:40:16 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[Opinions]]></category>
		<category><![CDATA[rants]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/?p=1779</guid>
		<description><![CDATA[I had to open a support ticket with Microsoft today: I found a bug in the TCP/IP stack of Windows Embedded Compact 7 that I wanted them to know about (and to fix). I also wanted to know when it &#8230; <a href="http://rlc.vlinder.ca/blog/2012/01/opening-a-support-ticket-with-microsoft-or-how-not-to-support-your-customers/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>I had to open a support ticket with Microsoft today: I found a bug in the TCP/IP stack of Windows Embedded Compact 7 that I wanted them to know about (and to fix). I also wanted to know when it would be <em>fixed</em> &#8212; after all, the bug is critical and the company I work for is a Microsoft Gold partner, so I had a reasonably high expectation of service.</p>
<p>Suffice it to say I was disappointed.<br />
<span id="more-1779"></span><br />
Reporting a bug should be easy: bugs provide the most valuable kind of feed-back about your software in that it tells you that it went wrong, that it is being used and that someone cares enough about it going wrong to tell you about it. As a rule of thumb, you should assume that for each bug report you get, there are at least seven bug reports that you don&#8217;t get. In Microsoft&#8217;s case, I would expect this latter number to be higher rather than lower.</p>
<p>Reporting a bug should be a two-step process: click an appropriate link, fill in a form. In the case of Microsoft&#8217;s system, it is rather more complicated. Here are the steps I went through:</p>
<ol>
<li>click the appropriate link</li>
<li>be sent on a wild goose chase to find a PID that doesn&#8217;t exist with my type of license &#8212; &#8220;charges may apply&#8221; being the motivator (why would charges every apply to report a bug?)</li>
<li>give up on that and choose the option that says charges may apply &#8212; now be presented with a choice of the &#8220;standard of care&#8221;</li>
<li>take a guess</li>
<li>be presented with a form to fill in two IDs; try the partner ID (twice) &#8212; doesn&#8217;t work &#8212; click on &#8220;chat&#8221;</li>
<li>wait to be connected&#8230;</li>
<li>the representative presents me with a &#8220;greeting&#8221; of sorts to tell me what she can and cannot do &#8212; which is good &#8212; I present her with my problem</li>
<li>get sent to a link that doesn&#8217;t answer the question</li>
<li>tell her it doesn&#8217;t answer the question</li>
<li>get told it does</li>
<li>explain why it doesn&#8217;t</li>
<li>well.. if I insist&#8230;</li>
<li>get the info to get through the form I was on</li>
<li><strong><em>now</em></strong> I get to fill in the data</li>
</ol>
<p>The whole experience, including several waits during the chat because the representative was apparently doing other things as well, took over 45 minutes (closer to an hour). A similar issue with KDE a few months ago took all of five minutes, and I&#8217;m still receiving feedback for that issue.</p>
<p>Again, bug reports are important assets you get from your customers for free. You should <strong><em>not</em></strong>:
<ul>
<li>threaten to make them pay for helping your</li>
<li>make them wait unnecessarily</li>
<li>send them where they won&#8217;t be able to report the bug</li>
<li>make it difficult to get feedback on the bug report</li>
</ul>
<p>You <strong><em>should</em></strong>:
<ul>
<li>thank them (profusely) for helping you</li>
<li>make the experience as simple as a walk in the park (in spring &#8212; without the four-foot layer of snow now obstructing the passage)</li>
<li>make it quick</li>
<li>provide feedback, quickly, friendly, &#8230;</li>
</ul>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/3qzM8YAXxCE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2012/01/opening-a-support-ticket-with-microsoft-or-how-not-to-support-your-customers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2012/01/opening-a-support-ticket-with-microsoft-or-how-not-to-support-your-customers/</feedburner:origLink></item>
		<item>
		<title>Winter wallpapers</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/V2hKPUZ_XMA/</link>
		<comments>http://rlc.vlinder.ca/blog/2011/12/winter-wallpapers/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 03:50:40 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/?p=1771</guid>
		<description><![CDATA[As has become my custom (at least since this summer) I&#8217;ve changed the theme a few days go, at the start of the season. Here are the associated wallpaper images&#8230;]]></description>
			<content:encoded><![CDATA[
<p>As has become my custom (at least since this summer) I&#8217;ve changed the theme a few days go, at the start of the season. Here are the associated wallpaper images&#8230;<br />
<span id="more-1771"></span><br />
<a href="http://rlc.vlinder.ca/wp-content/uploads/2011/12/winter-wide.png"><img src="http://rlc.vlinder.ca/wp-content/uploads/2011/12/winter-wide.png" alt="Wide version of the winter wallpaper" title="winter-wide" class="alignright size-full wp-image-1737" /></a><br />
<a href="http://rlc.vlinder.ca/wp-content/uploads/2011/12/winter.png"><img src="http://rlc.vlinder.ca/wp-content/uploads/2011/12/winter.png" alt="4x3 version" title="winter" class="alignright size-full wp-image-1736" /></a></p>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/V2hKPUZ_XMA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2011/12/winter-wallpapers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2011/12/winter-wallpapers/</feedburner:origLink></item>
		<item>
		<title>Setting up a new skeleton: re-factoring</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/JP8ENgXbaXo/</link>
		<comments>http://rlc.vlinder.ca/blog/2011/12/setting-up-a-new-skeleton-re-factoring/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 01:36:18 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[C++ for the self-taught]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/?p=1722</guid>
		<description><![CDATA[Before we go much further with our SOCKS server, we should do a bit of cleaning up in the project: we&#8217;ll move the Server and Observer classes to their own library, so we can more easily re-use them, and we&#8217;ll &#8230; <a href="http://rlc.vlinder.ca/blog/2011/12/setting-up-a-new-skeleton-re-factoring/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>	<audio id="wp_mep_1" src="http://vlinder.ca/podcasts/35-refactored.mp3"     controls="controls" preload="none"  >
		
		
		
		
		
		
		
		<object width="400" height="30" type="application/x-shockwave-flash" data="http://rlc.vlinder.ca/wp-content/plugins/media-element-html5-video-and-audio-player/mediaelement/flashmediaelement.swf">
			<param name="movie" value="http://rlc.vlinder.ca/wp-content/plugins/media-element-html5-video-and-audio-player/mediaelement/flashmediaelement.swf" />
			<param name="flashvars" value="controls=true&amp;file=http://vlinder.ca/podcasts/35-refactored.mp3" />			
		</object>		
	</audio>
<script type="text/javascript">
jQuery(document).ready(function($) {
	$('#wp_mep_1').mediaelementplayer({
		m:1
		
		,features: ['playpause','current','progress','duration','volume','tracks','fullscreen']
		,audioWidth:400,audioHeight:30
	});
});
</script>
Before we go much further with our SOCKS server, we should do a bit of cleaning up in the project: we&#8217;ll move the <code>Server</code> and <code>Observer</code> classes to their own library, so we can more easily re-use them, and we&#8217;ll copy the <code>Application</code> class over to our new project &#8212; the one that will become our next step towards a fully functional SOCKS server: <em>Episode35</em>.<br />
<span id="more-1722"></span><br />
Most of the dreary details are clearly visible in the diff of the <a href="https://gitorious.org/chausette/chausette/commit/209bb84/diffs" title="the commit diffs" target="_blank">main commit</a> but a few interesting details show up when we compare the two <code>Application</code> classes:<br />
<div class="aside-toggler closed"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code closed"></p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;"><span style="color: #888822;">--- bin/Episode28/Application.cpp	2011-09-26 19:58:52.938883900 -0400</span>
<span style="color: #888822;">+++ bin/Episode35/Application.cpp	2011-10-19 21:28:29.080693800 -0400</span>
<span style="color: #440088;">@@ -3,17 +3,51 @@</span>
 #include &lt;boost/lexical_cast.hpp&gt;
 #include &lt;iostream&gt;
 #include &lt;vector&gt;
<span style="color: #00b000;">+#include &lt;ws2ipdef.h&gt;</span>
<span style="color: #00b000;">+#include &lt;WinSock2.h&gt;</span>
 #include &quot;server/Server.h&quot;
 #include &quot;config.h&quot;
<span style="color: #00b000;">+#include &quot;rfc1928/types.h&quot;</span>
&nbsp;
 using namespace std;
 using namespace boost;
&nbsp;
<span style="color: #00b000;">+struct Application::FDGuard</span>
<span style="color: #00b000;">+<span style="">&#123;</span></span>
<span style="color: #00b000;">+	FDGuard<span style="">&#40;</span>int fd<span style="">&#41;</span></span>
<span style="color: #00b000;">+		: fd_<span style="">&#40;</span>fd<span style="">&#41;</span></span>
<span style="color: #00b000;">+		, dismissed_<span style="">&#40;</span>false<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* no-op */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+	~FDGuard<span style="">&#40;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		if <span style="">&#40;</span>!dismissed_<span style="">&#41;</span></span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+			closesocket<span style="">&#40;</span>fd_<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		<span style="">&#125;</span></span>
<span style="color: #00b000;">+		else</span>
<span style="color: #00b000;">+		<span style="">&#123;</span> /* dismissed */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+	void dismiss<span style="">&#40;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		dismissed_ = true;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+private :</span>
<span style="color: #00b000;">+	FDGuard<span style="">&#40;</span>const FDGuard&amp;<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	FDGuard&amp; operator=<span style="">&#40;</span>const FDGuard&amp;<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+	int fd_;</span>
<span style="color: #00b000;">+	bool dismissed_;</span>
<span style="color: #00b000;">+<span style="">&#125;</span>;</span>
<span style="color: #00b000;">+</span>
 Application::Application<span style="">&#40;</span><span style="">&#41;</span>
 : server_<span style="">&#40;</span><span style="">0</span><span style="">&#41;</span>
<span style="color: #991111;">-, data_to_send_attribute_id_<span style="">&#40;</span>Socket::alloc<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #991111;">-, target_address_attribute_id_<span style="">&#40;</span>Socket::alloc<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #991111;">-, un_paired_socket_<span style="">&#40;</span>0<span style="">&#41;</span></span>
<span style="color: #00b000;">+, socket_state_attribute_id_<span style="">&#40;</span>Socket::alloc<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+, receive_buffer_attribute_id_<span style="">&#40;</span>Socket::alloc<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+, send_buffer_attribute_id_<span style="">&#40;</span>Socket::alloc<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+, socks_reply_attribute_id_<span style="">&#40;</span>Socket::alloc<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
 <span style="">&#123;</span>
 	WSADATA wsadata;
 	WSAStartup<span style="">&#40;</span>MAKEWORD<span style="">&#40;</span><span style="">2</span>, <span style="">2</span><span style="">&#41;</span>, &amp;wsadata<span style="">&#41;</span>;
<span style="color: #440088;">@@ -29,8 +63,8 @@ void Application::run<span style="">&#40;</span>const Application:</span>
 	// for now, expect our own path in arguments<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span>, the IP address to 
 	// listen on in arguments<span style="">&#91;</span><span style="">1</span><span style="">&#93;</span> and the port in arguments<span style="">&#91;</span><span style="">2</span><span style="">&#93;</span>
 	assert<span style="">&#40;</span>arguments.size<span style="">&#40;</span><span style="">&#41;</span> &gt;= <span style="">1</span><span style="">&#41;</span>;
<span style="color: #991111;">-	string ip<span style="">&#40;</span>arguments.size<span style="">&#40;</span><span style="">&#41;</span> &gt; <span style="">1</span> ? arguments<span style="">&#91;</span><span style="">1</span><span style="">&#93;</span> : CHAUSETTE_EPISODE28_DEFAULT_IP<span style="">&#41;</span>;</span>
<span style="color: #991111;">-	unsigned short port<span style="">&#40;</span>arguments.size<span style="">&#40;</span><span style="">&#41;</span> &gt; <span style="">2</span> ? boost::lexical_cast&lt; unsigned short &gt;<span style="">&#40;</span>arguments<span style="">&#91;</span><span style="">2</span><span style="">&#93;</span><span style="">&#41;</span> : CHAUSETTE_EPISODE28_DEFAULT_PORT<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	string ip<span style="">&#40;</span>arguments.size<span style="">&#40;</span><span style="">&#41;</span> &gt; <span style="">1</span> ? arguments<span style="">&#91;</span><span style="">1</span><span style="">&#93;</span> : CHAUSETTE_EPISODE35_DEFAULT_IP<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	unsigned short port<span style="">&#40;</span>arguments.size<span style="">&#40;</span><span style="">&#41;</span> &gt; <span style="">2</span> ? boost::lexical_cast&lt; unsigned short &gt;<span style="">&#40;</span>arguments<span style="">&#91;</span><span style="">2</span><span style="">&#93;</span><span style="">&#41;</span> : CHAUSETTE_EPISODE35_DEFAULT_PORT<span style="">&#41;</span>;</span>
 	sockaddr_storage address;
 	memset<span style="">&#40;</span>&amp;address, <span style="">0</span>, sizeof<span style="">&#40;</span>address<span style="">&#41;</span><span style="">&#41;</span>;
 	sockaddr_in &amp;in_address = reinterpret_cast&lt; sockaddr_in&amp; &gt;<span style="">&#40;</span>address<span style="">&#41;</span>;
<span style="color: #440088;">@@ -62,128 +96,405 @@ void Application::run<span style="">&#40;</span>const Application:</span>
 /*virtual */void Application::onNewConnection<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span>
 <span style="">&#123;</span>
 	Socket &amp;new_socket<span style="">&#40;</span>server_-&gt;accept<span style="">&#40;</span>socket<span style="">&#41;</span><span style="">&#41;</span>;
<span style="color: #991111;">-	remote_address_to_socket_.insert<span style="">&#40;</span>RemoteAddressToSocket::value_type<span style="">&#40;</span>new_socket.remote_address_, &amp;new_socket<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-	pairSocket<span style="">&#40;</span>new_socket<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	new_socket.get<span style="">&#40;</span>socket_state_attribute_id_<span style="">&#41;</span> = expect_authentication_method_request__;</span>
 <span style="">&#125;</span>
&nbsp;
 /*virtual */void Application::onDataReady<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span>
 <span style="">&#123;</span>
<span style="color: #991111;">-	vector&lt; char &gt; temp; // in case the socket is un-paired</span>
<span style="color: #991111;">-	bool needed_to_initialize<span style="">&#40;</span>false<span style="">&#41;</span>;</span>
<span style="color: #991111;">-	vector&lt; char &gt;::size_type offset<span style="">&#40;</span><span style="">0</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-	Socket *partner<span style="">&#40;</span><span style="">&#40;</span>&amp;socket == un_paired_socket_<span style="">&#41;</span> ? <span style="">0</span> : remote_address_to_socket_<span style="">&#91;</span>any_cast&lt; sockaddr_storage &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>target_address_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#93;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-	if <span style="">&#40;</span>partner &amp;&amp;</span>
<span style="color: #991111;">-		partner-&gt;get<span style="">&#40;</span>data_to_send_attribute_id_<span style="">&#41;</span>.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>socket.get<span style="">&#40;</span>receive_buffer_attribute_id_<span style="">&#41;</span>.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
 	<span style="">&#123;</span>
<span style="color: #991111;">-		partner-&gt;get<span style="">&#40;</span>data_to_send_attribute_id_<span style="">&#41;</span> = vector&lt; char &gt;<span style="">&#40;</span><span style="">1024</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-		needed_to_initialize = true;</span>
<span style="color: #00b000;">+		socket.get<span style="">&#40;</span>receive_buffer_attribute_id_<span style="">&#41;</span> = Buffer<span style="">&#40;</span>default_buffer_size__<span style="">&#41;</span>;</span>
 	<span style="">&#125;</span>
<span style="color: #991111;">-	else if <span style="">&#40;</span>partner<span style="">&#41;</span></span>
<span style="color: #991111;">-	<span style="">&#123;</span> /* already have a buffer */ <span style="">&#125;</span></span>
 	else
<span style="color: #991111;">-	<span style="">&#123;</span></span>
<span style="color: #991111;">-		temp.resize<span style="">&#40;</span><span style="">1024</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-		needed_to_initialize = true;</span>
<span style="color: #991111;">-	<span style="">&#125;</span></span>
<span style="color: #991111;">-	vector&lt; char &gt; &amp;buffer = partner ? any_cast&lt; vector&lt; char &gt;&amp; &gt;<span style="">&#40;</span>partner-&gt;get<span style="">&#40;</span>data_to_send_attribute_id_<span style="">&#41;</span><span style="">&#41;</span> : temp;</span>
<span style="color: #991111;">-	if <span style="">&#40;</span>!needed_to_initialize &amp;&amp; buffer.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* already have a receive buffer */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	Buffer &amp;buffer<span style="">&#40;</span>any_cast&lt; Buffer&amp; &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>receive_buffer_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	Buffer::size_type offset<span style="">&#40;</span><span style="">0</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>buffer.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
 	<span style="">&#123;</span>
 		buffer.resize<span style="">&#40;</span>buffer.capacity<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;
 	<span style="">&#125;</span>
<span style="color: #991111;">-	else if <span style="">&#40;</span>!needed_to_initialize<span style="">&#41;</span></span>
<span style="color: #00b000;">+	else</span>
 	<span style="">&#123;</span>
 		offset = buffer.size<span style="">&#40;</span><span style="">&#41;</span>;
<span style="color: #991111;">-		if <span style="">&#40;</span>buffer.capacity<span style="">&#40;</span><span style="">&#41;</span> &lt;= offset + 1024<span style="">&#41;</span></span>
<span style="color: #00b000;">+		if <span style="">&#40;</span>buffer.capacity<span style="">&#40;</span><span style="">&#41;</span> - offset &lt; minimal_available_buffer_size__<span style="">&#41;</span></span>
 		<span style="">&#123;</span>
<span style="color: #991111;">-			buffer.resize<span style="">&#40;</span>offset + <span style="">1024</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			buffer.resize<span style="">&#40;</span>offset + minimal_available_buffer_size__<span style="">&#41;</span>;</span>
 		<span style="">&#125;</span>
 		else
 		<span style="">&#123;</span>
 			buffer.resize<span style="">&#40;</span>buffer.capacity<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;
 		<span style="">&#125;</span>
 	<span style="">&#125;</span>
<span style="color: #00b000;">+	Buffer::size_type avail<span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span> - offset<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	Buffer::pointer recv_ptr<span style="">&#40;</span>&amp;<span style="">&#40;</span>buffer<span style="">&#91;</span>offset<span style="">&#93;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	server_-&gt;read<span style="">&#40;</span>socket, recv_ptr, &amp;avail<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	buffer.resize<span style="">&#40;</span>offset + avail<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	// here, according to the state of the socket, dispatch the data</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>socket.get<span style="">&#40;</span>socket_state_attribute_id_<span style="">&#41;</span>.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		socket.get<span style="">&#40;</span>socket_state_attribute_id_<span style="">&#41;</span> = expect_authentication_method_request__;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
 	else
<span style="color: #991111;">-	<span style="">&#123;</span> /* needed to initialize - so no need to account for data already in the buffer */ <span style="">&#125;</span></span>
<span style="color: #991111;">-	unsigned int data_read<span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span> - offset<span style="">&#41;</span>;</span>
<span style="color: #991111;">-	char *read_ptr<span style="">&#40;</span>&amp;buffer<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-	read_ptr += offset;</span>
<span style="color: #991111;">-	try</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* the socket already has a state */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	switch <span style="">&#40;</span>any_cast&lt; SocketState &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>socket_state_attribute_id_<span style="">&#41;</span>.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span></span>
 	<span style="">&#123;</span>
<span style="color: #991111;">-		server_-&gt;read<span style="">&#40;</span>socket, read_ptr, &amp;data_read<span style="">&#41;</span>;</span>
<span style="color: #991111;">-		buffer.resize<span style="">&#40;</span>offset + data_read<span style="">&#41;</span>;</span>
<span style="color: #991111;">-		unsigned int data_written<span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-		if <span style="">&#40;</span>partner<span style="">&#41;</span></span>
<span style="color: #00b000;">+	case expect_authentication_method_request__ :</span>
<span style="color: #00b000;">+		onAuthenticationMethodRequest<span style="">&#40;</span>socket<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		break;</span>
<span style="color: #00b000;">+	case expect_socks_request__ :</span>
<span style="color: #00b000;">+		onSocksRequest<span style="">&#40;</span>socket<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		break;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+<span style="">&#125;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+/*virtual */void Application::onWriteReady<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
 		<span style="">&#123;</span>
<span style="color: #991111;">-			server_-&gt;write<span style="">&#40;</span>*partner, &amp;buffer<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span>, &amp;data_written<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>!socket.get<span style="">&#40;</span>send_buffer_attribute_id_<span style="">&#41;</span>.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		Buffer &amp;buffer<span style="">&#40;</span>any_cast&lt; Buffer&amp; &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>send_buffer_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		Buffer::size_type offset<span style="">&#40;</span><span style="">0</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		if <span style="">&#40;</span>!buffer.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+			Buffer::size_type avail<span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			Buffer::pointer send_ptr<span style="">&#40;</span>&amp;<span style="">&#40;</span>buffer<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			server_-&gt;write<span style="">&#40;</span>socket, send_ptr, &amp;avail<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			buffer.erase<span style="">&#40;</span>buffer.begin<span style="">&#40;</span><span style="">&#41;</span>, buffer.begin<span style="">&#40;</span><span style="">&#41;</span> + avail<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		<span style="">&#125;</span></span>
<span style="color: #00b000;">+		else</span>
<span style="color: #00b000;">+		<span style="">&#123;</span> /* nothing to send */ <span style="">&#125;</span></span>
 		<span style="">&#125;</span>
 		else
<span style="color: #991111;">-		<span style="">&#123;</span> /* no partner to send data to */ <span style="">&#125;</span></span>
<span style="color: #991111;">-		buffer.erase<span style="">&#40;</span>buffer.begin<span style="">&#40;</span><span style="">&#41;</span>, buffer.begin<span style="">&#40;</span><span style="">&#41;</span> + data_written<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* nothing to send */ <span style="">&#125;</span></span>
 	<span style="">&#125;</span>
<span style="color: #991111;">-	catch <span style="">&#40;</span>const Server::NetworkError&amp;<span style="">&#41;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+/*virtual */void Application::onExceptionalDataReady<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
 	<span style="">&#123;</span>
<span style="color: #991111;">-		// ignore this for now: the socket will have been dealt with but this is no reason for us to crash.</span>
 	<span style="">&#125;</span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+/*virtual */void Application::onCloseSocket<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
<span style="color: #00b000;">+<span style="">&#123;</span></span>
 <span style="">&#125;</span>
&nbsp;
<span style="color: #991111;">-/*virtual */void Application::onWriteReady<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
<span style="color: #00b000;">+void Application::onAuthenticationMethodRequest<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span> const</span>
 <span style="">&#123;</span>
<span style="color: #991111;">-	if <span style="">&#40;</span>socket.get<span style="">&#40;</span>data_to_send_attribute_id_<span style="">&#41;</span>.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #991111;">-	<span style="">&#123;</span> /* no-op */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	using Vlinder::Chausette::RFC1928::VersionIdentifierMethodSelectionMessage;</span>
<span style="color: #00b000;">+	using Vlinder::Chausette::RFC1928::MethodMessage;</span>
<span style="color: #00b000;">+	Buffer &amp;buffer<span style="">&#40;</span>any_cast&lt; Buffer&amp; &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>receive_buffer_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span> &lt; offsetof<span style="">&#40;</span>VersionIdentifierMethodSelectionMessage, methods_<span style="">&#41;</span> + 1<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		throw InsufficientData<span style="">&#40;</span>&quot;Not enough data for a version identifier/method selection message&quot;<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
 	else
<span style="color: #00b000;">+	<span style="">&#123;</span> /* all is well so far */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	VersionIdentifierMethodSelectionMessage *message<span style="">&#40;</span>reinterpret_cast&lt; VersionIdentifierMethodSelectionMessage* &gt;<span style="">&#40;</span>&amp;buffer<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>message-&gt;ver_ != CHAUSETTE_EPISODE35_SOCKS_VERSION<span style="">&#41;</span></span>
 	<span style="">&#123;</span>
<span style="color: #991111;">-		vector&lt; char &gt; &amp;buffer = any_cast&lt; vector&lt; char &gt;&amp; &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>data_to_send_attribute_id_<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-		if <span style="">&#40;</span>buffer.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
<span style="color: #991111;">-		<span style="">&#123;</span> /* no-op */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+		throw WrongSocksVersion<span style="">&#40;</span>&quot;Wrong socks version&quot;, CHAUSETTE_EPISODE35_SOCKS_VERSION, message-&gt;ver_<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+	else</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* the version is OK */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span> &lt; offsetof<span style="">&#40;</span>VersionIdentifierMethodSelectionMessage, methods_<span style="">&#41;</span> + message-&gt;nmethods_<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		throw InsufficientData<span style="">&#40;</span>&quot;Not enough data for a version identifier/method selection message&quot;<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
 		else
<span style="color: #00b000;">+	<span style="">&#123;</span> /* all is well so far */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	/* As we haven't implemented any authentication methods yet, we only </span>
<span style="color: #00b000;">+	 * support &quot;no authentication&quot; - method 0. If it is not present among </span>
<span style="color: #00b000;">+	 * the methods, throw an exception. */</span>
<span style="color: #00b000;">+	bool authentication_ok<span style="">&#40;</span>false<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	for <span style="">&#40;</span>unsigned char *method = message-&gt;methods_; !authentication_ok &amp;&amp; <span style="">&#40;</span><span style="">&#40;</span>method - message-&gt;methods_<span style="">&#41;</span> &lt; message-&gt;nmethods_<span style="">&#41;</span>; ++method<span style="">&#41;</span></span>
 		<span style="">&#123;</span>
<span style="color: #991111;">-			unsigned int data_written<span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-			server_-&gt;write<span style="">&#40;</span>socket, &amp;buffer<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span>, &amp;data_written<span style="">&#41;</span>;</span>
<span style="color: #991111;">-			buffer.erase<span style="">&#40;</span>buffer.begin<span style="">&#40;</span><span style="">&#41;</span>, buffer.begin<span style="">&#40;</span><span style="">&#41;</span> + data_written<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		authentication_ok = <span style="">&#40;</span>*method == <span style="">0</span><span style="">&#41;</span>;</span>
 		<span style="">&#125;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>!authentication_ok<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		throw NoSupportedAuthenticationMethod<span style="">&#40;</span>&quot;No supported authentication method&quot;<span style="">&#41;</span>;</span>
 	<span style="">&#125;</span>
<span style="color: #00b000;">+	else</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* all is well */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	MethodMessage methodMessage<span style="">&#40;</span>CHAUSETTE_EPISODE35_SOCKS_VERSION, <span style="">0</span>/* no authentication - put a constant here later */<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	unsigned char *ptr<span style="">&#40;</span>reinterpret_cast&lt; unsigned char* &gt;<span style="">&#40;</span>&amp;methodMessage<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	queueDataToSend<span style="">&#40;</span>socket, ptr, ptr + sizeof<span style="">&#40;</span>methodMessage<span style="">&#41;</span><span style="">&#41;</span>;</span>
 <span style="">&#125;</span>
&nbsp;
<span style="color: #991111;">-/*virtual */void Application::onExceptionalDataReady<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
<span style="color: #00b000;">+void Application::onSocksRequest<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
 <span style="">&#123;</span>
<span style="color: #00b000;">+	using Vlinder::Chausette::RFC1928::SocksRequest;</span>
<span style="color: #00b000;">+	Buffer &amp;buffer<span style="">&#40;</span>any_cast&lt; Buffer&amp; &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>receive_buffer_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span> &lt; offsetof<span style="">&#40;</span>SocksRequest, dst_addr_<span style="">&#41;</span> + 1<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		throw InsufficientData<span style="">&#40;</span>&quot;Not enough data for a SOCKS request&quot;<span style="">&#41;</span>;</span>
 <span style="">&#125;</span>
<span style="color: #991111;">-</span>
<span style="color: #991111;">-/*virtual */void Application::onCloseSocket<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
<span style="color: #00b000;">+	else</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* all is well so far */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	SocksRequest *message<span style="">&#40;</span>reinterpret_cast&lt; SocksRequest* &gt;<span style="">&#40;</span>&amp;buffer<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>message-&gt;ver_ != CHAUSETTE_EPISODE35_SOCKS_VERSION<span style="">&#41;</span></span>
 <span style="">&#123;</span>
<span style="color: #991111;">-	RemoteAddressToSocket::iterator where<span style="">&#40;</span>remote_address_to_socket_.find<span style="">&#40;</span>socket.remote_address_<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-	assert<span style="">&#40;</span>where != remote_address_to_socket_.end<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-	assert<span style="">&#40;</span>where-&gt;second == &amp;socket<span style="">&#41;</span>;</span>
<span style="color: #991111;">-	remote_address_to_socket_.erase<span style="">&#40;</span>where<span style="">&#41;</span>;</span>
<span style="color: #991111;">-	unpairSocket<span style="">&#40;</span>socket<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		throw WrongSocksVersion<span style="">&#40;</span>&quot;Wrong SOCKS version&quot;, CHAUSETTE_EPISODE35_SOCKS_VERSION, message-&gt;ver_<span style="">&#41;</span>;</span>
 <span style="">&#125;</span>
<span style="color: #991111;">-</span>
<span style="color: #991111;">-void Application::pairSocket<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
<span style="color: #00b000;">+	else</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* the version is OK */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	/* Inside the message, the port field is the only one that isn't </span>
<span style="color: #00b000;">+	 * necessarily in the same position as in the struct. The other</span>
<span style="color: #00b000;">+	 * fields are where they should be - and we can get to the port </span>
<span style="color: #00b000;">+	 * field by pasing the address type field. */</span>
<span style="color: #00b000;">+	sockaddr_storage address;</span>
<span style="color: #00b000;">+	memset<span style="">&#40;</span>&amp;address, <span style="">0</span>, sizeof<span style="">&#40;</span>address<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	switch <span style="">&#40;</span>message-&gt;atyp_<span style="">&#41;</span></span>
 <span style="">&#123;</span>
<span style="color: #991111;">-	if <span style="">&#40;</span>un_paired_socket_<span style="">&#41;</span></span>
<span style="color: #00b000;">+	case 1 :</span>
 	<span style="">&#123;</span>
<span style="color: #991111;">-		un_paired_socket_-&gt;get<span style="">&#40;</span>target_address_attribute_id_<span style="">&#41;</span> = socket.remote_address_;</span>
<span style="color: #991111;">-		socket.get<span style="">&#40;</span>target_address_attribute_id_<span style="">&#41;</span> = un_paired_socket_-&gt;remote_address_;</span>
<span style="color: #991111;">-		un_paired_socket_ = <span style="">0</span>;</span>
<span style="color: #00b000;">+		// IP V4 address: X'01'</span>
<span style="color: #00b000;">+		if <span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span> &lt; offsetof<span style="">&#40;</span>SocksRequest, dst_addr_<span style="">&#41;</span> + 6 /* four for the address, two for the port */<span style="">&#41;</span></span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+			throw InsufficientData<span style="">&#40;</span>&quot;Not enough data for a SOCKS request&quot;<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		<span style="">&#125;</span></span>
<span style="color: #00b000;">+		else</span>
<span style="color: #00b000;">+		<span style="">&#123;</span> /* all is well so far */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+		address.ss_family = AF_INET;</span>
<span style="color: #00b000;">+		sockaddr_in *a4<span style="">&#40;</span>reinterpret_cast&lt; sockaddr_in* &gt;<span style="">&#40;</span>&amp;address<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		memcpy<span style="">&#40;</span>&amp;a4-&gt;sin_addr, message-&gt;dst_addr_, <span style="">4</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		memcpy<span style="">&#40;</span>&amp;a4-&gt;sin_port, message-&gt;dst_addr_ + <span style="">4</span>, <span style="">2</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		break;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+	case 3 :</span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		// DOMAINNAME: X'03'</span>
<span style="color: #00b000;">+		unsigned char hostname_length<span style="">&#40;</span>message-&gt;dst_addr_<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		if <span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span> &lt; offsetof<span style="">&#40;</span>SocksRequest, dst_addr_<span style="">&#41;</span> + hostname_length + 2 /* hostname_length for the address, two for the port */<span style="">&#41;</span></span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+			throw InsufficientData<span style="">&#40;</span>&quot;Not enough data for a SOCKS request&quot;<span style="">&#41;</span>;</span>
 	<span style="">&#125;</span>
 	else
<span style="color: #00b000;">+		<span style="">&#123;</span> /* all is well so far */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+		char *hostname_begin<span style="">&#40;</span>reinterpret_cast&lt; char* &gt;<span style="">&#40;</span>message-&gt;dst_addr_ + <span style="">1</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		char *hostname_end = hostname_begin + hostname_length;</span>
<span style="color: #00b000;">+		unsigned short port<span style="">&#40;</span>*reinterpret_cast&lt; unsigned short* &gt;<span style="">&#40;</span>hostname_end<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		*hostname_end = <span style="">0</span>; // cap it off</span>
<span style="color: #00b000;">+		hostent *host_entry<span style="">&#40;</span>gethostbyname<span style="">&#40;</span>hostname_begin<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		if <span style="">&#40;</span>!host_entry<span style="">&#41;</span></span>
 	<span style="">&#123;</span>
<span style="color: #991111;">-		un_paired_socket_ = &amp;socket;</span>
<span style="color: #00b000;">+			throw NameResolutionError<span style="">&#40;</span>&quot;Name resolution error&quot;, GetLastError<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		<span style="">&#125;</span></span>
<span style="color: #00b000;">+		else</span>
<span style="color: #00b000;">+		<span style="">&#123;</span> /* resolved OK */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+		switch <span style="">&#40;</span>host_entry-&gt;h_addrtype<span style="">&#41;</span></span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+		case AF_INET :</span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+			sockaddr_in *a4<span style="">&#40;</span>reinterpret_cast&lt; sockaddr_in* &gt;<span style="">&#40;</span>&amp;address<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			memcpy<span style="">&#40;</span>&amp;a4-&gt;sin_addr, host_entry-&gt;h_addr_list<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span>, <span style="">4</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			a4-&gt;sin_port = port;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+		<span style="">&#125;</span></span>
<span style="color: #00b000;">+		case AF_INET6 :</span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+			sockaddr_in6 *a6<span style="">&#40;</span>reinterpret_cast&lt; sockaddr_in6* &gt;<span style="">&#40;</span>&amp;address<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			memcpy<span style="">&#40;</span>&amp;a6-&gt;sin6_addr, host_entry-&gt;h_addr_list<span style="">&#91;</span><span style="">0</span><span style="">&#93;</span>, <span style="">16</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			a6-&gt;sin6_port = port;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+		<span style="">&#125;</span></span>
 	<span style="">&#125;</span>
<span style="color: #00b000;">+		break;</span>
 <span style="">&#125;</span>
<span style="color: #00b000;">+	case 4 :</span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		// IP V6 address: X'04'</span>
<span style="color: #00b000;">+		if <span style="">&#40;</span>buffer.size<span style="">&#40;</span><span style="">&#41;</span> &lt; offsetof<span style="">&#40;</span>SocksRequest, dst_addr_<span style="">&#41;</span> + 16 + 2 /* four for the address, two for the port */<span style="">&#41;</span></span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+			throw InsufficientData<span style="">&#40;</span>&quot;Not enough data for a SOCKS request&quot;<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		<span style="">&#125;</span></span>
<span style="color: #00b000;">+		else</span>
<span style="color: #00b000;">+		<span style="">&#123;</span> /* all is well so far */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+		address.ss_family = AF_INET6;</span>
<span style="color: #00b000;">+		sockaddr_in6 *a6<span style="">&#40;</span>reinterpret_cast&lt; sockaddr_in6* &gt;<span style="">&#40;</span>&amp;address<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		memcpy<span style="">&#40;</span>&amp;a6-&gt;sin6_addr, message-&gt;dst_addr_, <span style="">16</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		memcpy<span style="">&#40;</span>&amp;a6-&gt;sin6_port, message-&gt;dst_addr_ + <span style="">16</span>, <span style="">2</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		break;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+	default :</span>
<span style="color: #00b000;">+		throw UnknownAddressType<span style="">&#40;</span>&quot;Unknown address type&quot;, message-&gt;atyp_<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+	switch <span style="">&#40;</span>message-&gt;cmd_<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+	case 1 :</span>
<span style="color: #00b000;">+		// CONNECT X'01'</span>
<span style="color: #00b000;">+		doConnect<span style="">&#40;</span>socket, address<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		break;</span>
<span style="color: #00b000;">+	case 2 :</span>
<span style="color: #00b000;">+		// BIND X'02' //TODO</span>
<span style="color: #00b000;">+	case 3 :</span>
<span style="color: #00b000;">+		// UDP ASSOCIATE X'03' //TODO</span>
<span style="color: #00b000;">+		break;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+<span style="">&#125;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+void Application::doConnect<span style="">&#40;</span>Socket &amp;parent_socket, const sockaddr_storage &amp;address<span style="">&#41;</span></span>
<span style="color: #00b000;">+<span style="">&#123;</span></span>
<span style="color: #00b000;">+	/* In the reply to a CONNECT, BND.PORT contains the port number that the</span>
<span style="color: #00b000;">+	 * server assigned to connect to the target host, while BND.ADDR</span>
<span style="color: #00b000;">+	 * contains the associated IP address.  The supplied BND.ADDR is often</span>
<span style="color: #00b000;">+	 * different from the IP address that the client uses to reach the SOCKS</span>
<span style="color: #00b000;">+	 * server, since such servers are often multi-homed.  It is expected</span>
<span style="color: #00b000;">+	 * that the SOCKS server will use DST.ADDR and DST.PORT, and the</span>
<span style="color: #00b000;">+	 * client-side source address and port in evaluating the CONNECT</span>
<span style="color: #00b000;">+	 * request. */</span>
<span style="color: #00b000;">+	using Vlinder::Chausette::RFC1928::SocksReply;</span>
<span style="color: #00b000;">+	SocksReply reply;</span>
<span style="color: #00b000;">+	memset<span style="">&#40;</span>&amp;reply, <span style="">0</span>, sizeof<span style="">&#40;</span>reply<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	reply.ver_ = CHAUSETTE_EPISODE35_SOCKS_VERSION;</span>
<span style="color: #00b000;">+	/* attempt to connect to the target address. If successful, open a </span>
<span style="color: #00b000;">+	 * server socket for the client to connect to, and forward the data</span>
<span style="color: #00b000;">+	 * between the two. */</span>
<span style="color: #00b000;">+	int sock_fd<span style="">&#40;</span>::socket<span style="">&#40;</span>address.ss_family, SOCK_STREAM, IPPROTO_TCP<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>sock_fd == INVALID_SOCKET<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		reply.rep_ = <span style="">1</span>; // X'01' general SOCKS server failure</span>
<span style="color: #00b000;">+		parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+		setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+	else</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* all is well */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	FDGuard fd_guard<span style="">&#40;</span>sock_fd<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	u_long arg<span style="">&#40;</span><span style="">1</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>ioctlsocket<span style="">&#40;</span>sock_fd, FIONBIO, &amp;arg<span style="">&#41;</span> != 0<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		reply.rep_ = <span style="">1</span>; // X'01' general SOCKS server failure</span>
<span style="color: #00b000;">+		parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+		setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		throw SocketIOCTLFailed<span style="">&#40;</span>&quot;Failed to set socket to non-blocking&quot;, WSAGetLastError<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+	else</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* all is well so far */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>connect<span style="">&#40;</span>sock_fd, <span style="">&#40;</span>const sockaddr*<span style="">&#41;</span>&amp;address, sizeof<span style="">&#40;</span>address<span style="">&#41;</span><span style="">&#41;</span> != 0<span style="">&#41;</span></span>
<span style="color: #00b000;">+	<span style="">&#123;</span></span>
<span style="color: #00b000;">+		unsigned long last_error<span style="">&#40;</span>WSAGetLastError<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		switch <span style="">&#40;</span>last_error<span style="">&#41;</span></span>
<span style="color: #00b000;">+		<span style="">&#123;</span></span>
<span style="color: #00b000;">+		// logic errors</span>
<span style="color: #00b000;">+		case WSANOTINITIALISED :</span>
<span style="color: #00b000;">+			/* A successful WSAStartup call must occur before using this function. */</span>
<span style="color: #00b000;">+		case WSAEADDRINUSE :</span>
<span style="color: #00b000;">+			/* The socket's local address is already in use and the socket was not </span>
<span style="color: #00b000;">+			 * marked to allow address reuse with SO_REUSEADDR. This error usually </span>
<span style="color: #00b000;">+			 * occurs when executing bind, but could be delayed until the connect </span>
<span style="color: #00b000;">+			 * function if the bind was to a wildcard address <span style="">&#40;</span>INADDR_ANY or </span>
<span style="color: #00b000;">+			 * in6addr_any<span style="">&#41;</span> for the local IP address. A specific address needs to </span>
<span style="color: #00b000;">+			 * be implicitly bound by the connect function. */</span>
<span style="color: #00b000;">+		case WSAEINTR :</span>
<span style="color: #00b000;">+			/* The blocking Windows Socket 1.1 call was canceled through </span>
<span style="color: #00b000;">+			 * WSACancelBlockingCall. */</span>
<span style="color: #00b000;">+		case WSAEINPROGRESS :</span>
<span style="color: #00b000;">+			/* A blocking Windows Sockets 1.1 call is in progress, or the service </span>
<span style="color: #00b000;">+			 * provider is still processing a callback function. */</span>
<span style="color: #00b000;">+		case WSAEALREADY :</span>
<span style="color: #00b000;">+			/* A nonblocking connect call is in progress on the specified socket.</span>
<span style="color: #00b000;">+			 * Note  In order to preserve backward compatibility, this error is reported </span>
<span style="color: #00b000;">+			 * as WSAEINVAL to Windows Sockets 1.1 applications that link to either </span>
<span style="color: #00b000;">+			 * Winsock.dll or Wsock32.dll. */</span>
<span style="color: #00b000;">+		case WSAEAFNOSUPPORT :</span>
<span style="color: #00b000;">+			/* Addresses in the specified family cannot be used with this socket. */</span>
<span style="color: #00b000;">+		case WSAEINVAL :</span>
<span style="color: #00b000;">+			/* The parameter s is a listening socket. */</span>
<span style="color: #00b000;">+		case WSAEISCONN :</span>
<span style="color: #00b000;">+			/* The socket is already connected <span style="">&#40;</span>connection-oriented sockets only<span style="">&#41;</span>. */</span>
<span style="color: #00b000;">+		case WSAENOTSOCK :</span>
<span style="color: #00b000;">+			/* The descriptor specified in the s parameter is not a socket. */</span>
<span style="color: #00b000;">+		case WSAEACCES :</span>
<span style="color: #00b000;">+			/* An attempt to connect a datagram socket to broadcast address failed </span>
<span style="color: #00b000;">+			 * because setsockopt option SO_BROADCAST is not enabled. */</span>
<span style="color: #00b000;">+		default :</span>
<span style="color: #00b000;">+			reply.rep_ = <span style="">1</span>; // X'01' general SOCKS server failure</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			throw ConnectError<span style="">&#40;</span>&quot;Internal error calling connect&quot;, last_error<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+		// run-time errors outside the caller's control</span>
<span style="color: #00b000;">+		case WSAENETDOWN :</span>
<span style="color: #00b000;">+			/* The network subsystem has failed. */</span>
<span style="color: #00b000;">+			reply.rep_ = <span style="">1</span>; // X'01' general SOCKS server failure</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+		case WSAECONNREFUSED :</span>
<span style="color: #00b000;">+			/* The attempt to connect was forcefully rejected. */</span>
<span style="color: #00b000;">+			reply.rep_ = <span style="">5</span>; // X'05' Connection refused</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+		case WSAENETUNREACH :</span>
<span style="color: #00b000;">+			/* The network cannot be reached from this host at this time. */</span>
<span style="color: #00b000;">+			reply.rep_ = <span style="">3</span>; // X'03' Network unreachable</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+		case WSAEHOSTUNREACH :</span>
<span style="color: #00b000;">+			/* A socket operation was attempted to an unreachable host. */</span>
<span style="color: #00b000;">+			reply.rep_ = <span style="">4</span>; // X'04' Host unreachable</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+		case WSAENOBUFS :</span>
<span style="color: #00b000;">+			/* Note  No buffer space is available. The socket cannot be connected. */</span>
<span style="color: #00b000;">+			reply.rep_ = <span style="">1</span>; // X'01' general SOCKS server failure</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+		case WSAETIMEDOUT :</span>
<span style="color: #00b000;">+			/* An attempt to connect timed out without establishing a connection. */</span>
<span style="color: #00b000;">+			reply.rep_ = <span style="">6</span>; // X'06' TTL expired</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+		// run-time errors that point to bugs in the caller/client</span>
<span style="color: #00b000;">+		case WSAEADDRNOTAVAIL :</span>
<span style="color: #00b000;">+			/* The remote address is not a valid address <span style="">&#40;</span>such as INADDR_ANY or in6addr_any<span style="">&#41;</span> . */</span>
<span style="color: #00b000;">+		case WSAEFAULT :</span>
<span style="color: #00b000;">+			/* The sockaddr structure pointed to by the name contains incorrect address format </span>
<span style="color: #00b000;">+			 * for the associated address family or the namelen parameter is too small. This error </span>
<span style="color: #00b000;">+			 * is also returned if the sockaddr structure pointed to by the name parameter with </span>
<span style="color: #00b000;">+			 * a length specified in the namelen parameter is not in a valid part of the user </span>
<span style="color: #00b000;">+			 * address space. */</span>
<span style="color: #00b000;">+			reply.rep_ = <span style="">8</span>; // X'08' Address type not supported</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, send_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+		// &quot;normal&quot; errors</span>
<span style="color: #00b000;">+		case WSAEWOULDBLOCK :</span>
<span style="color: #00b000;">+			/* The socket is marked as nonblocking and the connection cannot be completed immediately. */</span>
<span style="color: #00b000;">+			parent_socket.get<span style="">&#40;</span>socks_reply_attribute_id_<span style="">&#41;</span> = reply;</span>
<span style="color: #00b000;">+			setSocketState<span style="">&#40;</span>parent_socket, wait_socks_reply__<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+			break;</span>
<span style="color: #00b000;">+		<span style="">&#125;</span></span>
<span style="color: #00b000;">+	<span style="">&#125;</span></span>
<span style="color: #00b000;">+	else</span>
<span style="color: #00b000;">+	<span style="">&#123;</span> /* all is well */ <span style="">&#125;</span></span>
<span style="color: #00b000;">+<span style="">&#125;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+void Application::setSocketState<span style="">&#40;</span>Socket &amp;socket, Application::SocketState state<span style="">&#41;</span></span>
<span style="color: #00b000;">+<span style="">&#123;</span></span>
<span style="color: #00b000;">+	SocketState current_state<span style="">&#40;</span>any_cast&lt; SocketState &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>socket_state_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	//TODO</span>
<span style="color: #00b000;">+<span style="">&#125;</span></span>
<span style="color: #00b000;">+</span>
&nbsp;
<span style="color: #991111;">-void Application::unpairSocket<span style="">&#40;</span>Socket &amp;socket<span style="">&#41;</span></span>
<span style="color: #00b000;">+void Application::queueDataToSend<span style="">&#40;</span>Socket &amp;socket, unsigned char *begin, unsigned char *end<span style="">&#41;</span> const</span>
 <span style="">&#123;</span>
<span style="color: #991111;">-	// find the socket this one was paired to</span>
<span style="color: #991111;">-	if <span style="">&#40;</span>un_paired_socket_ == &amp;socket<span style="">&#41;</span></span>
<span style="color: #00b000;">+	Buffer *buffer<span style="">&#40;</span><span style="">0</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	if <span style="">&#40;</span>!socket.get<span style="">&#40;</span>send_buffer_attribute_id_<span style="">&#41;</span>.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span></span>
 	<span style="">&#123;</span>
<span style="color: #991111;">-		un_paired_socket_ = <span style="">0</span>;</span>
<span style="color: #00b000;">+		buffer = &amp;<span style="">&#40;</span>any_cast&lt; Buffer&amp; &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>send_buffer_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
 	<span style="">&#125;</span>
 	else
 	<span style="">&#123;</span>
<span style="color: #991111;">-		assert<span style="">&#40;</span>!socket.get<span style="">&#40;</span>target_address_attribute_id_<span style="">&#41;</span>.empty<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-		sockaddr_storage target_address<span style="">&#40;</span>any_cast&lt; sockaddr_storage &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>target_address_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #991111;">-		Socket *other_socket<span style="">&#40;</span>remote_address_to_socket_<span style="">&#91;</span>target_address<span style="">&#93;</span><span style="">&#41;</span>;</span>
		other_socket-&gt;get<span style="">&#40;</span>target_address_attribute_id_<span style="">&#41;</span> = any<span style="">&#40;</span><span style="">&#41;</span>;
<span style="color: #991111;">-		pairSocket<span style="">&#40;</span>*other_socket<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		socket.get<span style="">&#40;</span>send_buffer_attribute_id_<span style="">&#41;</span> = Buffer<span style="">&#40;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+		buffer = &amp;<span style="">&#40;</span>any_cast&lt; Buffer&amp; &gt;<span style="">&#40;</span>socket.get<span style="">&#40;</span>send_buffer_attribute_id_<span style="">&#41;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
 	<span style="">&#125;</span>
<span style="color: #00b000;">+	copy<span style="">&#40;</span>begin, end, back_inserter<span style="">&#40;</span>*buffer<span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	unsigned int data<span style="">&#40;</span>buffer-&gt;size<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	server_-&gt;write<span style="">&#40;</span>socket, &amp;<span style="">&#40;</span><span style="">&#40;</span>*buffer<span style="">&#41;</span><span style="">&#91;</span><span style="">0</span><span style="">&#93;</span><span style="">&#41;</span>, &amp;data<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+	buffer-&gt;erase<span style="">&#40;</span>buffer-&gt;begin<span style="">&#40;</span><span style="">&#41;</span>, buffer-&gt;begin<span style="">&#40;</span><span style="">&#41;</span> + data<span style="">&#41;</span>;</span>
 <span style="">&#125;</span></pre></div></div>

<p></div><script type="text/javascript">
            /* <![CDATA[ */
            jQuery( document ).ready( function(){
                jQuery( ".aside-toggler" ).click( function(){
                    jQuery( this ).toggleClass( "open" ).toggleClass( "closed" ).next( ".aside" ).slideToggle( "slow", function(){
                        jQuery( this ).toggleClass( "open" ).toggleClass( "closed" );
                    });
                });
            });
            /* ]]&gt; */
            </script></p>
<p>We are going to have to manage different kinds of sockets: sockets for command-and-control (which we will call &#8220;control sockets&#8221; from here on) and sockets that need to be proxied. At first, we&#8217;ll just do TCP proxying but, eventually, we will also proxy UDP.</p>
<p>The way this is going to work, we will have only one thread to do most of the work &#8212; so we&#8217;ll have to be relatively smart about multiplexing our work. In this context, multiplexing means that we tell the sockets API &#8212; and therefore the underlying TCP/IP stack &#8212; what we want it to do, but we don&#8217;t wait around for it to perform its tasks: rather, we tell it to notify us whenever a task is finished and will carry it on from there. For that to work we do, of course, need to know what task it was performing. We do that by associating a <em>state</em> with each socket, which we put in one of the attributes, which we will call <code>socket_state_attribute_id_</code>. That attribute is allocated in the constructor and used throughout the code. For example, in <code>onDataReady</code>, it is used to know what to do with the incoming data:</p>
<div class="aside-toggler closed"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code closed"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// here, according to the state of the socket, dispatch the data</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>socket_state_attribute_id_<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>socket_state_attribute_id_<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> expect_authentication_method_request__<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">else</span>
<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* the socket already has a state */</span> <span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span>any_cast<span style="color: #000080;">&lt;</span> SocketState <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>socket_state_attribute_id_<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">case</span> expect_authentication_method_request__ <span style="color: #008080;">:</span>
	onAuthenticationMethodRequest<span style="color: #008000;">&#40;</span>socket<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">case</span> expect_socks_request__ <span style="color: #008080;">:</span>
	onSocksRequest<span style="color: #008000;">&#40;</span>socket<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>
<p>This basically turns the socket itself into a <a href="http://rlc.vlinder.ca/blog/2010/01/error-handling-in-c/" title="state machines, and error handling, in C" target="_blank">state machine</a>. The available states would, of course, be different according to the role of the socket (i.e. a data socket would never be expected to send a SOCKS request).</p>
<p>We will look into state machines in the next installment.</p>
<p>Another important/interesting part of the new code is the parsing of SOCKS requests. We will look at the actions they imply later, but we will take a closer look at the parsing now:</p>
<p>The first step is to read the data from the buffer. If there isn&#8217;t enough data in the buffer, the request cannot be parsed and should be set aside. Some preliminary checks can be performed on the request immediately, as the following snippet of code will show:<br />
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>181
182
183
184
185
186
187
188
189
190
191
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Application<span style="color: #008080;">::</span><span style="color: #007788;">onAuthenticationMethodRequest</span><span style="color: #008000;">&#40;</span>Socket <span style="color: #000040;">&amp;</span>socket<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> Vlinder<span style="color: #008080;">::</span><span style="color: #007788;">Chausette</span><span style="color: #008080;">::</span><span style="color: #007788;">RFC1928</span><span style="color: #008080;">::</span><span style="color: #007788;">VersionIdentifierMethodSelectionMessage</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">using</span> Vlinder<span style="color: #008080;">::</span><span style="color: #007788;">Chausette</span><span style="color: #008080;">::</span><span style="color: #007788;">RFC1928</span><span style="color: #008080;">::</span><span style="color: #007788;">MethodMessage</span><span style="color: #008080;">;</span>
	Buffer <span style="color: #000040;">&amp;</span>buffer<span style="color: #008000;">&#40;</span>any_cast<span style="color: #000080;">&lt;</span> Buffer<span style="color: #000040;">&amp;</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>receive_buffer_attribute_id_<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>buffer.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">offsetof</span><span style="color: #008000;">&#40;</span>VersionIdentifierMethodSelectionMessage, methods_<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">throw</span> InsufficientData<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Not enough data for a version identifier/method selection message&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* all is well so far */</span> <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div></p>
<p>Now that we know we at least have enough data, we can treat the data as a message and, assuming the data in the buffer is either properly aligned, or we don&#8217;t care about the alignment, we can simply cast the buffer to the appropriate message type and do some more preliminary checks:</p>
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>192
193
194
195
196
197
198
199
200
201
202
203
204
205
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">	VersionIdentifierMethodSelectionMessage <span style="color: #000040;">*</span>message<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span> VersionIdentifierMethodSelectionMessage<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>buffer<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ver_ <span style="color: #000040;">!</span><span style="color: #000080;">=</span> CHAUSETTE_EPISODE35_SOCKS_VERSION<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">throw</span> WrongSocksVersion<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Wrong socks version&quot;</span>, CHAUSETTE_EPISODE35_SOCKS_VERSION, message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ver_<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* the version is OK */</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> message_size<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">offsetof</span><span style="color: #008000;">&#40;</span>VersionIdentifierMethodSelectionMessage, methods_<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nmethods_<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>buffer.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> message_size<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">throw</span> InsufficientData<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Not enough data for a version identifier/method selection message&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* all is well so far */</span> <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>
<p>In this specific message, we&#8217;re setting up an authentication method &#8211; we won&#8217;t support authentication right away, so for now, only method <code>0</code> is supported. According to the protocol, this means that at least one of the proposed authentication methods, of which there can be up to 255, has to be <code>0</code>.</p>
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>209
210
211
212
213
214
215
216
217
218
219
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">	<span style="color: #0000ff;">bool</span> authentication_ok<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>method <span style="color: #000080;">=</span> message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>methods_<span style="color: #008080;">;</span> <span style="color: #000040;">!</span>authentication_ok <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>method <span style="color: #000040;">-</span> message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>methods_<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>nmethods_<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>method<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		authentication_ok <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>method <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>authentication_ok<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">throw</span> NoSupportedAuthenticationMethod<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;No supported authentication method&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* all is well */</span> <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>
<p>Once we&#8217;ve established that we can, indeed, authenticate (or rather: that we don&#8217;t need to) we can set up our reply, and send it.</p>
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>220
221
222
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">	MethodMessage method_message<span style="color: #008000;">&#40;</span>CHAUSETTE_EPISODE35_SOCKS_VERSION, <span style="color: #0000dd;">0</span><span style="color: #ff0000; font-style: italic;">/* no authentication - put a constant here later */</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>ptr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>method_message<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	queueDataToSend<span style="color: #008000;">&#40;</span>socket, ptr, ptr <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>method_message<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p></div>
<p>Then, we set the state of the socket to one in which we expect SOCKS requests &#8212; now that the initial handshaking is done &#8212; and consume the data we&#8217;ve already handled.</p>
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>223
224
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">	socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>socket_state_attribute_id_<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> expect_socks_request__<span style="color: #008080;">;</span>
	buffer.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>buffer.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, buffer.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> message_size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p></div>
<p>SOCKS requests are handled pretty much the same way, but contain an address &#8212; the IP address of the host we will be proxying with. According to the address type, there are different ways to extract the IP address of the host we will be proxying with. It can either contain the actual IPv4 address, as in the next bit of code:<br />
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">	sockaddr_storage address<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">memset</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>address, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>address<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span>message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>atyp_<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span> <span style="color: #008080;">:</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// IP V4 address: X'01'</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>buffer.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">offsetof</span><span style="color: #008000;">&#40;</span>SocksRequest, dst_addr_<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">6</span> <span style="color: #ff0000; font-style: italic;">/* four for the address, two for the port */</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">throw</span> InsufficientData<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Not enough data for a SOCKS request&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span>
		<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* all is well so far */</span> <span style="color: #008000;">&#125;</span>
		address.<span style="color: #007788;">ss_family</span> <span style="color: #000080;">=</span> AF_INET<span style="color: #008080;">;</span>
		sockaddr_in <span style="color: #000040;">*</span>a4<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span> sockaddr_in<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>address<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>a4<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sin_addr, message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dst_addr_, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>a4<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sin_port, message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dst_addr_ <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div><br />
or it can be a domain name, in which case the address has to be looked up.</p>
<p>Address lookup is done synchronously, using <code>gethostbyname</code>, but should eventually be done asynchronously as calling this function may take some time &#8212; time we don&#8217;t necessarily want to spend waiting.</p>
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">	<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">3</span> <span style="color: #008080;">:</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// DOMAINNAME: X'03'</span>
		<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> hostname_length<span style="color: #008000;">&#40;</span>message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dst_addr_<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>buffer.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">offsetof</span><span style="color: #008000;">&#40;</span>SocksRequest, dst_addr_<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> hostname_length <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span> <span style="color: #ff0000; font-style: italic;">/* hostname_length for the address, two for the port */</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">throw</span> InsufficientData<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Not enough data for a SOCKS request&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span>
		<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* all is well so far */</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>hostname_begin<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dst_addr_ <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>hostname_end <span style="color: #000080;">=</span> hostname_begin <span style="color: #000040;">+</span> hostname_length<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">short</span> port<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">short</span><span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>hostname_end<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #000040;">*</span>hostname_end <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #666666;">// cap it off</span>
		hostent <span style="color: #000040;">*</span>host_entry<span style="color: #008000;">&#40;</span>gethostbyname<span style="color: #008000;">&#40;</span>hostname_begin<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>host_entry<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">throw</span> NameResolutionError<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Name resolution error&quot;</span>, GetLastError<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span>
		<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* resolved OK */</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span>host_entry<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>h_addrtype<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">case</span> AF_INET <span style="color: #008080;">:</span>
		<span style="color: #008000;">&#123;</span>
			sockaddr_in <span style="color: #000040;">*</span>a4<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span> sockaddr_in<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>address<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>a4<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sin_addr, host_entry<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>h_addr_list<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			a4<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sin_port <span style="color: #000080;">=</span> port<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">case</span> AF_INET6 <span style="color: #008080;">:</span>
		<span style="color: #008000;">&#123;</span>
			sockaddr_in6 <span style="color: #000040;">*</span>a6<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span> sockaddr_in6<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>address<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>a6<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sin6_addr, host_entry<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>h_addr_list<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000dd;">16</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			a6<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sin6_port <span style="color: #000080;">=</span> port<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>
<p>The address can also be an IPv6, in which case, like in the case of an IPv4 address, it is simply copied.</p>
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">	<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">4</span> <span style="color: #008080;">:</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// IP V6 address: X'04'</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>buffer.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">offsetof</span><span style="color: #008000;">&#40;</span>SocksRequest, dst_addr_<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">16</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span> <span style="color: #ff0000; font-style: italic;">/* four for the address, two for the port */</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">throw</span> InsufficientData<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Not enough data for a SOCKS request&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span>
		<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* all is well so far */</span> <span style="color: #008000;">&#125;</span>
		address.<span style="color: #007788;">ss_family</span> <span style="color: #000080;">=</span> AF_INET6<span style="color: #008080;">;</span>
		sockaddr_in6 <span style="color: #000040;">*</span>a6<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span> sockaddr_in6<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>address<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>a6<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sin6_addr, message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dst_addr_, <span style="color: #0000dd;">16</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>a6<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sin6_port, message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dst_addr_ <span style="color: #000040;">+</span> <span style="color: #0000dd;">16</span>, <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">default</span> <span style="color: #008080;">:</span>
		<span style="color: #0000ff;">throw</span> UnknownAddressType<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Unknown address type&quot;</span>, message<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>atyp_<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>
<p>There are a few things you should notice: invalid IP addresses and domain names are not necessarily a problem &#8212; they won&#8217;t work, of course, but there is really no other way to check for them than to try them out. The only thing we really check is that there&#8217;s enough data in the request to contain the information being extracted from the request.</p>
<p>Socks requests are fairly simple and don&#8217;t contain anything like checksums or cryptographic authentication (although cryptographic authentication can be part of the protocol, if supported by both sides) so other than these few checks, there is really nothing to be done.</p>
<p>Queueing data to send looks like this:<br />
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Application<span style="color: #008080;">::</span><span style="color: #007788;">queueDataToSend</span><span style="color: #008000;">&#40;</span>Socket <span style="color: #000040;">&amp;</span>socket, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>begin, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>end<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
	Buffer <span style="color: #000040;">*</span>buffer<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>send_buffer_attribute_id_<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		buffer <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span>any_cast<span style="color: #000080;">&lt;</span> Buffer<span style="color: #000040;">&amp;</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>send_buffer_attribute_id_<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #008000;">&#123;</span>
		socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>send_buffer_attribute_id_<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> Buffer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		buffer <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span>any_cast<span style="color: #000080;">&lt;</span> Buffer<span style="color: #000040;">&amp;</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>send_buffer_attribute_id_<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	copy<span style="color: #008000;">&#40;</span>begin, end, back_inserter<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>buffer<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> data<span style="color: #008000;">&#40;</span>buffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>size<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	server_<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>write<span style="color: #008000;">&#40;</span>socket, <span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>buffer<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>, <span style="color: #000040;">&amp;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	buffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>erase<span style="color: #008000;">&#40;</span>buffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>begin<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, buffer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>begin<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div></p>
<p>First thing we do is get a buffer to put the data into. That buffer is associated with the socket itself so that, if the socket isn&#8217;t ready for data being written to it, we can write the data to the socket when the socket <em>is</em> ready, in <code>onWriteReady</code>. The <code>Server</code> code will know what to do with a call to <code>write</code> if the socket isn&#8217;t ready, so we can simply call it and remove, from the buffer, any data that we could send.</p>
<p>The <code>onWriteReady</code> method is similar, of course:<br />
<div class="aside-toggler open"><span class="open-aside code">To see the <em>code</em> click here.</span><span class="close-aside code">To hide the <em>code</em> click here.</span>
                         </div><div class="bnsia aside code open"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*virtual */</span><span style="color: #0000ff;">void</span> Application<span style="color: #008080;">::</span><span style="color: #007788;">onWriteReady</span><span style="color: #008000;">&#40;</span>Socket <span style="color: #000040;">&amp;</span>socket<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>send_buffer_attribute_id_<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		Buffer <span style="color: #000040;">&amp;</span>buffer<span style="color: #008000;">&#40;</span>any_cast<span style="color: #000080;">&lt;</span> Buffer<span style="color: #000040;">&amp;</span> <span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>socket.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span>send_buffer_attribute_id_<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Buffer<span style="color: #008080;">::</span><span style="color: #007788;">size_type</span> offset<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>buffer.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Buffer<span style="color: #008080;">::</span><span style="color: #007788;">size_type</span> avail<span style="color: #008000;">&#40;</span>buffer.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			Buffer<span style="color: #008080;">::</span><span style="color: #007788;">pointer</span> send_ptr<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span>buffer<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			server_<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>write<span style="color: #008000;">&#40;</span>socket, send_ptr, <span style="color: #000040;">&amp;</span>avail<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			buffer.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>buffer.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, buffer.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> avail<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span>
		<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* nothing to send */</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* nothing to send */</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div></p>
<p>As you can see, it simply retrieves the buffer if there is one and sends as much of the available data as possible.</p>
<p>In the next few installments, we will look at the following:</p>
<ol>
<li><strong>state machines</strong>: as mentioned above, every socket will be construed as a state machine &#8212; but we will look at state machines in other contexts as well.</li>
<li><strong>threads</strong>: as mentioned above, some actions (such as DNS queries) will need to be performed asynchronously. We will do that by creating a thread to handle those requests.</li>
<li><strong>the Command pattern</strong> which we&#8217;ll be using to communicate with the thread</li>
</ol>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/JP8ENgXbaXo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2011/12/setting-up-a-new-skeleton-re-factoring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://vlinder.ca/podcasts/35-refactored.mp3" length="12669768" type="audio/mpeg" />
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2011/12/setting-up-a-new-skeleton-re-factoring/</feedburner:origLink></item>
		<item>
		<title>Sleep(…)</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/FcEanvHm7Vw/</link>
		<comments>http://rlc.vlinder.ca/blog/2011/12/sleep/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 20:11:11 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[C++ for the self-taught]]></category>
		<category><![CDATA[This blog]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/blog/2011/12/sleep/</guid>
		<description><![CDATA[For those of you waiting for the next installment of &#8220;C++ for the self-taught&#8221;: I&#8217;m on parental leave at the moment. The podcast (and the rest of the blog) will be back in a few weeks.]]></description>
			<content:encoded><![CDATA[
<p>For those of you waiting for the next installment of &#8220;C++ for the self-taught&#8221;: I&#8217;m on parental leave at the moment. The podcast (and the rest of the blog) will be back in a few weeks.</p>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/FcEanvHm7Vw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2011/12/sleep/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2011/12/sleep/</feedburner:origLink></item>
		<item>
		<title>Radix Sort</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/S5VWZAgmX-Y/</link>
		<comments>http://rlc.vlinder.ca/blog/2011/11/radix-sort/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 00:25:20 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[Interesting stuff]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/blog/2011/11/radix-sort/</guid>
		<description><![CDATA[The Radix Sort algorithm is a stable sorting algorithm that allows you to sort a series of numerical values in linear time. What amazed me, however, is that it is also a natural approach to sorting: this is a picture &#8230; <a href="http://rlc.vlinder.ca/blog/2011/11/radix-sort/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p><img style="display:block;margin-right:auto;margin-left:auto;" alt="image" src="http://rlc.vlinder.ca/wp-content/uploads/2011/11/wpid-IMG_20111107_191330.jpg" /><br />
The Radix Sort algorithm is a stable sorting algorithm that allows you to sort a series of numerical values in linear time. What amazed me, however, is that it is also a natural approach to sorting: this is a picture of my daughter applying a radix sort to her homework (without knowing it&#8217;s a radix sort, of course, but after explaining the algorithm perfectly)!<br />
<span id="more-1714"></span><br />
Radix sort is actually non-trivial to implement correctly, but apparently trivial enough to understand for an eight-year-old to describe correctly and implement on a piece of paper.</p>
<p>The radix sort she employed was a most-significant-digit (MSD) radix sort, in which she sorted integers between 1 and 999, inclusive. Wikipedia has a <a href="http://en.wikipedia.org/w/index.php?title=Radix_sort&#038;oldid=449846096#Recursive_forward_radix_sort_example" title="Wikipedia on Radix Sort" target="_blank">very good explanation</a> of the method she used (and she doesn&#8217;t know Wikipedia nor does she read english).</p>
<p>The amazing part isn&#8217;t that an eight-year-old can provide an accurate description, but that an algorithm that is so simple to describe (notwithstanding the brilliance of my daughter, of course) but that a correct implementation is so hard (see <a href="http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort" title="Radix Sort sorting algorithm at Rosetta Code" target="_blank">RosettaCode</a>).</p>
<p>That just goes to show that, when an algorithm is easy to explain, that does not mean it&#8217;s easy to implement &#8212; just like when you have an elegant piece of code, that doesn&#8217;t make the algorithm easy to explain, per se.</p>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/S5VWZAgmX-Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2011/11/radix-sort/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2011/11/radix-sort/</feedburner:origLink></item>
		<item>
		<title>The underestimated legacy of Dennis Ritchie</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/AkqyfxkFft8/</link>
		<comments>http://rlc.vlinder.ca/blog/2011/10/the-underestimated-legacy-of-dennis-ritchie/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 00:50:21 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[C & C++]]></category>
		<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/blog/2011/10/the-underestimated-legacy-of-dennis-ritchie/</guid>
		<description><![CDATA[Dennis Ritchie is the inventor of the C programming language, which is the ancestor of a whole family of programming languages that includes C++, Java and C# &#8212; probably the three most popular programming languages today &#8212; as well as &#8230; <a href="http://rlc.vlinder.ca/blog/2011/10/the-underestimated-legacy-of-dennis-ritchie/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>Dennis Ritchie is the inventor of the C programming language, which is the ancestor of a whole family of programming languages that includes C++, Java and C# &#8212; probably the three most popular programming languages today &#8212; as well as D and Objective-C, which are less popular but significant nonetheless.</p>
<p>Ritchie is also one of the authors of the early UNIX kernel, which was the first significant program written in C and for which C was originally designed, and which is the ancestor of a whole family of operating systems that includes Linux, MacOS X, iOS, BSD and many others.</p>
<p>C was the first programming language that allowed the programmer to structure data and code, making it relatively easy to handle very large quantities of data while also maintaining full control of how the hardware is used. Most operating systems today, including significant parts of Windows, are written in C &#8212; and most OS designs are at least partly based on UNIX.</p>
<p>Everywhere you look, you can see the fruits of Dennis Ritchie&#8217;s labor &#8212; and by his fruits you shall know the man &#8212; but it seems <em>this</em> man is known only to those of us who either have an intimate knowledge of C and/or UNIX, or are more-than-usually interested in programming language design.</p>
<p>That is a real shame: I think there is a lot we can learn from his legacy &#8212; and a lot to be gained from continuing his work.</p>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/AkqyfxkFft8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2011/10/the-underestimated-legacy-of-dennis-ritchie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2011/10/the-underestimated-legacy-of-dennis-ritchie/</feedburner:origLink></item>
		<item>
		<title>Making the enabling of online copyright infringement itself an infringement of copyright</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/1BAmB87h3F4/</link>
		<comments>http://rlc.vlinder.ca/blog/2011/10/making-the-enabling-of-online-copyright-infringement-itself-an-infringement-of-copyright/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 03:15:33 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/?p=1697</guid>
		<description><![CDATA[Bill C-11 amends the Copyright Act in several different ways. One of the states purposes of those amendments is to &#8220;make the enabling of online copyright infringement itself an infringement of copyright&#8221;. While I can understand that this adds significant &#8230; <a href="http://rlc.vlinder.ca/blog/2011/10/making-the-enabling-of-online-copyright-infringement-itself-an-infringement-of-copyright/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p><a href="http://www.parl.gc.ca/HousePublications/Publication.aspx?Docid=5144516&#038;file=4" title="Bill C-11" target="_blank">Bill C-11</a> amends the Copyright Act in several different ways. One of the states purposes of those amendments is to &#8220;make the enabling of online copyright infringement itself an infringement of copyright&#8221;. While I can understand that this adds significant new protections to copyrighted materials, I think this may quickly become either unenforceable, or introduce serious new restrictions on how communications over the Internet can legally take place. It all hinges on the definition of &#8220;enabling&#8221;, however.<br />
<span id="more-1697"></span><br />
Enabling copyright infringement under bill C-11 is enabling an infringement of the right as defined in subsection 3(1), as amended.</p>
<p><div class="aside-toggler closed"><span class="open-aside subsection-3(1)-as-amended">To show Subsection 3(1) as amended, click here</span><span class="close-aside subsection-3(1)-as-amended">To hide Subsection 3(1) as amended, click here</span>
                         </div><div class="bnsia aside subsection-3(1)-as-amended closed">Subsection 3(1), as amended, reads:<br />
<blockquote>3. (1) For the purposes of this Act, “copyright”, in relation to a work, means the sole right to produce or reproduce the work or any substantial part thereof in any material form whatever, to perform the work or any substantial part thereof in public or, if the work is unpublished, to publish the work or any substantial part thereof, and includes the sole right<br />
(a) to produce, reproduce, perform or publish any translation of the work,<br />
(b) in the case of a dramatic work, to convert it into a novel or other non-dramatic work,<br />
(c) in the case of a novel or other non-dramatic work, or of an artistic work, to convert it into a dramatic work, by way of performance in public or otherwise,<br />
(d) in the case of a literary, dramatic or musical work, to make any sound recording, cinematograph film or other contrivance by means of which the work may be mechanically reproduced or performed,<br />
(e) in the case of any literary, dramatic, musical or artistic work, to reproduce, adapt and publicly present the work as a cinematographic work,<br />
(f) in the case of any literary, dramatic, musical or artistic work, to communicate the work to the public by telecommunication,<br />
(g) to present at a public exhibition, for a purpose other than sale or hire, an artistic work created after June 7, 1988, other than a map, chart or plan,<br />
(h) in the case of a computer program that can be reproduced in the ordinary course of its use, other than by a reproduction during its execution in conjunction with a machine, device or computer, to rent out the computer program,<br />
(i) in the case of a musical work, to rent out a sound recording in which the work is embodied, and<br />
(j) in the case of a work that is in the form of a tangible object, to sell or otherwise transfer ownership of the tangible object, as long as that ownership has never previously been transferred in or outside Canada with the authorization of the copyright owner,</p>
<p>and to authorize any such acts</p></blockquote>
<p></div><br />
Copyright in performer&#8217;s performances, sound recordings and communication signals is defined  in section 15 of the act (as amended).</p>
<p><div class="aside-toggler closed"><span class="open-aside subsection-15(1)-as-amended">To show Subsection 15(1) as amended, click here</span><span class="close-aside subsection-15(1)-as-amended">To hide Subsection 15(1) as amended, click here</span>
                         </div><div class="bnsia aside subsection-15(1)-as-amended closed"><br />
<blockquote>15. (1) Subject to subsection (2), a performer has a copyright in the performer’s performance, consisting of the sole right to do the following in relation to the performer’s performance or any substantial part thereof:<br />
(a) if it is not fixed,<br />
(i) to communicate it to the public by telecommunication,<br />
(ii) to perform it in public, where it is communicated to the public by telecommunication otherwise than by communication signal, and<br />
(iii) to fix it in any material form,<br />
(b) if it is fixed,<br />
(i) to reproduce any fixation that was made without the performer’s authorization,<br />
(ii) where the performer authorized a fixation, to reproduce any reproduction of that fixation, if the reproduction being reproduced was made for a purpose other than that for which the performer’s authorization was given, and<br />
(iii) where a fixation was permitted under Part III or VIII, to reproduce any reproduction of that fixation, if the reproduction being reproduced was made for a purpose other than one permitted under Part III or VIII, and<br />
(c) to rent out a sound recording of it,<br />
and to authorize any such acts.</p>
<p>(1.1) Subject to subsections (2.1) and (2.2), a performer’s copyright in the performer’s performance consists of the sole right to do the following acts in relation to the performer’s performance or any substantial part of it and to authorize any of those acts:<br />
(a) if it is not fixed,<br />
(i) to communicate it to the public by telecommunication,<br />
(ii) to perform it in public, if it is communicated to the public by telecommunication otherwise than by communication signal, and<br />
(iii) to fix it in any material form;<br />
(b) if it is fixed in a sound recording, to reproduce that fixation;<br />
(c) to rent out a sound recording of it;<br />
(d) to make a sound recording of it available to the public by telecommunication in a way that allows a member of the public to have access to the sound recording from a place and at a time individually chosen by that member of the public and to communicate the sound recording to the public by telecommunication in that way; and<br />
(e) if it is fixed in a sound recording that is in the form of a tangible object, to sell or otherwise transfer ownership of the tangible object, as long as that ownership has never previously been transferred in or outside Canada with the authorization of the owner of the copyright in the performer’s performance.</p></blockquote>
<p></div><br />
Therefore, to infringe or to enable infringement of copyright, one has to infringe or perform or enable some-one to perform any of the acts that only the copyright holder may do or authorize, <em>without the copyright holder&#8217;s persmission</em>.</p>
<p>Enabling infringement is new to the copyright act and is defined in section 27, subsections 2.3 and 2.4, as amended <div class="aside-toggler closed"><span class="open-aside section-27,-subsections-2.3-and-2.4,-as-amended">To show Section 27, subsections 2.3 and 2.4, as amended, click here</span><span class="close-aside section-27,-subsections-2.3-and-2.4,-as-amended">To hide Section 27, subsections 2.3 and 2.4, as amended, click here</span>
                         </div><div class="bnsia aside section-27,-subsections-2.3-and-2.4,-as-amended closed"><br />
<blockquote>(2.3) It is an infringement of copyright for a person to provide, by means of the Internet or another digital network, a service that the person knows or should have known is designed primarily to enable acts of copyright infringement if an actual infringement of copyright occurs by means of the Internet or another digital network as a result of the use of that service.<br />
(2.4) In determining whether a person has infringed copyright under subsection (2.3), the court may consider<br />
(a) whether the person expressly or implicitly marketed or promoted the service as one that could be used to enable acts of copyright infringement;<br />
(b) whether the person had knowledge that the service was used to enable a significant number of acts of copyright infringement;<br />
(c) whether the service has significant uses other than to enable acts of copyright infringement;<br />
(d) the person’s ability, as part of providing the service, to limit acts of copyright infringement, and any action taken by the person to do so;<br />
(e) any benefits the person received as a result of enabling the acts of copyright infringement; and<br />
(f) the economic viability of the provision of the service if it were not used to enable acts of copyright infringement.</p></blockquote>
<p></div></p>
<p>So the service provided has to be designed with copyright infringement as its primary purpose, and the test for whether that is the primary purpose of the service in question includes the way the service is marketed, the significant uses of the service and the economic viability of the service if it is not used for copyright infringement.</p>
<p>Let&#8217;s take a look at an example: BitTorrent. The primary use of BitTorrent is to share files &#8212; regardless of the type of files. While it is not expressly marketed as a service for copyright infringement, it is implicitly marketed as such, as most torrent sites provide access to copyrighted materials for their illegal distribution. Anyone who has any knowledge whatsoever of BitTorrent knows it is used for the purpose of copyright infringement. Websites such as the Pirate Bay, TorLock and others that provide access to torrent files that are used for the purposes of illegal distribution of copyrighted material can therefore be construed as enabling copyright infringement &#8212; and would likely not be economically viable if they didn&#8217;t do so.</p>
<p>Under Bill C-11. these sites would therefore be construed as infringing copyright.</p>
<p>That does not mean that anything that has anything to do with BitTorrent inherently infringes copyright: if you are a copyright holder and choose to do so, you can license your works such that their distribution using BitTorrent is authorized and you can therefore use BitTorrent (or any other peer-to-peer network) for the (legal) distribution of your work. Services that provide access to works that are licensed to allow such distribution, and/or to works that are in the public domain, would not infringe on any copyright.</p>
<p>I myself use BitTorrent to download (and redistribute) the Eclipse SDK and other Free/Libre Open Source Software. As such, I provide a service (because my BitTorrent client, like most other clients, allows downloading such copyrighted materials from my computer) that does not infringe on copyright nor enable copyright infringement (because the copyrighted materials I provide access to are licensed to allow redistribution).</p>
<p>But how is a middle-man &#8212; someone who provides a service to the infringing service provider and thus enabling the infringement &#8212; to know whether he&#8217;s enabling infringement and thus infringing?</p>
<p>I.e., a high-speed Internet access provider could very well be enabling infringement according to the same standards: high-speed Internet access for non-commercial use is often used for downloading movies and thus, infringing copyright. This is a well-known use for high-speed Internet access and arguably makes relatively-low-cost high-speed Internet access economically viable. If all the users that use their high-speed Internet connection for infringing copyright stopped infringing copyright, would they still need their high-speed connections? If not, wouldn&#8217;t they drop their high-speed subscriptions for lower-speed ones, thus making high-speed connections economically not viable?</p>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/1BAmB87h3F4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2011/10/making-the-enabling-of-online-copyright-infringement-itself-an-infringement-of-copyright/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2011/10/making-the-enabling-of-online-copyright-infringement-itself-an-infringement-of-copyright/</feedburner:origLink></item>
		<item>
		<title>Harper government reintroduces toughened online copyright law</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/1ss_3GYClWI/</link>
		<comments>http://rlc.vlinder.ca/blog/2011/09/harper-government-reintroduces-toughened-online-copyright-law/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 22:37:45 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[Opinions]]></category>
		<category><![CDATA[C32]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/blog/2011/09/harper-government-reintroduces-toughened-online-copyright-law/</guid>
		<description><![CDATA[In the Vancouver Sun: bill C-32 from last session has been re-introduced (probably with some modification &#8212; I haven&#8217;t had a chance to read the bill yet) and is far more likely to pass, now that there&#8217;s a conservative majority &#8230; <a href="http://rlc.vlinder.ca/blog/2011/09/harper-government-reintroduces-toughened-online-copyright-law/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>In the <span class="removed_link" title="http://www.vancouversun.com/news/Harper+government+reintroduces+toughened+online+copyright/5477250/story.html">Vancouver Sun</span>: bill C-32 from last session has been re-introduced (probably with some modification &#8212; I haven&#8217;t had a chance to read the bill yet) and is far more likely to pass, now that there&#8217;s a conservative majority in Parliament.</p>
<p><strong>Update Oct 8, 2008:</strong> the re-introduced Copyright Modernization Act is numbered C-11, and is available <a href="http://www.parl.gc.ca/LegisInfo/BillDetails.aspx?Language=E&#038;Mode=1&#038;billId=5134851" title="Copyright Modernization Act" target="_blank">here</a>.</p>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/1ss_3GYClWI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2011/09/harper-government-reintroduces-toughened-online-copyright-law/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2011/09/harper-government-reintroduces-toughened-online-copyright-law/</feedburner:origLink></item>
		<item>
		<title>Autumn is here – and so is the autumn banner</title>
		<link>http://feedproxy.google.com/~r/making-life-easier/~3/_9qfLj4yDY8/</link>
		<comments>http://rlc.vlinder.ca/blog/2011/09/autumn-is-here-and-so-is-the-autumn-banner/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 16:59:08 +0000</pubDate>
		<dc:creator>rlc</dc:creator>
				<category><![CDATA[This blog]]></category>

		<guid isPermaLink="false">http://rlc.vlinder.ca/?p=1689</guid>
		<description><![CDATA[OK, autumn has been here for about a week already, and the banner was ready two months ago, but I only now had both the time and the inclination to put it up&#8230; You might remember that the corresponding desktop &#8230; <a href="http://rlc.vlinder.ca/blog/2011/09/autumn-is-here-and-so-is-the-autumn-banner/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>OK, autumn has been here for about a week already, and the banner was ready two months ago, but I only now had both the time and the inclination to put it up&#8230;</p>
<p>You might remember that the corresponding desktop wallpapers are in the <a href="http://rlc.vlinder.ca/blog/2011/07/happy-canada-day/" title="Happy Canada Day">Canada Day post</a>.</p>

<img src="http://feeds.feedburner.com/~r/making-life-easier/~4/_9qfLj4yDY8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rlc.vlinder.ca/blog/2011/09/autumn-is-here-and-so-is-the-autumn-banner/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://rlc.vlinder.ca/blog/2011/09/autumn-is-here-and-so-is-the-autumn-banner/</feedburner:origLink></item>
	</channel>
</rss>

