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.

Uploading

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[] = {
    "152.18.69.32",
    "152.18.68.26",
    "255.255.255.255",
    "255.255.0.0",
    "0.0.0.0",
    "152.18.69",
    "152.18.69.300",
    "-152.18.69.40",
    "152.18.69.70",
    "152.18.69.0",
    "255.255.255.0"
};

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:

152.18.69.32    --> 2551334176 (98124520)
152.18.68.26    --> 2551333914 (9812441a)
255.255.255.255 --> 4294967295 (ffffffff)
255.255.0.0     --> 4294901760 (ffff0000)
0.0.0.0         -->          0 (00000000)
152.18.69       CAN'T PARSE
152.18.69.300   CAN'T PARSE
-152.18.69.40   CAN'T PARSE
152.18.69.70    --> 2551334214 (98124546)
152.18.69.0     --> 2551334144 (98124500)
255.255.255.0   --> 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
0.0.0.0         152.18.69.254   0.0.0.0         UG        0 0          0 eth0
152.18.69.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

The destination and mask together define a matching rule. Suppose the IP number of a running computer is 152.18.69.70 in dotted decimal and consequently 0x98124546 in raw bits. Also, assume netstat has a routing entry for destination 152.18.69.0 (0x98124500) with the mask 255.255.255.0 (0xffffff00). If you go into gdb and perform a bit-wise AND (&) of 0x98124546 (152.19.69.70) and 0xffffff00 (255.255.255.0), you will get 0x98124500 (152.19.69.0), which matches a route entry’s getway. That’s how the operating system knows that packets to 152.19.69.70 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, 152.18.69.0 with gateway 255.255.255 — usually written as 152.18.69.0/24.

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:
    progname
    progname 192.168.0.0 255.255.0.0

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.