My C/C ++ tutorial-005 (simulating route table selection)

Source: Internet
Author: User

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
 
/*
Simulate route table Selection: pre-set the route table. When a packet arrives,
View the destination address, and use this address to match the subnet mask of each item in the route table. If the obtained address is the same as the destination address,
Forward the data packet from the port with this address (this example is only applicable to Static Routing, of course not as complex as dynamic routing)
*/
 
// Because the route table length is unknown (added by the router administrator), arrays cannot be used. Use a linked list here. If not, you can use an array to simulate it.
Typedef struct RouteNode
{
Int ip [4]; // ip
Int subnetMask [4]; // Subnet Mask
Int port; // The interface number sent by the data packet. INT type is used here (this is not the case in reality)
Struct RouteNode * next; // The next route record
 
} RouterTableList;
 
Void routerTableArithmetic ();
Void inputRouterTable (RouterTableList * & rtl );
Void addRouteNode (RouterTableList * & rtl, int ip [], int subnetMask [], int port );
Void displayRouterTable (RouterTableList * rtl );
Int findPort (RouterTableList * rtl, int dist_ip []);
// Data packet forwarding decision-making function
Void routerTableArithmetic ()
{
Int dist_ip [4] = {192,168, 2,110}; // destination address of the Data Packet

RouterTableList * rtl;
InputRouterTable (rtl );
Printf ("*********** print route table **************** \ n ");
DisplayRouterTable (rtl );
Int port = findPort (rtl, dist_ip );
If (port =-1) printf ("***** no records are found, and no forwarding from where is found ****** \ n ");
Else
Printf ("********** data packets will be forwarded from the vro port % d *************** \ n", port );
}
// Enter the route table
Void inputRouterTable (RouterTableList * & rtl)
{
Rtl = (RouterTableList *) malloc (sizeof (RouterTableList); // allocate space at the header Node
Rtl-> next = NULL;
// Add route entries. Add three entries here. You can design a function or something.
Int ip [4] = {192,168,}; // enter a CIDR block, not a specific IP address. The same is true for routing configurations.
Int subnetMask [] ={ 255,255,255, 0 };
AddRouteNode (rtl, ip, subnetMask, 1 );
Int ip2 [4] = {192,168 };
AddRouteNode (rtl, ip2, subnetMask, 2 );
Int ip3 [4] = {192,168 };
AddRouteNode (rtl, ip3, subnetMask, 3 );
}
// Route Node
Void addRouteNode (RouterTableList * & rtl, int ip [], int subnetMask [], int port)
{
RouterTableList * node, * find; // find is used to save the last node of the linked list.
Node = (RouterTableList *) malloc (sizeof (RouterTableList); // allocate space
For (int I = 0; I <4; I ++)
{
Node-> ip [I] = ip [I];
}
For (I = 0; I <4; I ++)
{
Node-> subnetMask [I] = subnetMask [I];
}
Node-> port = port;
Node-> next = NULL;

Find = rtl;
While (find-> next! = NULL)
{
Find = find-> next;
}
Find-> next = node;
}
// Find the port from which the output data packet is sent
Int findPort (RouterTableList * rtl, int dist_ip [])
{
RouterTableList * node = rtl-> next;
Int I;
While (node! = NULL)
{
For (I = 0; I <4; I ++)
If (dist_ip [I] & node-> subnetMask [I])! = Node-> ip [I]) break;
 
If (I> = 4) return node-> port;
Node = node-> next;
}
Return-1; //-1 indicates that no record is found.
}
// Output route table
Void displayRouterTable (RouterTableList * rtl)
{
RouterTableList * node = rtl-> next;
While (node! = NULL)
{
Printf ("IP: % d. % d. % d. % d subnetMask: % d. % d. % d. % d port: % d \ n ", node-> ip [0], node-> ip [1], node-> ip [2], node-> ip [3], \
Node-> subnetMask [0], node-> subnetMask [1], node-> subnetMask [2], node-> subnetMask [3], node-> port );
Node = node-> next;
}
}

It took three hours, but it was a tragedy, but at last we made the desired function-simulating the routing selection function of the route table.

The function is simple. I used some of the following knowledge (I used it before, but I used it directly. I don't need to read it if I don't know it). I am still saying: I don't know how to leave a message, so I can take a detour.



From mzlqh's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.