CSCI 235 — C practice

In this lab you create a little C application which has a some resemblance to a netorking routing table.

All your code will be writtin in one file. You can write your code from the comand line using nano, gcc and possibly gdb, or you can use netbeans.


This will be hard if you haven’t had much experience with C or formatter I/O. Feel free to consult with others. When the lab time is over, upload your efforts to the lab moodle page. I will use that to plan what we’ll do at the next lab.

Getting started

Start with this main.c file.

 CSCI 235 lab

#include <stdio.h>
#include <stdlib.h>

// Return 0 is ipName is in correct format; return non-zero otherwise
int name2num(const char *ipName, unsigned int* ipNumber) {
    *ipNumber = 0 ;
    return 1 ;

const char *testCase[] = {

int main(int argc, char** argv) {
    int i ;
    for (i=0; i<sizeof(testCase)/sizeof(testCase[0]); ++i) {
        unsigned int ipNum ;
        int rc = name2num(testCase[i], &ipNum) ;
        if (!rc) {
	    fprintf(stdout, "%-15s --> %10u (%08x)\n",
		    testCase[i], ipNum, ipNum) ;
        } else {
            fprintf(stdout, "%-15s CAN'T PARSE\n", testCase[i]) ;
    return (EXIT_SUCCESS);

IPv4 addresses

Take a took at the Wikipedia article on IPv4 addresses to make sure you are familiar with dot decimal notatation. Also try out the commands ifconfig, netstat -nr, and arp -vn to see some dotted decimals.

Task one: Parsing IPv4 addresses

Your first task is to complete the name2num routine. If you succeed the output of running your problem should look like the following:    --> 2551334176 (98124520)    --> 2551333914 (9812441a) --> 4294967295 (ffffffff)     --> 4294901760 (ffff0000)         -->          0 (00000000)
152.18.69       CAN'T PARSE   CAN'T PARSE
-   CAN'T PARSE    --> 2551334214 (98124546)     --> 2551334144 (98124500)   --> 4294967040 (ffffff00)

What you will need

You need to use the routine sscanf. Java doesn’t have anything like sscanf. Start with an internet search.

Small task two: Routing addresses

The output of netstat -nr contains the following lines:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface         UG        0 0          0 eth0   U         0 0          0 eth0

The destination and mask together define a matching rule. Suppose the IP number of a running computer is in dotted decimal and consequently 0x98124546 in raw bits. Also, assume netstat has a routing entry for destination (0x98124500) with the mask (0xffffff00). If you go into gdb and perform a bit-wise AND (&) of 0x98124546 ( and 0xffffff00 (, you will get 0x98124500 (, which matches a route entry’s getway. That’s how the operating system knows that packets to should be sent directly through the Ethernet interface eth0.

See the Wikipedia article on routing tables for more information and try out the following gdb command to verify my calculation.
    printf "%x",0x98124546 & 0xffffff00

The task

Modify your program to write LOCAL after each IP number that is local to the computer science VLAN, with gateway 255.255.255 — usually written as

Small task three: Reading command line arguments

Modify your program so that, if it is started with two arguments, one for the network destintion and the other for the network mas, it will use use the first argument for the gateway and the second for this task. This means that your program could be started with either of the following commands:

Small task four: Writing to a file

Modify your program so that it writes its output to the file /var/tmp/output.txt. This is really a very small change: It involves adding one statement (calling fopen) and modifying two others.