コード例 #1
0
ファイル: mii-tool.c プロジェクト: wanghao-xznu/vte
int main(int argc, char **argv)
{
    int i, c, ret, errflag = 0;
    char s[6];
    while ((c = getopt_long(argc, argv, "A:F:p:lrRvVw?", longopts, 0)) != EOF)
	switch (c) {
	case 'A': nway_advertise = parse_media(optarg); break;
	case 'F': fixed_speed = parse_media(optarg); break;
	case 'p': override_phy = atoi(optarg); break;
	case 'r': opt_restart++;	break;
	case 'R': opt_reset++;		break;
	case 'v': verbose++;		break;
	case 'V': opt_version++;	break;
	case 'w': opt_watch++;		break;
	case 'l': opt_log++;		break;
	case '?': errflag++;
	}
    /* Check for a few inappropriate option combinations */
    if (opt_watch) verbose = 0;
    if (errflag || (fixed_speed & (fixed_speed-1)) ||
	(fixed_speed && (opt_restart || nway_advertise))) {
	fprintf(stderr, usage, argv[0]);
	return 2;
    }

    if (opt_version)
	printf(version);

    /* Open a basic socket. */
    if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0) {
	perror("socket");
	exit(-1);
    }

    /* No remaining args means show all interfaces. */
    if (optind == argc) {
	ret = 1;
	for (i = 0; i < MAX_ETH; i++) {
	    sprintf(s, "eth%d", i);
	    ret &= do_one_xcvr(skfd, s, 1);
	}
	if (ret)
	    fprintf(stderr, "no MII interfaces found\n");
    } else {
	ret = 0;
	for (i = optind; i < argc; i++) {
	    ret |= do_one_xcvr(skfd, argv[i], 0);
	}
    }

    if (opt_watch && (ret == 0)) {
	while (1) {
	    sleep(1);
	    if (optind == argc) {
		for (i = 0; i < MAX_ETH; i++) {
		    sprintf(s, "eth%d", i);
		    watch_one_xcvr(skfd, s, i);
		}
	    } else {
		for (i = optind; i < argc; i++)
		    watch_one_xcvr(skfd, argv[i], i-optind);
	    }
	}
    }

    close(skfd);
    return ret;
}
コード例 #2
0
ファイル: mii-diag.c プロジェクト: 4doe/openembedded
int
main(int argc, char **argv)
{
	int c, errflag = 0;
	char **spp, *ifname;
    char *progname = rindex(argv[0], '/') ? rindex(argv[0], '/')+1 : argv[0];

	while ((c = getopt_long(argc, argv, shortopts, longopts, 0)) != EOF)
		switch (c) {
		case 'a': opt_a++; break;
		case 'A': nway_advertise |= parse_advertise(optarg);
			if (nway_advertise == -1) errflag++;
			break;
		case 'C': set_BMCR = strtoul(optarg, NULL, 16); break;
		case 'D': debug++;			break;
		case 'f': opt_f++; break;
		case 'F': fixed_speed = parse_advertise(optarg);
			if (fixed_speed == -1) errflag++;
			break;
		case 'g': opt_g++; break;
		case 'G': opt_G++; opt_G_string = strdup(optarg); break;
		case 'm': opt_watch++; opt_status++; break;
		case 'M': msg_level = strtoul(optarg, NULL, 0); break;
		case 'h': fprintf(stderr, long_usage_msg, progname); return 0;
		case 'p': override_phy = atoi(optarg); break;
		case 'r': opt_restart++;	break;
		case 'R': opt_reset++;		break;
		case 's': opt_status++;		break;
		case 'v': verbose++;		break;
		case 'V': opt_version++;	break;
		case 'w': opt_watch++;		break;
		case '?': errflag++;		break;
		}
	if (errflag) {
		fprintf(stderr, usage_msg, progname);
		return 2;
	}

	if (verbose || opt_version)
		printf(version);

	/* Open a basic socket. */
	if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0) {
		perror("socket");
		return 1;
	}

	if (debug)
		fprintf(stderr, "DEBUG: argc=%d, optind=%d and argv[optind] is %s.\n",
				argc, optind, argv[optind]);

	/* No remaining args means show all interfaces. */
	if (optind == argc) {
		ifname = "eth0";
		fprintf(stderr, "Using the default interface 'eth0'.\n");
	} else {
		/* Copy the interface name. */
		spp = argv + optind;
		ifname = *spp++;
	}

	if (ifname == NULL) {
		ifname = "eth0";
		fprintf(stderr, "Using the default interface 'eth0'.\n");
	}

	/* Verify that the interface supports the ioctl(), and if
	   it is using the new or old SIOCGMIIPHY value (grrr...).
	 */
	{
		u16 *data = (u16 *)(&ifr.ifr_data);

		strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
		data[0] = 0;

		if (ioctl(skfd, 0x8947, &ifr) >= 0) {
			new_ioctl_nums = 1;
		} else if (ioctl(skfd, SIOCDEVPRIVATE, &ifr) >= 0) {
			new_ioctl_nums = 0;
		} else {
			fprintf(stderr, "SIOCGMIIPHY on %s failed: %s\n", ifname,
					strerror(errno));
			(void) close(skfd);
			return 1;
		}
		if (verbose)
			printf("  Using the %s SIOCGMIIPHY value on PHY %d "
				   "(BMCR 0x%4.4x).\n",
				   new_ioctl_nums ? "new" : "old", data[0], data[3]);
	}

	do_one_xcvr(skfd);

	(void) close(skfd);
	return 0;
}
コード例 #3
0
ファイル: mii_diag.c プロジェクト: peakhunt/samples
int
main(int argc, char **argv)
{
   int c, errflag = 0;
   char **spp, *ifname;

   while ((c = getopt_long(argc, argv, "aA:DfF:G:p:rRsvVw?", longopts, 0)) != EOF)
      switch (c)
      {
      case 'a': opt_a++; break;
      case 'A': nway_advertise = parse_advertise(optarg); break;
      case 'D': debug++; break;
      case 'f': opt_f++; break;
      case 'F': fixed_speed = parse_advertise(optarg); break;
      case 'G': opt_G++; opt_G_value = atoi(optarg); break;
      case 'p': override_phy = atoi(optarg); break;
      case 'r': opt_restart++; break;
      case 'R': opt_reset++; break;
      case 's': opt_status++; break;
      case 'v': verbose++; break;
      case 'V': opt_version++; break;
      case 'w': opt_watch++; break;
      case '?': errflag++;
      }

   if (errflag)
   {
      fprintf(stderr, usage_msg);
      return 2;
   }

   if (verbose || opt_version)
      printf(version);

   /* Open a basic socket. */
   if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0)
   {
      perror("socket");
      exit(-1);
   }

   if (debug)
      fprintf(stderr, "DEBUG: argc=%d, optind=%d and argv[optind] is %s.\n", argc, optind, argv[optind]);

   /* No remaining args means show all interfaces. */
   if (optind == argc)
   {
      ifname = "eth0";
      fprintf(stderr, "Using the default interface 'eth0'.\n");
   }
   else
   {
      /* Copy the interface name. */
      spp = argv + optind;
      ifname = *spp++;
   }

   if (ifname == NULL)
   {
      ifname = "eth0";
      fprintf(stderr, "Using the default interface 'eth0'.\n");
   }

   /* Get the vitals from the interface. */
   strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
   if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0)
   {
      fprintf(stderr, "SIOCGMIIPHY on %s failed: %s\n", ifname, strerror(errno));
      (void) close(skfd);
      return 1;
   }

   do_one_xcvr(skfd);

   (void) close(skfd);
   return 0;
}