[ILUG] networking / ioctl() question..

From: John P. Looney (john at domain antefacto.com)
Date: Thu 21 Jun 2001 - 17:07:20 IST


 I'm using the following function to return an array of ifreq structs,
which I use for various reasons. But, for some reason, this doesn't work
on interfaces which are down, and have an IP set to 0.0.0.0 - any idea why
?

struct ifreq *get_if_info(int skfd, int *num_interfaces, char **old_ifc_bufp)
{
    struct ifconf ifconf;
    int if_increments=10;
    int result;

    if (skfd == -1) {
        syslog(LOG_INFO, "af_get_if_info: Invalid socket. [%s]", strerror(errno));
        return NULL;
    }
    ifconf.ifc_buf = NULL;
    ifconf.ifc_len = sizeof(struct ifreq) * if_increments;
    if(!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) {
        syslog(LOG_INFO, "Malloc failed in af_get_if_info");
        return NULL;
    }

    for(ever) {
        ifconf.ifc_len = sizeof(struct ifreq) * if_increments;
        if(!(ifconf.ifc_buf = realloc(ifconf.ifc_buf, ifconf.ifc_len))) {
            syslog(LOG_INFO, "Realloc failed in af_get_if_info");
            return NULL;
        }
        if ((result = ioctl(skfd, SIOCGIFCONF, &ifconf)) < 0) {
            if (!(errno == ENODEV)) {
                syslog(LOG_INFO, "Network information check failed %d (%s)", errno, strerror(errno));
            }
            break;
        }
        if (ifconf.ifc_len == sizeof(struct ifreq) * if_increments) {
            if_increments += 10;
            continue;
        }
        break;
    }
    *num_interfaces=ifconf.ifc_len/sizeof(struct ifreq);
    *old_ifc_bufp=ifconf.ifc_buf;
    return(ifconf.ifc_req);
}

-- 
 When I say 'free', I mean 'free': free from bond, of chain or command: 
to go where you will, even to Mordor, Saruman, if you desire. " 
    -- Gandalf, paraphrasing the choice between Free and Non-free software


This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:10:49 GMT