Example #1
0
File: asn.c Project: russor/mtr
static char *get_ipinfo(
    struct mtr_ctl *ctl,
    ip_t * addr)
{
    char key[NAMELEN];
    char lookup_key[NAMELEN];
    char *val = NULL;
    ENTRY item;

    if (!addr)
        return NULL;

    if (ctl->af == AF_INET6) {
#ifdef ENABLE_IPV6
        reverse_host6(addr, key, NAMELEN);
        if (snprintf(lookup_key, NAMELEN, "%s.origin6.asn.cymru.com", key)
            >= NAMELEN)
            return NULL;
#else
        return NULL;
#endif
    } else {
        unsigned char buff[4];
        memcpy(buff, addr, 4);
        if (snprintf
            (key, NAMELEN, "%d.%d.%d.%d", buff[3], buff[2], buff[1],
             buff[0]) >= NAMELEN)
            return NULL;
        if (snprintf(lookup_key, NAMELEN, "%s.origin.asn.cymru.com", key)
            >= NAMELEN)
            return NULL;
    }

    if (iihash) {
        ENTRY *found_item;

        DEB_syslog(LOG_INFO, ">> Search: %s", key);
        item.key = key;;
        if ((found_item = hsearch(item, FIND))) {
            if (!(val = (*((items_t *) found_item->data))[ctl->ipinfo_no]))
                val = (*((items_t *) found_item->data))[0];
            DEB_syslog(LOG_INFO, "Found (hashed): %s", val);
        }
    }

    if (!val) {
        DEB_syslog(LOG_INFO, "Lookup: %s", key);
        if ((val = split_txtrec(ctl, ipinfo_lookup(lookup_key)))) {
            DEB_syslog(LOG_INFO, "Looked up: %s", key);
            if (iihash)
                if ((item.key = xstrdup(key))) {
                    item.data = (void *) items;
                    hsearch(item, ENTER);
                    DEB_syslog(LOG_INFO, "Insert into hash: %s", key);
                }
        }
    }

    return val;
}
Example #2
0
File: asn.c Project: bzruk/mtr
char *get_ipinfo(ip_t *addr, int ndx) {
    if (!addr)
        return NULL;

    char key[NAMELEN];
    char lookup_key[NAMELEN];

    if (af == AF_INET6) {
#ifdef ENABLE_IPV6
        if (!origins[origin_no].ip6zone)
            return NULL;
        reverse_host6(addr, key);
        sprintf(lookup_key, "%s.%s", key, origins[origin_no].ip6zone);
#else
	return NULL;
#endif
    } else {
        if (!origins[origin_no].ip4zone)
            return NULL;
        unsigned char buff[4];
        memcpy(buff, addr, 4);
        sprintf(key, "%d.%d.%d.%d", buff[3], buff[2], buff[1], buff[0]);
        sprintf(lookup_key, "%s.%s", key, origins[origin_no].ip4zone);
    }

    char *val = NULL;
    ENTRY item;

    if (hash) {
        IIDEBUG_MSG((LOG_INFO, ">> Search: %s", key));
        item.key = key;
        ENTRY *found_item;
        if ((found_item = hsearch(item, FIND))) {
            if (!(val = (*((items_t*)found_item->data))[ipinfo_no[ndx]]))
                val = (*((items_t*)found_item->data))[0];
            IIDEBUG_MSG((LOG_INFO, "Found (hashed): %s", val));
        }
    }

    if (!val) {
        IIDEBUG_MSG((LOG_INFO, "Lookup: %s", key));
        if ((val = split_rec(ipinfo_lookup(lookup_key), ndx))) {
            IIDEBUG_MSG((LOG_INFO, "Looked up: %s", key));
            if (hash)
                if ((item.key = strdup(key))) {
                    item.data = (void*)items;
                    hsearch(item, ENTER);
#ifdef IIDEBUG
                    {
                        char buff[NAMELEN] = {0};
                        int i, len = 0;
                        for (i = 0; (i < II_ITEM_MAX) && (*items)[i]; i++) {
                            sprintf(buff + len, "\"%s\" ", (*items)[i]);
                            len = strlen(buff);
                        }
                        syslog(LOG_INFO, "Insert into hash: \"%s\" => %s", key, buff);
                    }
#endif
                }
        }
    }

    return val;
}