Sunday, May 25, 2014

Client / Server Application and 32-bit v. 64-bit

I recently wrote a client / server application where I developed on one of my linux boxes that happened to be 32-bit.  Everything was functioning swimmingly until I moved it onto the destination platform which was x86_64.  I observed a segfault occur where I was getting the client IP and Port for logging purposes. I suspected exactly where the exception was occurring and stepped right to it using gdb.

It was a call to:

printf("connection from client: %s\n", (char *)inet_ntoa(cliaddr.sin_addr));

The error was occurring in libc (vsprintf).

I simply compiled with the -m32 flag and linked to the 32-bit libs.  Everything functioned properly when compiled as 32-bit.  So this is a good reminder from an article written by Michael Barr (http://embeddedgurus.com/embedded-systems-bloggers/michael-barr/) titled something like "Test Everything." It was primarily written for embedded systems, but as you can imagine how it can extend to virtually any aspect of engineering just by reading the title.

I'll modify the code to use some of the newer C networking methods.  Here's a really good reference (http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html) that I've referenced over the years.  Primarily the calls that accommodate IPv4 and IPv6.