u_char         *
var_ipAddrEntry(struct variable *vp,
                oid * name,
                size_t * length,
                int exact, size_t * var_len, WriteMethod ** write_method)
{
    /*
     * object identifier is of form:
     * 1.3.6.1.2.1.4.20.1.?.A.B.C.D,  where A.B.C.D is IP address.
     * IPADDR starts at offset 10.
     */
    oid             lowest[14];
    oid             current[14], *op;
    u_char         *cp;
    int             lowinterface = 0;
#ifndef freebsd2
    short           interface;
#endif
#ifdef hpux11
    static mib_ipAdEnt in_ifaddr, lowin_ifaddr;
#else
#if !defined(linux) && !defined(sunV3)
    static struct in_ifaddr in_ifaddr, lowin_ifaddr;
#else
    static struct ifnet lowin_ifnet;
#endif
    static struct ifnet ifnet;
#endif                          /* hpux11 */
    static in_addr_t	addr_ret;

    /*
     * fill in object part of name for current (less sizeof instance part) 
     */

    memcpy((char *) current, (char *) vp->name,
           (int) vp->namelen * sizeof(oid));

#if !defined(freebsd2) && !defined(hpux11) && !defined(linux)
    Interface_Scan_Init();
#else
    Address_Scan_Init();
#endif
    for (;;) {

#if !defined(freebsd2) && !defined(hpux11) && !defined(linux)
        if (Interface_Scan_Next(&interface, NULL, &ifnet, &in_ifaddr) == 0)
            break;
#ifdef HAVE_STRUCT_IFNET_IF_ADDRLIST
        if (ifnet.if_addrlist == 0)
            continue;           /* No address found for interface */
#endif
#else                           /* !freebsd2 && !hpux11 */
#if defined(linux)
        if (Address_Scan_Next(&interface, &ifnet) == 0)
            break;
#else
        if (Address_Scan_Next(&interface, &in_ifaddr) == 0)
            break;
#endif
#endif                          /* !freebsd2 && !hpux11 && !linux */

#ifdef hpux11
        cp = (u_char *) & in_ifaddr.Addr;
#elif defined(linux) || defined(sunV3)
        cp = (u_char *) & (((struct sockaddr_in *) &(ifnet.if_addr))->
                           sin_addr.s_addr);
#else
        cp = (u_char *) & (((struct sockaddr_in *) &(in_ifaddr.ia_addr))->
                           sin_addr.s_addr);
#endif

        op = current + 10;
        *op++ = *cp++;
        *op++ = *cp++;
        *op++ = *cp++;
        *op++ = *cp++;
        if (exact) {
            if (snmp_oid_compare(current, 14, name, *length) == 0) {
                memcpy((char *) lowest, (char *) current,
                       14 * sizeof(oid));
                lowinterface = interface;
#if defined(linux) || defined(sunV3)
                lowin_ifnet = ifnet;
#else
                lowin_ifaddr = in_ifaddr;
#endif
                break;          /* no need to search further */
            }
        } else {
            if ((snmp_oid_compare(current, 14, name, *length) > 0) &&
                (!lowinterface
                 || (snmp_oid_compare(current, 14, lowest, 14) < 0))) {
                /*
                 * if new one is greater than input and closer to input than
                 * previous lowest, save this one as the "next" one.
                 */
                lowinterface = interface;
#if defined(linux) || defined(sunV3)
                lowin_ifnet = ifnet;
#else
                lowin_ifaddr = in_ifaddr;
#endif
                memcpy((char *) lowest, (char *) current,
                       14 * sizeof(oid));
            }
        }
    }

#if defined(linux)
    SNMP_FREE(ifc.ifc_buf);
#endif

    if (!lowinterface)
        return (NULL);
    memcpy((char *) name, (char *) lowest, 14 * sizeof(oid));
    *length = 14;
    *write_method = (WriteMethod*)0;
    *var_len = sizeof(long_return);
    switch (vp->magic) {
    case IPADADDR:
    	*var_len = sizeof(addr_ret);
#ifdef hpux11
        addr_ret = lowin_ifaddr.Addr;
        return (u_char *) & addr_ret;
#elif defined(linux) || defined(sunV3)
        return (u_char *) & ((struct sockaddr_in *) &lowin_ifnet.if_addr)->
            sin_addr.s_addr;
#else
        return (u_char *) & ((struct sockaddr_in *) &lowin_ifaddr.
                             ia_addr)->sin_addr.s_addr;
#endif
    case IPADIFINDEX:
        long_return = lowinterface;
        return (u_char *) & long_return;
    case IPADNETMASK:
        *var_len = sizeof(addr_ret);
#ifdef hpux11
        addr_ret = lowin_ifaddr.NetMask;
        return (u_char *) & addr_ret;
#elif defined(linux)
        return (u_char *) & ((struct sockaddr_in *) &lowin_ifnet.
                             ia_subnetmask)->sin_addr.s_addr;
#elif !defined(sunV3)
        addr_ret = lowin_ifaddr.ia_subnetmask;
        return (u_char *) & addr_ret;
#endif
    case IPADBCASTADDR:
#ifdef hpux11
        long_return = lowin_ifaddr.BcastAddr & 1;
#elif defined(linux) || defined(sunV3)
        *var_len = sizeof(long_return);
        long_return =
            ntohl(((struct sockaddr_in *) &lowin_ifnet.ifu_broadaddr)->
                  sin_addr.s_addr) & 1;
#elif defined(netbsd1)
        long_return =
            ((struct sockaddr_in *) &lowin_ifaddr.ia_broadaddr)->sin_addr.
            s_addr & 1;
#else
        long_return =
            ntohl(((struct sockaddr_in *) &lowin_ifaddr.ia_broadaddr)->
                  sin_addr.s_addr) & 1;
#endif
        return (u_char *) & long_return;
    case IPADREASMMAX:
#ifdef hpux11
        long_return = lowin_ifaddr.ReasmMaxSize;
        return (u_char *) & long_return;
#elif defined(NETSNMP_NO_DUMMY_VALUES)
        return NULL;
#else
        long_return = -1;
        return (u_char *) & long_return;
#endif
    default:
        DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ipAddrEntry\n",
                    vp->magic));
    }
    return NULL;
}
Beispiel #2
0
u_char *
var_ipAddrEntry(struct variable *vp,
		oid *name,
		int *length,
		int exact,
		int *var_len,
		WriteMethod **write_method)
{
    /*
     * object identifier is of form:
     * 1.3.6.1.2.1.4.20.1.?.A.B.C.D,  where A.B.C.D is IP address.
     * IPADDR starts at offset 10.
     */
    oid			    lowest[14];
    oid			    current[14], *op;
    u_char		    *cp;
    int			    lowinterface=0;
    short                   interface;
    static struct in_ifaddr in_ifaddr;
#if !defined(linux) && !defined(sunV3)
    static struct in_ifaddr lowin_ifaddr;
#else
    static struct ifnet lowin_ifnet;
#endif
    static struct ifnet ifnet;

    /* fill in object part of name for current (less sizeof instance part) */

    memcpy( (char *)current,(char *)vp->name, (int)vp->namelen * sizeof(oid));

#ifndef freebsd2
    Interface_Scan_Init();
#else
    Address_Scan_Init();
#endif
    for (;;) {

#ifndef freebsd2
	if (Interface_Scan_Next(&interface, NULL, &ifnet, &in_ifaddr) == 0)
	    break;
#ifdef STRUCT_IFNET_HAS_IF_ADDRLIST
	if ( ifnet.if_addrlist == 0 )
	    continue;                   /* No address found for interface */
#endif
#else
	if (Address_Scan_Next(&interface, &in_ifaddr) == 0)
	    break;
#endif /* freebsd2 */
#if defined(linux) || defined(sunV3)
	cp = (u_char *)&(((struct sockaddr_in *) &(ifnet.if_addr))->sin_addr.s_addr);
#else
	cp = (u_char *)&(((struct sockaddr_in *) &(in_ifaddr.ia_addr))->sin_addr.s_addr);
#endif

	op = current + 10;
	*op++ = *cp++;
	*op++ = *cp++;
	*op++ = *cp++;
	*op++ = *cp++;
	if (exact){
	    if (snmp_oid_compare(current, 14, name, *length) == 0){
		memcpy( (char *)lowest,(char *)current, 14 * sizeof(oid));
		lowinterface = interface;
#if defined(linux) || defined(sunV3)
		lowin_ifnet = ifnet;
#else
		lowin_ifaddr = in_ifaddr;
#endif
		break;	/* no need to search further */
	    }
	} else {
	    if ((snmp_oid_compare(current, 14, name, *length) > 0) &&
		 (!lowinterface || (snmp_oid_compare(current, 14, lowest, 14) < 0))){
		/*
		 * if new one is greater than input and closer to input than
		 * previous lowest, save this one as the "next" one.
		 */
		lowinterface = interface;
#if defined(linux) || defined(sunV3)
		lowin_ifnet = ifnet;
#else
		lowin_ifaddr = in_ifaddr;
#endif
		memcpy( (char *)lowest,(char *)current, 14 * sizeof(oid));
	    }
	}
    }

    if (!lowinterface) return(NULL);
    memcpy( (char *)name,(char *)lowest, 14 * sizeof(oid));
    *length = 14;
    *write_method = 0;
    *var_len = sizeof(long_return);
    switch(vp->magic){
	case IPADADDR:
#if defined(linux) || defined(sunV3)
            return(u_char *) &((struct sockaddr_in *) &lowin_ifnet.if_addr)->sin_addr.s_addr;
#else
	    return(u_char *) &((struct sockaddr_in *) &lowin_ifaddr.ia_addr)->sin_addr.s_addr;
#endif
	case IPADIFINDEX:
	    long_return = lowinterface;
	    return(u_char *) &long_return;
	case IPADNETMASK:
#ifndef sunV3
#ifdef linux
            return (u_char *)&((struct sockaddr_in *)&lowin_ifnet.ia_subnetmask)->sin_addr.s_addr;
#else
	    long_return = ntohl(lowin_ifaddr.ia_subnetmask);
	    return(u_char *) &long_return;
#endif
#endif
	case IPADBCASTADDR:
	    
#if defined(linux) || defined(sunV3)
	    long_return = ntohl(((struct sockaddr_in *) &lowin_ifnet.ifu_broadaddr)->sin_addr.s_addr) & 1;
#else
          long_return = ntohl(((struct sockaddr_in *) &lowin_ifaddr.ia_broadaddr)->sin_addr.s_addr) & 1;
#endif
	    return(u_char *) &long_return;	   
	case IPADREASMMAX:
	    long_return = -1;
	    return(u_char *) &long_return;
	default:
	    ERROR_MSG("");
    }
    return NULL;
}