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

📄 arp.c

📁 Linux下网络相关工具源代码。
💻 C
📖 第 1 页 / 共 2 页
字号:
    }    /* Read the lines in the file. */    linenr = 0;    while (fgets(buff, sizeof(buff), fp) != (char *) NULL) {	linenr++;	if (opt_v == 1)	    fprintf(stderr, ">> %s", buff);	if ((sp = strchr(buff, '\n')) != (char *) NULL)	    *sp = '\0';	if (buff[0] == '#' || buff[0] == '\0')	    continue;	argc = getargs(buff, args);	if (argc < 2) {	    fprintf(stderr, _("arp: format error on line %u of etherfile %s !\n"),		    linenr, name);	    continue;	}	if (arp_set(args) != 0)	    fprintf(stderr, _("arp: cannot set entry on line %u of etherfile %s !\n"),		    linenr, name);    }    (void) fclose(fp);    return (0);}/* Print the contents of an ARP request block. */static void arp_disp_2(char *name, int type, int arp_flags, char *hwa, char *mask, char *dev){    static int title = 0;    struct hwtype *xhw;    char flags[10];    xhw = get_hwntype(type);    if (xhw == NULL)	xhw = get_hwtype(DFLT_HW);    if (title++ == 0) {	printf(_("Address\t\t\tHWtype\tHWaddress\t    Flags Mask\t\t  Iface\n"));    }    /* Setup the flags. */    flags[0] = '\0';    if (arp_flags & ATF_COM)	strcat(flags, "C");    if (arp_flags & ATF_PERM)	strcat(flags, "M");    if (arp_flags & ATF_PUBL)	strcat(flags, "P");#ifdef HAVE_ATF_MAGIC    if (arp_flags & ATF_MAGIC)	strcat(flags, "A");#endif#ifdef HAVE_ATF_DONTPUB    if (arp_flags & ATF_DONTPUB)	strcat(flags, "!");#endif    if (arp_flags & ATF_USETRAILERS)	strcat(flags, "T");    if (!(arp_flags & ATF_NETMASK))	mask = "";    printf("%-23.23s\t", name);    if (!(arp_flags & ATF_COM)) {	if (arp_flags & ATF_PUBL)	    printf("%-8.8s%-20.20s", "*", "*");	else	    printf("%-8.8s%-20.20s", "", _("(incomplete)"));    } else {	printf("%-8.8s%-20.20s", xhw->name, hwa);    }    printf("%-6.6s%-15.15s %s\n", flags, mask, dev);}/* Print the contents of an ARP request block. */static void arp_disp(char *name, char *ip, int type, int arp_flags, char *hwa, char *mask, char *dev){    struct hwtype *xhw;    xhw = get_hwntype(type);    if (xhw == NULL)	xhw = get_hwtype(DFLT_HW);    printf(_("%s (%s) at "), name, ip);    if (!(arp_flags & ATF_COM)) {	if (arp_flags & ATF_PUBL)	    printf("* ");	else	    printf(_("<incomplete> "));    } else {	printf("%s [%s] ", hwa, xhw->name);    }    if (arp_flags & ATF_NETMASK)	printf(_("netmask %s "), mask);    if (arp_flags & ATF_PERM)	printf("PERM ");    if (arp_flags & ATF_PUBL)	printf("PUP ");#ifdef HAVE_ATF_MAGIC    if (arp_flags & ATF_MAGIC)	printf("AUTO ");#endif#ifdef HAVE_ATF_DONTPUB    if (arp_flags & ATF_DONTPUB)	printf("DONTPUB ");#endif    if (arp_flags & ATF_USETRAILERS)	printf("TRAIL ");    printf(_("on %s\n"), dev);}/* Display the contents of the ARP cache in the kernel. */static int arp_show(char *name){    char host[100];    struct sockaddr sa;    char ip[100];    char hwa[100];    char mask[100];    char line[200];    char dev[100];    int type, flags;    FILE *fp;    char *hostname;    int num, entries = 0, showed = 0;    host[0] = '\0';    if (name != NULL) {	/* Resolve the host name. */	safe_strncpy(host, name, (sizeof host));	if (ap->input(0, host, &sa) < 0) {	    ap->herror(host);	    return (-1);	}	strcpy(host, ap->sprint(&sa, 1));    }    /* Open the PROCps kernel table. */    if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL) {	perror(_PATH_PROCNET_ARP);	return (-1);    }    /* Bypass header -- read until newline */    if (fgets(line, sizeof(line), fp) != (char *) NULL) {	strcpy(mask, "-");	strcpy(dev, "-");	/* Read the ARP cache entries. */	for (; fgets(line, sizeof(line), fp);) {	    num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n",			 ip, &type, &flags, hwa, mask, dev);	    if (num < 4)		break;	    entries++;	    /* if the user specified hw-type differs, skip it */	    if (hw_set && (type != hw->type))		continue;	    /* if the user specified address differs, skip it */	    if (host[0] && strcmp(ip, host))		continue;	    /* if the user specified device differs, skip it */	    if (device[0] && strcmp(dev, device))		continue;	    showed++;	    /* This IS ugly but it works -be */	    if (opt_n)		hostname = "?";	    else {		if (ap->input(0, ip, &sa) < 0)		    hostname = ip;		else		    hostname = ap->sprint(&sa, opt_n | 0x8000);		if (strcmp(hostname, ip) == 0)		    hostname = "?";	    }	    if (opt_e)		arp_disp_2(hostname[0] == '?' ? ip : hostname, type, flags, hwa, mask, dev);	    else		arp_disp(hostname, ip, type, flags, hwa, mask, dev);	}    }    if (opt_v)	printf(_("Entries: %d\tSkipped: %d\tFound: %d\n"), entries, entries - showed, showed);    if (!showed) {	if (host[0] && !opt_a)	    printf(_("%s (%s) -- no entry\n"), name, host);	else if (hw_set || host[0] || device[0]) {	    printf(_("arp: in %d entries no match found.\n"), entries);	}    }    (void) fclose(fp);    return (0);}static void version(void){    fprintf(stderr, "%s\n%s\n%s\n", Release, Version, Features);    exit(E_VERSION);}static void usage(void){    fprintf(stderr, _("Usage:\n  arp [-vn]  [<HW>] [-i <if>] [-a] [<hostname>]             <-Display ARP cache\n"));    fprintf(stderr, _("  arp [-v]          [-i <if>] -d  <hostname> [pub][nopub]    <-Delete ARP entry\n"));    fprintf(stderr, _("  arp [-vnD] [<HW>] [-i <if>] -f  <filename>              <-Add entry from file\n"));    fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -s  <hostname> <hwaddr> [temp][nopub] <-Add entry\n"));    fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -s  <hostname> <hwaddr> [netmask <nm>] pub  <-''-\n"));    fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -Ds <hostname> <if> [netmask <nm>] pub      <-''-\n\n"));        fprintf(stderr, _("        -a                       display (all) hosts in alternative (BSD) style\n"));    fprintf(stderr, _("        -s, --set                set a new ARP entry\n"));    fprintf(stderr, _("        -d, --delete             delete a specified entry\n"));    fprintf(stderr, _("        -v, --verbose            be verbose\n"));    fprintf(stderr, _("        -n, --numeric            dont resolve names\n"));    fprintf(stderr, _("        -i, --device             specify network interface (e.g. eth0)\n"));    fprintf(stderr, _("        -D, --use-device         read <hwaddr> from given device\n"));    fprintf(stderr, _("        -f, --file               read new entries from file\n\n"));    fprintf(stderr, _("  <HW>=Use '-H <hw>' to specify hardware address type. Default: %s\n"), DFLT_HW);    fprintf(stderr, _("  List of possible hardware types (which support ARP):\n"));    print_hwlist(1); /* 1 = ARPable */    exit(E_USAGE);}int main(int argc, char **argv){    int i, lop, what;    struct option longopts[] =    {	{"verbose", 0, 0, 'v'},	{"version", 0, 0, 'V'},	{"all", 0, 0, 'a'},	{"delete", 0, 0, 'd'},	{"file", 0, 0, 'f'},	{"numeric", 0, 0, 'n'},	{"set", 0, 0, 's'},	{"protocol", 1, 0, 'A'},	{"hw-type", 1, 0, 'H'},	{"device", 0, 0, 'i'},	{"help", 0, 0, 'h'},	{"use-device", 0, 0, 'D'},	{"symbolic", 0, 0, 'N'},	{NULL, 0, 0, 0}    };#if I18N    bindtextdomain("net-tools", "/usr/share/locale");    textdomain("net-tools");#endif    /* Initialize variables... */    if ((hw = get_hwtype(DFLT_HW)) == NULL) {	fprintf(stderr, _("%s: hardware type not supported!\n"), DFLT_HW);	return (-1);    }    if ((ap = get_aftype(DFLT_AF)) == NULL) {	fprintf(stderr, _("%s: address family not supported!\n"), DFLT_AF);	return (-1);    }    what = 0;    /* Fetch the command-line arguments. */    /* opterr = 0; */    while ((i = getopt_long(argc, argv, "A:H:adfp:nsei:t:vh?DNV", longopts, &lop)) != EOF)	switch (i) {	case 'a':	    what = 1;	    opt_a = 1;	    break;	case 'f':	    what = 2;	    break;	case 'd':	    what = 3;	    break;	case 's':	    what = 4;	    break;	case 'e':	    opt_e = 1;	    break;	case 'n':	    opt_n = FLAG_NUM;	    break;	case 'D':	    opt_D = 1;	    break;	case 'N':	    opt_N = FLAG_SYM;	    fprintf(stderr, _("arp: -N not yet supported.\n"));	    break;	case 'v':	    opt_v = 1;	    break;	case 'A':	case 'p':	    ap = get_aftype(optarg);	    if (ap == NULL) {		fprintf(stderr, _("arp: %s: unknown address family.\n"),			optarg);		exit(-1);	    }	    break;	case 'H':	case 't':	    hw = get_hwtype(optarg);	    if (hw == NULL) {		fprintf(stderr, _("arp: %s: unknown hardware type.\n"),			optarg);		exit(-1);	    }	    hw_set = 1;	    break;	case 'i':	    safe_strncpy(device, optarg, sizeof(device));	    break;	case 'V':	    version();	case '?':	case 'h':	default:	    usage();	}    if (ap->af != AF_INET) {	fprintf(stderr, _("arp: %s: kernel only supports 'inet'.\n"),		ap->name);	exit(-1);    }    if (hw->alen <= 0) {	fprintf(stderr, _("arp: %s: hardware type without ARP support.\n"),		hw->name);	exit(-1);    }    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {	perror("socket");	exit(-1);    }    /* Now see what we have to do here... */    switch (what) {    case 0:	opt_e = 1;	what = arp_show(argv[optind]);	break;    case 1:			/* show an ARP entry in the cache */	what = arp_show(argv[optind]);	break;    case 2:			/* process an EtherFile */	what = arp_file(argv[optind]);	break;    case 3:			/* delete an ARP entry from the cache */	what = arp_del(&argv[optind]);	break;    case 4:			/* set an ARP entry in the cache */	what = arp_set(&argv[optind]);	break;    default:	usage();    }    exit(what);}

⌨️ 快捷键说明

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