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

📄 dyn_ip_test.c

📁 mobile ip 在linux下的一种实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: dyn_ip_test.c,v 1.28 2001/09/26 18:25:24 jm Exp $ * Tunnel and route interface tests * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2001, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#include <stdlib.h>#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string.h>#include <unistd.h>#include <sys/time.h>#include <sys/types.h>#include "util.h"#include "dyn_ip.h"#include "fixed_fd_zero.h"#define TEST_INTERFACE "TEST"#define TEST_ADDRESS "192.168.250.230"#define LOCAL_ADDRESS "192.168.250.231"#define TEST_NET "192.168.250.0"#define TEST_NET_PLEN 24#define TEST_NET_PLENS "24"#define IP_PATH "/sbin/ip"#define IFCONFIG_PATH "/sbin/ifconfig"extern int opt_debug;static void dyn_ip_tests(void){	struct in_addr a, b, c;	struct in_addr any;	int ok = 1, i;	any.s_addr = 0;	printf("Tunnel and route interface routine tests\n"	       "(this test must be run with root privileges)\n\n");	inet_aton(TEST_ADDRESS, &a);	inet_aton(LOCAL_ADDRESS, &b);	inet_aton(TEST_NET, &c);	i = 0;	printf("Test case M00: Check IPIP and GRE encapsulation support\n");	if (dyn_ip_check_ipip() < 0) {		i = 1;		printf("No kernel support for IPIP\n");	}	if (dyn_ip_check_gre() < 0) {		i = 1;		printf("No kernel support for GRE\n");	}	if (i == 1) {		ok = 0;		printf("M00: failed!\n\n");	} else		printf("M00: OK\n\n");	printf("Test case M01: Add a tunnel\n");	printf("The tunnel should not exist already - ifconfig:\n");	system(IFCONFIG_PATH " " TEST_INTERFACE);	printf("dyn_ip_tunnel_add(" TEST_INTERFACE ", " TEST_ADDRESS ")\n");	if (dyn_ip_tunnel_add(TEST_INTERFACE, a, any) != 0) {		fprintf(stderr, "M01: failed!\n\n");		ok = 0;	} else {		printf("Tunnel should have been added - ip tu:\n");		system(IP_PATH " tunnel show " TEST_INTERFACE);		printf("but it should not be up: ifconfig | grep "		       TEST_INTERFACE "\n");		system(IFCONFIG_PATH " | grep " TEST_INTERFACE);		printf("M01 OK\n\n");	}	printf("Test case M02: Set test interface up\n");	printf("dyn_ip_link_set_dev_up(" TEST_INTERFACE ")\n");	if (dyn_ip_link_set_dev_up(TEST_INTERFACE) != 0) {		fprintf(stderr, "M02: failed!\n\n");		ok = 0;	} else {		printf("Tunnel should now be up - ifconfig:\n");		system(IFCONFIG_PATH " " TEST_INTERFACE);		printf("M02 OK\n\n");	}	printf("Test case M03: Add local address to tunnel\n");	printf("Tunnel should not have the local address (yet) - ifconfig:\n");	system(IFCONFIG_PATH " " TEST_INTERFACE " | grep addr");	printf("dyn_ip_addr_add(" TEST_INTERFACE ", " LOCAL_ADDRESS ")\n");	if (dyn_ip_addr_add(TEST_INTERFACE, b) != 0) {		fprintf(stderr, "M03: failed!\n\n");		ok = 0;	} else {		printf("Tunnel should now have a local address - ifconfig:\n");		system(IFCONFIG_PATH " " TEST_INTERFACE " | grep addr");		printf("M03 OK\n\n");	}	printf("Test case M04: Add default route to routing table\n");	printf("No routing info should be found yet - ip:\n");	system(IP_PATH " route show table 99");	printf("dyn_ip_route_add_table(" TEST_INTERFACE ", 99, NULL)\n");	if (dyn_ip_route_add_table(TEST_INTERFACE, 99, NULL) != 0) {		fprintf(stderr, "M04: failed!\n\n");		ok = 0;	} else {		printf("Default route should have been added - ip:\n");		system(IP_PATH " route show table 99");		printf("M04 OK\n\n");	}	printf("Test case M05: Add rule for source routing\n");	printf("No rule in table 99 should be found yet - ip:\n");	system(IP_PATH " rule list");	printf("dyn_ip_rule_add_table(" TEST_ADDRESS ", 99," TEST_INTERFACE	       ")\n");	if (dyn_ip_rule_add_table(&a, NULL, 99, TEST_INTERFACE) != 0) {		fprintf(stderr, "M05: failed!\n\n");		ok = 0;	} else {		printf("Rule should have been added to table 99 - ip:\n");		system(IP_PATH " rule list");		printf("M05 OK\n\n");	}	printf("Test case M06: Remove the previously added rule\n");	printf("dyn_ip_rule_del_table(" TEST_ADDRESS ", 99, " TEST_INTERFACE	       ")\n");	if (dyn_ip_rule_del_table(&a, NULL, 99, TEST_INTERFACE) != 0) {		fprintf(stderr, "M06: failed!\n\n");		ok = 0;	} else {		printf("Rule should have been removed from table 99 - ip:\n");		system(IP_PATH " rule list");		printf("M06 OK\n\n");	}	printf("Test case M07: Add destination based route\n");	printf("dyn_ip_route_add(" TEST_ADDRESS ", " TEST_INTERFACE ")\n");	if (dyn_ip_route_add(a, TEST_INTERFACE) != 0) {		fprintf(stderr, "M07: failed!\n\n");		ok = 0;	} else {		printf("Route should have been added - ip:\n");		system(IP_PATH " route");		printf("M07 OK\n\n");	}	printf("Test case M07-B: Replace destination based route\n");	printf("dyn_ip_route_replace(" TEST_ADDRESS ",eth0)\n");	if (dyn_ip_route_replace(a, "eth0") != 0) {		fprintf(stderr, "M07-B: failed!\n\n");		ok = 0;	} else {		printf("Route should have been replaced - ip:\n");		system(IP_PATH " route");		printf("M07-B OK\n\n");	}	printf("Test case M08: Remove destination based route\n");	printf("dyn_ip_route_del(" TEST_ADDRESS ",eth0)\n");	if (dyn_ip_route_del(a, "eth0") != 0) { 		fprintf(stderr, "M08: failed!\n\n");		ok = 0;	} else {		printf("Route should have been deleted - ip:\n");		system(IP_PATH " route");		printf("M08 OK\n\n");	}	printf("Test case M08b: Add net route\n");	printf("dyn_ip_route_add_net(" TEST_INTERFACE ", " TEST_NET ", %i)\n",	       TEST_NET_PLEN);	if (dyn_ip_route_add_net(TEST_INTERFACE, &c, TEST_NET_PLEN) != 0) {		fprintf(stderr, "M08b: failed!\n\n");		ok = 0;	} else {		printf("The route entry should have been added - ip:\n");		system(IP_PATH " route list " TEST_NET "/" TEST_NET_PLENS);		printf("M08b OK\n\n");	}	printf("Test case M08c: Delete net route\n");	printf("dyn_ip_route_del_net(" TEST_INTERFACE ", " TEST_NET ", %i)\n",	       TEST_NET_PLEN);	if (dyn_ip_route_del_net(TEST_INTERFACE, &c, TEST_NET_PLEN) != 0) {		fprintf(stderr, "M08c: failed!\n\n");		ok = 0;	} else {		printf("The route entry should have been removed - ip:\n");		system(IP_PATH " route list " TEST_NET "/" TEST_NET_PLENS);		printf("M08c OK\n\n");	}	printf("Test case M09: Set the device down\n");	printf("dyn_ip_tunnel_del(" TEST_INTERFACE ")\n");	if (dyn_ip_link_set_dev_down(TEST_INTERFACE) != 0) {		fprintf(stderr, "M09: failed!\n\n");		ok = 0;	} else {		printf("Device should be down - ifconfig:\n");		system(IFCONFIG_PATH " " TEST_INTERFACE);		printf("M09 OK\n\n");	}	printf("Test case M10: Remove the tunnel\n");	printf("dyn_ip_tunnel_del(" TEST_INTERFACE ")\n");	if (dyn_ip_tunnel_del(TEST_INTERFACE) != 0) {		fprintf(stderr, "M10: failed!\n\n");		ok = 0;	} else {		printf("Tunnel should have been removed - ifconfig:\n");		system(IFCONFIG_PATH " " TEST_INTERFACE);		printf("M10 OK\n\n");	}	printf("Test case M11: Add a GRE tunnel\n");	printf("The tunnel should not exist already - ifconfig:\n");	system(IFCONFIG_PATH " " TEST_INTERFACE);	printf("dyn_ip_tunnel_add_gre(" TEST_INTERFACE ", " TEST_ADDRESS	       ",1000)\n");	if (dyn_ip_tunnel_add_gre(TEST_INTERFACE, a, any, 1000) != 0) {		fprintf(stderr, "M11: failed!\n\n");		ok = 0;	} else {		printf("Tunnel should have been added - ip tu:\n");		system(IP_PATH " tunnel show " TEST_INTERFACE);		printf("but it should not be up: ifconfig | grep "		       TEST_INTERFACE "\n");		system(IFCONFIG_PATH " | grep " TEST_INTERFACE);		printf("M11 OK\n\n");	}	printf("Test case M11: Remove the GRE tunnel\n");	printf("dyn_ip_tunnel_del(" TEST_INTERFACE ")\n");	if (dyn_ip_tunnel_del(TEST_INTERFACE) != 0) {		fprintf(stderr, "M11: failed!\n\n");		ok = 0;	} else {		printf("Tunnel should have been removed - ifconfig:\n");		system(IFCONFIG_PATH " " TEST_INTERFACE);		printf("M11 OK\n\n");	}	if (ok == 0) {		fprintf(stderr, "There were some errors found during "			"testing!\n");		if (geteuid() != 0) {			fprintf(stderr, "This should, however, come as no "				"suprise because you aren't root.\n");		}		exit(1);	}	printf("Tunnel and route interface routine tests completed "	       "successfully\n");}static void dyn_ip_get(char *dst){	struct in_addr addr;	char buf[10];	int ret;	inet_aton(dst, &addr);	ret = dyn_ip_route_get(addr, buf, sizeof(buf));	if (ret != 0) {		fprintf(stderr, "dyn_ip_route_get failed => %i\n", ret);		exit(-1);	}	printf("%s => %s\n", dst, buf);}static void dyn_ip_local(char *dst){	struct in_addr addr, local;	int ret;	inet_aton(dst, &addr);

⌨️ 快捷键说明

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