示例#1
0
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;
}
示例#2
0
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;
}