<?xml version="1.0" encoding="utf-8"?>
<rss version='2.0' xmlns:lj='http://www.livejournal.org/rss/lj/1.0/'>
	<channel>
		<title>The Corelatus Blog - Entries tagged c</title>
		<description>Entries tagged c</description>
                <link>../../</link>

	
	<item>
		<title>GTH &#39;C&#39; API</title>
		<link>../../GTH__C__API.html</link>
		<guid isPermaLink="true">../../GTH__C__API.html</guid>
                <pubDate>Wed, 20 Jan 2010 10:47:37 GMT</pubDate>
		<description>&lt;p&gt; 
This post is for people who want to use C to control a GTH. Other
languages (e.g. Erlang, Java, Python and Perl) are easier to work
with, but in some applications you want the complete control that C
gives you. 
&lt;/p&gt;

&lt;p&gt;
Corelatus provides a &lt;a href=&quot;http://www.corelatus.com/gth/api/gth_c_examples.zip&quot;&gt;
C API for GTH&lt;/a&gt;. The C API lets you control a GTH using plain C function
calls---all of the XML wire format is taken care of.
&lt;/p&gt;

&lt;p&gt;Here&#39;s an example of how to use it to record speech
on an E1/T1 timeslot, something you&#39;d typically do in a voicemail system:
&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
&lt;span class=&quot;synPreProc&quot;&gt;#include &lt;/span&gt;&lt;span class=&quot;synConstant&quot;&gt;&amp;quot;gth_apilib.h&amp;quot;&lt;/span&gt;
...

  GTH_api api;                     &lt;span class=&quot;synComment&quot;&gt;// GTH_api represents one GTH API connection&lt;/span&gt;
  &lt;span class=&quot;synType&quot;&gt;int&lt;/span&gt; result;
  &lt;span class=&quot;synType&quot;&gt;int&lt;/span&gt; data_socket;
  &lt;span class=&quot;synType&quot;&gt;char&lt;/span&gt; buffer[&lt;span class=&quot;synConstant&quot;&gt;2000&lt;/span&gt;];
  &lt;span class=&quot;synType&quot;&gt;char&lt;/span&gt; job_id[MAX_JOB_ID];
  &lt;span class=&quot;synType&quot;&gt;int&lt;/span&gt; octet_count;

  result = gth_connect(&amp;amp;api, &lt;span class=&quot;synConstant&quot;&gt;&amp;quot;172.16.1.10&amp;quot;&lt;/span&gt;);    &lt;span class=&quot;synComment&quot;&gt;// Assuming the default GTH IP &lt;/span&gt;
  assert(result == &lt;span class=&quot;synConstant&quot;&gt;0&lt;/span&gt;);

  &lt;span class=&quot;synComment&quot;&gt;// We want to record audio on the E1/T1 called &amp;quot;1A&amp;quot;, on timeslot 3.&lt;/span&gt;
  data_socket = gth_new_recorder(api, &lt;span class=&quot;synConstant&quot;&gt;&amp;quot;1A&amp;quot;&lt;/span&gt;, &lt;span class=&quot;synConstant&quot;&gt;3&lt;/span&gt;, job_id);

  &lt;span class=&quot;synStatement&quot;&gt;while&lt;/span&gt; ( (octet_count = recv(data_socket, buffer, &lt;span class=&quot;synStatement&quot;&gt;sizeof&lt;/span&gt; buffer, &lt;span class=&quot;synConstant&quot;&gt;0&lt;/span&gt;)) ) {
    &lt;span class=&quot;synComment&quot;&gt;// do whatever you want with the received data&lt;/span&gt;
  }

...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
The recording above happens in the &#39;while&#39; loop. It continues forever
(i.e. until you abort it). The audio data is bit-for-bit identical to
what was on the E1/T1 timeslot, so this can be used for recording both
voice and signalling.
&lt;/p&gt;

&lt;h3&gt;Further examples&lt;/h3&gt;

&lt;p&gt;The C API code includes further examples to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Record audio or signalling from a timeslot to a .wav file&lt;/li&gt;
  &lt;li&gt;Play back previously recorded files&lt;/li&gt;
  &lt;li&gt;Install (upgrade) the firmware on a GTH&lt;/li&gt;
  &lt;li&gt;Monitor (sniff) SS7 MTP-2 and save the signalling to a wireshark
    (PCAP) compatible file.&lt;/li&gt;
  &lt;li&gt;Monitor CAS R2 signalling.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Standalone Parser&lt;/h3&gt;

&lt;p&gt;Aside: the C API code includes a standalone parser for the XML
responses the GTH emits. You can use the parser without using the
rest of the API library, if you want to.
&lt;/p&gt;
</description>
	</item>
	
	<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>
	
	<item>
		<title>Live Wireshark Captures on Windows</title>
		<link>../../Live_Wireshark_Captures_on_Windows.html</link>
		<guid isPermaLink="true">../../Live_Wireshark_Captures_on_Windows.html</guid>
                <pubDate>Wed, 15 Jun 2011 11:38:36 GMT</pubDate>
		<description>&lt;p&gt;
I&#39;ve written about
using &lt;a href=&#39;http://www.wireshark.org/&#39;&gt;Wireshark&lt;/a&gt; to look at
signalling captured from an E1/T1
&lt;a href=&#39;http://blog.corelatus.com/Capturing_SS7_with_wireshark_or_tshark.html&#39;&gt;before&lt;/a&gt;. As
of a few days ago, it&#39;s possible to do &lt;em&gt;live&lt;/em&gt; captures on
Windows, like this:

&lt;pre&gt;
 start save_to_pcap 172.16.1.10 1A 2A 16 \\.\pipe\ss7.1
 wireshark -k -i \\.\pipe\ss7.1
&lt;/pre&gt;

When you&#39;re capturing live, the SS7 packets appear in Wireshark in real-time.
&lt;/p&gt;

&lt;p&gt;
The new version of &#39;save_to_pcap&#39; program is part of the 
&lt;a href=&#39;http://www.corelatus.com/gth/api/gth_c_examples.zip&#39;&gt;C examples&lt;/a&gt;,
the .zip file contains both source (compiles on Unix and Windows) and .exe
files.
&lt;/p&gt;

&lt;p&gt;
It&#39;s always been possible to do live captures on Unix, you just pipe
stdout:

&lt;pre&gt;
./save_to_pcap gth21 1A 2A 16 - | wireshark -k -i -
&lt;/pre&gt;
&lt;/p&gt;

</description>
	</item>
	
        </channel>
</rss>

