#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct node {
struct node *left;
struct node *right;
char *gateway;
};
void buildtree(struct node *root, char *rule);
void ruleSplit(char *rule, int *dest, int *masklen, char *gw);
int ipparser(const char *dststr);
void route(struct node *root, const char *to, char *dst);
int main(int argc, char **argv)
{
char rule1[] = "192.168.5.0/24 192.168.5.254";
char rule2[] = "10.131.0.0/16 10.131.0.254";
char rule3[] = "16.0.0.0/8 16.0.0.254";
printf("------------ route table ------------\n");
printf("%s\n", rule1);
printf("%s\n", rule2);
printf("%s\n", rule3);
printf("0.0.0.0/0 192.168.102.253\n");
printf("-------------------------------------\n");
char *defaultroute = "192.168.102.253";
struct node *root = (struct node *)malloc(sizeof(struct node));
if (NULL != root) {
root->left = NULL;
root->right = NULL;
root->gateway = defaultroute;
}
buildtree(root, rule1);
buildtree(root, rule2);
buildtree(root, rule3);
char to1[16] = "192.168.5.20";
char gw[16] = "";
route(root, to1, gw);
printf("%s --> %s\n", to1, gw);
char to2[16] = "192.168.102.40";
route(root, to2, gw);
printf("%s --> %s\n", to2, gw);
char to3[16] = "10.238.20.11";
route(root, to3, gw);
printf("%s --> %s\n", to3, gw);
char to4[16] = "16.0.0.169";
route(root, to4, gw);
printf("%s --> %s\n", to4, gw);
exit(EXIT_SUCCESS);
}
void buildtree(struct node *root, char *rule)
{
int netip, masklen;
char *gw = (char *)malloc(16);
ruleSplit(rule, &netip, &masklen, gw);
struct node *p = root;
int i = 0;
for (i = 31; i >= 0 && masklen > 0; i--, masklen--) {
if (netip & (1 << i)) {
// 1: -->
if (NULL == p->right) {
struct node *newNode =
(struct node *)malloc(sizeof(struct node));
p->right = newNode;
}
p = p->right;
} else {
// 0: <--
if (NULL == p->left) {
struct node *newNode =
(struct node *)malloc(sizeof(struct node));
p->left = newNode;
}
p = p->left;
}
}
p->gateway = gw;
}
void ruleSplit(char *rule, int *dest, int *masklen, char *gw)
{
char *p = strchr(rule, '/');
char ip[16];
strncpy(ip, rule, p - rule + 1);
ip[p - rule] = '\0';
*dest = ipparser(ip);
char *q = strchr(rule, ' ');
char len[3];
strncpy(len, p + 1, q - p);
len[q - p] = '\0';
*masklen = atoi(len);
strcpy(gw, q + 1);
}
void route(struct node *root, const char *to, char *dst)
{
char *gw = root->gateway;
struct node *p = root;
int netip = ipparser(to);
int i;
for (i = 31; i >= 0; i--) {
int k = 1 << i;
if (k < 0)
k = 0 - k;
if (netip & k) { // 指定位为1, 向右走
if (NULL == p->right) {
break;
} else {
p = p->right;
if (p->gateway != NULL) {
gw = p->gateway;
}
}
} else { // 指定位为0, 向左走
if (NULL == p->left) {
break;
} else {
p = p->left;
if (p->gateway != NULL) {
gw = p->gateway;
}
}
}
}
strcpy(dst, gw);
}
int ipparser(const char *dststr)
{
char str[strlen(dststr)];
strcpy(str, dststr);
char *token = strtok(str, ".");
int ip = 0;
while (NULL != token) {
ip <<= 8;
ip += atoi(token);
token = strtok(NULL, ".");
}
return ip;
}
// 运行结果
steven@lenny:~/study/c$ ./a.out
------------ route table ------------
192.168.5.0/24 192.168.5.254
10.131.0.0/16 10.131.0.254
16.0.0.0/8 16.0.0.254
0.0.0.0/0 192.168.102.253
-------------------------------------
192.168.5.20 --> 192.168.5.254
192.168.102.40 --> 192.168.102.253
10.238.20.11 --> 192.168.102.253
16.0.0.169 --> 16.0.0.254
steven@lenny:~/study/c$
分享到:
相关推荐
(3)中序遍历二叉树:非递归算法(最好也能实现先序,后序非递归算法)。 (4)求二叉树的高度 。 (5)求二叉树的叶子个数。 (6)对于树中每一个元素值为x的结点,删去以它为根的子树,并释放相应的空间。 (8...
建立二叉树,实现二叉树的先序、中序、后序的递归遍历算法,输出遍历结果。 实现二叉树的先序、中序、后序和层次遍历的非递归算法,输出遍历结果。
基于二叉树的RFID 防碰撞算法的研究
基于C和C++实现二叉树编码的遗传算法实现数据拟合源码+实验报告+答辩PPT(课设作业).zip基于C和C++实现二叉树编码的遗传算法实现数据拟合源码+实验报告+答辩PPT(课设作业).zip基于C和C++实现二叉树编码的遗传算法实现...
cout****** 二叉树二叉链表结构测试程序 ******"; cout* 0--退出 *"; cout* 1--交互创建二叉树 *"; cout* 2--文件创建二叉树 *"; cout* 3--完全二叉树序列创建二叉树 *"; cout* 4--打印二叉树 *"; cout* 5--...
二叉树的实现各种遍历算法,插入删除成员函数非常全。二叉树的实现各种遍历算法,插入删除成员函数非常全
经典的数据结构问题:二叉树非递归遍历算法实现 二叉树递归遍历算法实现
很好用的算法介绍 而且是有详细解析的哟 很多的内容都是值得反复推敲和思考的
编写复制一棵二叉树的非递归算法编写复制一棵二叉树的非递归算法编写复制一棵二叉树的非递归算法编写复制一棵二叉树的非递归算法编写复制一棵二叉树的非递归算法编写复制一棵二叉树的非递归算法编写复制一棵二叉树的...
为解决SOA服务组合中服务选择问题,提出了一种基于二叉树编码的遗传算法。首先将一个服务的组合方案等效成AOV图,并将其转换成二叉树,然后进行后续遍历并编码。该编码基于二叉树结构,树的非叶子节点保存了其子树的...
由于传统的Apriori算法计算量过大,需要反复的搜索频繁项集,导致计算量过大,因此在原算法的基础上加以改进,以减少计算量
二叉树三种遍历算法的源码背诵版...希望对大家有用
二叉树的建立和遍历算法 数据结构课程设计用
包括了二叉树的各种递归与非递归的遍历算法 还可对二叉树所有结点求和
二叉树创建及遍历算法
基于二叉树SVM多类分类算法研究文章对应的代码附件,基于二叉树SVM多类分类算法研究文章对应的代码附件,基于二叉树SVM多类分类算法研究文章对应的代码附件。
二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法
一种基于二叉树修正算法的经理股票期权估价模型,李旸,李曜,经理股票期权计划的价值评估是当前企业股票期权计划研究领域的最主要难题之一,也是企业实际操作面临的现实问题,这一问题目前还
//二叉树的二叉链表存储表示 typedef struct BiTNode { TElemType data; BiTNode *lchild, *rchild; //左右孩子指针 } BiTNode, *BiTree; typedef struct { BiTree *base; BiTree *top; int stacksize; //当前...