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

📄 arptest.c

📁 mobile ip 在linux下的一种实现
💻 C
字号:
/* $Id: arptest.c,v 1.8 2000/04/06 07:26:53 jm Exp $ * ARP interface tests * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2000, 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 <net/if_arp.h>#include "proxyarp.h"#define MAC_ADDR_SIZE 6#define TEST_INTERFACE "eth0"#define TEST_ADDRESS "192.168.250.230"#define ARP_PATH "/sbin/arp"static void arp_tests(void){	struct in_addr a;	struct sockaddr mac;	int ok = 1;		printf("ARP interface routine tests\n"	       "(this test must be run with root privileges)\n\n");	printf("Test case M01: Add an entry with valid parameters\n");	printf("proxyarp_add_item(" TEST_ADDRESS ", " TEST_INTERFACE ")\n");	inet_aton(TEST_ADDRESS, &a);	if (proxyarp_add_item(a, TEST_INTERFACE) != 0) {		fprintf(stderr, "M01: proxyarp_add_item test failed!\n\n");		ok = 0;	} else {		printf("Kernel ARP cache: (should have proxyarp entry)\n");		system(ARP_PATH " " TEST_ADDRESS);		printf("M01 OK\n\n");	}	printf("Test case M02: Remove the previously added entry\n");	printf("proxyarp_del_item(" TEST_ADDRESS ", " TEST_INTERFACE ")\n");	if (proxyarp_del_item(a, TEST_INTERFACE) != 0) {		fprintf(stderr, "M02: proxyarp_del_item test failed!\n\n");		ok = 0;	} else {		printf("Kernel ARP cache:  (the entry should have been "		       "removed)\n");		system(ARP_PATH " " TEST_ADDRESS);		printf("M02 OK\n\n");	}	printf("Test case M03: Remove an entry that is not in the ARP cache"	       "\n");	printf("proxyarp_del_item(" TEST_ADDRESS ", " TEST_INTERFACE ")\n");	if (proxyarp_del_item(a, TEST_INTERFACE) == 0) {		fprintf(stderr, "M03: proxyarp_del_item test failed!\n\n");		ok = 0;	} else {		printf("M03 OK\n\n");	}	printf("Test case M04: add with invalid argument (NULL pointer to "	       "interface)\n");	printf("proxyarp_add_item(" TEST_ADDRESS ", NULL)\n");	if (proxyarp_add_item(a, NULL) == 0) {		fprintf(stderr, "M04: proxyarp_add_item test failed!\n\n");		ok = 0;	} else {		printf("M04 OK\n\n");	}	printf("Test case M05: del with invalid argument (NULL pointer to "	       "interface)\n");	printf("proxyarp_del_item(" TEST_ADDRESS ", NULL)\n");	if (proxyarp_del_item(a, NULL) == 0) {		fprintf(stderr, "M05: proxyarp_del_item test failed!\n\n");		ok = 0;	} else {		printf("M05 OK\n\n");	}	printf("Test case M06: add with invalid argument (too long "	       "interface)\n");	printf("proxyarp_add_item(" TEST_ADDRESS ", 1234567890123456)\n");	if (proxyarp_add_item(a, "1234567890123456") == 0) {		fprintf(stderr, "M06: proxyarp_add_item test failed!\n\n");		ok = 0;	} else {		printf("M06 OK\n\n");	}	printf("Test case M07: del with invalid argument (too long "	       "interface)\n");	printf("proxyarp_del_item(" TEST_ADDRESS ", 1234567890123456)\n");	if (proxyarp_del_item(a, "1234567890123456") == 0) {		fprintf(stderr, "M07: proxyarp_del_item test failed!\n\n");		ok = 0;	} else {		printf("M07 OK\n\n");	}	printf("Test case M07: add with invalid argument (unknown "	       "interface)\n");	printf("proxyarp_add_item(" TEST_ADDRESS ", unknown0)\n");	if (proxyarp_add_item(a, "unknown0") == 0) {		fprintf(stderr, "M07: proxyarp_add_item test failed!\n\n");		ok = 0;	} else {		printf("M07 OK\n\n");	}	printf("Test case M08: del with invalid argument (unknown "	       "interface)\n");	printf("proxyarp_del_item(" TEST_ADDRESS ", unknown0)\n");	if (proxyarp_del_item(a, "unknown0") == 0) {		fprintf(stderr, "M08: proxyarp_del_item test failed!\n\n");		ok = 0;	} else {		printf("M08 OK\n\n");	}	printf("Test case M09: Add an entry with valid parameters twice\n");	printf("proxyarp_add_item(" TEST_ADDRESS ", " TEST_INTERFACE ")\n");	if (proxyarp_add_item(a, TEST_INTERFACE) != 0) {		fprintf(stderr, "M09: proxyarp_add_item test failed!\n\n");		ok = 0;	} else {		/* kernel should not add the same entry twice, but the call		 * should succeed */		if (proxyarp_add_item(a, TEST_INTERFACE) != 0) {			fprintf(stderr, "M09: second proxyarp_add_item test "				"failed!\n\n");			ok = 0;		} else {			printf("Kernel ARP cache: (should have proxyarp entry"			       " only once)\n");			system(ARP_PATH " " TEST_ADDRESS);			printf("M09 OK\n\n");		}	}	printf("M09 cleanup\n");	if (proxyarp_del_item(a, TEST_INTERFACE) != 0) {		fprintf(stderr, "M09: could not clean ARP cache!\n\n");		ok = 0;	}	printf("\nTest case M10: Add a permanent arp entry with valid "	       "parameters\n");	printf("arp_add_permanent_item(" TEST_ADDRESS ", " 	       TEST_INTERFACE ", ");	memset(&mac, 0, sizeof(struct sockaddr));	mac.sa_data[0] = 0x00;	mac.sa_data[1] = 0x10; 	mac.sa_data[2] = 0x6A;	mac.sa_data[3] = (char)0xDC;	mac.sa_data[4] = 0x62;	mac.sa_data[5] = (char)0xAE;	mac.sa_family = ARPHRD_ETHER;	printf("%02X:%02X:%02X:%02X:%02X:%02X)\n",	       (mac.sa_data[0] & 0xff),	       (mac.sa_data[1] & 0xff),	       (mac.sa_data[2] & 0xff),	       (mac.sa_data[3] & 0xff),	       (mac.sa_data[4] & 0xff),	       (mac.sa_data[5] & 0xff));	if (arp_add_permanent_item(a, TEST_INTERFACE, &mac) != 0) {		fprintf(stderr, "M10: arp_add_permanent_item "			"test failed!\n\n");		ok = 0;	}	else {		printf("Kernel ARP cache:\n");		system(ARP_PATH " " TEST_ADDRESS);		printf("M10 OK\n\n");				}	printf("M10 cleanup\n");	if (arp_del_permanent_item(a, TEST_INTERFACE) != 0) {		fprintf(stderr, "M10: could not clean ARP cache!\n\n");		ok = 0;	}	printf("Kernel ARP cache: (no entries should be found)\n");	system(ARP_PATH " " TEST_ADDRESS);	printf("\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("ARP interface routine tests completed successfully\n");}static void show_usage(){	fprintf(stderr, "proxyarp interface routine tests\n"		"\tarptest test = basic module tests\n"		"\tarptest add <ip addr> <interface> = add proxyarp "		"entry\n"		"\tarptest del <ip addr> <interface> = remove "		"proxyarp entry\n"		"\tarptest gratuitous <ip addr> <interface> = send a "		"gratuitous ARP message\n");}int main(int argc, char *argv[]){	int command;	struct in_addr a;	if (argc < 2) {		show_usage();		return 0;	}	if (strcmp(argv[1], "test") == 0) {		arp_tests();		return 0;	}	command = 0;	if (strcmp(argv[1], "add") == 0)		command = 1;	else if (strcmp(argv[1], "del") == 0)		command = 2;	else if (strcmp(argv[1], "gratuitous") == 0)		command = 3;	if (command == 0) {		fprintf(stderr, "Unknown command '%s'\n\n", argv[1]);		show_usage();		return -1;	}	if (argc != 4) {		fprintf(stderr, "Invalid number of arguments\n");		show_usage();		return -1;	}	if (!inet_aton(argv[2], &a)) {		fprintf(stderr, "Invalid address '%s'\n", argv[2]);		return -1;	}	switch (command) {	case 1:		printf("Calling proxyarp_add_item");		break;	case 2:		printf("Calling proxyarp_del_item");		break;	case 3:		printf("Calling proxyarp_gratuitous");		break;	}	printf("(%s, %s)\n", argv[2], argv[3]);	switch (command) {	case 1:		if (proxyarp_add_item(a, argv[3]) != 0)			fprintf(stderr, "proxyarp_add_item failed\n");		break;	case 2:		if (proxyarp_del_item(a, argv[3]) != 0)			fprintf(stderr, "proxyarp_del_item failed\n");		break;	case 3:		if (proxyarp_gratuitous(a, argv[3]) != 0)			fprintf(stderr, "proxyarp_gratuitous failed\n");		break;	}	return 0;}

⌨️ 快捷键说明

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