?? my_list.c
字號:
memcpy((unsigned char *)&new_rules->ip_src, (unsigned char *)&pfib_key->ip_src, sizeof(struct in_addr)); new_rules->pdstaddr = (struct fib_address *)new_addr; new_rules->next = NULL; new_rules->prev = pTail; pTail->next = new_rules; if (pfib_key->direction == FIB_EXTERNEL_NETWORK) { memcpy(&new_addr->ip_dst, &pfib_key->ip_dst, sizeof(struct fib_address)); } else if(pfib_key->direction == FIB_INTERNEL_NETWORK) { memcpy(&new_addr->chipnum, &pfib_key->chipnum, sizeof(int)); } new_addr->next = NULL; new_addr->prev = new_addr; new_rules->pdstaddr = new_addr; printf("fib_rules_insert: insert a new rule\n"); } /* end null */ return 0;}/*********************************************************************** 函數(shù)名稱:fib_rules_remove* 功能描述:刪除指定符合特征的鏈表節(jié)點(diǎn)* 輸入?yún)?shù):pfib_key 規(guī)則關(guān)鍵字* 輸出參數(shù):無* 返 回 值:無* 其它說明:* 修改日期 版本號 修改人 修改內(nèi)容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創(chuàng)建************************************************************************/int fib_rules_remove(struct fib_key *pfib_key){ struct fib_rules *pNode = fibTableHead; struct fib_rules *pTail = fibTableHead; struct fib_rules *new_rules; struct fib_address *fib_addr = NULL; struct fib_address *fib_addr_next; struct fib_address *new_addr; unsigned char *key; unsigned char *rule_key; int keylen; /*鏈表頭為空,說明鏈表還沒有初始化*/ if (fibTableHead == NULL) { printf("error! fibtable has not been inited! \n"); fib_rules_init(); } pNode = fibTableHead->next; while (pNode != NULL) { pTail = pNode; /* 遍歷鏈表,判斷是否有滿足條件的節(jié)點(diǎn),有則再判斷添加的dst是否存在,若dst不存在,則只添加對應(yīng)的dst */ if (rules_memcmp(&pNode->ip_src, &pfib_key->ip_src, sizeof(struct in_addr)) == 0) { printf("ip_src equal----\n"); /* 從一個ip地址來的只能是轉(zhuǎn)給外網(wǎng)或內(nèi)網(wǎng)中的一種策略 */ if(pNode->direction != pfib_key->direction) { printf("the fib rules's direction is't equal to pfib_key's direction\n"); return 0; } if (pNode->direction == FIB_EXTERNEL_NETWORK) { rule_key = (unsigned char *)&fib_addr->ip_dst; key = (unsigned char *)&pfib_key->ip_dst; keylen = sizeof(struct in_addr); /* 再比較dst */ printf("direction FIB_EXTERNEL_NETWORK----\n"); } else if(pNode->direction == FIB_INTERNEL_NETWORK) { rule_key = (unsigned char *)&fib_addr->chipnum; key = (unsigned char *)&pfib_key->chipnum; keylen = sizeof(int); } fib_addr_next = pNode->pdstaddr; while(fib_addr_next != NULL) { printf("fib addr != null---\n"); fib_addr = fib_addr_next; if(rules_memcmp(rule_key,key, keylen) == 0) { printf("now delete the given address ...\n"); /* 鏈表中的第一個元素 */ if(fib_addr->prev == fib_addr) { /* 后面還有其他元素 */ if(fib_addr_next->next != NULL) { fib_addr_next->next->prev=fib_addr_next->next; pNode->pdstaddr = fib_addr_next->next; free(fib_addr); return; } else { pNode->pdstaddr = NULL; /* 刪除一整項(xiàng) */ fib_rules_removeRule((struct in_addr *)&pfib_key->ip_src); return 1; } /*end if(fib_addr_next->next)...*/ } /*end if(fib_addr->prev ..) */ /* 鏈表最后一個元素 */ else if (fib_addr->next == NULL) { fib_addr->prev->next = NULL; free(fib_addr); } else { fib_addr_next->next->prev=fib_addr_next->prev; fib_addr_next->prev->next = fib_addr_next->next; free(fib_addr); }/* end if(fib_addr->prev )...*/ } /* end id(rules_memcmp)... */ fib_addr_next = fib_addr_next->next; } /* end while */ } /* if (rules_memcmp(&pNode->ip_src, &pfib_key->ip_src, sizeof(struct in_addr)) == 0) */ pNode = pNode->next; }/*end while */ return 0;}/*********************************************************************** 函數(shù)名稱:fib_rules_removeRule* 功能描述:刪除指定以源ip為關(guān)鍵字的鏈表節(jié)點(diǎn)* 輸入?yún)?shù):pfib_key 規(guī)則關(guān)鍵字* 輸出參數(shù):無* 返 回 值:無* 其它說明:* 修改日期 版本號 修改人 修改內(nèi)容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創(chuàng)建************************************************************************/int fib_rules_removeRule(struct in_addr *pip_src){ struct fib_rules *pFibRule, *pNode; struct fib_address *pAddr, *pAddNode; int i=0; int j = 0; pNode = fibTableHead->next; while(pNode != NULL) { pFibRule = pNode; if (rules_memcmp(&pNode->ip_src, pip_src, sizeof(struct in_addr)) == 0) { pAddr = pNode->pdstaddr; while(pAddr != NULL) { pAddNode = pAddr; pAddr = pAddr->next; pAddNode->next = NULL; free(pAddNode); i++; printf("i = %d\n" , i); } pNode->pdstaddr = NULL; pNode->prev->next = pNode->next; /* rules最后一個元素 */ if(pNode->next != NULL) pNode->next->prev = pNode->prev; free(pNode); j++; printf("j = %d\n" , j); return 1; } pNode = pNode->next; } }/*********************************************************************** 函數(shù)名稱:rules_isExist* 功能描述:根據(jù)源ip地址來判斷轉(zhuǎn)發(fā)表中是否有對應(yīng)的轉(zhuǎn)發(fā)規(guī)則* 輸入?yún)?shù): ip_src 收到的數(shù)據(jù)包的源ip地址* 輸出參數(shù):* 返 回 值:* 其它說明:* 修改日期 版本號 修改人 修改內(nèi)容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創(chuàng)建************************************************************************/FIB_RULE *rules_isExist(struct in_addr *ip_src){ struct fib_rules *pNode; struct fib_rules *pTail; /*鏈表頭為空,說明鏈表還沒有初始化*/ if ((fibTableHead == NULL) || (fibTableHead->next == NULL)) { printf("rule is null \n"); return NULL; } pNode = fibTableHead->next; while (pNode != NULL) { pTail = pNode; /* 遍歷鏈表,判斷是否有滿足條件的節(jié)點(diǎn),有則再判斷添加的dst是否存在,若dst不存在,則只添加對應(yīng)的dst */ if (rules_memcmp(&pNode->ip_src, ip_src, sizeof(struct in_addr)) == 0) { return pNode; } pNode = pNode->next; } return NULL; }/*********************************************************************** 函數(shù)名稱:rules_print* 功能描述:將轉(zhuǎn)發(fā)表內(nèi)容打印出來* 輸入?yún)?shù): * 輸出參數(shù):* 返 回 值:* 其它說明:* 修改日期 版本號 修改人 修改內(nèi)容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創(chuàng)建************************************************************************/void rules_print(){ struct fib_rules *pNode = fibTableHead; struct fib_rules *pFibRule; struct fib_address *fib_addr; struct fib_address *fib_addr_next; /*鏈表頭為空,說明鏈表還沒有初始化*/ if (fibTableHead == NULL) { printf("error! fibtable has not been inited! \n"); return ; } printf("src() direct(0--out 1---intern) dst() \n"); while(pNode->next != NULL) { pFibRule = pNode; pNode = pNode->next; printf("src:%s, direct:%x \n", inet_ntoa(pNode->ip_src), pNode->direction); if(pNode->direction == 1) { fib_addr_next = pNode->pdstaddr; while(fib_addr_next != NULL) { fib_addr = fib_addr_next; printf(" the dst address :%s\n", inet_ntoa(fib_addr->ip_dst)); fib_addr_next = fib_addr_next->next; } } } return; }/*********************************************************************** 函數(shù)名稱:rules_memcmp* 功能描述:getfilename* 輸入?yún)?shù): chipNum 對應(yīng)哪個內(nèi)網(wǎng)口* 輸出參數(shù): pFile 文件名* 返 回 值: 對應(yīng)的內(nèi)網(wǎng)口序列號* 其它說明:* 修改日期 版本號 修改人 修改內(nèi)容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創(chuàng)建************************************************************************/int getfilename(unsigned char chipNum, unsigned char *pFile){ int ret = 0; if(chipNum == 1) { strcpy(pFile, "8360_1"); ret = 1; } else if(chipNum == 2) { strcpy(pFile, "8360_2"); ret =2; } else if(chipNum == 3) { strcpy(pFile, "8360_3"); ret = 3; } else if(chipNum == 4) { strcpy(pFile, "8360_4"); ret = 4; } return ret; }/*********************************************************************** 函數(shù)名稱:rules_memcmp* 功能描述:內(nèi)存塊比較函數(shù)* 輸入?yún)?shù): cs 內(nèi)存塊1 ct 內(nèi)存塊2 count 大小* 輸出參數(shù):無* 返 回 值: 0 內(nèi)存塊內(nèi)容一樣* 其它說明:* 修改日期 版本號 修改人 修改內(nèi)容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創(chuàng)建************************************************************************/int rules_memcmp(const void * cs,const void * ct,size_t count){ const unsigned char *su1, *su2; signed char res = 0; for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) if ((res = *su1 - *su2) != 0) break; return res;}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -