Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ function (not program) to compute forwarding of a packet in an IP ro

ID: 3697923 • Letter: W

Question

Write a C++ function (not program) to compute forwarding of a packet in an IP router.

The function should be named route and has three input paramaters:

1) an IP address (a 32 bit integer)

2) a table with three integers per entry:

1) an IP address

2) a subnet mask

3) the output line to be used

3) the number of entries in the table

the function should return the output line to be used vi given the input IP address

you are expected to write your own test programs

The filename of your function MUST BE {your last name}_router.cc

My test function is available as test_route.cc

Explanation / Answer

Explanation :-

------------------------------

         Go through

                   https://www.lri.fr/~fmartignon/documenti/reseaux/3-RoutingForwarding-Martignon.pdf

                Network(IP Address)    || Sub net mask || Output line (Ip Address, next node)

               a) Divide Ip address in integer to 4 bytes chunks (function "print_ip" in our program do this)

               b) Loop through complete page table entries

                     b.1) For each entry , Divide Network(IP Address) and Sub net mask to 4 byte chunks

                     b.2) Apply bit wise and for each respecting byte for Divide Network(IP Address) and Sub net mask

result byte[1] = Divide Network. byte1 & Sub net mask.byte1 ........ for all 4 bytes

b.3) Do same for Destination Ip Address and       and Sub net mask of current page table entry

b.4) If result in b.2 and b.3 are same, Output line is matched

c) If More than one Output lines matched,

   pick Route entry which have highest Subnet mask, i.e subnet mak with less "0" entries

   255.255.255.0 is greater than 255.255.0.0

Please find below method and structure definitions

struct r_table_entry{
int network;
int netmask;
int line;
};

unsigned char* print_ip(int ip)
{
    unsigned char bytes[4];
    bytes[0] = ip & 0xFF;
    bytes[1] = (ip >> 8) & 0xFF;
    bytes[2] = (ip >> 16) & 0xFF;
    bytes[3] = (ip >> 24) & 0xFF;  
    //printf("%d.%d.%d.%d ", bytes[3], bytes[2], bytes[1], bytes[0]);      
    return bytes;
}
unsigned char* mask(int ip , int netmask){
    unsigned char mask[4];

    unsigned char *bytes1;
    unsigned char *bytes2;
    bytes1 = print_ip(ip);
    bytes2 = print_ip(netmask);
    for(int i=0 ; i < 4 ; i++){
        mask[i] = bytes1[i] & bytes2[i];

    }

}

int getOutputLine(int destIp,struct r_table_entry *r_table_entry, int length){
    int matchedIndex = -1;
  
    for(int i =0 ; i < length ; i++){
         unsigned char *bytes1;
         unsigned char *bytes2;
         bytes1 = mask(r_table_entry[i].network,r_table_entry[i].netmask);
         bytes2 = mask(destIp,r_table_entry[i].netmask);
         if(bytes1[0] == bytes2[0] &&
            bytes1[1] == bytes2[1] &&
            bytes1[2] == bytes2[2] &&
            bytes1[3] == bytes2[3]){

            if(matchedIndex != -1){
                matchedIndex = i;
            }
            else{
                if(r_table_entry[i].netmask > r_table_entry[matchedIndex].netmask){
                   matchedIndex = i;
                
                }
            }
          

         }


    }  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote