📄 rip.cpp
字号:
#include "sysinclude.h"
extern void rip_sendIpPkt(unsigned char *pData, UINT16 len,unsigned short dstPort,UINT8 iNo);
extern struct stud_rip_route_node *g_rip_route_table;
typedef struct RIP_header
{
unsigned char command;
unsigned char version;
unsigned short zero;
};
typedef struct RIP_data
{
unsigned short addrfamilyid;
unsigned short routetag;
unsigned int ipaddr;
unsigned int subnetmask;
unsigned int nexthop;
unsigned int metric;
};
void sendPacket( unsigned char version, UINT8 iNo) {
stud_rip_route_node *p = g_rip_route_table;
int current_size = 0;
unsigned char *to_send;
while( p != NULL ){
if( p->metric != 16 && p->if_no != iNo){
if(current_size == 0){
to_send = new unsigned char[512];
RIP_header *h = (RIP_header *)to_send;
h->command = 2;
h->version = version;
h->zero = 0;
}
RIP_data * temp = (RIP_data *)(to_send + sizeof(RIP_header) + current_size * sizeof(RIP_data));
temp->addrfamilyid = htons(2);
temp->routetag = 0;
temp->ipaddr = htonl(p->dest);
temp->metric = htonl(p->metric);
temp->subnetmask = htonl(p->mask);
temp->nexthop = htonl(p->nexthop);
if( version == 1){
temp->subnetmask = 0;
temp->nexthop = 0;
}
current_size = current_size + 1;
}
p = p->next;
if( current_size == 25 || p == NULL){
rip_sendIpPkt(to_send, sizeof(RIP_header) + current_size * sizeof(RIP_data), 520, iNo);
current_size = 0;
}
}
}
int stud_rip_packet_recv(char *pBuffer,int bufferSize,UINT8 iNo,UINT32 srcAdd)
{
RIP_header *head = new RIP_header();
*head = *((RIP_header *)pBuffer);
//cout<<(int)head->command<<endl;
//cout<<(int)head->version<<endl;
//cout<<(int)head->zero<<endl;
if( head->command != 1 && head->command != 2 ){
cout<<"STUD_RIP_TEST_COMMAND_ERROR"<<endl;
ip_DiscardPkt(&(*pBuffer), STUD_RIP_TEST_COMMAND_ERROR);
}
if( head->zero != 0 ){
cout<<"STUD_RIP_TEST_MZERO_ERROR"<<endl;
ip_DiscardPkt(&(*pBuffer), STUD_RIP_TEST_MZERO_ERROR);
}
if( head->version != 1 && head->version != 2 ){
cout<<"STUD_RIP_TEST_VERSION_ERROR"<<endl;
ip_DiscardPkt(&(*pBuffer), STUD_RIP_TEST_VERSION_ERROR);
}
if( head->command == 1 ) {
//request
cout<<"REQUEST"<<endl;
sendPacket( head->version, iNo );
} else if( head->command == 2 ) {
//response
cout<<"RESPONSE"<<endl;
int num = ( bufferSize - sizeof(RIP_header) )/ sizeof(RIP_data);
RIP_data ** data = new RIP_data* [num];
RIP_data * temp = new RIP_data();
for(int i = 0; i < num; i++) {
temp = (RIP_data *)(pBuffer + sizeof(RIP_header) + i * sizeof(RIP_data));
data[i] = new RIP_data();
data[i]->ipaddr = ntohl(temp->ipaddr);
data[i]->subnetmask = ntohl(temp->subnetmask);
data[i]->nexthop = ntohl(temp->nexthop);
data[i]->metric = ntohl(temp->metric);
stud_rip_route_node *p = g_rip_route_table;
while( p != NULL){
if( p->dest == data[i]->ipaddr && p->mask == data[i]->subnetmask ) {
if( p->nexthop == data[i]->nexthop ){
p->metric = data[i]->metric + 1;
if( p->metric > 16 )
p->metric = 16;
} else {
if( p->metric > data[i]->metric ){
p->nexthop = data[i]->nexthop;
p->metric = data[i]->metric + 1;
if( p->metric > 16 )
p->metric = 16;
}
}
break;
}
p = p->next;
}
if( p == NULL && data[i]->metric < 15){
stud_rip_route_node *new_node = new stud_rip_route_node();
new_node->dest = data[i]->ipaddr;
new_node->mask = data[i]->subnetmask;
new_node->nexthop = data[i]->nexthop;
new_node->metric = data[i]->metric + 1;
new_node->if_no = iNo;
new_node->next = g_rip_route_table;
g_rip_route_table = new_node;
}
}
}
return 0;
}
void stud_rip_route_timeout(UINT32 destAdd, UINT32 mask, unsigned char msgType)
{
cout<<"TIMEOUT"<<endl;
if( msgType == RIP_MSG_SEND_ROUTE) {
sendPacket( 2, 1 );
sendPacket( 2, 2 );
}
else if( msgType == RIP_MSG_DELE_ROUTE ) {
stud_rip_route_node *p = g_rip_route_table;
while( p != NULL) {
if( p->dest == destAdd && p->mask == mask ){
p->metric = 16;
}
p = p->next;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -