This is going to be a super short and super simple tutorial for beginners about LD_PRELOAD. If you're familiar with LD_PRELOAD, you'll learn nothing new. Otherwise keep reading!

Did you know you could override the C standard library's functions (such as printf, fopen, etc) with your own version of these functions in any program? In this article I'll teach you how this can be done through the LD_PRELOAD environment variable.

Let's start with a simple C program (prog.c):

#include <stdio.h>

int main(void) {
    printf("Calling the fopen() function...\n");

    FILE *fd = fopen("test.txt","r");
    if (!fd) {
        printf("fopen() returned NULL\n");
        return 1;

    printf("fopen() succeeded\n");

    return 0;

The code above simply makes a call to the standard fopen function and then checks its return value. Now, let's compile and execute it:

$ ls
prog.c  test.txt

$ gcc prog.c -o prog

$ ls
prog  prog.c  test.txt

$ ./prog
Calling the fopen() function...
fopen() succeeded

Now let's write our own version of fopen and compile it as a shared library:

#include <stdio.h>

FILE *fopen(const char *path, const char *mode) {
    printf("Always failing fopen\n");
    return NULL;

Let's call this file myfopen.c, and let's compile it as a shared library:

gcc -Wall -fPIC -shared -o myfopen.c

Now we can simply modify LD_PRELOAD:

$ LD_PRELOAD=./ ./prog
Calling the fopen() function...
Always failing fopen
fopen() returned NULL

As you can see the fopen got replaced with our own version that is always failing. This is really handy if you've to debug or replace certain parts of libc or any other shared library.

Next time I'll write about how the LD_PRELOAD works internally.

We all know the regular expression character classes, right? There are 12 standard classes:

[:alnum:]  [:digit:]  [:punct:]
[:alpha:]  [:graph:]  [:space:]
[:blank:]  [:lower:]  [:upper:]
[:cntrl:]  [:print:]  [:xdigit:]

But have you seen a visual representation of what these classes match? Probably not. Therefore I created a visualization that illustrates which part of the ASCII set each character class matches. Call it a cheat sheet if you like:

small version, large version

A bunch of programs that I used

Just for my own reference, in case I ever need them again, here are the one-liners I used to create this cheat sheet:

perl -nle 'printf "%08b - %08b\n", map { hex "0x".(split / /)[0], hex "0x".(split / /)[1] } $_ '
perl -nle 'printf "%03o - %03o\n", map { (split / /)[0], (split / /)[1] } $_'

And I used this perl program to generate and check the red/green matches:

use warnings;
use strict;

my $red = "\e[31m";
my $green = "\e[32m";
my $clear = "\e[0m";

my ($start, $end) = @ARGV;

die 'start or end not given' unless defined $start && defined $end;

my @classes = qw/alnum alpha blank cntrl digit graph lower print punct space upper xdigit/;

for (map { chr } $start..$end) {
    for my $class (@classes) {
        print "${green}1${clear}" if /[[:$class:]]/;
        print "${red}0${clear}" unless /[[:$class:]]/;
    print "\n"


I was inspired to create this visualization when I saw a similar table for C's ctype.h character classification functions.

Misc 22 Comments February 24, 2013

TCP Traceroute

Did you know you could traceroute over the TCP protocol?

The regular traceroute usually uses either ICMP or UDP protocols. Unfortunately firewalls and routers often block the ICMP protocol completely or disallow the ICMP echo requests (ping requests), and/or block various UDP ports.

However you'd rarely have firewalls and routers drop TCP protocol on port 80 because it's the web's port.

Check this out. Let's try to traceroute using ICMP protocol:

# traceroute -I  
traceroute to (, 30 hops max, 60 byte packets
 1 (  0.552 ms  0.647 ms  0.742 ms
 2 (  0.415 ms  0.555 ms  0.653 ms
 3 (  0.707 ms  0.873 ms  0.984 ms
 4 (  1.345 ms  1.341 ms  1.337 ms
 5  * * *
 6 (  3.614 ms  3.747 ms  3.244 ms
 7 (  3.319 ms  4.019 ms  4.010 ms
 8 (  53.543 ms  53.105 ms  53.074 ms
 9 (  52.942 ms  52.710 ms  52.670 ms
10  * * *
11  * * *
12  * * *
13  * * *

We get lots of * * * and we've no idea how the packets reach

Now let's try UDP traceroute:

# traceroute -U
traceroute to (, 30 hops max, 60 byte packets
 1 (  0.529 ms  0.599 ms  0.662 ms
 2 (  0.480 ms  0.571 ms  0.658 ms
 3 (  0.507 ms (  0.463 ms  0.569 ms
 4 (  1.345 ms  1.322 ms  1.290 ms
 5  * * *
 6  * (  2.697 ms *
 7 (  3.665 ms (  53.363 ms  52.597 ms
 8 (  52.284 ms  52.643 ms (  52.665 ms
 9  * * *
10  * * *
11  * * *
12  * * *
13  * * *

Same. Finally let's try traceroute over TCP protocol port 80:

# traceroute -T -p 80
traceroute to (, 30 hops max, 60 byte packets
 1 (  0.540 ms  0.629 ms  0.709 ms
 2 (  0.486 ms  0.604 ms  0.691 ms
 3 (  0.511 ms (  0.564 ms  0.810 ms
 4 (  1.339 ms  1.310 ms (  1.307 ms
 5 (  3.619 ms  2.560 ms  2.528 ms
 6  * (  3.640 ms *
 7 (  52.523 ms (  3.825 ms (  3.355 ms
 8 (  61.042 ms  61.032 ms  60.457 ms
 9  * * (  100.069 ms
10 (  53.868 ms  53.038 ms  52.097 ms

A full network path to!

There are various different traceroute implementations and if your system doesn't have one that supports tcp protocol, I suggest you either get the new modern implementation of traceroute, or get the tcptraceroute by Michael Toren.

Just a quick update on Browserling. We just launched IE 10s! IE 10s are available to all our paying customers. The free browserling version offers IE 9.

Internet Explorer 10.0.9200.16466

Next we're getting Mac OSX browsers and iPhone emulators. Stay tuned! Follow us on twitter for updates - @browserling, @testling, @substack, @pkrumins.

Try Browserling!

Browserling is an interactive cross-browser testing tool. It lets you test your website in older Internet Explorers such as IE 6, IE 7, IE 8 and IE 9, IE 10, Firefox, Chrome, Opera and Safari!

I thought I'd share this idea I've had for a while and that I want to build in the future. It's a 1:1 teaching network where people meet up and have 1:1 sessions teaching each other things they don't know. I've found that 1:1 teaching sessions are much more effective than a group teaching session or learning on your own. In a 1:1 teaching session you can keep asking the other person questions instantly and infinitely and you can learn really advanced topics really quickly.

Here's an example of how this meetup network would work. Let's say I'm an expert in Python. And let's say you're an expert in Ruby. And let's say you want to master Python and I want to master Ruby. So we schedule a meet up and I teach you Python for a few hours, and you teach me Ruby for a few hours. And we rescheduling meetups whenever we're both available.

The way I imagine it could be built is having a website where you indicate where you're at, what you're a master at, indicate what you want to master, and indicate the time that you've available. The system then matches you with someone and suggests a meetup.

I'd teach and learn and teach and learn forever this way!

There are so many other ideas that I want to build and I'll just keep sharing them in my "ideas for later" blog post series! See ya!