Patrick’s development blog

become anonymous on the internet using Tor

Posted in Articles, Security by Patrick on July 23, 2008

Tor is a platform independent program that protects you from traffic analysis. Traffic analysis is a form of surveillance of your network traffic which is a threat to your personal integrity. Tor is usually used to surf anonymously, but can also be used with for example instant-messaging applications.

How Tor works
When using Tor, your communication with the internet is protected by distributing it trough a network of different relays trough the world which are run by volunteers. The communication is also encrypted so no one can see what you’re doing or learn your location.

I recommend using the Firefox add-on Torbutton which provides an easy way to disable/enable Tor in Firefox. This way, you don’t have to configure your browser either.

Pitfalls with Tor
Watch out for cookies, flash files, java applets and similiar web applications. They can reveal your IP-address even if you use Tor. Using Add-ons for Mozilla Firefox like No Script and Flash Block can prevent this. Be sure that you don’t fall for things like this.

Even if Tor encrypts your traffic inside the Tor-network and makes you anonymous. The last relay you are connected to, which is directly communication with the webserver can still see your traffic. Don’t use Tor if you do things that can expose your identity, for example logging in to your webmail (if you truly want to be anonymous that is). Using an encrypted protocol like HTTPS prevents this though.

Read more about potential pitfalls here:

To sum it up
If you want to surf anonymously, this is a very useful program as it also hides your IP-address. But remember that web apps like Flash can still expose your IP. A lot of people believes that installing Tor will automagically make them anonymous. That’s wrong however. You have to configure the application correctly you want to use with Tor. If you’re using Firefox, the Torbutton add-on makes this automatically.

A good idea might be to install a portable browser which you use only when you want to be anonymous. Configure this browser so it doesn’t accept cookies and install a flash blocker, block java and other “media” that could expose your IP-address. It’s recommended to only use services like webmail if the site uses SSL or another secure connection.

There is a portable version of Firefox called Firefoxportable.
Tor’s official website:

Recover lost partitions with Testdisk

Posted in Linux/GNU by Patrick on July 21, 2008

By mistake, I deleted some of my partitions and totally messed up my partition table a couple of days ago, removing the possibility of booting up my computer at all.  Luckily, I found a open source program called Testdisk.

Testdisk is a free data recovery software that can recover lost partitions and make partitions bootable again. Here’s how I recovered my Windows XP partition using a Ubuntu live-CD.

First, I enabled all uncommented repositories in /etc/apt/sources.list.

sudo su
nano /etc/apt/sources.list

Then I updated the package repository list and installed Testdisk:

apt-get update
apt-get install testdisk

When it’s installed, you can start it by typing its name in the terminal. You’ll be asked to create a log file (optional). After that, choose analyze and Intel/PC partition. Your partition table will be shown, wait until it’s analyzed. After the analyze is done, you can also choose to make a deep scan which is recommended. If you decide to recover a partition, just click the right arrow button so the current selection turns green and continue by pressing Enter. You’ll be asked to write this partition on the partition table. Do this.

That’s all, that worked for me at least. I only have some weird problems with my boot manager right now, but the partition is fully bootable.

Useful free software to optimize and secure your system

Posted in Security by Patrick on June 28, 2008

Here’s a list of useful programs in different categories. Most of these programs are for Windows, but some of them are also open source and works for Linux. I only list programs that are free. I might update this post over time.


Mozilla Firefox
The web-browser Firefox, has optional add-ons that you can install which makes it possible to extends the browsers functionality. I’ve picked out three add-ons that I find very useful. They speed up the browser and adds more security.

It blocks all Javascript, Java and other malicious code from sites you don’t trust. Ever since I installed this, i’ve never had any spywares on my XP computer at all. A most have for people who wants a secure browser. My favorite add-on.

AdBlock Plus
Get rid of all those annoying banners and ads on websites.

Flash Block
Blocks all flash animations on all websites. If you want to play a certain flash animation though, just click on it, that’s what I like most about this add-on.


Zonealarm Firewall – A free version of the popular firewall Zonealarm. This software also has program control and port stealthing for example.

AVG Anti-Virus Free Edition – Free anti-virus program, that is easy to use, provides high level of detection capability and doesn’t use so much system resources.

Ad-Aware 2008 – Scan for spywares, data-mining, advertising, and tracking components in general.

Spybot Search & Destroy – Spyware cleaner that also has real time protection.

TrueCrypt – Open Source encryption software. Can encrypt partitions, make virtual drives and encrypt harddrives completely.


Optimize and cleaning software
JkDefrag – Free disk defragmenter program. Optimizes your harddrive.

CCleaner – System optimization tool which cleans registry and removes temporary files. It’s very fast.

How to make a simple window in SDL

Posted in Articles, SDL / OpenGL by Patrick on June 25, 2008

Creating windows in SDL is dead easy and doesn’t involve so much code as the Win32 API does for example. SDL is a popular library for game development and it’s cross platform too, so it works on Linux as well.

Install SDL for Windows
1. Download the latest version of SDL from their site
2. Choose the file
3. Move all include files from the .zip file you downloaded, into the corresponding include folder in your IDE/compilator. Do the same with the files from the /lib folder.
4. In order to be able to open SDL apps without distributing the .dll files with each application. Move the SDL.dll file into the windows/ folder. When you test your application on another computer, you have to distribute the dll files however.
5. Add the SDL lib files into your linker options. In Visual C++ Express this is done by going into Project -> Project Properties -> Linker -> Input. Type the following into the Additional Dependencies field: SDL.lib SDLmain.lib

Install SDL for GNU/Linux
Use your package manager and install the development libraries for SDL. For Debian based systems, i’ve posted an article about how to install SDL here.

Install SDL for Mac OS X
There are development libraries on SDL’s homepage for Mac OS X as well. There are so many different IDE’s for Mac as well, so i’ll not go into that in this article. There are many great tutorials about this already.

Create a window in SDL

#include “SDL.h”

SDL_Surface *screen;

SDL_Event event;

int main(int argc, char *argv[]) {

screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);

SDL_WM_SetCaption(“Simple Window”, “Simple Window”);

bool done=false;

while(!done) {
while(SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {

// fill the screen with black color
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0));

// update the screen buffer


return 0;

First I include SDL and create a surface called screen. This surface is going to represent the window, it’s called a display surface. A surface in SDL is a rectangular area which contains pixels. Surfaces are also used to represent images, text and pixels in general.

The main function in a SDL program most have the arguments int argc, char *argv[] or int argc, char **argv (technically the same) in order to work.

The initialization of the window is done by the following lines:

screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);

SDL_Init initializes SDL and should be called first. The second line sets the video mode to a specified width and height (640×480). The third parameter of SDL_SetVideoMode represents bits per pixels and the fourth parameter is a flag used to specify some settings for the video mode and how it is going to be created. Here’s a list of all the flags that can be used as the fourth parameter. It’s also possible to combine flags with each other.

Flags that can be used in SDL_SetVideoMode(..)
SDL_SWSURFACE – creates the video surface in the system memory
SDL_HWSURFACE – creates the video surface in the video memory
SDL_ASYNCBLIT – enable asynchronous display mode, may speed up on some cpu’s
SDL_ANYFORMAT – if requested bpp value cannot be used, SDL generates an appropriate video display
SDL_HWPALETTE – this flag gives SDL some kind of palette access
SDL_DOUBLEBUF – enable double buffering, most be used with the HWSURFACE flag
SDL_FULLSCREEN – fullscreen mode will be used if possible
SDL_OPENGL – creates a opengl rendering context
SDL_OPENGL – same as above but it uses normal blitting operations (blitting = showing surfaces)
SDL_RESIZABLE – makes the window resizable
SDL_NOFRAME – creates a window without a frame or titlebar if possible

Read more about SDL_SetVideoMode at the documentation.


Set window title
The next line after SDL_SetVideoMode is the setcaption line. This is straightforward as it is. It sets the window title. This is optional, but simple enough so it would be a waste not to use it.

Main loop
The first while loop contains an expression which uses a boolean variable (done). In this program, this variable is set to true when we want to close the program. This while loop is often called the main loop. It’s in the main loop where all the processing is done, that is, drawing, moving things, updating the screen, etc. You can say this is where all the program code is supposed to be, except the initialization code.

Event loop
At the start of the game loop, a new while loop is located. This is the event loop, where all events are processed in the program. Events are messages that is sent to the program whenever the user do something, for example moves the mouse, clicks a button or closes the window. It’s up to us which events we want to handle. In this example, the only event that’s being checked is the SDL_QUIT event. This event is sent to the program when the user clicks on the close button in the top right corner of the application. If we left this event out, we would have to kill the application in order to close it, the close button wouldn’t work.

Fill the background color and update the screen
After the event loop, there are two lines. The first one fills the display surface (screen) with a black color. The second line updates the screen buffer, we have to call this at the end of the main loop, to make sure that the display surface is updated. Well, the SDL_FillRect function call isn’t really necessary in this program since we doesn’t draw anything, not the SDL_Flip() call either. The background color of the application is by default black. However, don’t forget to fill the background or add a background image, if you draw something on the screen though.

Sending mail via SMTP in Python

Posted in Python by Patrick on June 22, 2008

SMTP (Simple Mail Transfer Protocol) is the most common protocol for sending mail. The communication in SMTP is done using TCP.
Read more about the SMTP protocol here: RFC821

Here’s an example of a simple Python program that sends a mail.

Send mail in Python using SMTP

import smtplib

addr_from = “from@mail.domain”
addr_to = “to@mail.domain”


msg = (“From: %s\r\nTo: %s\r\n\r\n”
% (addr_from, “, “.join(addr_to)))

msg = msg + “This is the message”

server.sendmail(addr_from, addr_to, msg)


‘smtp.server.domain’ specifies the SMTP server the program is going to use. You can experiment with different free SMTP servers until you find something that works. Personally, I use my ISP SMTP server.

smtplib is the module I used in order to use SMTP in Python. Information about smtplib can be found in the official Python documentation here.

Using SDL in Debian Linux

Posted in Linux/GNU, SDL / OpenGL by Patrick on June 10, 2008

Update the repository list and install SDL using apt.

apt-get update && apt-get install libsdl1.2-dev

For other sub libraries (optional) I used the command “apt-cache search sdl” to get the name of the rest of the dev packages. For example libsdlimage-dev or libsdlnet-dev.

Compile SDL application in g++
When I compiled my SDL_net application in Linux, this worked like a charm.

g++ -o appname file.cpp -lSDLmain -lSDL_net

Simple Ubuntu speed-up tweak

Posted in Linux/GNU by Patrick on April 25, 2008

I recently searched around for some Ubuntu speed-up tips in order to speed up my Ubuntu computer. I found an interesting thing, it’s considered a bug that slows down the application start-up time and performance considerably.

The tweak/bug

Open the /etc/hosts file as root using any texteditor and replace the following line: localhost

With the name of your computer: localhost computername

Read more about the bug at this site:

Day 2: Handle code using classes

Posted in Uncategorized by Patrick on April 16, 2008

I’m currently trying to create a thread for the client that receives messages from the server. These messages are later going to be sent to all the clients currently connected to the server, but for now it only sends to one client. The client class is almost finished, there are only some small details left. I haven’t done much on the server part of the class yet, i’ll do that next time I work on this project.

This is how my client looks like right now.


#include “client.h”

SDL_Surface *screen = NULL;
SDL_Event event;

int main(int argc, char* args[]) {
if (SDLNet_Init() < 0) {
std::cout << SDLNet_GetError();

std::cout << “SDL_net was initialized sucessfully!” << std::endl;

IPaddress ip;
TCPsocket tcpsock;
const int PORT = 2000;
char buffer[512];

if (SDLNet_ResolveHost(&ip, “localhost”, PORT) < 0) {
std::cout << “SDLNet_ResolveHost: ” << SDLNet_GetError();

std::cout << “Resolved host at port ” << PORT << std::endl;

tcpsock = SDLNet_TCP_Open(&ip);
if (!tcpsock) {
std::cout << “SDLNet_TCP_Open: ” << SDLNet_GetError();

std::cout << “Connected to server using a TCP-socket on port ” << PORT << std::endl;

bool done = false;

Client client_h(tcpsock); // this class is used to receive messages from the server sort of

while (!done) {
std::cout <“;
std::cin.getline(buffer, 512);

if (SDLNet_TCP_Send(tcpsock, static_cast(buffer), 512) < 512) {
std::cerr << “SDLNet_TCP_Send: ” << SDLNet_GetError();
return 0;


return 0;

I’ll write about SDL_Net in more detail another time. Right now i’m fully occupied with this project. Things are going more smootly now though.

Chat App Day 1: SDLNet and networking

Posted in Uncategorized by Patrick on April 15, 2008

I started coding on my network application again this week. Things have gone very slow, I was getting confused with the library I used so I decided today that I would go back to the first library I had in mind and make a fresh start. I’m using the cross-platform library SDL_Net which is a part of my favourite library SDL (

About four or five weeks ago when I attempted to make the very first part of my chat application in SDL, I didn’t have any idea at all how I would build it. After that i’ve gone trough many different libraries, trying to find the one which best suited my needs. This time, i’ve gotten some information about networked applications in general after all this testing and to my own surprise I managed to build a simple application where you can send messages to a server with many clients, that are handled using threads.

There is still a long way to go, but I sort of know what to implement now and most importantly HOW to implement it. I’m going to make the basic application first and after that make it object-oriented. I’m glad that I managed in one hour, what I couldn’t in 5-7 hours before, i’ve learned a lot : )

I’m going to make a post tomorrow with the code I got so far. I made a fresh start with this project, so I won’t post my old code however. I’m going to restart this project after all.

Win32 API – Creating a window

Posted in Uncategorized by Patrick on March 29, 2008

Thing aren’t exactly going fast forward with this project or learning about the Win32 API, due to my lack of motivation of reading stuff. I’ve managed to take my time to finish the application window for today at least. This is the complete code.

Create an application window

#include <windows.h>


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName[] = TEXT("Simple Window");
HWND hwnd;
MSG msg;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = szAppName;

if (!RegisterClass(&wndclass)) {
    MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
    return 0;

hwnd = CreateWindow(szAppName,
TEXT("Simple Application Window"),

ShowWindow(hwnd, iCmdShow);

while (GetMessage(&msg, NULL, 0, 0)) {

return msg.wParam;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
HDC hdc;
RECT rect;

switch (message) {
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);

GetClientRect(hwnd, &rect);

DrawText(hdc, TEXT("Hello world!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

EndPaint(hwnd, &ps);

return 0;

return 0;

return DefWindowProc(hwnd, message, wParam, lParam);


It’s a lot of code to explain, so i’ll try to cover the most relevant parts of the code in this post.

The window class
All kinds of windows and “controls” like buttons and check boxes are based on a certain window class. In my program above, this class is instantiated to use for the window. The window procedure that processes messages to the window is identified by the window class.

In my program, you can see that WNDCLASS wndclass; is definied in the WinMain function. This class/structure has to be registered using a call to RegisterClass after all fields of the structure are initialized.

Window class definition

WNDCLASS wndclass; = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; definies how the window should be created. The flags I specified in this example makes sure the program repaints every time the window is resized either horizontal or vertical. (CS_HREDRAW | CS_VREDRAW)

wndclass.lpfnWndProc This field sets the window procedure for this class (WndProc). The window procedure is the functon that handles all the messages. This window procedure will now process messages to all windows based on this class.

wndclass.cbClsExtra = 0, wndclass.cbWndExtra = 0
These fields are used internally to maintain some space, I have no idea what. All I know is that this program doesn’t use this extra space.

wndclass.hInstance = hInstance this line sets the instance of the program. It’s the same as the parameter.

wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION)
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW)
These lines sets the icon and cursor type. Since I call these functions with the first parameter as NULL, a predetermined icon will be choosed.

wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
The background of the application will become white.

wndclass.lpszMenuName = NULL
I don’t use a menu in this program so I set this value to NULL. I’ll most likely become familiar with this later though, let’s save it for that time.

Last but not least, the application name. In this case, the name is of the type TCHAR.

This instance has to be registered as well.

Register the window class

if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("Show some error message"), szAppName, MB_ICONERROR); return 0; }

After we’ve registered the window class, the actual window creation code comes into play. HWND hwnd definied at the top of the “main” function is a handle to the window. The created window will be saved into this variable.

Create the actual window

hwnd = CreateWindow(szAppName, TEXT("Simple Application Window"), WS_OVERLAPPEDWINDOW, 200, 250, 640, 480, NULL, NULL, hInstance, NULL);

The above example will simply create a window with a size of 640×480 pixels at the start position (200,250).

Right now, the window has only been created internally in Windows. In order to make the window appear on the video display, it’s necessary to make a call to ShowWindow and UpdateWindow.

Display window

ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd);

Like usual, it’s nothing really that’s necessary to memorize. It’s good to know though, that hwnd is the window handler we just created using CreateWindow and iCmdShow is used to keep track of the current state of the window (maximized, minimized, etc). ShowWindow shows the window on the screen, it sends the WM_PAINT message to the window procedure.

The last part of the application that makes it complete, is the event handling. Before going on to the window procedure, a while loop that retrieves all the messages from the message queue is necessary.

Retrieve all messages

while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }

Where msg is a part of the MSG structure and the NULL parameters indicate that we want to retrieve ALL messages for this program. The DispatchMessage() function call sends the message to the window procedure. It’s the windows procedure that takes care of the actual event checking.

The window procedure

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, TEXT("Hello world!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }

This is where we take care of the input and what actions that we take depending on which message we get. There isn’t much to explain about this code without going into too much background. The last return statement that returns the DefWindowProc(…) call, is the default processing of all messages that the window procedure doesn’t process. It’s important to remember.

The WM_PAINT code block is executed whenever the window is updated. In other words, it’s the code that displays the text on the screen. I won’t go into detail how it draws the text right now, because I honestly have no idea right now and I want this post to end.

Tagged with: , , , , , ,