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

📄 tunnel_test.c

📁 mobile ip 在linux下的一种实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: tunnel_test.c,v 1.39 2001/02/25 09:38:29 jm Exp $ * Tunnel module test functions * * 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. */#ifndef _GNU_SOURCE#define _GNU_SOURCE#endif#include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#include <netinet/in.h>#include <net/if.h>#include <arpa/inet.h>#include <string.h>#include <syslog.h>#include <assert.h>#include "tunnel.h"#include "hashtable.h"#include "debug.h"#include "list.h"#include "message.h"#include <assert.h>#include "binding.h"#define DEBUG_FLAG 'T'#define IP_PATH "/sbin/ip "#ifndef TRUE#define TRUE 1#endif#ifndef FALSE#define FALSE 0#endif#ifndef ASSERT#define ASSERT assert#endifextern int opt_debug;void test_many_tunnels(int num){	struct hashtable *hash;	int i, res;	struct in_addr dst, local;	char device[IFNAMSIZ];	struct tunnel *tunl;	printf("test_many_tunnels(%i)\n", num);	if (num < 1) {		return;	}	system("/sbin/ip tunnel show");	inet_aton("127.0.0.1", &local);	hash = tunnel_init("TEST", 1, 253);	assert(hash != NULL);	inet_aton("1.2.3.4", &dst);	for (i = 0; i < num; i++) {		tunl = tunnel_add(hash, dst, device, local, 1, TUNNEL_IPIP, 0);		if (tunl == NULL)			printf("%i. failed!\n", i);		else			printf("%i. tunnel_add dev=%s, ref=%i\n", i,			       tunl->device, tunl->ref);		dst.s_addr++;	}	printf("\ntunnels done\n");	system("/sbin/ip tunnel show");	printf("\nremoving tunnels\n");	inet_aton("1.2.3.4", &dst);	for (i = 0; i < num; i++) {		res = tunnel_delete(hash, dst, 1, TUNNEL_IPIP, 0);		if (res != 1)			printf("%i. tunnel_delete => %i\n", i, res);		dst.s_addr++;	}	system("/sbin/ip tunnel show");}int main(int argc, char *argv[]){	int tester, res, fetchcount, deletion_result;	int addcount = 0, ok = 1;	struct hashtable *hash;	struct tunnel *tunl = NULL;	struct in_addr local, dst[5];	char device[IFNAMSIZ];	struct bindingentry binding[5];	struct tunnel *t[5];	opt_debug = 1;	if (argc == 2) {		printf("(this test must be run with root privileges)\n\n");		test_many_tunnels(atoi(argv[1]));		return 0;	}	printf("\n"	       "\t **********************************\n"	       "\t     STARTING TUNNEL MODULE TESTS!\n"	       "\t ***********************************\n\n");	printf("(this test must be run with root privileges)\n\n");	memset((char *) &local, 0, sizeof(local));	inet_aton((char *)"127.0.0.1", &local);	memset((char *) &dst[0], 0, sizeof(dst[0]));	inet_aton((char *)"10.0.0.1", &dst[0]);	memset((char *) &dst[1], 0, sizeof(dst[1]));	inet_aton((char *)"10.0.0.2", &dst[1]);	memset((char *) &dst[2], 0, sizeof(dst[2]));	inet_aton((char *)"10.0.0.3", &dst[2]);	memset((char *) &dst[3], 0, sizeof(dst[3]));	inet_aton((char *)"10.0.0.4", &dst[3]);	memset((char *) &dst[4], 0, sizeof(dst[4]));	inet_aton((char *)"10.0.0.5", &dst[4]);	memset((char *) &binding[0], 0, sizeof(binding[0]));	inet_aton((char *)"10.0.100.1", &binding[0].mn_addr);	memset((char *) &binding[1], 0, sizeof(binding[1]));	inet_aton((char *)"10.0.100.2", &binding[1].mn_addr);	memset((char *) &binding[2], 0, sizeof(binding[2]));	inet_aton((char *)"10.0.100.3", &binding[2].mn_addr);	memset((char *) &binding[3], 0, sizeof(binding[3]));	inet_aton((char *)"10.0.100.4", &binding[3].mn_addr);	memset((char *) &binding[4], 0, sizeof(binding[4]));	inet_aton((char *)"10.0.100.5", &binding[4].mn_addr);	/* M01 */        printf("\nTest case M01: Initialize, local loop as my_ip.\n");	DEBUG(DEBUG_FLAG,"tunnel_test: Tunnel hashtable initializing\n");	hash = tunnel_init("TUNL", 1, 253);	ASSERT(hash != NULL);	printf("M01 test cases (1) DONE\n");	/* M02 */        printf("\nTest case M02: Testing tunnel_add"	       " with several addresses.\n");	DEBUG(DEBUG_FLAG,"\ntunnel_test: Tunnel to %s: adding\n",	      inet_ntoa(dst[0]));	t[0] = tunnel_add(hash, binding[0].mn_addr, binding[0].tun_dev,			  local, 1, TUNNEL_IPIP, 0);	if (t[0] == NULL || t[0]->ref != 1) {		printf("M02 FAILED!\n");		ok = 0;	}	DEBUG(DEBUG_FLAG,"\ntunnel_test: Tunnel to %s: adding\n",	      inet_ntoa(dst[1]));	t[1] = tunnel_add(hash, binding[1].mn_addr, binding[1].tun_dev,			  local, 1, TUNNEL_IPIP, 0);	if (t[1] == NULL || t[1]->ref != 1) {		printf("M02 FAILED!\n");		ok = 0;	}	DEBUG(DEBUG_FLAG,"\ntunnel_test: Tunnel to %s: adding\n",	      inet_ntoa(dst[2]));	t[2] = tunnel_add(hash, binding[2].mn_addr, binding[2].tun_dev,			  local, 1, TUNNEL_IPIP, 0);	if (t[2] == NULL || t[2]->ref != 1) {		printf("M02 FAILED!\n");		ok = 0;	}	DEBUG(DEBUG_FLAG,"\ntunnel_test: Tunnel to %s: adding\n",	      inet_ntoa(dst[3]));	t[3] = tunnel_add(hash, binding[3].mn_addr, binding[3].tun_dev,			  local, 1, TUNNEL_IPIP, 0);	if (t[3] == NULL || t[3]->ref != 1) {		printf("M02 FAILED!\n");		ok = 0;	}	DEBUG(DEBUG_FLAG,"\ntunnel_test: Tunnel to %s: adding\n",	      inet_ntoa(dst[4]));	t[4] = tunnel_add(hash, binding[4].mn_addr, binding[4].tun_dev,			  local, 1, TUNNEL_IPIP, 0);	if (t[4] == NULL || t[4]->ref != 1) {		printf("M02 FAILED!\n");		ok = 0;	}	printf("M02 test cases (5) DONE\n");	/* M03 */	printf("\nTest case M03: Adding another tunnel to dst[2]!\n");	printf("This is like adding a virtual circuit "	       "into an existing tunnel.\n");	DEBUG(DEBUG_FLAG, "\ntunnel_test: Tunnel to %s: adding\n",	      inet_ntoa(dst[2]));	t[2] = tunnel_add(hash, binding[2].mn_addr, device, local, 1,			  TUNNEL_IPIP, 0);	if (t[2] == NULL || t[2]->ref != 2) {		printf("M03 FAILED!\n");		ok = 0;	}	printf("M03 test cases (1) DONE!\n");	/* M04 */	printf("\nTest case M04: Fetching a tunnel\n");	DEBUG(DEBUG_FLAG,"\ntunnel_test: Fetching %s\n",inet_ntoa(dst[0]));	if ((tunl = tunnel_fetch(hash, binding[0].mn_addr, TUNNEL_IPIP, 0)) !=	    NULL) {		DEBUG(DEBUG_FLAG,"tunnel_test:"		      "Tunnel structure (%s) fetched\n",		      inet_ntoa(dst[0]));	} else {		DEBUG(DEBUG_FLAG, "tunnel_test: Couldn't fetch (%s)\n",		      inet_ntoa(dst[0]));		printf("M04 Test cases (1) FAILED!\n");		ok = 0;	}	printf("M04 test cases (1) DONE!\n");	/* M05 */	printf("\nTest case M05: deleting a tunnel\n");	DEBUG(DEBUG_FLAG, "\ntunnel_test: Tunnel to %s: "	      "deleting\n", inet_ntoa(binding[0].mn_addr));	deletion_result = tunnel_delete(hash, binding[0].mn_addr, 1,					TUNNEL_IPIP, 0);	if (deletion_result != 1) {		printf("M05 FAILED!\n");		ok = 0;	}	printf("M05 Test cases (1) DONE!\n");	/* M06 */	printf("\nTest case M06: fetching a previously deleted tunnel\n");	DEBUG(DEBUG_FLAG,"\ntunnel_test: Fetching AGAIN %s\n",	      inet_ntoa(dst[0]));	if ((tunl = tunnel_fetch(hash, binding[0].mn_addr, TUNNEL_IPIP, 0)) !=	    NULL) {		DEBUG(DEBUG_FLAG,"tunnel_test: "		      "Tunnel structure (%s) fetched\n",		      inet_ntoa(binding[0].mn_addr));		printf("M06 Test cases (1) FAILED!\n");		ok = 0;	} else {		DEBUG(DEBUG_FLAG,"tunnel_test: Couldn't fetch (%s)\n",		      inet_ntoa(binding[0].mn_addr));		printf("M06 Test cases (1) DONE!\n");	}	/* M07 */	printf("\nTest case M07: Fetching and deleting many tunnels\n");	fetchcount = 0;	deletion_result = 0;	DEBUG(DEBUG_FLAG,"\ntunnel_test: Fetching %s\n", inet_ntoa(dst[1]));	if ((tunl = tunnel_fetch(hash, binding[1].mn_addr, TUNNEL_IPIP, 0)) !=	    NULL) {		DEBUG(DEBUG_FLAG,"tunnel_test:"		      "Tunnel structure (%s) fetched\n",		      inet_ntoa(binding[1].mn_addr));		fetchcount++;	} else {		DEBUG(DEBUG_FLAG,"tunnel_test: Couldn't fetch (%s)\n",		      inet_ntoa(binding[1].mn_addr));		printf("M07 FAILED!\n");		ok = 0;	}	DEBUG(DEBUG_FLAG,"\ntunnel_test: Tunnel to %s: deleting\n",	      inet_ntoa(dst[1]));	deletion_result = tunnel_delete(hash, binding[1].mn_addr, 1,					TUNNEL_IPIP, 0);	printf("The first deletion result is: %d\n", deletion_result);	DEBUG(DEBUG_FLAG,"\ntunnel_test: Fetching %s\n",inet_ntoa(dst[2]));	if ((tunl = tunnel_fetch(hash, binding[2].mn_addr, TUNNEL_IPIP, 0)) !=	   NULL) {		DEBUG(DEBUG_FLAG,"tunnel_test:"		      "Tunnel structure (%s) fetched\n",		      inet_ntoa(binding[2].mn_addr));		fetchcount++;	} else		DEBUG(DEBUG_FLAG, "tunnel_test: Couldn't fetch (%s)\n",		      inet_ntoa(binding[2].mn_addr));	DEBUG(DEBUG_FLAG,"\ntunnel_test: Tunnel to %s: deleting\n",	      inet_ntoa(binding[2].mn_addr));	deletion_result = tunnel_delete(hash, binding[2].mn_addr, 1,					TUNNEL_IPIP, 0);	printf("The second deletion result is: %d\n", deletion_result);	if (fetchcount != 2) {		printf("M07 FAILED!\n");		ok = 0;	}	printf("M07 test cases (2) DONE!\n");	/* M08 */	printf("\nTest case M08: Deleting a previously deleted tunnel\n");	DEBUG(DEBUG_FLAG, "\ntunnel_test: Tunnel to %s: deleting\n",	      inet_ntoa(binding[2].mn_addr));	deletion_result = tunnel_delete(hash, binding[2].mn_addr, 1,					TUNNEL_IPIP, 0);	printf("deletion_result: %d\n", deletion_result);	if (deletion_result != 1) {		printf("M08 FAILED!\n");		ok = 0;	}	/* M09 */	printf("\nTest case M09: Blind delete without fetching\n");	DEBUG(DEBUG_FLAG, "\ntunnel_test: Tunnel to %s: deleting\n",	      inet_ntoa(binding[3].mn_addr));	deletion_result = tunnel_delete(hash, binding[3].mn_addr, 1,					TUNNEL_IPIP, 0);	if (deletion_result != 1) {		printf("M09 FAILED!\n");		ok = 0;	}	DEBUG(DEBUG_FLAG,"\ntunnel_test: Tunnel to %s: deleting\n",	      inet_ntoa(binding[4].mn_addr));	deletion_result = tunnel_delete(hash, binding[4].mn_addr, 1,					TUNNEL_IPIP, 0);	if (deletion_result != 1) {		printf("M09 FAILED!\n");

⌨️ 快捷键说明

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