Example #1
0
/* Perform an async dns lookup. This is host -> ip. For ip -> host, use
 * egg_dns_reverse(). We return a dns id that you can use to cancel the
 * lookup. */
int egg_dns_lookup(const char *host, interval_t timeout, dns_callback_t callback, void *client_data)
{
	dns_query_t *q = NULL;
	int i, cache_id;

	sdprintf("egg_dns_lookup(%s, %d)", host, timeout);

	if (is_dotted_ip(host)) {
		/* If it's already an ip, we're done. */
		dns_answer_t answer;

		answer_init(&answer);
		answer_add(&answer, host);
		callback(-1, client_data, host, answer.list);
		answer_free(&answer);
		return(-1);
	}

	/* Ok, now see if it's in our host cache. */
	for (i = 0; i < nhosts; i++) {
		if (!egg_strcasecmp(host, hosts[i].host)) {
			dns_answer_t answer;

			answer_init(&answer);
			answer_add(&answer, hosts[i].ip);
			callback(-1, client_data, host, answer.list);
			answer_free(&answer);
			return(-1);
		}
	}

	cache_id = cache_find(host);
	if (cache_id >= 0) {
		shuffleArray(cache[cache_id].answer.list, cache[cache_id].answer.len);
		callback(-1, client_data, host, cache[cache_id].answer.list);
		return(-1);
	}

	/* check if the query was already made */
        if (find_query(host))
          return(-2);

	/* Allocate our query struct. */
        q = alloc_query(client_data, callback, host);

        dns_send_query(q);

//        /* setup a timer to detect dead ns */
//	dns_create_timeout_timer(&q, host, timeout);

	/* Send the ipv4 query. */

	return(q->id);
}
Example #2
0
AvahiLLMNRQuery* avahi_llmnr_query_add(AvahiInterface *i, AvahiKey *key, AvahiLLMNRQueryType type, AvahiLLMNRQueryCallback callback, void *userdata) {
	AvahiLLMNRQuery *lq;
	int c;

	/* Engine maintains a hashmap of 'AvahiLLMNRQuery' objects by id */
	AvahiLLMNRLookupEngine *e = i->monitor->server->llmnr.llmnr_lookup_engine;
	
	assert(i);	
	assert(key);
	assert(callback);

	if(!(lq = avahi_new(AvahiLLMNRQuery, 1)))
		return NULL;

	/* Initialize parameters */
	lq->dead = 0;
	lq->key = avahi_key_ref(key);
	lq->type = type;
	lq->interface = i;
	lq->callback = callback;
	lq->post_id_valid = 0;
	lq->post_id = 0;
	lq->userdata = userdata;

	/* Initialize record lists for this query object */
	lq->c_bit_set = avahi_record_list_new();
	lq->c_bit_clear = avahi_record_list_new();

	c = avahi_record_list_is_empty(lq->c_bit_set);
	assert(c);
	assert(lq->c_bit_clear);
	
	/* Set ID */
	/* KEEP IT 32 BIT for HASHMAP */
	for (;; e->next_id++)
		if(!(find_query(e, e->next_id)))
			break;

	lq->id = e->next_id++;
	
	/* AvahiLLMNRLookupEngine hashmap of queries by ID, 
	'engine-AvahiLLMNRQuery'. (lq->id and lq) */
	avahi_hashmap_insert(e->queries_by_id, &(lq->id), lq);

	/* Schedule this LLMNR query. This will create an 
	'AvahiLLMNRQueryJob' for this query and will issue
	further queries there only. im0 */
	if(!avahi_interface_post_llmnr_query(i, lq, 0)) 
		return NULL;

	return lq;
	
}
Example #3
0
/* Perform an async dns reverse lookup. This does ip -> host. For host -> ip
 * use egg_dns_lookup(). We return a dns id that you can use to cancel the
 * lookup. */
int egg_dns_reverse(const char *ip, interval_t timeout, dns_callback_t callback, void *client_data)
{
	dns_query_t *q;
	int i, cache_id;

	sdprintf("egg_dns_reverse(%s, %d)", ip, timeout);

	if (!is_dotted_ip(ip)) {
		/* If it's not a valid ip, don't even make the request. */
		callback(-1, client_data, ip, NULL);
		return(-1);
	}

	/* Ok, see if we have it in our host cache. */
	for (i = 0; i < nhosts; i++) {
		if (!egg_strcasecmp(hosts[i].ip, ip)) {
			dns_answer_t answer;

			answer_init(&answer);
			answer_add(&answer, hosts[i].host);
			callback(-1, client_data, ip, answer.list);
			answer_free(&answer);
			return(-1);
		}
	}

	cache_id = cache_find(ip);
        if (cache_id >= 0) {
		shuffleArray(cache[cache_id].answer.list, cache[cache_id].answer.len);
		callback(-1, client_data, ip, cache[cache_id].answer.list);
		return(-1);
	}

	/* check if the query was already made */
        if (find_query(ip))
          return(-1);

	q = alloc_query(client_data, callback, ip);

	/* We need to transform the ip address into the proper form
	 * for reverse lookup. */
	if (strchr(ip, ':')) {
		char temp[128] = "";

		socket_ipv6_to_dots(ip, temp);
sdprintf("dots: %s", temp);
		size_t iplen = strlen(temp) + 9 + 1;
		q->ip = (char *) my_calloc(1, iplen);
//		reverse_ip(temp, q->ip);
		strlcat(q->ip, temp, iplen);
		strlcat(q->ip, "ip6.arpa", iplen);
sdprintf("reversed ipv6 ip: %s", q->ip);
	}
	else {
		size_t iplen = strlen(ip) + 13 + 1;
		q->ip = (char *) my_calloc(1, iplen);
		reverse_ip(ip, q->ip);
		strlcat(q->ip, ".in-addr.arpa", iplen);
	}

        dns_send_query(q);

//	/* setup timer to detect dead ns */
//	dns_create_timeout_timer(&q, ip, timeout);

	return(q->id);
}