Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

adns.c File Reference


Detailed Description

Asynchronous DNS lookup.

Author:
Christian Biere
Date:
2004

#include "common.h"
#include "adns.h"
#include "atoms.h"
#include "inputevt.h"
#include "misc.h"
#include "glib-missing.h"
#include "tm.h"
#include "walloc.h"
#include "socket.h"
#include "override.h"

Data Structures

struct  adns_common
struct  adns_reverse_query
struct  adns_query
struct  adns_reply
struct  adns_reverse_reply
struct  adns_request
struct  adns_response
struct  adns_async_write
struct  adns_cache_entry
struct  adns_cache_struct

Defines

#define ADNS_CACHE_TIMEOUT   (60)
 Cache entries will expire after ADNS_CACHE_TIMEOUT seconds.
#define ADNS_CACHE_MAX_SIZE   (1024)
 Cache max.
#define CLOSE_IF_VALID(fd)
 Private macros.

Typedefs

typedef adns_async_write adns_async_write_t
typedef adns_cache_entry adns_cache_entry_t
typedef adns_cache_struct adns_cache_t

Functions

size_t adns_cache_entry_size (size_t n)
size_t count_addrs (const host_addr_t *addrs, size_t m)
adns_cache_tadns_cache_init (void)
 Private functions.
adns_cache_entry_tadns_cache_get_entry (adns_cache_t *cache, guint i)
void adns_cache_free_entry (adns_cache_t *cache, guint i)
void adns_cache_free (adns_cache_t *cache)
 Frees all memory allocated by the cache and returns NULL.
void adns_cache_add (adns_cache_t *cache, time_t now, const gchar *hostname, const host_addr_t *addrs, size_t n)
 Adds ``hostname'' and ``addr'' to the cache.
size_t adns_cache_lookup (adns_cache_t *cache, time_t now, const gchar *hostname, host_addr_t *addrs, size_t n)
 Looks for ``hostname'' in ``cache'' wrt to cache->timeout.
gboolean adns_do_transfer (gint fd, gpointer buf, size_t len, gboolean do_write)
 Transfers the data in `buf' of size `len' through `fd'.
gboolean adns_do_read (gint fd, gpointer buf, size_t len)
 Read the complete buffer ``buf'' of size ``len'' from file descriptor ``fd''.
gboolean adns_do_write (gint fd, gpointer buf, size_t len)
 Write the complete buffer ``buf'' of size ``len'' to file descriptor ``fd''.
void adns_gethostbyname (const struct adns_request *req, struct adns_response *ans)
 Copies user_callback and user_data from the query buffer to the reply buffer.
void adns_helper (gint fd_in, gint fd_out)
 The ``main'' function of the adns helper process (server).
void adns_invoke_user_callback (const struct adns_response *ans)
void adns_fallback (const struct adns_request *req)
 Handles the query in synchronous (blocking) mode and is used if the dns helper is busy i.e., the pipe buffer is full or in case the dns helper is dead.
void adns_reply_ready (const struct adns_response *ans)
void adns_reply_callback (gpointer data, gint source, inputevt_cond_t condition)
 Callback function for inputevt_add().
adns_async_write_tadns_async_write_alloc (const struct adns_request *req, gconstpointer buf, size_t size)
 Allocate a "spill" buffer of size `size'.
void adns_async_write_free (adns_async_write_t *remain)
 Dispose of the "spill" buffer.
void adns_query_callback (gpointer data, gint dest, inputevt_cond_t condition)
 Callback function for inputevt_add().
void adns_init (void)
 Initializes the adns helper i.e., fork()s a child process which will be used to resolve hostnames asynchronously.
gboolean adns_send_request (const struct adns_request *req)
gboolean adns_resolve (const gchar *hostname, enum net_type net, adns_callback_t user_callback, gpointer user_data)
 Creates a DNS resolve query for ``hostname''.
gboolean adns_reverse_lookup (const host_addr_t addr, adns_reverse_callback_t user_callback, gpointer user_data)
 Creates a DNS reverse lookup query for ``addr''.
void adns_close (void)
 Removes the callback and frees the cache.

Variables

guint32 common_dbg = 0
const gchar adns_process_title [] = "DNS helper for gtk-gnutella"
adns_cache_tadns_cache = NULL
gint adns_query_fd = -1
guint adns_query_event_id = 0
guint adns_reply_event_id = 0
gboolean is_helper = FALSE
 Are we the DNS helper process?


Define Documentation

#define ADNS_CACHE_MAX_SIZE   (1024)
 

Cache max.

ADNS_CACHE_SIZE of adns_cache_entry_t entries.

#define ADNS_CACHE_TIMEOUT   (60)
 

Cache entries will expire after ADNS_CACHE_TIMEOUT seconds.

#define CLOSE_IF_VALID fd   ) 
 

Value:

do {                        \
    if (-1 != (fd)) {       \
        close(fd);          \
        fd = -1;            \
    }                       \
} while(0)
Private macros.


Typedef Documentation

typedef struct adns_async_write adns_async_write_t
 

typedef struct adns_cache_entry adns_cache_entry_t
 

typedef struct adns_cache_struct adns_cache_t
 


Function Documentation

adns_async_write_t* adns_async_write_alloc const struct adns_request req,
gconstpointer  buf,
size_t  size
[static]
 

Allocate a "spill" buffer of size `size'.

void adns_async_write_free adns_async_write_t remain  )  [static]
 

Dispose of the "spill" buffer.

void adns_cache_add adns_cache_t cache,
time_t  now,
const gchar *  hostname,
const host_addr_t addrs,
size_t  n
[static]
 

Adds ``hostname'' and ``addr'' to the cache.

The cache is implemented as a wrap-around FIFO. In case it's full, the oldest entry will be overwritten.

size_t adns_cache_entry_size size_t  n  )  [inline, static]
 

void adns_cache_free adns_cache_t cache  ) 
 

Frees all memory allocated by the cache and returns NULL.

void adns_cache_free_entry adns_cache_t cache,
guint  i
[static]
 

adns_cache_entry_t* adns_cache_get_entry adns_cache_t cache,
guint  i
[inline, static]
 

adns_cache_t* adns_cache_init void   )  [static]
 

Private functions.

size_t adns_cache_lookup adns_cache_t cache,
time_t  now,
const gchar *  hostname,
host_addr_t addrs,
size_t  n
[static]
 

Looks for ``hostname'' in ``cache'' wrt to cache->timeout.

If ``hostname'' is not found or the entry is expired, FALSE will be returned. Expired entries will be removed! ``addr'' is allowed to be NULL, otherwise the cached IP will be stored into the variable ``addr'' points to.

Parameters:
addrs An array of host_addr_t items. If not NULL, up to ``n'' items will be copied from the cache.
n The number of items "addrs" can hold.
Returns:
The number of cached addresses for the given hostname.

void adns_close void   ) 
 

Removes the callback and frees the cache.

gboolean adns_do_read gint  fd,
gpointer  buf,
size_t  len
[static]
 

Read the complete buffer ``buf'' of size ``len'' from file descriptor ``fd''.

Returns:
TRUE on success, FALSE if the operation failed

gboolean adns_do_transfer gint  fd,
gpointer  buf,
size_t  len,
gboolean  do_write
[static]
 

Transfers the data in `buf' of size `len' through `fd'.

If `do_write' is FALSE the buffer will be filled from `fd'. Otherwise, the data from the buffer will be written to `fd'. The function returns only if all data has been transferred or if an unrecoverable error occurs. This function should only be used with a blocking `fd'.

gboolean adns_do_write gint  fd,
gpointer  buf,
size_t  len
[static]
 

Write the complete buffer ``buf'' of size ``len'' to file descriptor ``fd''.

Returns:
TRUE on success, FALSE if the operation failed

void adns_fallback const struct adns_request req  )  [static]
 

Handles the query in synchronous (blocking) mode and is used if the dns helper is busy i.e., the pipe buffer is full or in case the dns helper is dead.

void adns_gethostbyname const struct adns_request req,
struct adns_response ans
[static]
 

Copies user_callback and user_data from the query buffer to the reply buffer.

This function won't fail. However, if gethostbyname() fails ``reply->addr'' will be set to zero.

void adns_helper gint  fd_in,
gint  fd_out
[static]
 

The ``main'' function of the adns helper process (server).

Simply reads requests (queries) from fd_in, performs a DNS lookup for it and writes the result to fd_out. All operations should be blocking. Exits in case of non-recoverable error during read or write.

void adns_init void   ) 
 

Initializes the adns helper i.e., fork()s a child process which will be used to resolve hostnames asynchronously.

Close all standard FILEs so that they don't keep a reference to the log files when they are reopened by the main process on SIGHUP. This means there will be no visible messages from ADNS at all.

void adns_invoke_user_callback const struct adns_response ans  )  [inline, static]
 

void adns_query_callback gpointer  data,
gint  dest,
inputevt_cond_t  condition
[static]
 

Callback function for inputevt_add().

This function pipes the query to the server using the pipe in non-blocking mode, partial writes are handled appropriately. In case of an unrecoverable error the query pipe will be closed and the blocking adns_fallback() will be invoked.

void adns_reply_callback gpointer  data,
gint  source,
inputevt_cond_t  condition
[static]
 

Callback function for inputevt_add().

This function invokes the callback function given in DNS query on the client-side i.e., gtk-gnutella itself. It handles partial reads if necessary. In case of an unrecoverable error the reply pipe will be closed and the callback will be lost.

void adns_reply_ready const struct adns_response ans  )  [static]
 

gboolean adns_resolve const gchar *  hostname,
enum net_type  net,
adns_callback_t  user_callback,
gpointer  user_data
 

Creates a DNS resolve query for ``hostname''.

The given function ``user_callback'' (which MUST NOT be NULL) will be invoked with the resolved IP address and ``user_data'' as its parameters. The IP address 0.0.0.0 i.e., ``(guint32) 0'' is used to indicate a failure. In case the hostname is given as an IP string, it will be directly converted and the callback immediately invoked. If the adns helper process is ``out of service'' the query will be resolved synchronously.

Returns:
TRUE if the resolution is asynchronous i.e., the callback will be called AFTER adns_resolve() returned. If the resolution is synchronous i.e., the callback was called BEFORE adns_resolve() returned, adns_resolve() returns FALSE.

gboolean adns_reverse_lookup const host_addr_t  addr,
adns_reverse_callback_t  user_callback,
gpointer  user_data
 

Creates a DNS reverse lookup query for ``addr''.

The given function ``user_callback'' (which MUST NOT be NULL) will be invoked with the resolved hostname and ``user_data'' as its parameters. If the lookup failed, the callback will be invoked with ``hostname'' NULL. If the adns helper process is ``out of service'' the query will be processed synchronously.

Returns:
TRUE if the resolution is asynchronous i.e., the callback will be called AFTER adns_reverse_lookup() returned. If the resolution is synchronous i.e., the callback was called BEFORE adns_reverse_lookup() returned, adns_reverse_lookup() returns FALSE.

gboolean adns_send_request const struct adns_request req  )  [static]
 

Returns:
TRUE on success, FALSE on failure.

size_t count_addrs const host_addr_t addrs,
size_t  m
[inline, static]
 


Variable Documentation

adns_cache_t* adns_cache = NULL [static]
 

const gchar adns_process_title[] = "DNS helper for gtk-gnutella" [static]
 

guint adns_query_event_id = 0 [static]
 

gint adns_query_fd = -1 [static]
 

guint adns_reply_event_id = 0 [static]
 

guint32 common_dbg = 0 [static]
 

Bug:
XXX need to init lib's props --RAM

gboolean is_helper = FALSE [static]
 

Are we the DNS helper process?


Generated on Sat Jun 30 17:53:25 2007 for gtk-gnutella by  doxygen 1.3.9.1