代码拉取完成,页面将自动刷新
#include "basetype.h"
#include "common.h"
#include "device_lib.h"
#include "packet_lib.h"
#include "icmp_lib.h"
#include "debug_lib.h"
#include "addr_lib.h"
#include "timer_data.h"
#include "device_data.h"
#include "switch_data.h"
#include "route_data.h"
#include "arp_data.h"
#include "pc_stackframe.h"
#include "switch_fwdframe.h"
#include "route_fwdframe.h"
#include "timer_process.h"
#include "diagnose_processcmd.h"
#include "device_processcmd.h"
#include "staticroute_processcmd.h"
#include "route_processcmd.h"
#include "arp_processcmd.h"
#include "switch_processcmd.h"
UINT32 g_uiTimeMode;
/* 全局枚举转字符串 */
UINT8* g_acTimeMode2String[2];
UINT8* g_acDebugFlags2String[2];
UINT8* g_acDeviceType2String[DEVICE_TYPE_MAX];
UINT8* g_acErrorCode2String[35];
/*
* description: 命令分发处理函数, 通过识别出命令属于那个模块,从而调用相应函数
*
* input:
* pucModuleName 预处理识别模块
* pstWordArray 单词数组
* uiFlag
* pucDeviceName 当前命令执行时的设备视图(也就是字符串,指示是那台设备),如果不是在设备视图下,该指针为NULL。
* usIfNum 设备端口, 如果不是在接口视图下,该值为无效值。
*
* output: NULL
* return: 错误码
* tip:
*/
INT32 cmdDispath(UINT8* pucModuleName, COMMANDLINE_S* pstWordArray, UINT32 uiFlag, UINT8* pucDeviceName, UINT16 usIfNum)
{
if (uiFlag == CMD_FLAG_DISPLAY)
{
if (0 == strcmp("map", pucModuleName))
{
return Device_ProcessCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else if (0 == strcmp("ip", pucModuleName))
{
if ((0 == strcmp("interface", pstWordArray->aacCmd[2])) && (0 == strcmp("brief", pstWordArray->aacCmd[3])))
{
return Device_ProcessIfCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else if (0 == strcmp("routing-table", pstWordArray->aacCmd[2]))
{
//display ip routing-table
return RouteProcess_RouterCmd(pstWordArray, uiFlag, pucDeviceName);
}
else
{
return ERROR_INVALID_INPUT;
}
}
else if (0 == strcmp("timer", pucModuleName))
{
return Timer_Print();
}
else if (0 == strcmp("arp", pucModuleName))
{
return Arp_ProcessCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
//display mac-address 与 display vlan id
else if (0 == strcmp("mac-address", pucModuleName))
{
return Mactable_ProcessDisplayCmd(pstWordArray, uiFlag, pucDeviceName);
}
else if (0 == strcmp("vlan", pucModuleName))
{
return Mactable_ProcessDisplayCmd(pstWordArray, uiFlag, pucDeviceName);
}
else
{
return ERROR_INVALID_INPUT;
}
return ERROR_SUCCESS;
}
if (NULL == pucDeviceName)
{
if ((0 == strcmp("device", pucModuleName)) || (0 == strcmp("link", pucModuleName)))
{
return Device_ProcessCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else
{
return ERROR_INVALID_INPUT;
}
}
else
{
if (0 == strcmp("shutdown", pucModuleName))
{
return Device_ProcessIfCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else if (0 == strcmp("arp", pucModuleName))
{
if (pstWordArray->uiWordCount == 1)
{
return ERROR_CMD_TOOSHORT;
}
if (uiFlag != CMD_FLAG_UNDO)
{
if (0 == strcmp("static", pstWordArray->aacCmd[1]))
{
return Arp_ProcessCmd(pstWordArray->aacCmd[1], pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else
{
return Arp_ProcessCmd(pstWordArray->aacCmd[1], pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
}
else if (uiFlag == CMD_FLAG_UNDO)
{
if (0 == strcmp("static", pstWordArray->aacCmd[2]))
{
return Arp_ProcessCmd(pstWordArray->aacCmd[2], pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
}
}
else if (0 == strcmp("ip", pucModuleName))
{
if (pstWordArray->uiWordCount == 1)
{
return ERROR_CMD_TOOSHORT;
}
if (uiFlag != CMD_FLAG_UNDO)
{
if (0 == strcmp("address", pstWordArray->aacCmd[1]))
{
return Device_ProcessIfCmd(pstWordArray->aacCmd[1], pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
if (0 == strcmp("route-static", pstWordArray->aacCmd[1]))
{
return StaticRoute_ProcessCmd(pstWordArray, uiFlag, pucDeviceName);
}
}
else
{
if (0 == strcmp("address", pstWordArray->aacCmd[2]))
{
return Device_ProcessIfCmd(pstWordArray->aacCmd[2], pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
if (0 == strcmp("route-static", pstWordArray->aacCmd[2]))
{
return StaticRoute_ProcessCmd(pstWordArray, uiFlag, pucDeviceName);
}
}
}
else if (0 == strcmp("ipconfig", pucModuleName))
{
return Device_ProcessIfCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else if (0 == strcmp("route", pucModuleName))
{
if (0 == strcmp("print", pstWordArray->aacCmd[1]))
{
return RouteProcess_PcCmd(pstWordArray, uiFlag, pucDeviceName);
}
return StaticRoute_ProcessCmd(pstWordArray, uiFlag, pucDeviceName);
}
else if (0 == strcmp("mac-address", pucModuleName))
{
//1.澶勭悊mac鍦板潃鐨勫鍒犳搷浣?
// mac-address { static | dynamic} mac-address interface <if_num1> vlan <vlan_id>
return MacTable_ProcessConfigCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else if (0 == strcmp("vlan", pucModuleName))
{
//vlan增删
return MacTable_ProcessConfigCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else if (0 == strcmp("port", pucModuleName))
{
//Port的增删
return MacTable_ProcessConfigCmd(pucModuleName, pstWordArray, uiFlag, pucDeviceName, usIfNum);
}
else
{
return ERROR_VIEW_WRONG;
}
}
}
/*
* description: 视图切换为设备视图或设备接口视图
*
* input:
* WordArray 单词数组
* Identifiers 这个为<sys> 或者 [pc-pc1], 是当前设备视图
* S 设备视图栈
*
* output: NULL
* return: 错误码
* tip:
*/
INT32 ViewDevice(COMMANDLINE_S* WordArray, SElemType* Identifiers, SqStack* S)
{
UINT16 usDeviceId, usDeviceType;
if ('\0' != Identifiers->acDevice[0])
{
return ERROR_VIEW_WRONG;
}
usDeviceId = DeviceData_FindByName(WordArray->aacCmd[4]);
usDeviceType = DeviceData_GetType(usDeviceId);
if (DEVICE_ID_INVALID == usDeviceId)
{
return ERROR_NOT_EXIST;
}
if (strcmp(Stoupper(WordArray->aacCmd[2]), g_acDeviceType2String[usDeviceType]) != 0)
{
return ERROR_INVALID_INPUT;
}
memset(Identifiers, 0, sizeof(SElemType));
strcpy(Identifiers->acView, "[");
strcat(Identifiers->acView, g_acDeviceType2String[usDeviceType]);
strcat(Identifiers->acView, "-");
strcat(Identifiers->acView, WordArray->aacCmd[4]); //装设备类型 设备名字
if (WordArray->uiWordCount > 5)
{
strcat(Identifiers->acView, "-interface-");
strcat(Identifiers->acView, WordArray->aacCmd[6]); //装端口编号
}
strcat(Identifiers->acView, "]");
strcat(Identifiers->acDevice, WordArray->aacCmd[4]); //复制设备名字
strcat(Identifiers->acIfName, WordArray->aacCmd[6]); //复制端口编号
Push(S, Identifiers);
return ERROR_SUCCESS;
}
/*
* description: 在switch或router视图下,切换为设备接口视图
*
* input:
* WordArray 单词数组
* Identifiers 是当前设备视图
* S 设备视图栈
*
* output: NULL
* return: 错误码
* tip
*/
INT32 ViewDeviceIF(UINT8* pucIfName, SElemType* Identifiers, SqStack* S)
{
INT8 pcDeviceName[DEVICE_NAME_MAX_LENGTH];
UINT16 usDeviceId, usDeviceType, usifcount, usIfNum;
if ('\0' == Identifiers->acDevice[0])
{
return ERROR_VIEW_WRONG;
}
strcpy(pcDeviceName, Identifiers->acDevice);
usDeviceId = DeviceData_FindByName(pcDeviceName);
usDeviceType = DeviceData_GetType(usDeviceId);
usifcount = DeviceData_GetIfCount(usDeviceId);
usIfNum = stoi(pucIfName);
if (usifcount <= usIfNum || usIfNum == ERROR_CHECK)
{
return ERROR_CHECK;
}
if (usDeviceType == DEVICE_TYPE_SWITCH || usDeviceType == DEVICE_TYPE_ROUTER)
{
memset(Identifiers, 0, sizeof(SElemType));
strcpy(Identifiers->acView, "[");
strcat(Identifiers->acView, g_acDeviceType2String[usDeviceType]);
strcat(Identifiers->acView, "-");
strcat(Identifiers->acView, pcDeviceName); //装设备类型 设备名字
strcat(Identifiers->acView, "-interface-");
strcat(Identifiers->acView, pucIfName); //装端口编号
strcat(Identifiers->acView, "]");
strcat(Identifiers->acDevice, pcDeviceName); //复制设备名字
strcat(Identifiers->acIfName, pucIfName); //复制端口编号
Push(S, Identifiers);
return ERROR_SUCCESS;
}
else
{
return ERROR_VIEW_SWITCH_FAILED;
}
}
/*
* description: 从视图[device]中退出,返回系统视图<sys>
*
* input:
* WordArray 单词数组
* Identifiers 是当前设备视图
* S 设备视图栈
*
* output: NULL
* return: 错误码
* tip
*/
INT32 ViewDeviceQuit(COMMANDLINE_S* WordArray, SElemType* Identifiers, SqStack* S)
{
if (S->top == 0)//<sys>
{
memset(Identifiers, 0, sizeof(SElemType));
GetTop(S, Identifiers);
return ERROR_SUCCESS;
}
if (strcmp("quit", WordArray->aacCmd[1]) == 0 && WordArray->uiWordCount == 2)
{
memset(Identifiers, 0, sizeof(SElemType));
Pop(S);
GetTop(S, Identifiers);
return ERROR_SUCCESS;
}
return ERROR_INVALID_INPUT;
}
/*
* description: 打印设备报文状态
*
* input:
* WordArray 单词数组
* Identifiers 是当前设备视图
*
* output: NULL
* return: 错误码
*
* tip: display packets {device name <name> | statistics | clean}
* device: 打印当前设备报文信息, statistics: 所以设备报文统计信息, clean: 清除所有报文
*/
INT32 PrintPacketSituation(COMMANDLINE_S* WordArray, SElemType* Identifiers)
{
UINT16 usDeviceId, usPacketNum;
PACKET_S* pstPacket = NULL;
if ('\0' != Identifiers->acDevice[0])
{
return ERROR_VIEW_WRONG;
}
if (0 == strcmp("device", WordArray->aacCmd[2]))
{
usDeviceId = DeviceData_FindByName(WordArray->aacCmd[4]);
if (DEVICE_ID_INVALID == usDeviceId)
{
return ERROR_DEVICE_NOT_EXSIT;
}
pstPacket = DeviceData_GetPacket(usDeviceId);
if (NULL == pstPacket)
{
printf("Message chain is empty\n\n");
return ERROR_SUCCESS;
}
while (NULL != pstPacket)
{
PacketLib_PrintPacket(pstPacket->aucBuffer, pstPacket->usPacketLen);
printf("Next message view\n\n");
pstPacket = pstPacket->pstNextPacket;
}
}
else if (0 == strcmp("statistics", WordArray->aacCmd[2]))
{
printf("DeviceName PacketCount\n");
for (usDeviceId = 0; usDeviceId < DEVICE_MAX_ID; usDeviceId++)
{
usPacketNum = DeviceData_GetPacketNum(usDeviceId);
if (DEVICE_ID_INVALID != usPacketNum)
{
printf("%s %d\n", DeviceData_GetIdToName(usDeviceId), usPacketNum);
}
}
}
else if (0 == strcmp("clear", WordArray->aacCmd[2]))
{
for (usDeviceId = 0; usDeviceId < DEVICE_MAX_ID; usDeviceId++)
{
DeviceData_ClearPacket(usDeviceId);
}
}
else
{
return ERROR_INVALID_INPUT;
}
return ERROR_SUCCESS;
}
/*
* description: 报文分发处理函数, 发送到PC, 交换机, 路由器协议栈处理
*
* input:
* usDeviceId 当前设备ID
* usIfNum 当前设备接口编号
* pucBuffer 报文
* usPacketLen 报文长度
*
* output: NULL
* return: 错误码
* tip:
*/
INT32 packet_Process(UINT16 usDeviceId, UINT16 usIfNum, UINT8* pucBuffer, UINT16 usPacketLen)
{
UINT32 i = 0;
DebugLib_Printf(MODULE_PACKET, "\n PACKET PRINT: ");
for (i = 0; i < usPacketLen; i++)
{
DebugLib_Printf(MODULE_PACKET, "%02x ", pucBuffer[i]);
}
DebugLib_Printf(MODULE_PACKET, "\n");
INT32 iRet = ERROR_DEFAULT_GATEWAY;
UINT16 usDeviceType = DeviceData_GetType(usDeviceId);
if (usDeviceType == DEVICE_TYPE_PC)
{
iRet = PcPacketProcess(usDeviceId, usIfNum, pucBuffer, usPacketLen);
}
else if (usDeviceType == DEVICE_TYPE_SWITCH)
{
iRet = SwitchPacketProcess(usDeviceId, usIfNum, pucBuffer, usPacketLen);
}
else if (usDeviceType == DEVICE_TYPE_ROUTER)
{
iRet = RouterPacketProcess(usDeviceId, usIfNum, pucBuffer, usPacketLen);
}
else
{
printf("Fatal Error. Device %d's type is %d.\n", usDeviceId, usDeviceType);
return iRet;
}
return ERROR_SUCCESS;
}
/*
* description: 时间往前走函数,在时间往前走的过程中,处理各个设备报文
*
* input:
* uiMircoSeconds 时间往前行走的秒数
*
* output: NULL
* return: 错误码
* tip:每向前走动一秒,则会对设备链的所有设备的报文进行进行处理一次
*/
VOID processTimeGo(UINT32 uiMircoSeconds)
{
UINT16 usDeviceId;
UINT16 usPacketLen;
UINT16 usIfNum;
UINT8 aucBuffer[PACKET_MAX_SIZE];
UINT32 uiPacketCount = 0;
if (0 == uiMircoSeconds)
{
return;
}
for (; uiPacketCount <= uiMircoSeconds; uiPacketCount++)
{
for (usDeviceId = 0; usDeviceId < DEVICE_MAX_ID; usDeviceId++)
{
// 从当前设备中获取一个报文
DeviceData_RecvPacket(usDeviceId, aucBuffer, &usPacketLen, &usIfNum);
if (0 != usPacketLen)
{
// 对报文进行分发处理
packet_Process(usDeviceId, usIfNum, aucBuffer, usPacketLen);
uiPacketCount++;
}
}
// 时间变化紧随其后
TimerData_SetTimeNow(1);
}
return;
}
/* 初始化函数 */
VOID Init()
{
DeviceData_Init();
TimerData_Init();
MacData_Init();
RouteData_Init();
ArpData_Init();
// 初始化错误码
g_acTimeMode2String[0] = "NORMAL";
g_acTimeMode2String[1] = "STOP";
g_acDeviceType2String[0] = "ERROR";
g_acDeviceType2String[1] = "PC";
g_acDeviceType2String[2] = "SWITCH";
g_acDeviceType2String[3] = "ROUTER";
g_acErrorCode2String[0] = "Error Success";
g_acErrorCode2String[1] = "Error Device Not Exist";
g_acErrorCode2String[2] = "Error Already Exist";
g_acErrorCode2String[3] = "Error Notenough Memory";
g_acErrorCode2String[4] = "Error Invalid Input";
g_acErrorCode2String[5] = "Error Index Exceed";
g_acErrorCode2String[6] = "Error Port Busy";
g_acErrorCode2String[7] = "Error View Wrong";
g_acErrorCode2String[8] = "Error Check";
g_acErrorCode2String[9] = "Error Default Gateway";
g_acErrorCode2String[10] = "Error Cmd Tooshort";
g_acErrorCode2String[11] = "Error View Switch Failed";
g_acErrorCode2String[12] = "Error Invalid Prefixlen";
g_acErrorCode2String[13] = "Error If Not Exsit";
g_acErrorCode2String[14] = "Error Not Exist";
g_acErrorCode2String[15] = "Error Invalid Ipv4addr";
g_acErrorCode2String[16] = "Error Invalid Number";
g_acErrorCode2String[17] = "Destination unreachable.";
}
/*
* 首先将<sys> (以下将<sys>称为视图)压入栈S中, 并进行初始化init
* 进入死循环:
* 从输入中读取一行字符
* 切分成单词, 并统计单词数目, 将二者封装成 WordArray
* 判断属于那个分支后, 从而进行分发处理
* 根据返回错误码,打印相应的输出
* 最后调用processTimeGo时间向前走 0 步
*/
INT32 main()
{
INT8 acCmd[CMD_MAX_LENGTH + 1]; // '\0'
INT32 iRet = ERROR_INVALID_INPUT, i = 0;
SqStack S;
SElemType Identifiers; // 将三个acDevice,acIfName,acView,封装成一个标识符
COMMANDLINE_S WordArray;
// g_uiTimeMode = TIME_MODE_NORMAL; /* 系统模式:时间静止模式/正常模式 */
g_uiTimeMode = TIME_MODE_STOP;
UINT32 uiMircoSeconds;
memset(&Identifiers, 0, sizeof(SElemType));
memset(&S, 0, sizeof(SqStack));
InitStack(&S); // 定义一个栈,用来压栈标识符
strcpy(Identifiers.acView, "<sys>");
Push(&S, &Identifiers);
Init();
for (;;)
{
printf("%s", Identifiers.acView);
//将acCmd切割成一个一个单词,如果单词过长、过多、出现非法字符,报错。
memset(&WordArray, 0, sizeof(COMMANDLINE_S));
fgets(acCmd, 300, stdin);
char* p = strchr(acCmd, '\n');
if (p != NULL) *p = '\0';
iRet = ERROR_INVALID_INPUT;
explode(' ', acCmd, WordArray.aacCmd, &WordArray.uiWordCount);
if (0 == WordArray.uiWordCount)
{
continue;
}
//根据第一个单词设置uiCmdFlag:undo,display
if (0 == strcmp("view", WordArray.aacCmd[0]))
{
if (WordArray.uiWordCount > 2)
{
iRet = ViewDevice(&WordArray, &Identifiers, &S);
}
else
{
iRet = ViewDeviceQuit(&WordArray, &Identifiers, &S);
}
}
else if (0 == strcmp("time", WordArray.aacCmd[0])) // time mode normal | stop
{
if ('\0' != Identifiers.acDevice[0])
{
iRet = ERROR_VIEW_WRONG;
}
else if (0 == strcmp("mode", WordArray.aacCmd[1]))
{
if (WordArray.uiWordCount <= 3 || WordArray.aacCmd[2] == NULL)
{
if (0 == strcmp("stop", WordArray.aacCmd[2]))
{
g_uiTimeMode = TIME_MODE_STOP;
printf("\nThe current mode is: time mode stop\n\n");
}
else
{
g_uiTimeMode = TIME_MODE_NORMAL;
}
}
else
{
iRet = ERROR_INVALID_INPUT;
}
}
else if (0 == strcmp("go", WordArray.aacCmd[1]))
{
if (3 != WordArray.uiWordCount)
{
iRet = ERROR_INVALID_INPUT;
}
else if (ERROR_SUCCESS == AddrLib_DecStringtoInt(WordArray.aacCmd[2], &uiMircoSeconds, 10000))
{
processTimeGo(uiMircoSeconds);
continue;
}
}
}
else if ((0 == strcmp("debug", WordArray.aacCmd[0]))
|| (0 == strcmp("debug", WordArray.aacCmd[1]))) //(undo)debug module_name
{
if (0 == strcmp("undo", WordArray.aacCmd[0]))
{
iRet = DebugLib_UnSet(WordArray.aacCmd[2]);
if (iRet != ERROR_SUCCESS)
{
printf("Module name error,operation failed\n\n");
}
else
{
printf("Debugging information has been turned off\n\n");
}
}
else
{
iRet = DebugLib_Set(WordArray.aacCmd[1]);
if (iRet != ERROR_SUCCESS)
{
printf("Module name error,operation failed\n\n");
}
else
{
printf("Debugging information has been turned on\n\n");
}
}
continue;
}
else if (0 == strcmp("undo", WordArray.aacCmd[0]))
{
iRet = cmdDispath(WordArray.aacCmd[1], &WordArray, CMD_FLAG_UNDO, Identifiers.acDevice, stoi(Identifiers.acIfName));
}
else if (0 == strcmp("display", WordArray.aacCmd[0]))
{
//display system,输入后打印下系统当前的time mode,debug flag,packet 残留情况
if ((0 == strcmp(WordArray.aacCmd[1], "system")) && WordArray.uiWordCount == 2)
{
printf("time mode: %s, time: %d(ms), debugflag: 0x%x.\n",
g_acTimeMode2String[g_uiTimeMode], TimerData_GetTimeNow(), DebugLib_Get());
continue;
}
else if (0 == strcmp(WordArray.aacCmd[1], "packets"))
{
iRet = PrintPacketSituation(&WordArray, &Identifiers);
}
else
{
iRet = cmdDispath(WordArray.aacCmd[1], &WordArray, CMD_FLAG_DISPLAY, Identifiers.acDevice, DEVICE_IFNUM_INVALID);
}
}
else if (0 == strcmp("interface", WordArray.aacCmd[0]))
{
iRet = ViewDeviceIF(WordArray.aacCmd[1], &Identifiers, &S);
}
else
{
if (0 == strcmp("<sys>", Identifiers.acView))
{
iRet = cmdDispath(WordArray.aacCmd[0], &WordArray, CMD_FLAG_CFG, NULL, DEVICE_IFNUM_INVALID);
}
else
{
iRet = cmdDispath(WordArray.aacCmd[0], &WordArray, CMD_FLAG_CFG, Identifiers.acDevice, stoi(Identifiers.acIfName));
}
}
if (0 == strcmp("ping", WordArray.aacCmd[0]))
{
iRet = Diagnose_ProcessCmd(NULL, &WordArray, Identifiers.acDevice);
}
/* 输入错误则返回 */
if (ERROR_SUCCESS != iRet)
{
printf("Error: Configuration failed, return %s\n", g_acErrorCode2String[-iRet]);
}
printf("\n");
/* packet process */
processTimeGo(0);
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。