IO-Interface

 view release on metacpan or  search on metacpan

lib/IO/Interface.xs  view on Meta::CPAN

#else
	 croak("Cannot set metric on this platform.");
#endif
     } else {
       operation = SIOCGIFMETRIC;
     }
     if (!Ioctl(sock,operation,&ifr)) XSRETURN_UNDEF;
     RETVAL = ifr.ifr_metric;
   }
   OUTPUT:
     RETVAL

int
if_index(sock, name, ...)
     InputStream sock
     char*       name
     PROTOTYPE: $$;$
     CODE:
   {
#ifdef __USE_BSD
     RETVAL = if_nametoindex(name);
#else
     XSRETURN_UNDEF;
#endif
   }
   OUTPUT:
     RETVAL

char*
if_indextoname(sock, index, ...)
     InputStream sock
     int   index
     PROTOTYPE: $$;$
     PREINIT:
     char  name[IFNAMSIZ];
     CODE:
   {
#ifdef __USE_BSD
     RETVAL = if_indextoname(index,name);
#else
    XSRETURN_UNDEF;
#endif
   }
   OUTPUT:
     RETVAL

void
_if_list(sock)
     InputStream sock
     PROTOTYPE: $
     PREINIT:
#ifdef USE_GETIFADDRS
       struct ifaddrs *ifa_start;
       struct ifaddrs *ifa;
#else
       struct ifconf ifc;
       struct ifreq  *ifr;
       int    lastlen,len;
       char   *buf,*ptr;
#endif
     PPCODE:
#ifdef USE_GETIFADDRS
       if (getifaddrs(&ifa_start) < 0)
	 XSRETURN_EMPTY;

       for (ifa = ifa_start ; ifa ; ifa = ifa->ifa_next)
	 XPUSHs(sv_2mortal(newSVpv(ifa->ifa_name,0)));

       freeifaddrs(ifa_start);
#else
       lastlen = 0;
       len     = 10 * sizeof(struct ifreq); /* initial buffer size guess */
       for ( ; ; ) {
	 if ( (buf = safemalloc(len)) == NULL)
	   croak("Couldn't malloc buffer for ioctl: %s",strerror(errno));
	 ifc.ifc_len = len;
	 ifc.ifc_buf = buf;
	 if (ioctl(PerlIO_fileno(sock),MY_SIOCGIFCONF,&ifc) < 0) {
	   if (errno != EINVAL || lastlen != 0)
	     XSRETURN_EMPTY;
	 } else {
	   if (ifc.ifc_len == lastlen) break;  /* success, len has not changed */
	   lastlen = ifc.ifc_len;
	 }
	 len += 10 * sizeof(struct ifreq); /* increment */
	 safefree(buf);
       }
       
       for (ptr = buf ; ptr < buf + ifc.ifc_len ; ptr += sizeof(struct ifreq)) {
	 ifr = (struct ifreq*) ptr;
	 XPUSHs(sv_2mortal(newSVpv(ifr->ifr_name,0)));
       }
       safefree(buf);
#endif



( run in 2.008 seconds using v1.01-cache-2.11-cpan-71847e10f99 )