示例#1
0
void
ipv6cp_Setup(struct ipv6cp *ipv6cp)
{
  ncpaddr_init(&ipv6cp->myaddr);
  ncpaddr_init(&ipv6cp->hisaddr);

  ipv6cp->his_reject = 0;
  ipv6cp->my_reject = 0;
}
示例#2
0
文件: iface.c 项目: coyizumi/cs111
struct iface *
iface_Create(const char *name)
{
  int mib[6], maxtries, err;
  size_t needed, namelen;
  char *buf, *ptr, *end;
  struct if_msghdr *ifm;
  struct ifa_msghdr *ifam;
  struct sockaddr_dl *dl;
  struct sockaddr *sa[RTAX_MAX];
  struct iface *iface;
  struct iface_addr *addr;

  mib[0] = CTL_NET;
  mib[1] = PF_ROUTE;
  mib[2] = 0;
  mib[3] = 0;
  mib[4] = NET_RT_IFLIST;
  mib[5] = 0;

  maxtries = 20;
  err = 0;
  do {
    if (maxtries-- == 0 || (err && err != ENOMEM)) {
      fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(err));
      return NULL;
    }

    if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
      fprintf(stderr, "iface_Create: sysctl: estimate: %s\n",
                strerror(errno));
      return NULL;
    }

    if ((buf = (char *)malloc(needed)) == NULL) {
      fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno));
      return NULL;
    }

    if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
      err = errno;
      free(buf);
      buf = NULL;
    }
  } while (buf == NULL);

  ptr = buf;
  end = buf + needed;
  iface = NULL;
  namelen = strlen(name);

  while (ptr < end && iface == NULL) {
    ifm = (struct if_msghdr *)ptr;			/* On if_msghdr */
    if (ifm->ifm_type != RTM_IFINFO)
      break;
    dl = (struct sockaddr_dl *)(ifm + 1);		/* Single _dl at end */
    if (dl->sdl_nlen == namelen && !strncmp(name, dl->sdl_data, namelen)) {
      iface = (struct iface *)malloc(sizeof *iface);
      if (iface == NULL) {
        fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
        return NULL;
      }
      iface->name = strdup(name);
      iface->descr = NULL;
      iface->index = ifm->ifm_index;
      iface->flags = ifm->ifm_flags;
      iface->mtu = 0;
      iface->addrs = 0;
      iface->addr = NULL;
    }
    ptr += ifm->ifm_msglen;				/* First ifa_msghdr */
    for (; ptr < end; ptr += ifam->ifam_msglen) {
      ifam = (struct ifa_msghdr *)ptr;			/* Next if address */

      if (ifam->ifam_type != RTM_NEWADDR)		/* finished this if */
        break;

      if (iface != NULL && ifam->ifam_addrs & RTA_IFA) {
        /* Found a configured interface ! */
        iface_ParseHdr(ifam, sa);

        if (sa[RTAX_IFA] && (sa[RTAX_IFA]->sa_family == AF_INET
#ifndef NOINET6
                             || sa[RTAX_IFA]->sa_family == AF_INET6
#endif
                             )) {
          /* Record the address */

          addr = (struct iface_addr *)
            realloc(iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
          if (addr == NULL)
            break;
          iface->addr = addr;

          addr += iface->addrs;
          iface->addrs++;

          ncprange_setsa(&addr->ifa, sa[RTAX_IFA], sa[RTAX_NETMASK]);
          if (sa[RTAX_BRD])
            ncpaddr_setsa(&addr->peer, sa[RTAX_BRD]);
          else
            ncpaddr_init(&addr->peer);
        }
      }
    }
  }

  free(buf);

  return iface;
}
示例#3
0
struct iface *
iface_Create(const char *name)
{
  size_t namelen;
  struct sockaddr_dl *dl;
  struct ifaddrs *ifap, *ifa;
  struct iface *iface;
  struct iface_addr *addr;

  if (getifaddrs(&ifap) != 0) {
    fprintf(stderr, "iface_Create: getifaddrs: %s\n", strerror(errno));
    return NULL;
  }

  iface = NULL;
  namelen = strlen(name);

  for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
    if (strcmp(name, ifa->ifa_name))
      continue;
    if (ifa->ifa_addr->sa_family == AF_LINK) {
      dl = (struct sockaddr_dl *)ifa->ifa_addr;
      iface = (struct iface *)malloc(sizeof *iface);
      if (iface == NULL) {
        fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
        freeifaddrs(ifap);
        return NULL;
      }
      iface->name = strdup(name);
      iface->index = if_nametoindex(name);
      iface->flags = ifa->ifa_flags;
      iface->mtu = 0;
      iface->addrs = 0;
      iface->addr = NULL;
    }

    if (ifa->ifa_addr->sa_family == AF_INET
#ifndef NOINET6
        || ifa->ifa_addr->sa_family == AF_INET6
#endif
       ) {
      /* Record the address */

      addr = (struct iface_addr *)
        realloc(iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
      if (addr == NULL)
        break;
      iface->addr = addr;

      addr += iface->addrs;
      iface->addrs++;

      ncprange_setsa(&addr->ifa, ifa->ifa_addr, ifa->ifa_netmask);
      if (ifa->ifa_broadaddr)
        ncpaddr_setsa(&addr->peer, ifa->ifa_broadaddr);
      else
        ncpaddr_init(&addr->peer);
    }
  }

  freeifaddrs(ifap);

  return iface;
}