* Unique request ID of a client if a query for this hostname/record_type
* is currently pending, undefined otherwise.
*/
- uint16_t request_id;
+ uint32_t client_request_id;
/**
* Unique DNS request ID of a client if a query for this hostname/record_type
*/
static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
+/**
+ * My domain, to be appended to the hostname to get a FQDN.
+ */
+static char *my_domain;
+
/**
* How many entries do we have in #cache_head DLL?
*/
static unsigned int cache_size;
+
/**
* Remove @a entry from cache.
*
}
+/**
+ * Find out if the configuration file line contains a string
+ * starting with "search ", and if so, return a copy of
+ * the machine's search domain.
+ *
+ * @param line line to parse
+ * @param line_len number of characters in @a line
+ * @return NULL if no nameserver is configured in this @a line
+ */
+static char *
+extract_search_domain (const char* line,
+ size_t line_len)
+{
+ if (0 == strncmp (line,
+ "search ",
+ strlen ("search ")))
+ return GNUNET_strndup (line + strlen ("search "),
+ line_len - strlen ("search "));
+ return NULL;
+}
+
+
/**
* Reads the list of nameservers from /etc/resolve.conf
*
dns_server = extract_dns_server (buf + read_offset,
line_len);
if (NULL != dns_server)
+ {
GNUNET_array_append (*server_addrs,
num_dns_servers,
dns_server);
+ }
+ else if (NULL == my_domain)
+ {
+ my_domain = extract_search_domain (buf + read_offset,
+ line_len);
+ }
read_offset += line_len + 1;
}
GNUNET_DISK_file_close (fh);
*
* @param record information to transmit
* @param record_type requested record type from client
- * @param request_id to which request are we responding
+ * @param client_request_id to which request are we responding
* @param client where to send @a record
* @return #GNUNET_YES if we sent a reply,
* #GNUNET_NO if the record type is not understood or
static int
send_reply (struct GNUNET_DNSPARSER_Record *record,
uint16_t record_type,
- uint16_t request_id,
+ uint32_t client_request_id,
struct GNUNET_SERVICE_Client *client)
{
struct GNUNET_RESOLVER_ResponseMessage *msg;
env = GNUNET_MQ_msg_extra (msg,
payload_len,
GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
- msg->id = request_id;
+ msg->client_id = client_request_id;
GNUNET_memcpy (&msg[1],
payload,
payload_len);
/**
* Send message to @a client that we transmitted all
- * responses for @a request_id
+ * responses for @a client_request_id
*
- * @param request_id to which request are we responding
+ * @param client_request_id to which request are we responding
* @param client where to send @a record
*/
static void
-send_end_msg (uint16_t request_id,
+send_end_msg (uint32_t client_request_id,
struct GNUNET_SERVICE_Client *client)
{
struct GNUNET_RESOLVER_ResponseMessage *msg;
"Sending END message\n");
env = GNUNET_MQ_msg (msg,
GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
- msg->id = request_id;
+ msg->client_id = client_request_id;
GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
env);
}
*
* @param hostname DNS name to resolve
* @param record_type desired record type
- * @param request_id client's request ID
+ * @param client_request_id client's request ID
* @param client who should get the result?
*/
static void
process_get (const char *hostname,
uint16_t record_type,
- uint16_t request_id,
+ uint32_t client_request_id,
struct GNUNET_SERVICE_Client *client);
*
* @param hostname what hostname was to be resolved
* @param record_type what type of record was requested
- * @param request_id unique identification of the client's request
+ * @param client_request_id unique identification of the client's request
* @param client handle to the client making the request (for sending the reply)
*/
static int
try_cache (const char *hostname,
uint16_t record_type,
- uint16_t request_id,
+ uint32_t client_request_id,
struct GNUNET_SERVICE_Client *client)
{
struct ResolveCache *pos;
process_get (hostname,
record_type,
- request_id,
+ client_request_id,
client);
return GNUNET_YES; /* counts as a cache "hit" */
}
found |= send_reply (rle->record,
record_type,
- request_id,
+ client_request_id,
client);
}
if (GNUNET_NO == found)
return GNUNET_NO; /* had records, but none matched! */
- send_end_msg (request_id,
+ send_end_msg (client_request_id,
client);
return GNUNET_YES;
}
GNUNET_DNSPARSER_free_packet (parsed);
return;
}
- if (0 == parsed->num_answers)
+ if (0 == parsed->num_answers + parsed->num_authority_records + parsed->num_additional_records)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"DNS reply (hostname %s, request ID %u) contains no answers\n",
al->hostname,
- al->request_id);
+ (unsigned int) al->client_request_id);
GNUNET_DNSPARSER_free_packet (parsed);
- send_end_msg (al->request_id,
+ send_end_msg (al->client_request_id,
al->client);
free_active_lookup (al);
return;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Got reply for hostname %s and request ID %u\n",
al->hostname,
- al->request_id);
+ (unsigned int) al->client_request_id);
/* add to cache */
for (unsigned int i = 0; i != parsed->num_answers; i++)
{
rc->records_tail,
rle);
}
+ for (unsigned int i = 0; i != parsed->num_authority_records; i++)
+ {
+ struct GNUNET_DNSPARSER_Record *record = &parsed->authority_records[i];
+ struct RecordListEntry *rle;
+
+ for (rc = cache_head; NULL != rc; rc = rc->next)
+ if (0 == strcasecmp (rc->hostname,
+ record->name))
+ break;
+ if (NULL == rc)
+ {
+ rc = GNUNET_new (struct ResolveCache);
+ rc->hostname = GNUNET_strdup (record->name);
+ GNUNET_CONTAINER_DLL_insert (cache_head,
+ cache_tail,
+ rc);
+ cache_size++;
+ }
+ /* TODO: ought to check first if we have this exact record
+ already in the cache! */
+ rle = GNUNET_new (struct RecordListEntry);
+ rle->record = GNUNET_DNSPARSER_duplicate_record (record);
+ GNUNET_CONTAINER_DLL_insert (rc->records_head,
+ rc->records_tail,
+ rle);
+ }
+ for (unsigned int i = 0; i != parsed->num_additional_records; i++)
+ {
+ struct GNUNET_DNSPARSER_Record *record = &parsed->additional_records[i];
+ struct RecordListEntry *rle;
+
+ for (rc = cache_head; NULL != rc; rc = rc->next)
+ if (0 == strcasecmp (rc->hostname,
+ record->name))
+ break;
+ if (NULL == rc)
+ {
+ rc = GNUNET_new (struct ResolveCache);
+ rc->hostname = GNUNET_strdup (record->name);
+ GNUNET_CONTAINER_DLL_insert (cache_head,
+ cache_tail,
+ rc);
+ cache_size++;
+ }
+ /* TODO: ought to check first if we have this exact record
+ already in the cache! */
+ rle = GNUNET_new (struct RecordListEntry);
+ rle->record = GNUNET_DNSPARSER_duplicate_record (record);
+ GNUNET_CONTAINER_DLL_insert (rc->records_head,
+ rc->records_tail,
+ rle);
+ }
/* see if we need to do the 2nd request for AAAA records */
if ( (GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
(GNUNET_NO == al->did_aaaa) )
if (GNUNET_NO ==
try_cache (al->hostname,
al->record_type,
- al->request_id,
+ al->client_request_id,
al->client))
/* cache failed, tell client we could not get an answer */
- send_end_msg (al->request_id,
+ {
+ send_end_msg (al->client_request_id,
al->client);
+ }
free_active_lookup (al);
GNUNET_DNSPARSER_free_packet (parsed);
}
al->timeout_task = NULL;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"DNS lookup timeout!\n");
- send_end_msg (al->request_id,
+ send_end_msg (al->client_request_id,
al->client);
free_active_lookup (al);
}
*
* @param hostname DNS name to resolve
* @param record_type record type to locate
- * @param request_id client request ID
+ * @param client_request_id client request ID
* @param client handle to the client
* @return #GNUNET_OK if the DNS query is now pending
*/
static int
resolve_and_cache (const char* hostname,
uint16_t record_type,
- uint16_t request_id,
+ uint32_t client_request_id,
struct GNUNET_SERVICE_Client *client)
{
char *packet_buf;
uint16_t type;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "resolve_and_cache\n");
+ "resolve_and_cache `%s'\n",
+ hostname);
dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
UINT16_MAX);
al = GNUNET_new (struct ActiveLookup);
al->hostname = GNUNET_strdup (hostname);
al->record_type = record_type;
- al->request_id = request_id;
+ al->client_request_id = client_request_id;
al->dns_id = dns_id;
al->client = client;
al->timeout_task = GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT,
lookup_tail,
al);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Resolving %s, request_id = %u, dns_id = %u\n",
+ "Resolving %s, client_request_id = %u, dns_id = %u\n",
hostname,
- (unsigned int) request_id,
+ (unsigned int) client_request_id,
(unsigned int) dns_id);
return GNUNET_OK;
}
/**
- * Process DNS request for @a hostname with request ID @a request_id
+ * Process DNS request for @a hostname with request ID @a client_request_id
* from @a client demanding records of type @a record_type.
*
* @param hostname DNS name to resolve
* @param record_type desired record type
- * @param request_id client's request ID
+ * @param client_request_id client's request ID
* @param client who should get the result?
*/
static void
process_get (const char *hostname,
uint16_t record_type,
- uint16_t request_id,
+ uint32_t client_request_id,
struct GNUNET_SERVICE_Client *client)
{
+ char fqdn[255];
+
+ if ( (NULL != my_domain) &&
+ (NULL == strchr (hostname,
+ (unsigned char) '.')) &&
+ (strlen (hostname) + strlen (my_domain) <= 253) )
+ {
+ GNUNET_snprintf (fqdn,
+ sizeof (fqdn),
+ "%s.%s",
+ hostname,
+ my_domain);
+ }
+ else if (strlen (hostname) < 255)
+ {
+ GNUNET_snprintf (fqdn,
+ sizeof (fqdn),
+ "%s",
+ hostname);
+ }
+ else
+ {
+ GNUNET_break (0);
+ GNUNET_SERVICE_client_drop (client);
+ return;
+ }
if (GNUNET_NO ==
- try_cache (hostname,
+ try_cache (fqdn,
record_type,
- request_id,
+ client_request_id,
client))
{
if (GNUNET_OK !=
- resolve_and_cache (hostname,
+ resolve_and_cache (fqdn,
record_type,
- request_id,
+ client_request_id,
client))
{
- send_end_msg (request_id,
+ send_end_msg (client_request_id,
client);
}
}
struct GNUNET_SERVICE_Client *client = cls;
int direction;
int af;
- uint16_t request_id;
+ uint32_t client_request_id;
char *hostname;
direction = ntohl (msg->direction);
af = ntohl (msg->af);
- request_id = ntohs (msg->id);
+ client_request_id = msg->client_id;
+ GNUNET_SERVICE_client_continue (client);
if (GNUNET_NO == direction)
{
/* IP from hostname */
{
process_get (hostname,
GNUNET_DNSPARSER_TYPE_ALL,
- request_id,
+ client_request_id,
client);
break;
}
{
process_get (hostname,
GNUNET_DNSPARSER_TYPE_A,
- request_id,
+ client_request_id,
client);
break;
}
{
process_get (hostname,
GNUNET_DNSPARSER_TYPE_AAAA,
- request_id,
+ client_request_id,
client);
break;
}
default:
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "got invalid af: %d\n",
- af);
+ "got invalid af: %d\n",
+ af);
GNUNET_assert (0);
}
}
af);
process_get (hostname,
GNUNET_DNSPARSER_TYPE_PTR,
- request_id,
+ client_request_id,
client);
}
GNUNET_free_non_null (hostname);
- GNUNET_SERVICE_client_continue (client);
}