コード例 #1
0
bool namecache_fetch(const char *name,
                     int name_type,
                     struct ip_service **ip_list,
                     int *num_names)
{
    char *key, *value;
    time_t timeout;

    /* exit now if null pointers were passed as they're required further */
    if (!ip_list || !num_names) {
        return False;
    }

    if (!gencache_init()) {
        return False;
    }

    if (name_type > 255) {
        return False; /* Don't fetch non-real name types. */
    }

    *num_names = 0;

    /*
     * Use gencache interface - lookup the key
     */
    key = namecache_key(name, name_type);
    if (!key) {
        return False;
    }

    if (!gencache_get(key, &value, &timeout)) {
        DEBUG(5, ("no entry for %s#%02X found.\n", name, name_type));
        SAFE_FREE(key);
        return False;
    } else {
        DEBUG(5, ("name %s#%02X found.\n", name, name_type));
    }

    /*
     * Split up the stored value into the list of IP adresses
     */
    *num_names = ipstr_list_parse(value, ip_list);

    SAFE_FREE(key);
    SAFE_FREE(value);

    return *num_names > 0; /* true only if some ip has been fetched */
}
コード例 #2
0
bool namecache_delete(const char *name, int name_type)
{
    bool ret;
    char *key;

    if (!gencache_init())
        return False;

    if (name_type > 255) {
        return False; /* Don't fetch non-real name types. */
    }

    key = namecache_key(name, name_type);
    if (!key) {
        return False;
    }
    ret = gencache_del(key);
    SAFE_FREE(key);
    return ret;
}
コード例 #3
0
ファイル: namecache.c プロジェクト: jophxy/samba
void namecache_store(const char *name, int name_type,
		     int num_names, struct in_addr *ip_list)
{
	TDB_DATA key, value;
	time_t expiry;
	int i;

	if (!enable_namecache)
		return;

	DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
		  num_names, num_names == 1 ? "": "es", name, name_type));

	for (i = 0; i < num_names; i++) 
		DEBUGADD(5, ("%s%s", inet_ntoa(ip_list[i]),
			     i == (num_names - 1) ? "" : ", "));

	DEBUGADD(5, ("\n"));

	key = namecache_key(name, name_type);

	/* Cache pdc location or dc lists for only a little while
	   otherwise if we lock on to a bad DC we can potentially be
	   out of action for the entire cache timeout time! */

	if (name_type != 0x1b || name_type != 0x1c)
		expiry = time(NULL) + 10;
	else
		expiry = time(NULL) + lp_name_cache_timeout();

	value = namecache_value(ip_list, num_names, expiry);

	tdb_store(namecache_tdb, key, value, TDB_REPLACE);

	free(key.dptr);
	free(value.dptr);
}
コード例 #4
0
ファイル: namecache.c プロジェクト: livebox/livebox2
BOOL namecache_fetch(const char *name, int name_type, struct in_addr **ip_list,
		     int *num_names)
{
	TDB_DATA key, value;
	struct nc_value *data = NULL;
	time_t now;
	int i;

	*ip_list = NULL;
	*num_names = 0;

	if (!enable_namecache)
		return False;

	/* Read value */

	key = namecache_key(name, name_type);

	value = tdb_fetch(namecache_tdb, key);
	
	if (!value.dptr) {
		DEBUG(5, ("namecache_fetch: %s#%02x not found\n",
			  name, name_type));
		goto done;
	}

	data = (struct nc_value *)value.dptr;

	/* Check expiry time */

	now = time(NULL);

	if (now > data->expiry) {

		DEBUG(5, ("namecache_fetch: entry for %s#%02x expired\n",
			  name, name_type));

		tdb_delete(namecache_tdb, key);

		SAFE_FREE(value.dptr);
		value = tdb_null;

		goto done;
	}

	if ((data->expiry - now) > lp_name_cache_timeout()) {

		/* Someone may have changed the system time on us */

		DEBUG(5, ("namecache_fetch: entry for %s#%02x has bad expiry\n",
			  name, name_type));

		tdb_delete(namecache_tdb, key);

		SAFE_FREE(value.dptr);
		value = tdb_null;

		goto done;
	}

	/* Extract and return namelist */

	DEBUG(5, ("namecache_fetch: returning %d address%s for %s#%02x: ",
		  data->count, data->count == 1 ? "" : "es", name, name_type));

	if (data->count) {

		*ip_list = (struct in_addr *)malloc(
			sizeof(struct in_addr) * data->count);
		
		memcpy(*ip_list, data->ip_list, sizeof(struct in_addr) * data->count);
		
		*num_names = data->count;
		
		for (i = 0; i < *num_names; i++)
			DEBUGADD(5, ("%s%s", inet_ntoa((*ip_list)[i]),
				     i == (*num_names - 1) ? "" : ", "));

	}

	DEBUGADD(5, ("\n"));

done:
	SAFE_FREE(key.dptr);
	SAFE_FREE(value.dptr);

	return value.dsize > 0;
}
コード例 #5
0
bool namecache_store(const char *name,
                     int name_type,
                     int num_names,
                     struct ip_service *ip_list)
{
    time_t expiry;
    char *key, *value_string;
    int i;
    bool ret;

    /*
     * we use gecache call to avoid annoying debug messages about
     * initialised namecache again and again...
     */
    if (!gencache_init()) {
        return False;
    }

    if (name_type > 255) {
        return False; /* Don't store non-real name types. */
    }

    if ( DEBUGLEVEL >= 5 ) {
        TALLOC_CTX *ctx = talloc_stackframe();
        char *addr = NULL;

        DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
                  num_names, num_names == 1 ? "": "es", name, name_type));

        for (i = 0; i < num_names; i++) {
            addr = print_canonical_sockaddr(ctx,
                                            &ip_list[i].ss);
            if (!addr) {
                continue;
            }
            DEBUGADD(5, ("%s%s", addr,
                         (i == (num_names - 1) ? "" : ",")));

        }
        DEBUGADD(5, ("\n"));
        TALLOC_FREE(ctx);
    }

    key = namecache_key(name, name_type);
    if (!key) {
        return False;
    }

    expiry = time(NULL) + lp_name_cache_timeout();

    /*
     * Generate string representation of ip addresses list
     * First, store the number of ip addresses and then
     * place each single ip
     */
    if (!ipstr_list_make(&value_string, ip_list, num_names)) {
        SAFE_FREE(key);
        SAFE_FREE(value_string);
        return false;
    }

    /* set the entry */
    ret = gencache_set(key, value_string, expiry);
    SAFE_FREE(key);
    SAFE_FREE(value_string);
    return ret;
}