Category Archives: programming

P2PVPN 0.7

Anyone looking for a piece  of software similar to Hamachi, but open source free software – take a look at P2PVPN. It uses torrent trackers for finding nodes, it works on GNU/Linux and Windows and individual nodes do the forwarding. (This also means at least one node in network needs to be able to accept connections but that can probably be arranged.)

Now, since this is a blog with programmer’s thoughts, I’ve taken special interest in this program’s source code; especially in how it manages to use OpenVPN‘s TUN/TAP driver under Windows. You see, TUN/TAP devices under GNU/Linux reveal themselves to be just simple named file descriptors you can write into and read from. But how to do it under Windows?  This seemed like it might be another one of those things where you can venture around the net for days looking for an answer, but luckily, there was no need for that.

P2PVPN’s got two versions of its low level code for accessing TUN/TAP devices, one for GNU/Linux and one for Windows. They are written in C and are Java wrappers for accessing the devices. Windows version has 280 lines of code excluding header, and GNU/Linux version has 111 lines excluding header. They are a must-read tutorial for anyone wanting to write some code that has to do with virtual network devices and VPN. P2PVPN’s licensed under GPLv3, and the TUN/TAP Java wrapper code is licensed under LGPLv3.

Wolfgang Ginolas did some great work here. Anyone that wants to try something new aside from Hamachi, or wants to write their own VPN software, should take a look at P2PVPN.

Iron Roses released!


Iron Roses, the adventure game by Cateia Games that marked the last few months of my work-life, has went through the QA, and was released by Sandlot Games today! Yip-yip hurray!

Iron Roses, aside from great work by our own musicians, includes nice tunes from Megasapien.
Check out the game, and go buy it! 🙂

Fixing "warning: missing sentinel in function call"

I got it for the folowing line:

execl("/bin/sh", "sh", "-c", sessioncommand.c_str(), 0);

Quite easy to fix:

execl("/bin/sh", "sh", "-c", sessioncommand.c_str(), (char*)0);

—————————————–

Alright. Since I’m getting “lots” of hits on this, let’s see if we can improve this post. (June 28th 2010, more examples added July 22nd 2010, style but not content updated April 19 2014)

Generally, you’re missing a sentinel if you don’t add a “NULL” in C/C++, or a “nil” in Objective-C. In above example in C, I presume the compiler did not notice the zero (a valid sentinel) and threw a warning. That’s what this post was mostly about.

However, it could also happen if you really did forget to add the sentinel. What is the purpose of this “sentinel”? When iterating through a varlist**, the function needs to know where to stop. In printf() and scanf(), the format string specifies this number of arguments. Functions could also accept the number of arguments as one of the arguments. Third option is this — specifying a sentinel, such as NULL or nil, as something that will stop further iteration.

Examples of sentinels in C:

execl("/bin/sh", "sh", "-c", sessioncommand.c_str(), NULL);

and in Objective-C:

NSArray *names = [NSArray arrayWithObjects: @"Ivan", @"Ana", @"Marko", @"Petar", nil];

** Iterating through a varlist – which is how you iterate through arguments in variadic functions, those with variable number of arguments

close() of listening socket from another thread

Just a warning.

Calling close() on listening socket in another thread will NOT prevent another connection from being made. Instead, you must call shutdown(). This should abort existing select() calls (probably accept() too, but I didn’t try).

Guess how I found out 🙂

Great new GCC option – Effective-C++ Warnings

Did you know about -Weffc++ in GCC? Neither did I, until I upgraded to new Code::Blocks from Jens’ unofficial Debian repository for Code::Blocks.

Here’s a few sample warnings, with sample lines and commentary:

  • /home/ivucica/Development/project/src/types.h|12|warning: ‘operator=’ should return a reference to ‘*this’|

Sample:
Position &operator=(const Position& other) { x=other.x; y=other.y; z=other.z; return *this;}

What’s the big deal here? By leaving out the ampersand in this operator overloading, I accidentally returned a copy of entire Position class. (On the other hand, I had no reason to actually overload this operator so perhaps real fix is to erase entire line. Still…)

  • /home/ivucica/Development/project/src/mat.h|10|warning: ‘class Mat’ has pointer data members|
    /home/ivucica/Development/project/src/mat.h|10|warning: but does not override ‘Mat(const Mat&)’|
    /home/ivucica/Development/project/src/mat.h|10|warning: or ‘operator=(const Mat&)’|

No sample needed.

What’s the problem here? Well, we have a Mat class containing pointer. So what happens when someone does this: Mat x = y; or this: Mat x(y);? We have another instance of a class with copies of all the data, except the data pointed to by the pointer. Instead the pointer itself gets copied.

So if we keep a C-style string in there, we’re not really copying the string … we’re copying the pointer to it, and modifying the string in the new instance still modifies the string in the original ocpy.

While not applicable everywhere, still a good and useful warning.

  • /home/ivucica/Development/project/src/obj.h||In constructor ‘Obj::Obj()’:|
    /home/ivucica/Development/project/src/obj.h|15|warning: ‘Obj::m_radius’ should be initialized in the member initialization list|

Simple. Instead of initializing (or forgetting to initialize, or intentionally doing so) a variable in the constructor:
Obj() {m_classname=”Obj”; m_usecount = 0; }
why not do it in a “safe” and readable way (and perhaps more optimal?)
Obj() : m_classname(“Obj”), m_usecount(0) { }

While these warnings are certainly not the Universal Elixir to cure all your troubles, they will certainly prove useful in furthering your 1337 skills.