?? my_list.c
字號:
/*********************************************************** 版權所有 (C)2007, 深圳市中興通訊股份有限公司。** 文件名稱:my_list.c* 文件標識:* 內容摘要: 轉發表的處理函數* 其它說明:* 當前版本:* 作 者:丁 鵬* 完成日期:2007/12/11** 修改記錄1: * 修改日期: * 版 本 號:* 修 改 人: * 修改內容:**********************************************************//*********************************************************** * 頭文件 ************************************************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <linux/if_ether.h>#include <linux/if_packet.h>#include <netinet/ip.h>#include <sys/ioctl.h>#include <net/if.h>#define __FAVOR_BSD#include <netinet/tcp.h>#include <netinet/udp.h>#undef __FAVOR_BSD#include <netinet/in.h>#include <errno.h>#include <string.h>#include <netinet/tcp.h>#include <arpa/inet.h>#include <sys/socket.h>#include <netinet/in.h>#include <strings.h>#include "packet_gen.h"#include "my_list.h"/*********************************************************** * 全局變量 ************************************************************/ struct fib_rules *g_fibTable;struct fib_rules *fibTableHead;/*********************************************************** * 全局函數 ************************************************************/int rules_memcmp(const void * cs,const void * ct,size_t count);void rules_print();int fib_rules_init(void);int fib_rules_destroy(void);int fib_rules_length(void);int fib_rules_insert(struct fib_key *pfib_key);int fib_rules_remove(struct fib_key *pfib_key);int rules_memcmp(const void * cs,const void * ct,size_t count);int fib_rules_removeRule(struct in_addr *pip_src);FIB_RULE *rules_isExist(struct in_addr *ip_src);/*********************************************************************** 函數名稱:fib_rules_init* 功能描述:轉發表初始化* 輸入參數:* 輸出參數:無* 返 回 值:無* 其它說明:無* 修改日期 版本號 修改人 修改內容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創建************************************************************************/int fib_rules_init(void){ fibTableHead = malloc(sizeof(struct fib_rules)); if(fibTableHead == NULL) { printf("fib_rules_int: malloc fib memory err\n"); return -1; } memset(fibTableHead, 0x00, sizeof(struct fib_rules)); fibTableHead->next = fibTableHead->prev = NULL; return 1;}/*********************************************************************** 函數名稱:fib_rules_destroy* 功能描述:轉發表銷毀* 輸入參數:* 輸出參數:無* 返 回 值:無* 其它說明:無* 修改日期 版本號 修改人 修改內容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創建************************************************************************/int fib_rules_destroy(void){ struct fib_rules *pFibRule, *pNode = fibTableHead; struct fib_address *pAddr, *pAddNode; int i=0; int j = 0; pNode = pNode->next; while(pNode != NULL) { pFibRule = pNode; /* find dst address */ pAddr = pNode->pdstaddr; while(pAddr != NULL) { pAddNode = pAddr; pAddNode->next = NULL; free(pAddNode); pAddr = pAddr->next; i++; printf("i = %d\n" , i); } pNode->pdstaddr = NULL; free(pNode); pNode = pFibRule->next; j++; printf("j = %d\n" , j); } free(fibTableHead); fibTableHead = NULL; return 1;}/*********************************************************************** 函數名稱:fib_rules_length* 功能描述:取得轉發表長度* 輸入參數:* 輸出參數:無* 返 回 值:無* 其它說明:無* 修改日期 版本號 修改人 修改內容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創建************************************************************************/int fib_rules_length(void){ unsigned int num = 0; struct fib_rules *pNode = fibTableHead; while(pNode != NULL) { num ++; pNode = pNode->next; } return num;}/*********************************************************************** 函數名稱:fib_rules_insert* 功能描述:在轉發表中插入一個規則* 輸入參數:pfib_key 規則關鍵字* 輸出參數:無* 返 回 值:無* 其它說明:轉發表 轉發項中根據src 源ip地址確定轉發策略 轉發策略有 轉發給外網 1. 轉發到一個外網口 2. 轉發到多個外網口 轉發給內網 1. 轉發到一個內網口 2. 轉發到一個內網口* 修改日期 版本號 修改人 修改內容* ---------------------------------------------------------------------* 2007/12/11 丁鵬 創建************************************************************************/int fib_rules_insert(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(); } /* 第一個鏈表元素 */ /* 第一次創建 */ if(fibTableHead->next == NULL) { printf("the fist creat----\n"); /* 在鏈表尾部插入 */ new_rules = (struct fib_rules *)malloc(sizeof(struct fib_rules)); if(new_rules == NULL) { printf("fib_rules_insert: malloc the fib_rules err\n"); return 0; } new_addr = (struct fib_address *)malloc(sizeof(struct fib_address)); if(new_addr == NULL) { printf("fib_rules_insert: malloc the fib_address err\n"); free(new_rules); return 0; } pTail->next = new_rules; new_rules->direction = pfib_key->direction; 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; 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"); return 1; }/*end if(fibTableHead->next == NULL) */ pNode = fibTableHead->next; while (pNode != NULL) { printf("1----\n"); pTail = pNode; /* 遍歷鏈表,判斷是否有滿足條件的節點,有則再判斷添加的dst是否存在,若dst不存在,則只添加對應的dst */ if (rules_memcmp(&pNode->ip_src, &pfib_key->ip_src, sizeof(struct in_addr)) == 0) { printf("ip_src equal----\n"); /* 從一個ip地址來的只能是轉給外網或內網中的一種策略 */ if(pNode->direction != pfib_key->direction) { printf("the fib rules's direction is't equal to pfib_key's direction\n"); return 0; } /* 再比較dst */ printf("direction FIB_EXTERNEL_NETWORK----\n"); fib_addr_next = pNode->pdstaddr; while(fib_addr_next != NULL) { printf("fib addr != null---\n"); fib_addr = fib_addr_next; 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_INTERNEL_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); }/*end if (pNode->direction == FIB_EXTERNEL_NETWORK) */ if(rules_memcmp(rule_key, key, keylen) == 0) { printf("the dst address exist\n"); return -1; } fib_addr_next = fib_addr_next->next; } /* end while */ new_addr = (struct fib_address *)malloc(sizeof(struct fib_address)); if(new_addr == NULL) { printf("fib_rules_insert: malloc the fib_address err\n"); return 0; } if (pNode->direction == FIB_EXTERNEL_NETWORK) { memcpy(&new_addr->ip_dst, &pfib_key->ip_dst, sizeof(struct fib_address)); } else if(pNode->direction == FIB_INTERNEL_NETWORK) { memcpy(&new_addr->chipnum, &pfib_key->chipnum, sizeof(int)); } /* end direction */ new_addr->next = NULL; /* 這種情況一般不會發生,pdstaddr is null----no fib_address, else has pdstaddr */ if(fib_addr == NULL) { printf("unlikely pdstadd null"); pNode->pdstaddr = new_addr; new_addr->prev = new_addr; printf("fib_rules_insert: creat a new dst list\n"); } else { fib_addr->next = new_addr; new_addr->prev = fib_addr; printf("fib_rules_insert: insert a new dst into the list\n"); } /*end fib_addr */ } /* if (rules_memcmp(&pNode->ip_src, &pfib_key->ip_src, sizeof(struct in_addr)) == 0) */ pNode = pNode->next; } /* end while */ /* create a new rules */ if(pNode == NULL) { printf("create a new rules----\n"); /* 在鏈表尾部插入 */ new_rules = (struct fib_rules *)malloc(sizeof(struct fib_rules)); if(new_rules == NULL) { printf("fib_rules_insert: malloc the fib_rules err\n"); return 0; } new_addr = (struct fib_address *)malloc(sizeof(struct fib_address)); if(new_addr == NULL) { printf("fib_rules_insert: malloc the fib_address err\n"); free(new_rules); return 0; } new_rules->direction = pfib_key->direction;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -