<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version='2.0' xmlns:lj='http://www.livejournal.org/rss/lj/1.0/'>
	<channel>
		<title>The Corelatus Blog - Entries from June 2010</title>
		<description>Entries from June 2010</description>
                <link>../../../</link>

	
	<item>
		<title>Porting C Socket Code to Windows</title>
		<link>../../../Porting_C_Socket_Code_to_Windows.html</link>        
		<guid isPermaLink="true">../../../Porting_C_Socket_Code_to_Windows.html</guid>
                <pubDate>Wed, 16 Jun 2010 12:47:57 GMT</pubDate>
		<description>&lt;p&gt;
  I just ported
  the &lt;a href=&#39;http://www.corelatus.com/gth/api/gth_c_examples.zip&#39;&gt;C
  example code&lt;/a&gt; for controlling Corelatus E1/T1 hardware to Visual
  Studio 2010. Doing that was easier than expected. This first part
  of this post is about how to use that code. The second part is 
  a bit of history about what I had to do to port the code.
&lt;/p&gt;

&lt;h4&gt;Obtaining Visual Studio&lt;/h4&gt;

&lt;p&gt;Microsoft have their C/C++ compiler available for free download
  on &lt;a href=&#39;http://www.microsoft.com/express/Windows/&#39;&gt;microsoft.com&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;The install process is pretty standard for windows, if you&#39;re
  accustomed to Microsoft products, there are no surprises here.
&lt;/p&gt;


&lt;h4&gt;Compiling the code&lt;/h4&gt;

  &lt;ol&gt;
    &lt;li&gt;Start the &quot;Visual Studio Command Prompt&quot;. By default, that&#39;s
      in &quot;Start/Programs/Microsoft Visual Studio 2010 Express&quot;.&lt;/li&gt;

    &lt;li&gt;Navigate to the directory you unpacked
      the &lt;a href=&#39;http://www.corelatus.com/gth/api/gth_c_examples.zip&#39;&gt;sample
      code&lt;/a&gt; in. Remove any old .exe files with &#39;del *.exe&#39;.&lt;/li&gt;

    &lt;li&gt;Run &#39;nmake /f NMakefile&#39;. After a couple of seconds, you have 
        six brand-new .exe files. Here&#39;s what this step looks like:&lt;/li&gt;
  &lt;/ol&gt;

&lt;p&gt;
  &lt;img style=&#39;margin-left:-70px&#39; 
       src=&#39;static/visual_studio_compile.png&#39; 
       alt=&#39;screenshot of a VC compile&#39;/&gt;

  If you just wanted to see how to compile the code, &lt;b&gt;you can stop reading
  now&lt;/b&gt;. The rest of the post is just about what I had to do to make this
  work with Microsoft&#39;s compiler.
&lt;/p&gt;


&lt;h4&gt;Hurdle #1: MS nmake vs gnumake vs Visual Studio projects&lt;/h4&gt;

&lt;p&gt;
  Conclusion: I ended up using Microsoft&#39;s nmake and wrote a separate
  NMakefile for it.
&lt;/p&gt;

&lt;p&gt;
  My code gets built by &#39;make&#39;, through a Makefile. Visual Studio doesn&#39;t
  include a make program which can understand normal Makefiles, leaving
  me a choice of either using the Visual Studio IDE or using Microsoft&#39;s
  &lt;code&gt;nmake&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
  I spent half an hour trying the IDE route. When you make a new
  project, you have to choose what sort it is. &quot;CLR Console
  Application&quot; seemed closest to what I wanted to do, so I chose
  that. Maybe &quot;Makefile Project&quot; would have worked better.
  Things got increasingly confusing from there. What&#39;s a &quot;solution?&quot;
  Why are &quot;precompiled headers&quot; enabled? I just want to compile a few
  hundred lines of C. At that point, I gave up, the IDE is clearly not
  the easiest way for me to get started.
&lt;/p&gt;

&lt;p&gt;
  Next, I tried Microsoft&#39;s replacement for &lt;code&gt;make&lt;/code&gt;, called
  &lt;code&gt;nmake&lt;/code&gt;. That didn&#39;t start flawlessly either, nmake uses
  its own syntax. Initially, I thought I might be able to write one
  Makefile which both gnumake and nmake understand. But Microsoft&#39;s
  syntax seems to be too different to allow that. Oh well. The NMakefile
  is just a dozen lines or so.
&lt;/p&gt;


&lt;h4&gt;Hurdle #2: Missing header files&lt;/h4&gt;

&lt;p&gt;
  Conclusion: I used #ifdefs to include different header files when
  compiling in a win32 environment.&lt;/p&gt;

&lt;p&gt;
  Visual Studio doesn&#39;t provide some of the header files the code
  uses, e.g. there&#39;s no &#39;unistd.h&#39; and no &#39;sys/socket.h&#39;. Odd.
  Providing most of unistd.h shouldn&#39;t be too hard.
&lt;/p&gt;

&lt;p&gt;
  The point of this exercise is to get the code to compile using
  a Microsoft toolchain, so I didn&#39;t investigate using cygwin or
  mingw.
&lt;/p&gt;

&lt;p&gt;
  In the end, it turns out that I wasn&#39;t using anything from &#39;unistd.h&#39;
  which didn&#39;t get provided by something else in win32. &#39;socket.h&#39; (and
  friends) was a bit harder, but it turns out that&#39;s provided by 
  &#39;winsock2.h&#39;.
&lt;/p&gt;


&lt;h4&gt;Hurdle #3: &#39;Mostly compatible&#39; socket API&lt;/h4&gt;

&lt;p&gt;
  Conclusion: winsock2 is different to the BSD socket API, but the
  differences can be worked around with just a few changes.
&lt;/p&gt;

&lt;p&gt;
  Sockets were invented on unix, so I assumed that other OSes would
  just copy the interface. But no, not on win32. There seem to be at
  least two attempts to make a socket API for windows, called
  &#39;winsock&#39; and &#39;winsock2&#39;. The first seems to be deprecated.  The
  main problems I hit were:

&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;You can&#39;t use close() on a socket, you have to use closesocket()&lt;/li&gt;
    &lt;li&gt;You can&#39;t use read() on a socket, you have to use recv()&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;errno&lt;/code&gt; doesn&#39;t report socket errors, you have to
      call a special Windows Socket error reporting function.&lt;/li&gt;
  &lt;/ul&gt;

&lt;p&gt;
  None of those problems are hard to get around.
&lt;/p&gt;
  

&lt;h4&gt;Hurdle #4: GNU and Microsoft can&#39;t agree on function names&lt;/h4&gt;

&lt;p&gt;
  Both GNU and Microsoft have replacements for some functions which
  are prone to security problems. If you use plain &lt;code&gt;strcat()&lt;/code&gt;
  or &lt;code&gt;strcpy&lt;/code&gt;, visual studio warns about security problems.
&lt;/p&gt;

&lt;p&gt;
  Disabling the warnings would be quick but feels like cheating. So
  I looked at the &#39;less insecure&#39; alternatives. GNU call them
  &lt;code&gt;strncat&lt;/code&gt; and &lt;code&gt;strncpy&lt;/code&gt; whereas Microsoft call
  them &lt;code&gt;strcat_s&lt;/code&gt; and &lt;code&gt;strcpy_s&lt;/code&gt;. And the
  arguments are the same way around. It would have been nice if GNU
  and MS had solved this the same way, but no. Minor annoyance.
&lt;/p&gt;


&lt;h4&gt;Hurdle #5: Structure packing pragmas&lt;/h4&gt;

&lt;p&gt;
  Conclusion: Both MS and GNU understand the #pragma pack(N) syntax,
  but only GNU understands the __attribute__((__packed__)) syntax.
&lt;/p&gt;

&lt;p&gt;
  A couple of the examples (save_to_pcap.c and record.c) use
  structures to encode or decode data defined by an external
  format. For instance, here&#39;s what the wire format of a
  &#39;pcap&#39; protocol capture file looks like:

&lt;/p&gt;
  &lt;pre&gt;
    &lt;code&gt;
      &lt;span class=&quot;synType&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;synType&quot;&gt;struct&lt;/span&gt; {
        &lt;span class=&quot;synType&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;synType&quot;&gt;int&lt;/span&gt; magic;
        &lt;span class=&quot;synType&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;synType&quot;&gt;short&lt;/span&gt; major_version;
        &lt;span class=&quot;synType&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;synType&quot;&gt;short&lt;/span&gt; minor_version;
        &lt;span class=&quot;synType&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;synType&quot;&gt;int&lt;/span&gt; GMT_to_localtime;
        &lt;span class=&quot;synType&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;synType&quot;&gt;int&lt;/span&gt; sigfigs;
        &lt;span class=&quot;synType&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;synType&quot;&gt;int&lt;/span&gt; snaplen;
        &lt;span class=&quot;synType&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;synType&quot;&gt;int&lt;/span&gt; network;
      } PCAP_global_header;
    
&lt;/code&gt;
  &lt;/pre&gt;

&lt;p&gt;
  We have to tell the compiler that spacing out the fields to get
  a certain alignment isn&#39;t allowed. The recommended GNU way to
  do that is to add &lt;code&gt;__attribute__((__packed__))&lt;/code&gt; after
  the structure. The recommended Microsoft way is to add
  &lt;code&gt;#pragma pack(1)&lt;/code&gt; somewhere before the structure, and
  then remember to change the packing back again before hitting
  any code which is sensitive to performance.
&lt;/p&gt;

&lt;p&gt;
  Since this is just example code, we don&#39;t care about that last
  bit of performance, so we can just leave the packing at 1.
&lt;/p&gt;

&lt;p&gt;
  Aside: the structure above is a bit sloppy because just quietly
  assumes that an int is 32 bits and a short 16.
&lt;/p&gt;

&lt;h4&gt;Hurdle #6: Windows firewall&lt;/h4&gt;

&lt;p&gt;
  Some of the example code opens TCP sockets for listening. By default,
  the windows firewall doesn&#39;t allow that. Just click &quot;fine, ok, let
  me do this&quot;.
&lt;/p&gt;

&lt;p&gt;
  Unfortunately, that looks ugly for the user---the program will fail
  the first time you run it, and the pop-up box says &quot;publisher
  unknown&quot;.  Does anyone know how to improve on this? It&#39;d be nice if
  the user could approve the publisher, i.e. me, once, and then every
  program works.
&lt;/p&gt;

&lt;h4&gt;How long did the porting take?&lt;/h4&gt;

&lt;p&gt;
  A couple of days, including getting Visual Studio installed.
  That&#39;s for a few thousand lines of code with a bit of socket IO
  and file IO.
&lt;/p&gt;

&lt;p&gt;
  If I had to do it again, it&#39;d take an afternoon or so.
&lt;/p&gt;
</description>
	</item>
	
        </channel>
</rss>

