1. Implement IP router function, which does the following: - takes as an input:
ID: 3875450 • Letter: 1
Question
1. Implement IP router function, which does the following: - takes as an input: - a 32 bit IP address - a routing table: a list of IP address - subnet mask - Interface (NIC card) pairs - does a binary AND of the subnet mask with the input IP address to extract the network part of the address - compares the network part of the address with each address in its routing table - if it matches with one of them , it routes the packet to the matching Interface - if it matches none of them , it routes the packet to the Default Router (Gateway) 2. Use this data in the program Address/mask Next hop 135.46.56.0/22 Interface 0 135.46.60.0/22 Interface 1 192.53.40.0/23 Router 1 default Router 2
Hint: the IP addresses first need to be converted to binary in order to extract and compare the network part
3. Create a Graphical User Interface - showing which packet gets routed to which Interface at any given time. Use Java programming language for this project.
Explanation / Answer
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class IProuter {
public static void main(String argv[])
{
Scanner sc = new Scanner(System.in);
int n;
//reading contents
String ip;
System.out.print("Enter ip to search:");
ip = sc.nextLine();
System.out.print("Enter routing table size :");
n =sc.nextInt();
String s;
System.out.println("Press enter----------");
s =sc.nextLine();
String routing_table[]=new String[n];
System.out.println("Enter Table contents :");
System.out.println("Address/mask Next hop");
for(int i=0;i<n;i++)
{//System.out.println(i);
routing_table[i]=sc.nextLine();
}
//System.out.println(ip);
//splitting contents
String ip_split[];
ip_split = ip.split("[.]");
String rt_split[][] = new String[n][];
for(int i=0;i<n;i++)
{
rt_split[i] = routing_table[i].split("[./ ]");
}
//printing splitted values...
for (String e :ip_split)
{
System.out.print(e+" ");
}System.out.println();
for(int i=0;i<n;i++)
{
for(String e : rt_split[i])
System.out.print(e+" ");
System.out.println();
}
//finding route....by performing and operation
int i;
for(i=0;i<n-1;i++)
{
int k = Integer.parseInt(rt_split[i][4]);
int num[]={0,0,0,0};
int l=7,c=0;
while(k>0)
{
if(l==0)
{
num[c]=num[c]+1;
c++;
l=7;
}
else
{
num[c]=num[c]+(int)Math.pow(2, l);
l--;
}
k--;
}
int j=0;
for(j =0;j<ip_split.length;j++)
{
int m = Integer.parseInt(ip_split[j])&num[j];
//System.out.println(m);
if(m!=Integer.parseInt(rt_split[i][j]))
{
// System.out.println("hello"+i);
break;
}
}
// System.out.println(i+" "+j);
if(j==ip_split.length)
{
System.out.println("Route to :"+rt_split[i][rt_split[i].length-2]+" "+rt_split[i][rt_split[i].length-1]);
break;
}
}
if(i==n-1)
{
System.out.println("Route to :"+rt_split[i][rt_split[i].length-2]+" "+rt_split[i][rt_split[i].length-1]);
}
}
}
output:
run:
Enter ip to search:135.46.56.0
Enter routing table size :4
Press enter----------
Enter Table contents :
Address/mask Next hop
135.46.56.0/22 Interface 0
135.46.60.0/22 Interface 1
192.53.40.0/23 Router 1
default Router 2
135 46 56 0
135 46 56 0 22 Interface 0
135 46 60 0 22 Interface 1
192 53 40 0 23 Router 1
default Router 2
Route to :Interface 0
BUILD SUCCESSFUL (total time: 28 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.