<?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>Th30z - Coding on the Fly &#187; Unix C</title>
	<atom:link href="http://th30z.netsons.org/category/unix-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://th30z.netsons.org</link>
	<description>Matteo Bertozzi, Objective-C, Cocoa, C, C++, Qt4, iPhone, Mac OS X, Open Moko, Matteo Bertozzi Development</description>
	<lastBuildDate>Sun, 22 Nov 2009 09:47:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>File-System: Delayed Allocation, fsync() solution</title>
		<link>http://th30z.netsons.org/2009/09/file-system-delayed-allocation-fsync-solution/</link>
		<comments>http://th30z.netsons.org/2009/09/file-system-delayed-allocation-fsync-solution/#comments</comments>
		<pubDate>Sat, 19 Sep 2009 09:51:07 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Unix C]]></category>
		<category><![CDATA[File-System]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=1229</guid>
		<description><![CDATA[Last week on LWN Valerie Aurora as posted a great article (as always) POSIX v. reality: A position on O_PONIES. http://lwn.net/Articles/351422/.
fsync() is often more expensive than it absolutely needs to be.  The easiest way to implement it is to force out every outstanding write to the file system, regardless of whether it is a [...]]]></description>
			<content:encoded><![CDATA[<p>Last week on LWN <a title="Valerie Aurora" href="http://valerieaurora.org/">Valerie Aurora</a> as posted a great article (as always) POSIX v. reality: A position on O_PONIES. <a title="POSIX v. reality: A position on O_PONIES" href="http://lwn.net/Articles/351422/">http://lwn.net/Articles/351422/</a>.</p>
<p><em><code>fsync()</code> is often more expensive than it absolutely needs to be.  The easiest way to implement <code>it</code> is to force out every outstanding write to the file system, regardless of whether it is a journaling file system, a COW file system, or a file system with no crash recovery mechanism whatsoever.  This is because it is very difficult to map backward from a given file to the dirty file system blocks needing to be written to disk in order to create a consistent file system containing those changes.  For example, the block containing the bitmap for newly allocated file data blocks may also have been changed by a later allocation for a different file, which then requires that we also write out the indirect blocks pointing to the data for that second file, which changes another bitmap block&#8230; When you solve the problem of tracing specific dependencies of any particular write, you end up with the complexity of <a href="http://lwn.net/Articles/339337/">soft updates</a>.  No surprise then, that most file systems take the brute force approach, with the result that <code>fsync()</code> commonly takes time proportional to all outstanding writes to the file system.</em></p>
<p>Thinking for a while&#8230; Is not hard to implement fsync(), to flush just one file using <a title="Delayed Allocation" href="http://en.wikipedia.org/wiki/Allocate-on-flush">Delayed Allocation</a> (Allocate on Flush). We&#8217;ve all new data in memory, and old data stay on its block. So, modification in place means that you&#8217;ve just to flush blocks. Append means that you need to allocate something.<br />
<a href="http://th30z.netsons.org/wp-content/uploads/RaleighFs-InMem.png"><img class="aligncenter size-full wp-image-1232" title="RaleighFS in Memory Structure" src="http://th30z.netsons.org/wp-content/uploads/RaleighFs-InMem.png" alt="RaleighFS in Memory Structure" width="581" height="435" /></a>The image above, is a little bit old, but it&#8217;s the original idea of the RaleighFS in Memory Structure. There&#8217;re general information like super-block, bad blocks list and free blocks lis, current cache size and some other things. But today I&#8217;m focusing on <strong>Block Cache</strong> and <strong>Write Items Cache</strong>.</p>
<p>When you open a file, you load its metadata in memory then when you need file content you load it in the Block Cache. RaleighFS data block contains the File Key, so you can easily find blocks with specified key, but also you can easily find your file blocks using pointers.</p>
<p>So, why is easy to fsync() only the specified file with Delayed Allocation:</p>
<ul>
<li>Modification in place, requires just a scan of the block cache to find what blocks are to flush (and obviously metadata)</li>
<li>Append to file, has all new data in memory and there&#8217;re no Modification to B*Tree(s) or Free blocks until you flush something.</li>
<li>Remove is just a command, and when fsync() is called all the delete operation on B*Tree(s) and list will take places.</li>
</ul>
<p>But remember, syncing just one file is not a good idea, Trust your File-System&#8217;s flush policy!</p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/09/file-system-delayed-allocation-fsync-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File-System and Data Block Back Reference</title>
		<link>http://th30z.netsons.org/2009/09/file-system-and-data-block-back-reference/</link>
		<comments>http://th30z.netsons.org/2009/09/file-system-and-data-block-back-reference/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 04:50:45 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[File-System]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=1221</guid>
		<description><![CDATA[While I&#8217;m thinking and waiting for suggestions on how to improve my file-system block cache algorithm, I&#8217;ve decided to apply some changes to the Raleigh File-System Format (source code is not published yet).
Following the ideas of Valerie Aurora of Repair-driven File System Design, I&#8217;ve decided to add for each block (B*Tree and Data blocks) an [...]]]></description>
			<content:encoded><![CDATA[<p>While I&#8217;m thinking and waiting for suggestions on how to improve my file-system <a title="Block Cache Algorithm" href="http://th30z.netsons.org/2009/08/block-cache-algorithm/">block cache algorithm</a>, I&#8217;ve decided to apply some changes to the Raleigh File-System Format (<em>source code is not published yet</em>).</p>
<p>Following the ideas of <a title="Valerie Aurora" href="http://valerieaurora.org/">Valerie Aurora</a> of <a href="review/repair.pdf">Repair-driven File System Design</a>, I&#8217;ve decided to add for each block (B*Tree and Data blocks) an head that contains a <strong>Magic Number</strong> and a <strong>CRC Sum</strong> of the block. In this way you can easily identify what kind of block you&#8217;ve peeked without scanning all metadata. Another step is to add a back reference (or back pointer) to the data block, in this way you can easily jump back to it&#8217;s the extent block (and obviously to its OID) so you can easily understand what is the Object owner of this block and you can easily swap two blocks reading at most 4 blocks (2 Data and 2 Extends).</p>
<p>Another idea stolen from Valerie is to <strong>double the metadata blocks</strong> with a COW-like approach, as explained in this paper &#8220;<a title="Double the Metadata Double the Fun A COW-like Approach to File System Consistency" href="http://valerieaurora.org/review/doublefs.pdf">Double the Metadata, Double the Fun: A COW-like Approach to FS Consistency</a>&#8220;, really useful for personal file-systems but maybe less in a distributed file-system. I&#8217;m working on it adding only as an mkfs option.</p>
<p><em>When the source Code will be online? I don&#8217;t know.. I&#8217;ve less time to work on it. Maybe at the end of this year I&#8217;ll publish the File-System and the Distributed System (explained some posts ago).</em></p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 47px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Double the Metadata, Double the Fun: A COW-like Approach to File</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 47px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">System Consistency<span style="background-color: #ffffff;">&#8220;</span></div>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/09/file-system-and-data-block-back-reference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Block Cache Algorithm</title>
		<link>http://th30z.netsons.org/2009/08/block-cache-algorithm/</link>
		<comments>http://th30z.netsons.org/2009/08/block-cache-algorithm/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 18:44:37 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[File-System]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=1163</guid>
		<description><![CDATA[I need to replace my old filesystem cache algorithm with something more new and efficient. The old one is based on LRU/LFU algorithm. There&#8217;s a queue of cached blocks and an Hashtable to speedup block lookup.


struct blkcache_buf {
    struct blkcache_buf *  next;    /* Next Queue Item */
  [...]]]></description>
			<content:encoded><![CDATA[<p>I need to replace my old filesystem cache algorithm with something more new and efficient. The old one is based on LRU/LFU algorithm. <em>There&#8217;s a queue of cached blocks and an Hashtable to speedup block lookup.</em><br />
<code></p>
<pre>
struct blkcache_buf {
    struct blkcache_buf *  next;    /* Next Queue Item */
    struct blkcache_buf *  prev;    /* Prev Queue Item */
    struct blkcache_buf *  hash;    /* Next Item with the same hash */

    xuint16_t              count;   /* Retain count */
    xxx_block_t            block;   /* Cached Block */
};

typedef struct {
    struct blkcache_buf ** buf_hash;        /* Bufs Hashtable */
    xuint16_t              buf_hash_size;   /* Bufs Hashtable Size */
    xuint16_t              buf_used;        /* Bufs in use */

    struct blkcache_buf *  head;            /* Head of the Bufs Queue */
    struct blkcache_buf *  tail;            /* Tail of the Bufs Queue */

    xxx_device_t *         device;          /* Block Device used for I/O */
} xxx_blkcache_t;
</pre>
<p></code><br />
Above, you can see the cache data structure and below the core of the cache Algorithm.<br />
<code></p>
<pre>
#define BUFHASH(cache, blocknr)     ((blocknr) % (cache)-&gt;buf_hash_size)

xxx_block_t *xxx_blkcache_read (xxx_blkcache_t *cache,
                                xxx_blkptr_t blocknr)
{
    struct blkcache_buf *buf;
    xuint16_t hash_index;

    /* Scan the hash chain for block */
    hash_index = BUFHASH(cache, blocknr);
    if ((buf = __blkcache_find(cache, blocknr, hash_index)) != NULL) {
        buf-&gt;count++;

        /* Move Buf far from head */
        __blkcache_buf_shift(cache, buf);

        return(&amp;(buf-&gt;block));
    }

    /* Cache is Full, Remove one Item */
    if ((cache-&gt;buf_used + 1) &gt; cache-&gt;buf_hash_size) {
        /* All buffers are in use */
        if (cache-&gt;head-&gt;count &gt; 0)
            return(NULL);

        /* Remove Least-Frequently Used */
        buf = __blkcache_remove_lfu(cache);
        cache-&gt;buf_used--;
    }

    /* Desidered block is not on available chain, Read It! */
    if ((buf = __blkcache_buf_alloc(cache, buf, blocknr)) == NULL)
        return(NULL);

    /* Add One Use, Block cannot be removed */
    buf-&gt;count++;

    /* Go get the requested block unless searching or prefetching. */
    __blkcache_readwrite(cache, buf, RFALSE);

    /* Update Cache Hash */
    cache-&gt;buf_used++;
    buf-&gt;hash = cache-&gt;buf_hash[hash_index];
    cache-&gt;buf_hash[hash_index] = buf;

    /* Update Cache Queue */
    if (cache-&gt;head == NULL) {
        cache-&gt;head = cache-&gt;tail = buf;
    } else {
        buf-&gt;prev = cache-&gt;tail;
        cache-&gt;tail-&gt;next = buf;
        cache-&gt;tail = buf;
    }

    return(&amp;(buf-&gt;block));
}
</pre>
<p></code><br />
You can download a demo implementation here: <a href="http://th30z.netsons.org/wp-content/uploads/lru-block-cache.c" title="LRU Cache Source Code">lru-block-cache.c</a>, but <em>I&#8217;m waiting some ideas or suggestions to improve (or change radically) the Cache Algorithm. Thanks in advance!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/08/block-cache-algorithm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[TIP] Generic Binary Format</title>
		<link>http://th30z.netsons.org/2009/08/tip-generic-binary-format/</link>
		<comments>http://th30z.netsons.org/2009/08/tip-generic-binary-format/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 11:26:31 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Binary Format]]></category>
		<category><![CDATA[Protocols]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=1156</guid>
		<description><![CDATA[I Love use Binary formats, instead of XML, and JSON. Here is my Generic Binary Format for data transmissions or serializations. Data is composed by three blocks. The first one 1byte that describe all information about the object, like &#8220;is a single Int object&#8221; or &#8220;is a list&#8221;, then tells you the second block length. [...]]]></description>
			<content:encoded><![CDATA[<p>I Love use Binary formats, instead of XML, and JSON. Here is my Generic Binary Format for data transmissions or serializations. Data is composed by three blocks. The first one 1byte that describe all information about the object, like &#8220;is a single Int object&#8221; or &#8220;is a list&#8221;, then tells you the second block length. The second block contains the size of the third block (The Data-Block) or the Number of Items in List.</p>
<p><a href="http://th30z.netsons.org/wp-content/uploads/Generic-Binary-Format.png"><img class="aligncenter size-full wp-image-1155" title="Generic Binary Format" src="http://th30z.netsons.org/wp-content/uploads/Generic-Binary-Format.png" alt="Generic Binary Format" width="577" height="289" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/08/tip-generic-binary-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unified Notification Service: Avoid The Wheel Reinvention</title>
		<link>http://th30z.netsons.org/2009/08/unified-notification-service-avoid-the-wheel-reinvention/</link>
		<comments>http://th30z.netsons.org/2009/08/unified-notification-service-avoid-the-wheel-reinvention/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 04:43:31 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Notification Service]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=1118</guid>
		<description><![CDATA[Every programmer loves to reinvent the wheel, and reinventing the wheel is still my primary hobby. Sometimes you need to reimplement a Network protocol to use with your favorite language/library, sometimes is only for fun, but if you&#8217;re in the Business World maybe is &#8220;better&#8221; (faster) to use one of the thousand existing libraries.
In the [...]]]></description>
			<content:encoded><![CDATA[<p>Every programmer loves to reinvent the wheel, and reinventing the wheel is still my primary hobby. Sometimes you need to reimplement a Network protocol to use with your favorite language/library, sometimes is only for fun, but if you&#8217;re in the Business World maybe is &#8220;better&#8221; (faster) to use one of the thousand existing libraries.</p>
<p>In the most cases you need to reimplement a Protocol to embed it in your application, and sometimes you have to reimplement two/three protocols that does the same job like IM Protocols (XMPP, AIM, Yahoo&#8230;).</p>
<p>A better solution, that avoid you to reinvent the wheel is to use an existent library to handle the protocol(s) that you need, and build an Abstract Interface, with your data format, that allows you to use a generic way to communicate between various provider. Below you can find a graphical example of what I mean.</p>
<p><a href="http://th30z.netsons.org/wp-content/uploads/UnifiedNotificationService.png"><img class="aligncenter size-full wp-image-1119" title="Unified Notification Service" src="http://th30z.netsons.org/wp-content/uploads/UnifiedNotificationService.png" alt="Unified Notification Service" width="513" height="371" /></a></p>
<p>You can have many providers, written in different languages. These providers talk with the Notification Service providing an abstract interface for the Apps. In this way, the end App has just to say &#8220;Write a Mail To X&#8221;, &#8220;Download Todays Mail&#8221;, &#8220;Send an IM to X&#8221;&#8230; and you can intercept notification to displays as a popup on your desktop&#8230; or something similar.</p>
<p><em>This solution will be used in <a title="MokoTouch Project" href="http://th30z.netsons.org/mokotouch">MokoTouch Project</a>, to provide Core Services to the Apps. For more information send me a mail.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/08/unified-notification-service-avoid-the-wheel-reinvention/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Bit of Distributed Computation&#8230;</title>
		<link>http://th30z.netsons.org/2009/08/a-bit-of-distributed-computation/</link>
		<comments>http://th30z.netsons.org/2009/08/a-bit-of-distributed-computation/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 17:23:26 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Distributed Computation]]></category>
		<category><![CDATA[Parallel Computation]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=1101</guid>
		<description><![CDATA[In the last months I&#8217;ve worked on various 2D rendering projects, that requires lots of row power to be executed in smallest time as possible.
The rendering result is an aggregation of components (or better, Group of Components) that can be rendered independently of each other in a  process because each components has its own input [...]]]></description>
			<content:encoded><![CDATA[<p>In the last months I&#8217;ve worked on various 2D rendering projects, that requires lots of row power to be executed in smallest time as possible.</p>
<p>The rendering <em>result is an aggregation of components</em> (or better, Group of Components) that <strong>can be rendered independently</strong> of each other in a  process because each components has its own input data and until the aggregation process starts there&#8217;re <strong>no dependencies</strong> between components. In a few words, foreach input data I&#8217;ve to call a Render method that returns the &#8220;computed&#8221; data, that at the end will be aggregated with all the computed values.</p>
<p>So, how speed-up the rendering? The standard answer is using <strong>Threads</strong>, <em>to take advantage of multi-core system</em>. But I need lots row power and lots of core to be fast enought. Another solution that doesn&#8217;t require to spent a lot of money for a faster computer it to <strong>distribute the computation across different machines</strong>. And this is my attempt&#8230;</p>
<p><a href="http://th30z.netsons.org/wp-content/uploads/DistributedComputation-Schema-01.png"><img class="aligncenter size-full wp-image-1102" title="Distributed Computation Arch" src="http://th30z.netsons.org/wp-content/uploads/DistributedComputation-Schema-01.png" alt="Distributed Computation Arch" width="533" height="445" /></a></p>
<p>The <strong>Master</strong> receive an array of elements as input and splits up it according to the number of the available Nodes (Machines), and foreach node assigns its sub-array and sends an &#8220;informative message&#8221; to the node.</p>
<p>The <strong>Node</strong> waits for an &#8220;informative message&#8221; and when it has received it, starts its computation. Foreach Data Item that fetch runs the Computation and sends back the result.</p>
<p>The Master decompose data into <em>equal-size partitions</em>, so each node has  an equal-size queue to process, but if someone finishes its job and there&#8217;re more data to process (in someone else queue) The Master dequeue couple of items from the slower node queue and add to the queue of the one that has finished its job. In this way you&#8217;ve done your computation faster then ever, and <strong>if one machine crashes</strong> (slow one case) its job will be taken by someone else that has finished its own.</p>
<p><em>And this is just a bit of General Theory, but the implementation is really simple. Maybe I&#8217;ll try to reimplement something more generic in the near future (October, November) when I&#8217;ll have a bit more of free time.</em> If you&#8217;ve any question send me a mail!</p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/08/a-bit-of-distributed-computation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reliable Protocol on Unreliable Channel</title>
		<link>http://th30z.netsons.org/2009/06/reliable-protocol-on-unreliable-channel/</link>
		<comments>http://th30z.netsons.org/2009/06/reliable-protocol-on-unreliable-channel/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 05:52:56 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Protocols]]></category>
		<category><![CDATA[Theory]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=963</guid>
		<description><![CDATA[When you work with Unreliable communication channel (like UDP), you need to implement your own &#8220;Reliable Mechanism&#8221; to be sure that packet that you&#8217;ve send was received correctly. Here there&#8217;s my attempt.
Reliable Transmitter:
The schema below shows the operations that reliable transmitter has to do. &#8220;Red&#8221; are &#8220;error or unexpected operations&#8221;, Green are &#8220;expected operations&#8221;, Blue [...]]]></description>
			<content:encoded><![CDATA[<p>When you work with Unreliable communication channel (like <a title="UDP User Datagram Protocol" href="http://en.wikipedia.org/wiki/User_Datagram_Protocol">UDP</a>), you need to implement your own &#8220;Reliable Mechanism&#8221; to be sure that packet that you&#8217;ve send was received correctly. Here there&#8217;s my attempt.</p>
<p><strong>Reliable Transmitter</strong>:</p>
<p>The schema below shows the operations that reliable transmitter has to do. &#8220;Red&#8221; are &#8220;error or unexpected operations&#8221;, Green are &#8220;expected operations&#8221;, Blue are TX Operations.</p>
<p style="text-align: left;"><a href="http://th30z.netsons.org/wp-content/uploads/ReliablePacketTx.png"><img class="aligncenter size-full wp-image-962" title="Reliable Packet Tx" src="http://th30z.netsons.org/wp-content/uploads/ReliablePacketTx.png" alt="Reliable Packet Tx" width="589" height="119" /></a>Transmitter send a packet and waits for the ACK packet (sended by receiver). If ACK doesn&#8217;t arrive before the Tx Timeout packet is resended. When ACK arrive Tx send the CACK (Confirm ACK) packet and waits for a timeout, if another ACK Arrive TX resend the CACK, this because receiver doesn&#8217;t have the confirm of transmitter. When TX timeout all is done, and the packet is sended correctly.</p>
<p><strong>Reliable Receiver</strong>:</p>
<p>The schema below shows the operations that reliable receiver has to do. &#8220;Red&#8221; are &#8220;error or unexpected operations&#8221;, Green are &#8220;expected operations&#8221;, Blue are Rx Operations.</p>
<p style="text-align: left;"><a href="http://th30z.netsons.org/wp-content/uploads/ReliablePacketRx.png"><img class="aligncenter size-full wp-image-967" title="Reliable Packet Rx" src="http://th30z.netsons.org/wp-content/uploads/ReliablePacketRx.png" alt="Reliable Packet Rx" width="456" height="132" /></a>When RX receive a Packet sends an ACK, and waits for the CACK, if CACK doesn&#8217;t arrive before timeout RX resend the ACK. When CACK arrive all is done. We&#8217;ve received the packet and the transmitter knows that we&#8217;ve received it.</p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/06/reliable-protocol-on-unreliable-channel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenSSL: SSL Client/Server Example</title>
		<link>http://th30z.netsons.org/2009/06/openssl-ssl-clientserver-example/</link>
		<comments>http://th30z.netsons.org/2009/06/openssl-ssl-clientserver-example/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 04:53:51 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[Unix C]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=936</guid>
		<description><![CDATA[I&#8217;m experimenting a bit with the new iPhone 3.0 SDK, Core Data, Game Kit, Push Notification Service and so on&#8230; 
I&#8217;m using C, so today a little Example of SSL Client/Server written in C using OpenSSL, I&#8217;ve written a small wrapper for SSL Socket, and here is How to use it.


/* SERVER CODE
 * ==============================
 [...]]]></description>
			<content:encoded><![CDATA[<p><em>I&#8217;m experimenting a bit with the new iPhone 3.0 SDK, Core Data, Game Kit, Push Notification Service and so on&#8230; </em></p>
<p>I&#8217;m using C, so today a little Example of SSL Client/Server written in C using OpenSSL, I&#8217;ve written a small wrapper for SSL Socket, and here is How to use it.<br />
<code></p>
<pre>
/* SERVER CODE
 * ==============================
 */
SFSocketGlobalInit();       /* Initialize SSL */

/* Alloc Socket, Initialize SSL and Listen */
SFSocket *socket = SFSocketAlloc();
SFSocketInit(socket, CA_FILE, DH_FILE, KEY_FILE, KEY_PASSWORD, NULL);
SFSocketListen(socket, INADDR_ANY, PORT);

do {
   SFSocket *clientSocket;
   char buffer[64];
   int rdSize;

   /* Accept Client Connection */
   if ((clientSocket = SFSocketAccept(socket)) == NULL)
       break;        

   /* Read Data from Client */
   if ((rdSize = SFSocketRead(clientSocket, buffer, 64 - 1)) &gt; 0) {
       buffer[rdSize] = '\0';
       printf(&quot;Client: %s\n&quot;, buffer);
   }

   /* Write to Client */
   strcpy(buffer, &quot;Hello Client!&quot;);
   SFSocketWrite(clientSocket, buffer, strlen(buffer));

   /* Disconnect Client */
   SFSocketRelease(clientSocket);
} while (1);

/* Close and Release Socket Resources */
SFSocketRelease(socket);
</pre>
<p></code><br />
Above you&#8217;ve the simplified server code (without error check!) and below you&#8217;ve the client code. The client try to connects to server, send an &#8220;Hello&#8221; message and the server reply with other greetings.<br />
<code></p>
<pre>
/* CLIENT CODE
 * ==============================
 */
SFSocketGlobalInit();       /* Initialize SSL */

/* Alloc Socket, Initialize SSL */
SFSocket *socket = SFSocketAlloc();
SFSocketInit(socket, CA_FILE, NULL, KEY_FILE, KEY_PASSWORD, NULL);

/* Connect to Host */
SFSocketConnectToHost(socket, HOSTNAME, PORT);

/* Send Message to Server */
char buffer[64];
strcpy(buffer, &quot;Hello from Client!&quot;);
SFSocketWrite(socket, buffer, strlen(buffer));

/* Read Message from Server */
if ((rdSize = SFSocketRead(socket, buffer, 64 - 1)) &gt; 0) {
   buffer[rdSize] = '\0';
   printf(&quot;Server: %s\n&quot;, buffer);
}

/* Close and Release Socket Resources */
SFSocketRelease(socket);
</pre>
<p></code></p>
<p>Remember that you need to generate, at least, the Authority Certificate, Server Certificate and Clients Certificates. and here is How to do it.<br />
<code></p>
<pre>
- AUTHORITY Certificate:
 openssl genrsa -des3 -out ca.key 1024
 openssl req -new -x509 -key ca.key -out ca.crt

- SERVER Certificate
 openssl genrsa -des3 -out server.key 1024
 openssl req -new -key server.key -out server.csr
 openssl x509 -req -in server.csr -out server.crt -sha1 \
                   -CA ca.crt -CAkey ca.key -CAcreateserial 

- CLIENT Certificate
 openssl genrsa -des3 -out client.key 1024
 openssl req -new -key client.key -out client.csr
 openssl x509 -req -in client.csr -out client.crt -sha1 \
                   -CA ca.crt -CAkey ca.key -CAcreateserial
</pre>
<p></code></p>
<p>The Full Source Code is Available Here: <a title="SSL Client/Server Example" href="http://th30z.netsons.org/wp-content/uploads/SSL-Example.tar.bz2">SSL Client/Server Example Source Code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/06/openssl-ssl-clientserver-example/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
