資源簡介
在linux環境下模擬了基本的靜態路由,網絡搭建在幾臺虛擬機之間。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define?MAX_ROUTE_INFO?8;
#define?MAX_ARP_SIZE?8;
#define?MAX_DEVICE?4;
#define?BUFFER_MAX?2048
typedef?struct?route_item{
unsigned?int?destination;
unsigned?int?gateway;
unsigned?int?netmask;
unsigned?char?interface[14];
};
struct?route_item?route_info[8];
int?route_item_index;
typedef?struct?arp_table_item{
unsigned?int?ip_addr;
unsigned?char?mac_addr[6];
};
struct?arp_table_item?arp_table[8];
int?arp_item_index;
typedef?struct?device_item{
unsigned?char?interface[14];
unsigned?char?mac_addr[6];
unsigned?int?ip_addr;
};
struct?device_item?device[4];
int?device_index;
/*初始化函數,首先是把你的靜態路由信息添加到你的route_info[]這個數組中
??我在自己機器上時如下添加的,具體情況根據你的網絡拓撲情況添加
??參照linux自帶的路由結構,destination表示的是目的網絡號,gateway表示要經過
??的網關,如果為零說明已經到達最后一個路由節點,下一條就是目的地,netmask:
??用來提取目的網絡號:實際操作時把netmask?和?ip報文內的目的地址進行按位與操???
??作,然后把結果同當前的目的網絡號對比
??下面說一下device[]這個數組,它用來存放路由的網卡信息的,比如說你在終端中輸
??入ifconfig后,會出現一些網卡的信息,比如說eth0,在里面找到MAC和IP,填入我
??的device[]數組中
*/
void?initial()
{
route_item_index?=?3;
route_info[0].destination?=?inet_addr(“192.168.2.0“);
route_info[0].gateway?=?inet_addr(“192.168.1.2“);
route_info[0].netmask?=?inet_addr(“255.255.255.0“);
strcpy(route_info[0].interface?“eth0“);
route_info[1].destination?=?inet_addr(“192.168.1.0“);
route_info[1].gateway?=?0;
route_info[1].netmask?=?inet_addr(“255.255.255.0“);
strcpy(route_info[1].interface?“eth0“);
route_info[2].destination?=?inet_addr(“192.168.0.0“);
route_info[2].gateway?=?0;
route_info[2].netmask?=?inet_addr(“255.255.255.0“);
strcpy(route_info[2].interface?“eth1“);
arp_item_index?=?0;
device_index?=?2;
strcpy(device[0].interface?“eth0“);
device[0].mac_addr[0]?=?0X00;
device[0].mac_addr[1]?=?0X0C;
device[0].mac_addr[2]?=?0X29;
device[0].mac_addr[3]?=?0XE3;
device[0].mac_addr[4]?=?0X25;
device[0].mac_addr[5]?=?0X86;
device[0].ip_addr?=?inet_addr(“192.168.1.1“);
strcpy(device[1].interface?“eth1“);
device[1].mac_addr[0]?=?0X00;
device[1].mac_addr[1]?=?0X0C;
device[1].mac_addr[2]?=?0X29;
device[1].mac_addr[3]?=?0XE3;
device[1].mac_addr[4]?=?0X25;
device[1].mac_addr[5]?=?0X90;
device[1].ip_addr?=?inet_addr(“192.168.0.1“);
};
void?UpdateArpTable(unsigned?char?Iface[]?int?n?unsigned?int?target)
{
int?sockfdij;
sockfd?=?socket(PF_INET?SOCK_PACKET?htons(ETH_P_ARP));
char?buffer[BUFFER_MAX];
struct?sockaddr?tofrom;
int?fromlen=sizeof(struct?sockaddr);
strcpy(to.sa_dataIface);
strcpy(from.sa_dataIface);
char?datagram[BUFFER_MAX];
struct?ethhdr?*eth_header?=?datagram;
struct?arphdr?*arp_header?=?datagram?+?sizeof(struct?ethhdr);
memset?(datagram?0?BUFFER_MAX);
for(i?=?0;i?6;?i++)
eth_header->h_dest[i]?=?0XFF;
int?flag;
for(i?=?0;i?2;?i++)
if(strcmp(Iface
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.CA....??????9148??2010-04-12?22:36??route.c
????.CA....?????65334??2009-12-14?02:19??實驗4?靜態路由編程實現.docx
-----------?---------??----------?-----??----
????????????????74482????????????????????2
評論
共有 條評論