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

<channel>
	<title>Microbits &#187; linq</title>
	<atom:link href="http://microbits.info/index.php/tag/linq/feed" rel="self" type="application/rss+xml" />
	<link>http://microbits.info</link>
	<description>Random thoughts about programming, gaming, and the world.</description>
	<lastBuildDate>Mon, 31 Oct 2011 04:43:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>TCP server security &#8211; LINQ and C#</title>
		<link>http://microbits.info/index.php/2010/01/tcp-server-security-linq-and-c</link>
		<comments>http://microbits.info/index.php/2010/01/tcp-server-security-linq-and-c#comments</comments>
		<pubDate>Thu, 21 Jan 2010 21:59:01 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=449</guid>
		<description><![CDATA[I&#8217;m currently working on a TCP client/server system, and I wanted to prevent clients from DoSing the server. One way to do that is by restricting clients to a certain number of connections in a given time period. Since the code is designed to prevent a DoS condition, it must be extremely fast and efficient [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on a TCP client/server system, and I wanted to prevent clients from DoSing the server.</p>
<p>One way to do that is by restricting clients to a certain number of connections in a given time period. Since the code is designed to prevent a DoS condition, it must be extremely fast and efficient because it may be run thousands of times per minute (during a DoS attack for example).</p>
<p>I designed the connection limit system around a collection of connection records, a prune timer, and a test when the server accepts a client connection.</p>
<p>Since I needed to check two variables (timestamp and ip) in each connection record to determine the connection count for a given IP address in a given time period, I decided to use LINQ, which worked nicely:</p>
<pre class="brush:c-sharp">// Get this client's connection count
int connCount = (from conn in _connectionList
                 where conn.Ip == clientIp &amp;&amp;
                 conn.Timestamp &gt;
                   DateTime.Now.AddMinutes(-howFarBackInMinutesToCheck)
                 select conn.Ip).Count();
</pre>
<p>The other query I needed was for pruning the connection list:</p>
<pre class="brush:c-sharp"> // Get the connections to prune
var connQuery = from conn in _connections
                where conn.Timestamp &lt;
                  DateTime.Now.AddMinutes(-howFarBackInMinutesToCheck)
                select conn;
</pre>
<p>After checking the number of records to prune was greater than zero (connQuery.Count()), I pruned them by iterating through the query (LINQ queries implement IEnumerable).</p>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2010/01/tcp-server-security-linq-and-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.694 seconds -->

