Exemple #1
0
struct hostent *
netsnmp_gethostbyaddr(const void *addr, socklen_t len, int type)
{
#if HAVE_GETHOSTBYADDR
    struct hostent *hp = NULL;
    struct sockaddr_in *saddr_in =
        NETSNMP_REMOVE_CONST(struct sockaddr_in *,addr);

    DEBUGMSGTL(("dns:gethostbyaddr", "resolving { AF_INET, %s:%hu }\n",
                inet_ntoa(saddr_in->sin_addr), ntohs(saddr_in->sin_port)));

#ifdef DNSSEC_LOCAL_VALIDATION
    val_status_t val_status;
    hp = val_gethostbyaddr(netsnmp_validator_context(),
                           (const void*)&saddr_in->sin_addr,
                           sizeof(struct in_addr), AF_INET, &val_status);
    DEBUGMSGTL(("dns:sec:val", "val_status %d / %s; trusted: %d\n",
                val_status, p_val_status(val_status),
                val_istrusted(val_status)));
    if (!val_istrusted(val_status)) {
        snmp_log(LOG_WARNING,
                 "The authenticity of DNS response is not trusted (%s)\n",
                 p_val_status(val_status));
        /** continue anyways if DNSSEC_WARN_ONLY is set */
        if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
                                    NETSNMP_DS_LIB_DNSSEC_WARN_ONLY))
            hp = NULL;
    }
    else if (val_does_not_exist(val_status) && hp)
        hp = NULL;
#else
    hp = gethostbyaddr((const void*) &saddr_in->sin_addr,
                       sizeof(struct in_addr), AF_INET);
#endif
    if (hp == NULL) {
        DEBUGMSGTL(("dns:gethostbyaddr", "couldn't resolve addr\n"));
    } else if (hp->h_addrtype != AF_INET) {
        DEBUGMSGTL(("dns:gethostbyaddr",
                    "warning: response for addr not AF_INET!\n"));
    } else {
        DEBUGMSGTL(("dns:gethostbyaddr", "addr resolved okay\n"));
    }
    return hp;
#else
    NETSNMP_LOGONCE((LOG_ERR, "gethostbyaddr not available"));
    return NULL;
#endif
}
Exemple #2
0
char *
AddrToAddrStr(char *const dst, size_t dsize, struct sockaddr_in * const saddrp, int dns, const char *fmt)
{
    char addrName[128];
    char *addrNamePtr;
    struct hostent *hp;
    char str[128];
    char s_name[64];
    char *dlim, *dp;
    const char *cp;

    if (dsize == 0)
        return NULL;
    memset(dst, 0, dsize);

    addrNamePtr = NULL;
    if (dns == 0)
    {
        InetNtoA(addrName, &saddrp->sin_addr, sizeof(addrName));
        addrNamePtr = addrName;
    }
    else
    {
#ifdef DNSSEC_LOCAL_VALIDATION
        val_status_t val_status;
        hp = val_gethostbyaddr(NULL, (const char*)&saddrp->sin_addr, sizeof(struct in_addr), AF_INET, &val_status);
        if ((hp != NULL) && (!val_istrusted(val_status)))
            hp = NULL;
#else
        hp = gethostbyaddr((gethost_addrptr_t) &saddrp->sin_addr, sizeof(struct in_addr), AF_INET);
#endif
        if ((hp != NULL) && (hp->h_name != NULL) && (hp->h_name[0] != '\0'))
        {
            addrNamePtr = hp->h_name;
        }
        else
        {
            InetNtoA(addrName, &saddrp->sin_addr, sizeof(addrName));
            addrNamePtr = addrName;
        }
    }
    if (fmt == NULL)
        fmt = "%h:%p";
    for (dp = dst, dlim = dp + dsize - 1; ; fmt++)
    {
        if (*fmt == '\0')
        {
            break;	/* done */
        }
        else if (*fmt == '%')
        {
            fmt++;
            if (*fmt == '%')
            {
                if (dp < dlim)
                    *dp++ = '%';
            }
            else if (*fmt == 'p')
            {
                sprintf(str, "%u", (unsigned int) ntohs(saddrp->sin_port));
                for (cp = str; *cp != '\0'; cp++)
                    if (dp < dlim)
                        *dp++ = *cp;
                *dp = '\0';
            }
            else if (*fmt == 'h')
            {
                if (addrNamePtr != NULL)
                {
                    cp = addrNamePtr;
                }
                else
                {
                    cp = "unknown";
                }
                for ( ; *cp != '\0'; cp++)
                    if (dp < dlim)
                        *dp++ = *cp;
                *dp = '\0';
            }
            else if (*fmt == 's')
            {
                cp = s_name;
                (void) ServicePortNumberToName(ntohs(saddrp->sin_port), s_name, sizeof(s_name), 0);
                for ( ; *cp != '\0'; cp++)
                    if (dp < dlim)
                        *dp++ = *cp;
                /* endservent(); */
                *dp = '\0';
            }
            else if ((*fmt == 't') || (*fmt == 'u'))
            {
                cp = s_name;
                (void) ServicePortNumberToName(ntohs(saddrp->sin_port), s_name, sizeof(s_name), (int) *fmt);
                for ( ; *cp != '\0'; cp++)
                    if (dp < dlim)
                        *dp++ = *cp;
                /* endservent(); */
                *dp = '\0';
            }
            else if (*fmt == '\0')
            {
                break;
            }
            else
            {
                if (dp < dlim)
                    *dp++ = *fmt;
            }
        }
        else if (dp < dlim)
        {
            *dp++ = *fmt;
        }
    }
    *dp = '\0';
    return (dst);
}	/* AddrToAddrStr */