Main Page | Modules | Alphabetical List | Data Structures | 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_query
struct  adns_async_write
struct  adns_cache_entry
struct  adns_cache_struct

Defines

#define ADNS_CACHE_TIMEOUT   (5 * 60)
 Cache entries will expire after ADNS_CACHE_TIMEOUT seconds.

#define ADNS_CACHED_NUM   (1024)
 Cache max.

#define ADNS_PROCESS_TITLE   "DNS helper for gtk-gnutella"
#define CLOSE_IF_VALID(fd)
 Private macros.


Typedefs

typedef adns_query adns_query_t
typedef adns_async_write adns_async_write_t
typedef adns_cache_entry adns_cache_entry_t
typedef adns_cache_struct adns_cache_t

Functions

 RCSID ("$Id:adns.c, v 1.27 2005/12/17 10:37:15 cbiere Exp $")
adns_cache_tadns_cache_init (void)
 Private functions.

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 addr)
 Adds ``hostname'' and ``addr'' to the cache.

gboolean adns_cache_lookup (adns_cache_t *cache, time_t now, const gchar *hostname, host_addr_t *addr)
 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 adns_query_t *query, adns_query_t *reply)
 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 (adns_query_t *reply)
void adns_fallback (const adns_query_t *query)
 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_callback (gpointer data, gint source, inputevt_cond_t condition)
 Callback function for inputevt_add().

adns_async_write_tadns_async_write_alloc (const adns_query_t *query, size_t n)
 Allocate a the "spill" buffer for the query, with `n' bytes being already written into the pipe.

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_query (const adns_query_t *query)
gboolean adns_resolve (const gchar *hostname, 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
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_TIMEOUT   (5 * 60)
 

Cache entries will expire after ADNS_CACHE_TIMEOUT seconds.

#define ADNS_CACHED_NUM   (1024)
 

Cache max.

ADNS_CACHED_NUM of adns_cache_entry_t entries.

#define ADNS_PROCESS_TITLE   "DNS helper for gtk-gnutella"
 

#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
 

typedef struct adns_query adns_query_t
 


Function Documentation

adns_async_write_t* adns_async_write_alloc const adns_query_t query,
size_t  n
[static]
 

Allocate a the "spill" buffer for the query, with `n' bytes being already written into the pipe.

The query is cloned.

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  addr
[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.

void adns_cache_free adns_cache_t cache  ) 
 

Frees all memory allocated by the cache and returns NULL.

adns_cache_t* adns_cache_init void   )  [static]
 

Private functions.

gboolean adns_cache_lookup adns_cache_t cache,
time_t  now,
const gchar *  hostname,
host_addr_t addr
[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.

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 adns_query_t query  )  [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 adns_query_t query,
adns_query_t reply
[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.

void adns_invoke_user_callback adns_query_t reply  )  [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.

gboolean adns_resolve const gchar *  hostname,
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_query const adns_query_t query  )  [static]
 

Returns:
TRUE on success, FALSE on failure.

RCSID "$Id:adns.  c,
v 1.27 2005/12/17 10:37:15 cbiere Exp $" 
 


Variable Documentation

adns_cache_t* adns_cache = NULL [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 Sun Feb 12 10:49:58 2006 for Gtk-Gnutella by doxygen 1.3.6