Monday, 27 February 2012

Streaming data to a specific interface without using the routing table (Linux)

I recently created a multicast generator application to test an application that processes multicast data.  The application worked well.  However, I became frustrated with having to set up the routes on any server I used to ensure the data was sent out of the correct interface.  tcpreplay overcomes this by allowing you to give the interface name on the command line and bypasses the routing.  After doing some investigation into how this can be achieved I thought I would share a couple of techniques you can use to do this.  I haven't tested this code so I apologise if it does not compile but it should give you a rough idea about how this works.

Using the interface IP address:

#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/if_ether.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <linux/ip.h>
#include <stdio.h>
#include <string.h>

int getAddressFromIP ( int fd, struct in_addr *address, const char *ip )
{
    int Ret = inet_aton (ip, address);
   
    if ( Ret == 0 )
    {
        fprintf(stderr, "Invalid IP address: %s\n", ip);
        return -1;
    }
   
    return 0;
}

int bindSocketWithIP(int fd, const char *ip)
{
    struct sockaddr_in address;

    memset(&address, 0, sizeof(address));

    if ( !getAddressFromIP(fd, &address.sin_addr, ip) )
    {
        address.sin_family = AF_INET;
        address.sin_port   = 0;
        if (bind (fd, (struct sockaddr *) &address, sizeof(address)))
        {
            fprintf(stderr, "Failed to bind to socket: %s\n", ip);
            return -1;
        }
    }
    else
    {
        /* error already reported by getAddressFromIP() */
        return -1;
    }
   
    return 0;
}

Using the interface name:

#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/if_ether.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <linux/ip.h>
#include <stdio.h>
#include <string.h>


int getAddressFromName ( int fd, struct in_addr *address, const char *name )
{
    struct ifreq request;
    struct sockaddr_in *sockaddr_in;
   
    if (strlen (name) > IFNAMSIZ)
    {
        fprintf(stderr, "Invalid interface name: %s\n", name);
        return -1;
    }
   
    strncpy (request.ifr_name, name, IFNAMSIZ);
   
    if (ioctl(fd, SIOCGIFADDR, &request))
    {
        fprintf(stderr, "ioctl call failed for interface name: %s\n", name);   
        return -1;
    }
   
    sockaddr_in = (struct sockaddr_in *) &request.ifr_addr;
   
    memcpy (address, &sockaddr_in->sin_addr, sizeof(*address));
   
    return 0;
}

int bindSocketWithName(int fd, const char *name)
{
    struct sockaddr_in address;

    memset(&address, 0, sizeof(address));

    if ( !getAddressFromName(fd, &address.sin_addr, name) )
    {
        address.sin_family = AF_INET;
        address.sin_port   = 0;
        if (bind (fd, (struct sockaddr *) &address, sizeof(address)))
        {
            fprintf(stderr, "Failed to bind to socket: %s\n", name);
            return -1;
        }
    }
    else
    {
        /* error already reported by getAddressFromName() */
        return -1;
    }
   
    return 0;
}

Monday, 29 August 2011

Basic SSH Tunneling and port forwarding

Just the other day I was looking for a good HowTo guide for SSH tunnelling in Linux but I could not exactly what I was looking for.  So once I found all the information I needed I thought I should write so someone - perhaps me - might find it useful in future.

The basics - What exactly is SSH tunnelling

SSH tunnelling allows you to encrypt data between two servers.  The data is transported via SSH.  This mean the remote server treats the data coming from the tunnel as its own data.  Common uses for SSH include encrypting data to and from email servers and bypassing firewall restrictions on company networks to all remote access to servers using VNC.

In my case I had a network that was only accessible from one of my company servers and I wanted to access it from my desk without first logging into the other server.  I know there are other possibly better ways to do this but I want to try this method.


How To Use It

Here is an example command to set up an SSH tunnel:

ssh -f user@some-server.com -L 3865:some-server.com:22 -N

To break the command down:
  • -f tells SSH to go into the background.
  • -N tells SSH not to execute any commands.  If you do not include this it will drop you into a shell as normal.
  • The login is the normal login you would use to log into the server.
  • -L 3865:some-server.com:22 sets up the tunnel.  Importantly it tells SSH to listen on port 3865 and forward the data to port 22 on the host given - in this case some-server.com.
In practical terms the port is opened on the server the run this command on.  You can connect to 3865 from any other server on your network and the data will be transported securely between the local server and the remote server.  However, importantly the connection to the server you used to set up the SSH tunnel will not be encrypted unless to take steps to ensure it is or the protocol used is already encrypted.

With this example if the server you set up the tunnel on was called myserver.com you could connect to some-server.com using the following command:

ssh user@myserver.com -p 3865

Note: Although you are pointing SSH at myserver.com the SSH tunnel forwards the connection to some-server.com so the user name and password should match that server and not myserver.com.  This confuses SSH a bit because it believes the identity of the server has changed.

Monday, 25 April 2011

Creating Your Own Custom Wireshark Dissector

Wireshark is a powerful open source tool used to dissect Ethernet packets. Have you ever wondered what it takes to implement your own custom dissector? Furthermore, have you attempted to learn Wireshark's API and found it difficult to understand? This article will attempt to demystify the development of your very own protocol dissector. This article uses Amin Gholiha's "A Simple IOCP Server/Client class" [^] as a basis for dissection, thus producing the AMIN protocol.

http://www.codeproject.com/KB/IP/custom_dissector.aspx

Monday, 28 March 2011

Linux Daemon Writing HOWTO

Creating a daemon in Linux uses a specific set of rules in a given order. Knowing how they work will help you understand how daemons operate in userland Linux, but can operate with calls to the kernel also. In fact, a few daemons interface with kernel modules that work with hardware devices, such as external controller boards, printers,and PDAs. They are one of the fundamental building blocks in Linux that give it incredible flexibility and power.

http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html

Friday, 18 February 2011

IEEE Standard 754 Floating Point Numbers

IEEE Standard 754 floating point is the most common representation today for real numbers on computers, including Intel-based PC's, Macintoshes, and most Unix platforms. This article gives a brief overview of IEEE floating point and its representation. Discussion of arithmetic implementation may be found in the book mentioned at the bottom of this article.


http://steve.hollasch.net/cgindex/coding/ieeefloat.html

Wednesday, 16 February 2011

Byte Swapping Floating Point Types

The standard library functions only have byte swapping functions for 16 bit and 32 bit integral types. 8 bit data doesn't need swapping. But sometimes you want to write floating point data to the network or a file. This is problematic in that different processor architectures may use different bit level representations of floating point data, but these days most machines use IEEE 754 implementations that are mostly compatible. Assume for this article we are not concerned with this level of compatibility. (But don't assume it for your application! if you are sending/storing doubles and floats, then it behooves you to understand the platforms you care about).

The C standard library doesn't have native functions for byte swapping 8 byte double, or for that matter 32 bit float data types. So almost everyone has to code up their own swapping routines one way or the other. It turns out that a naive implementation of byte swapping floats can lead to subtle errors, which is what this article is about.

http://www.dmh2000.com/cpp/dswap.shtml

Monday, 7 February 2011

Understanding and Using Floating Point Numbers

Most programmers have heard or observed one strange thing or another about floating point numbers. For example, we often discover that floating point numbers that look the same do not necessarily satisfy C's "==" test. New programmers are usually taught never to use == for floating point numbers for this reason. Occasionally we run into other exceptional cases, for instance mathematically sound formulae which, when implemented using floating point, produce seemingly random or disappointingly inaccurate results.

http://www.cprogramming.com/tutorial/floating_point/understanding_floating_point.html

Monday, 24 January 2011

C Programming: Macro functions that return values

A few weeks ago I was searching around the Internet for an explanation of how to write a macro (preprocessor) function that returns a value.  All I could find was various articles suggesting something like this:

#define MAX(A,B)  ((A > B) ? A : B)

However, I was looking for a more complicated example for instance something with a loop like this:

#define MAX(A,B)  ((A > B) ? A : B)

#define HIGHEST(ARRAY) /* .... */

int main (void)
{
    int A[] = { 1, 2, 4, 3 };

    printf("%d\n", HIGHEST(A));

    return 0;
} 

All the articles I could find were suggesting adding an out parameter to the function to return the highest value.  After a lot of searching around I eventually gave up and used inline functions instead.  However, I recently stumbled upon an example of how this can be achieved without resorting to additional output parameters.  It is actually quite simple as I expected.  All you need to do is enclose the body in parentheses and braces and ensure the last statement evaluates to the required value.  I thought I should share this just in case someone else is interested.  Here is a complete example written in C:

#include <stdio.h>

#define MAX(A,B)  ((A > B) ? A : B)

#define HIGHEST(ARRAY) \
({ \
    int i; \
    typeof (ARRAY[0]) ret = ARRAY[0]; \
    for (i = 1; \
         i < (sizeof(ARRAY) / (sizeof(typeof (ret)))); \
         i++) \
    { \
        ret = MAX(ret, ARRAY[i]); \
    } \
    ret; \
})

int main (void)
{
    int A[] = { 1, 2, 4, 3 };

    printf("%d\n", HIGHEST(A));
}

The output of this application is: 4

Note: This example was tested with gcc.

Wednesday, 5 January 2011

Moving from Java to C++

This appendix explains how to transfer your Java programming skills to a substantial subset of C++. This is necessary for students who take their first programming course in Java and the second course in C++. Naturally, it would be easiest if the second course were also offered in Java, but learning to move from one language to another is a fact of life for today's software professionals. Fortunately, C++ has many features in common with Java, and it is easy for a Java programmer to gain a working knowledge of C++. Nevertheless, C++ is a much more complex language than Java. This appendix does not attempt to cover all features of C++. But if you master all of the constructs described in this appendix, you will be able to use C++ effectively.

http://www.horstmann.com/ccj2/ccjapp3.html

Tuesday, 4 January 2011

C++ Unit Testing With Boost.Test

A short tutorial showing how to use the Boost unit test framework.

http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/

C++ Unit Testing Framework: A Boost Test Tutorial

So many C++ unit testing framework exist, so why Boost Test Library? The excellent but outdated article Exploring the C++ Unit Testing Framework Jungle showed a nice comparison. Since then, the Boost Test Library evolved a lot. Let's see if it improved.

http://www.beroux.com/english/articles/boost_unit_testing/

Wednesday, 29 December 2010

GNU coding standards

Coding standards for writing GNU and GNU style source code.  This is useful because the GNU standards are commonly used and understood by many.

http://www.gnu.org/prep/standards/

Autotools Tutorial

This is a very handy introduction to Autotools. The document itself is a PDF document available to download from this website:

http://www.lrde.epita.fr/~adl/autotools.html

Shared Libraries (UNIX based platforms)

Shared libraries are libraries that are loaded by programs when they start. When a shared library is installed properly, all programs that start afterwards automatically use the new shared library.

http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html

Static Libraries

Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ``.a'' suffix. This collection is created using the ar (archiver) program. Static libraries aren't used as often as they once were, because of the advantages of shared libraries (described below). Still, they're sometimes created, they existed first historically, and they're simpler to explain.

http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/static-libraries.html

Monday, 27 December 2010

Autotools Tutorial

Autotools are the set of GNU tools that configure their source packages for a particular computer system. If you have ever compiled a program using “configure” followed by “make” commands then chances are you have already used the output of Autotools. The purpose of these “configure” scripts is to create Makefiles and “config.h” files for your projects that point to libraries, define or undefine C macros, and make a range of other adjustments as needed to compile a program on a particular computer.

http://www.developingprogrammers.com/index.php/2006/01/05/autotools-tutorial/

Thursday, 23 December 2010

Shared memory: Where it belongs in the computer space

Shared memory technology is a part of a powerful IPC (interprocess communication) toolbox in UNIX-derived systems, which allows arbitrary processes to exchange data and synchronize execution. There are many forms of IPC on a UNIX-derived system (Several forms of IPC in the base UNIX toolbox are serial communication mechanisms. These linear forms have many uses, but I’ll be focusing on shared memory in this Daily Drill Down.)

http://articles.techrepublic.com.com/5100-10878_11-5033533.html

Wednesday, 22 December 2010

gcc attribute overview

List of attributes that can be used improve gcc optimisations.  The ones I find most useful are:

http://www.ohse.de/uwe/articles/gcc-attributes.html

Tuesday, 21 December 2010

IPC:Shared Memory

Shared Memory is an efficeint means of passing data between programs. One program will create a memory portion which other processes (if permitted) can access.
In the Solaris 2.x operating system, the most efficient way to implement shared memory applications is to rely on the mmap() function and on the system's native virtual memory facility. Solaris 2.x also supports System V shared memory, which is another way to let multiple processes attach a segment of physical memory to their virtual address spaces. When write access is allowed for more than one process, an outside protocol or mechanism such as a semaphore can be used to prevent inconsistencies and collisions.

http://www.cs.cf.ac.uk/Dave/C/node27.html

http://www.cs.cf.ac.uk/Dave/C/index.html

Saturday, 18 December 2010

Volatile: Almost Useless for Multi-Threaded Programming

"There is a widespread notion that the keyword volatile is good for multi-threaded programming. I've seen interfaces with volatile qualifiers justified as 'it might be used for multi-threaded programming'. I thought was useful until the last few weeks, when it finally dawned on me (or if you prefer, got through my thick head) that volatile is almost useless for multi-threaded programming. I'll explain here why you should scrub most of it from your multi-threaded code."

http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/

Sunday, 21 March 2010

A brief programming tutorial in C for raw sockets

In this tutorial, you'll learn the basics of using raw sockets in C, to insert any IP protocol based datagram into the network traffic. This is useful,for example, to build raw socket scanners like nmap, to spoof or to perform operations that need to send out raw sockets. Basically, you can send any packet at any time, whereas using the interface functions for your systems IP-stack (connect, write, bind, etc.) you have no direct control over the packets. This theoretically enables you to simulate the behavior of your OS's IP stack, and also to send stateless traffic (datagrams that don't belong to a valid connection). For this tutorial, all you need is a minimal knowledge of socket programming in C


http://mixter.void.ru/rawip.html

Wednesday, 23 December 2009

How to Add OEM Plug and Play Drivers to Windows XP

This article describes the steps required to add original equipment manufacturer (OEM)-supplied drivers to Microsoft Windows installations. This article includes only those drivers that are typically installed during graphical user interface (GUI)-mode Setup or post-Setup by standard Plug and Play enumeration. This permits you to pre-load OEM Plug and Play drivers that you can use later, when the associated hardware is introduced in the system.

http://support.microsoft.com/kb/314479

Monday, 30 November 2009

Mafia Wars Wiki

Mafia Wars is a popular Facebook application in which players start a mob family with their friends and run crime businesses. From time to time the developers add new weapons and features. Mafia Wars was developed by Zynga. On June 2008, Mafia Wars was officially released to the general public. 1 year later, Mafia Wars won the Webby award for People's Voice Winner. As of 2nd November 2009, there are more than 25 million users in Facebook and 12 million users in Myspace playing Mafia Wars.

http://mafiawars.wikia.com/wiki/Mafia_Wars_Wiki

Monday, 23 November 2009

Ubuntu Hack – How to Make Ubuntu run Super Fast!

For those of us who are anti-Windows and anti-Mac, we will benefit from learning how to make Ubuntu run super fast.

Here’s some great tips on minimizing processes to make your Ubuntu run faster than ever:

http://zedomax.com/blog/2008/09/29/ubuntu-hack-how-to-make-ubuntu-run-super-fast/

Thursday, 19 November 2009

Do Speedy Math in Your Head

Arthur Benjamin is a wizard at maths. Literally. At Hollywood's Magic Castle, the world-famous conjurer's club, he wows the crowd by multiplying big numbers — quick, what's 57,682 squared? — faster than you can use a calculator. Here he shares three cool tricks. (The answer, by the way: 3,327,213,124. See how easy it is?)

http://howto.wired.com/wiki/Do_Speedy_Math_in_Your_Head

SyncToy 2.1 Update Speeds Up Windows File Syncing

Windows: Microsoft's SyncToy is a simple but effective tool for mirroring folders across hard drive locations, networks, or USB devices. With a 2.1 update, it's been retooled to run faster, back up configurations, and handle errors much better.

http://lifehacker.com/5405755/synctoy-21-update-speeds-up-windows-file-syncing

How to Manage a Group Project in Google Wave

The mere promise of Google Wave inspired a rainbow of potential use cases, but Wave's best real-world use boils down to this: it helps a group get things done together. Here's how to manage a group project in Wave.

Note: If you haven't gotten your Wave invite yet, check out our invitation donation thread first (or, better yet, keep an eye out for the same thread this Friday). If you have gotten into Wave, search for title:"Invite others to Google Wave" to find the wave with your invites. Wave's only fun if your cohorts and workmates also have it, so give out your nominations to the people you want to wave with.

Wave's invitations have been rolling out steadily over the last few weeks, so you and your co-workers might have already gotten some Wave love. If so, let's take a look at how you can manage a project in the real world, even given Wave's current unfinished state.

http://lifehacker.com/5407183/how-to-manage-a-group-project-in-google-wave