Ejemplo n.º 1
0
Archivo: sioc.c Proyecto: ckousik/rr
int main(void) {
  int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  struct ifreq req;
  char name[PATH_MAX];
  int index;
  struct ethtool_cmd etc;
  int err, ret;
  struct iwreq wreq;

  get_ifconfig(sockfd, &req);
  strcpy(name, req.ifr_name);

  req.ifr_ifindex = -1;
  strcpy(req.ifr_name, name);
  ret = ioctl(sockfd, SIOCGIFINDEX, &req);
  atomic_printf("SIOCGIFINDEX(ret:%d): %s index is %d\n", ret, req.ifr_name,
                req.ifr_ifindex);
  test_assert(0 == ret);
  index = req.ifr_ifindex;

  memset(&req.ifr_name, 0x5a, sizeof(req.ifr_name));
  req.ifr_ifindex = index;
  ret = ioctl(sockfd, SIOCGIFNAME, &req);
  atomic_printf("SIOCGIFNAME(ret:%d): index %d(%s) name is %s\n", ret, index,
                name, req.ifr_name);
  test_assert(0 == ret);
  test_assert(!strcmp(name, req.ifr_name));

  memset(&req.ifr_addr, 0x5a, sizeof(req.ifr_addr));
  ret = ioctl(sockfd, SIOCGIFADDR, &req);
  atomic_printf("SIOCGIFADDR(ret:%d): %s addr is", ret, req.ifr_name);
  atomic_printf(" %s\n", sockaddr_name(&req.ifr_addr));
  test_assert(0 == ret);

  memset(&req.ifr_addr, 0x5a, sizeof(req.ifr_addr));
  ret = ioctl(sockfd, SIOCGIFHWADDR, &req);
  atomic_printf("SIOCGIFHWADDR(ret:%d): %s addr is", ret, req.ifr_name);
  atomic_printf(" %s\n", sockaddr_hw_name(&req.ifr_addr));
  test_assert(0 == ret);

  memset(&req.ifr_flags, 0x5a, sizeof(req.ifr_flags));
  ret = ioctl(sockfd, SIOCGIFFLAGS, &req);
  atomic_printf("SIOCGIFFLAGS(ret:%d): %s flags are", ret, req.ifr_name);
  test_assert(0 == ret);
  atomic_printf(" %#x\n", req.ifr_flags);

  memset(&req.ifr_flags, 0x5a, sizeof(req.ifr_mtu));
  ret = ioctl(sockfd, SIOCGIFMTU, &req);
  atomic_printf("SIOCGIFMTU(ret:%d): %s MTU is", ret, req.ifr_name);
  test_assert(0 == ret);
  atomic_printf(" %d\n", req.ifr_mtu);

  memset(&etc, 0, sizeof(etc));
  etc.cmd = ETHTOOL_GSET;
  req.ifr_data = (char*)&etc;
  ret = ioctl(sockfd, SIOCETHTOOL, &req);
  err = errno;
  atomic_printf("SIOCETHTOOL(ret:%d): %s ethtool data:\n", ret, req.ifr_name);
  atomic_printf("  speed:%#x duplex:%#x port:%#x physaddr:%#x, maxtxpkt:%u "
                "maxrxpkt:%u ...\n",
                ethtool_cmd_speed(&etc), etc.duplex, etc.port, etc.phy_address,
                etc.maxtxpkt, etc.maxrxpkt);
  if (-1 == ret) {
    atomic_printf("WARNING: %s doesn't appear to support SIOCETHTOOL; the test "
                  "may have been meaningless (%s/%d)\n",
                  name, strerror(err), err);
    test_assert(EOPNOTSUPP == err || EPERM == err);
  }

  memset(&wreq, 0x5a, sizeof(wreq));
  strcpy(wreq.ifr_ifrn.ifrn_name, name);
  ret = ioctl(sockfd, SIOCGIWRATE, &wreq);
  err = errno;
  atomic_printf("SIOCGIWRATE(ret:%d): %s:\n", ret, wreq.ifr_name);
  atomic_printf("  bitrate:%d (fixed? %s; disabled? %s) flags:%#x\n",
                wreq.u.bitrate.value, wreq.u.bitrate.fixed ? "yes" : "no",
                wreq.u.bitrate.disabled ? "yes" : "no", wreq.u.bitrate.flags);
  if (-1 == ret) {
    atomic_printf("WARNING: %s doesn't appear to be a wireless iface; "
                  "SIOCGIWRATE test may have been meaningless (%s/%d)\n",
                  name, strerror(err), err);
    test_assert(EOPNOTSUPP == err || EPERM == err);
  }

  atomic_puts("EXIT-SUCCESS");
  return 0;
}
Ejemplo n.º 2
0
Archivo: sioc.c Proyecto: glandium/rr
int main(void) {
  int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  struct ifreq* req;
  struct ifreq* eth_req;
  char name[PATH_MAX];
  int index;
  struct ethtool_cmd* etc;
  int err, ret;
  struct iwreq* wreq;
  char buf[1024];

  ALLOCATE_GUARD(req, 'a');
  ALLOCATE_GUARD(eth_req, 'a');
  get_ifconfig(sockfd, req, eth_req);
  strcpy(name, req->ifr_name);

  req->ifr_ifindex = -1;
  strcpy(req->ifr_name, name);
  ret = ioctl(sockfd, SIOCGIFINDEX, req);
  VERIFY_GUARD(req);
  atomic_printf("SIOCGIFINDEX(ret:%d): %s index is %d\n", ret, req->ifr_name,
                req->ifr_ifindex);
  test_assert(0 == ret);
  test_assert(req->ifr_ifindex != -1);
  index = req->ifr_ifindex;

  memset(&req->ifr_name, 0xff, sizeof(req->ifr_name));
  req->ifr_ifindex = index;
  ret = ioctl(sockfd, SIOCGIFNAME, req);
  VERIFY_GUARD(req);
  atomic_printf("SIOCGIFNAME(ret:%d): index %d(%s) name is %s\n", ret, index,
                name, req->ifr_name);
  test_assert(0 == ret);
  test_assert(!strcmp(name, req->ifr_name));

  GENERIC_REQUEST_BY_NAME(SIOCGIFFLAGS);
  atomic_printf("flags are %#x\n", req->ifr_flags);

  GENERIC_REQUEST_BY_NAME(SIOCGIFADDR);
  atomic_printf("addr is %s\n", sockaddr_name(&req->ifr_addr));

  GENERIC_REQUEST_BY_NAME(SIOCGIFDSTADDR);
  atomic_printf("addr is %s\n", sockaddr_name(&req->ifr_addr));

  GENERIC_REQUEST_BY_NAME(SIOCGIFBRDADDR);
  atomic_printf("addr is %s\n", sockaddr_name(&req->ifr_addr));

  GENERIC_REQUEST_BY_NAME(SIOCGIFNETMASK);
  atomic_printf("netmask is %s\n", sockaddr_name(&req->ifr_addr));

  GENERIC_REQUEST_BY_NAME(SIOCGIFMETRIC);
  atomic_printf("metric is %d\n", req->ifr_metric);

  memset(&req->ifr_metric, 0xff, sizeof(req->ifr_metric));
  ret = ioctl(sockfd, SIOCGIFMEM, req);
  VERIFY_GUARD(req);
  test_assert(-1 == ret && errno == ENOTTY);

  GENERIC_REQUEST_BY_NAME(SIOCGIFMTU);
  atomic_printf("MTU is %d\n", req->ifr_mtu);

  GENERIC_REQUEST_BY_NAME(SIOCGIFHWADDR);
  atomic_printf("hwaddr %s\n", sockaddr_hw_name(&req->ifr_addr));

  memset(&req->ifr_flags, 0xff, sizeof(req->ifr_flags));
  ret = ioctl(sockfd, SIOCGIFPFLAGS, req);
  VERIFY_GUARD(req);
  if (ret != -1 || errno != EINVAL) {
    test_assert(0 == ret);
    atomic_printf("SIOCGIFPFLAGS(ret:%d): %s flags are", ret, req->ifr_name);
    atomic_printf(" %#x\n", req->ifr_flags);
  }

  GENERIC_REQUEST_BY_NAME(SIOCGIFTXQLEN);
  atomic_printf("qlen is %d\n", req->ifr_qlen);

  ALLOCATE_GUARD(etc, 'b');
  etc->cmd = ETHTOOL_GSET;
  req->ifr_data = (char*)&etc;
  ret = ioctl(sockfd, SIOCETHTOOL, req);
  VERIFY_GUARD(req);
  VERIFY_GUARD(etc);
  err = errno;
  atomic_printf("SIOCETHTOOL(ret:%d): %s ethtool data: ", ret, req->ifr_name);
  if (-1 == ret) {
    atomic_printf("WARNING: %s doesn't appear to support SIOCETHTOOL\n", name);
    test_assert(EOPNOTSUPP == err || EPERM == err);
  } else {
    atomic_printf("speed:%#x duplex:%#x port:%#x physaddr:%#x, maxtxpkt:%u "
                  "maxrxpkt:%u ...\n",
                  ethtool_cmd_speed(etc), etc->duplex, etc->port,
                  etc->phy_address, etc->maxtxpkt, etc->maxrxpkt);
  }

  GENERIC_WIRELESS_PARAM_REQUEST_BY_NAME(SIOCGIWRATE, bitrate);

  GENERIC_WIRELESS_REQUEST_BY_NAME(SIOCGIWNAME,
                                   ("wireless protocol name:%s", wreq->u.name));

  GENERIC_WIRELESS_REQUEST_BY_NAME(SIOCGIWMODE,
                                   (" wireless mode:%d", wreq->u.mode));

  ALLOCATE_GUARD(wreq, 'e');
  strcpy(wreq->ifr_ifrn.ifrn_name, name);
  wreq->u.essid.length = sizeof(buf);
  wreq->u.essid.pointer = buf;
  wreq->u.essid.flags = 0;
  ret = ioctl(sockfd, SIOCGIWESSID, wreq);
  VERIFY_GUARD(wreq);
  err = errno;
  atomic_printf("SIOCGIWESSID(ret:%d): %s: ", ret, wreq->ifr_name);
  if (-1 == ret) {
    atomic_printf("WARNING: %s doesn't appear to be a wireless iface\n", name);
    test_assert(EOPNOTSUPP == err || EPERM == err || EINVAL == err);
  } else {
    atomic_printf("wireless ESSID:%s\n", buf);
  }

  GENERIC_WIRELESS_PARAM_REQUEST_BY_NAME(SIOCGIWSENS, sens);

  atomic_puts("EXIT-SUCCESS");
  return 0;
}