Once upon a time there was a hacker called John. He ran many websites in various web servers. The web servers were located in Canada. They were directly connected to the Internet. There were no proxy servers or firewalls between the servers and the Internet.

The websites were very popular and thousands of users from all over the world visited his websites everyday. From a glance at the web servers' access logs, he knew that his servers received most of the hits from China. One day he wanted to create a report of the usage of his various web servers. The old fashioned John decided to write his own tool to do this.

He wrote a C program to process logs from these web servers. The tool would pull down the access logs from each web server via SCP and process them to create reports. For every valid request found in the log, a certain function called add_to_statistics was called. The file containing the definition of the function began in this manner:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void add_to_statistics(struct in_addr *client_addr,
                       struct in_addr *server_addr) {
    char *client_ip = inet_ntoa(*client_addr);
    char *server_ip = inet_ntoa(*server_addr);
After those two lines in this function, the struct in_addr objects were not used at all. Later in the code the client IP address in client_ip was used to find the geographical location of the client from a database that mapped IP address ranges to countries. Finally, the code plotted a pie chart for each web server. The chart showed how many hits a server had received from each country. The larger the number of hits from a particular country to a server was, the larger was the slice for that country in the pie chart for the web server.

The colour to be used to fill the pie for each country was stored in a property file which began like this:

Canada: rgb(0, 0, 255)
India: rgb(0, 255, 0)
US: rgb(255, 0, 0)
China: rgb(255, 255, 0)
Which color was used most in the pie charts generated by the tool? Why?

Update: Prunthaban has answered the question correctly. The answer is: blue. In each pie chart Canada occupied the whole pie and hence blue was used to fill the entire pie chart for each web server. From the man page of inet_ntoa:

The inet_ntoa() function converts the Internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation. The string is returned in a statically allocated buffer, which subsequent calls will overwrite.

1 comment

Prunthaban said:

The pie chart will be fully blue! inet_ntoa reuses the char array it returns. It is the programmer's duty to copy it. So the second call to it with server_addr will replace the string pointed to by 'client_ip' to the server IP. Since the server is in Canada, the chart will be fully blue :-)

Post a comment