c-router-emulator/router/test/link.c

223 lines
4.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "link.h"
void title()
{
printf("[人机交互线程的全部功能]\n");
printf("2设置过滤 IP \n");
printf("3删除过滤 IP \n");
printf("4查看过滤 IP \n");
printf("5查看 arp 缓存 \n");
printf("10退出路由器\n");
}
//*************************过滤链表******************************
//尾插
MY_ROU *rou_pTailInsert(MY_ROU *head)
{
//申请一个待插入的空间
MY_ROU *pi=(MY_ROU*)malloc(sizeof(MY_ROU));
pi->next=NULL;
printf("输入过滤ip:");
//向空间中插入数据
scanf("%s",pi->ip);
//判断是否有数据
if(head==NULL)
{
head=pi;
}
else
{
//寻找插入的节点
MY_ROU *p1=head;
while(p1->next!=NULL)
{
p1=p1->next;
}
//插入
p1->next=pi;
}
printf("设置完成\n");
return head;
}
//遍历
void rou_print_link(MY_ROU *head)
{
if(head==NULL)
{
printf("没有数据\n");
}
else
{
while(head!=NULL)
{
printf("ip%s\n",head->ip);
head=head->next;
}
}
return;
}
//释放链表
MY_ROU* rou_freeLink(MY_ROU *head)
{
MY_ROU *pd;
pd=head;
while(head!=NULL)
{
head=pd->next;
free(pd);
pd=head;
}
printf("过滤链表释放完毕\n");
return head;
}
//查找ip
int rou_searcharpLink(MY_ROU *head,char *ip)
{
int i=0;
while(head!=NULL)
{
if(strcmp(ip,head->ip)==0)
{
i++;
//printf("存在相同ip\n");
return 1;
}
head=head->next;
}
if(0==i)
{
//printf("未找到ip\n");
return 0;
}
}
//删除
MY_ROU *rou_pDeleteLink(MY_ROU *head)
{
char num[16]="";
MY_ROU *pe=head;
MY_ROU *pf=head;
printf("请输入你要删除的ip");
scanf("%s",num);
if(NULL==head)
{
printf("无可删除数据\n");
}
else
{
while(strcmp(pe->ip,num))
{
pf=pe;
pe=pe->next;
if(NULL==pf->next)
{
printf("未找到要删除数据\n");
return head;
}
}
if(pe==head)
{
head=pe->next;
free(pe);
}
else
{
pf->next=pe->next;
free(pe);
}
}
return head;
}
//*************************过滤链表******************************
//*************************arp缓存表*****************************
//arp缓存表尾插
MY_ARP *arp_pTailInsert(MY_ARP *head,char *mac,char *ip)
{
//申请一个待插入的空间
MY_ARP *pi=(MY_ARP*)malloc(sizeof(MY_ARP));
pi->next=NULL;
//向空间中插入数据
strcpy(pi->ip,ip);
strcpy(pi->mac,mac);
//判断是否有数据
if(head==NULL)
{
head=pi;
}
else
{
//寻找插入的节点
MY_ARP *p1=head;
while(p1->next!=NULL)
{
p1=p1->next;
}
//插入
p1->next=pi;
}
printf("插入一个ip:%s mac:%s 到arp缓存表\n",pi->ip,pi->mac);
return head;
}
//arp中查找ip和mac
int arp_searcharpLink(MY_ARP *head,char *ip)
{
int i=0;
while(head!=NULL)
{
if(strcmp(ip,head->ip)==0)
{
i++;
//printf("存在相同ip和mac\n");
return 1;
}
head=head->next;
}
if(0==i)
{
//printf("未找到ip和mac\n");
return 0;
}
}
//遍历arp缓存表
void arp_print_link(MY_ARP *head)
{
if(head==NULL)
{
printf("没有数据\n");
}
else
{
while(head!=NULL)
{
printf("ip%s mac%s\n",head->ip,head->mac);
head=head->next;
}
}
return;
}
//释放arp链表
MY_ARP* arp_freeLink(MY_ARP *head)
{
MY_ARP *pd;
pd=head;
while(head!=NULL)
{
head=pd->next;
free(pd);
pd=head;
}
printf("arp链表释放完毕\n");
return head;
}
//*************************arp缓存表*****************************