⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 my_list.c

📁 一个使用raw socket 构造数据包的实例
💻 C
📖 第 1 页 / 共 2 页
字号:
        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;}/*********************************************************************** 函数名称:fib_rules_remove* 功能描述:删除指定符合特征的链表节点* 输入参数:pfib_key  规则关键字* 输出参数:无* 返 回 值:无* 其它说明:* 修改日期      版本号  修改人      修改内容* ---------------------------------------------------------------------* 2007/12/11             丁鹏              创建************************************************************************/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;              /* 遍历链表,判断是否有满足条件的节点,有则再判断添加的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;            }                       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;                           /* 删除一整项 */                           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;}/*********************************************************************** 函数名称:fib_rules_removeRule* 功能描述:删除指定以源ip为关键字的链表节点* 输入参数:pfib_key  规则关键字* 输出参数:无* 返 回 值:无* 其它说明:* 修改日期      版本号  修改人      修改内容* ---------------------------------------------------------------------* 2007/12/11             丁鹏              创建************************************************************************/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;               }    }/*********************************************************************** 函数名称:rules_isExist* 功能描述:根据源ip地址来判断转发表中是否有对应的转发规则* 输入参数:    ip_src  收到的数据包的源ip地址* 输出参数:* 返 回 值:* 其它说明:* 修改日期      版本号  修改人      修改内容* ---------------------------------------------------------------------* 2007/12/11             丁鹏              创建************************************************************************/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;        /* 遍历链表,判断是否有满足条件的节点,有则再判断添加的dst是否存在,若dst不存在,则只添加对应的dst */        if (rules_memcmp(&pNode->ip_src, ip_src, sizeof(struct in_addr)) == 0)        {             return pNode;        }        pNode = pNode->next;    }    return NULL;    }/*********************************************************************** 函数名称:rules_print* 功能描述:将转发表内容打印出来* 输入参数: * 输出参数:* 返 回 值:* 其它说明:* 修改日期      版本号  修改人      修改内容* ---------------------------------------------------------------------* 2007/12/11             丁鹏              创建************************************************************************/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;    }/*********************************************************************** 函数名称:rules_memcmp* 功能描述:getfilename* 输入参数:     chipNum  对应哪个内网口* 输出参数:      pFile   文件名* 返 回 值:    对应的内网口序列号* 其它说明:* 修改日期      版本号  修改人      修改内容* ---------------------------------------------------------------------* 2007/12/11             丁鹏              创建************************************************************************/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;  }/*********************************************************************** 函数名称:rules_memcmp* 功能描述:内存块比较函数* 输入参数:     cs  内存块1     ct  内存块2     count 大小* 输出参数:无* 返 回 值:    0  内存块内容一样* 其它说明:* 修改日期      版本号  修改人      修改内容* ---------------------------------------------------------------------* 2007/12/11             丁鹏              创建************************************************************************/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;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -