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

netdb.c File Reference

#include "ares_private.h"
#include "lwip/arch.h"

Include dependency graph for netdb.c:

Go to the source code of this file.

Data Structures

struct  gethost_result

Defines

#define ENSROK   0
#define array_dup(r, a, len_func)
#define array_free(a)
#define DNS_MAXDOMAIN   255

Functions

int ares_error_to_errno (int e)
void callback (void *arg, int status, struct hostent *host)
void setnameservers (const struct in_addr *ns1, const struct in_addr *ns2)
void getnameservers (struct in_addr *ns1, struct in_addr *ns2)
hostentgethostbynameaddr (const char *name, struct in_addr addr, struct gethost_result *r)
hostentgethostbyname (const char *name)
hostentgethostbyaddr (const char *addr, int len, int type)

Variables

struct {
   unsigned short   errno2
   unsigned short   ares_errno
ares_errno_trans []
in_addr __nameservers [2]


Define Documentation

#define array_dup r,
a,
len_func   ) 
 

Value:

do {                                                            \
        int __i;                                                        \
        for (__i = 0; (a)[__i]; __i++);                                 \
        (r) = malloc ((__i + 1) * sizeof (char *));                     \
        (r)[__i] = 0;                                                   \
        while (__i--) {                                                 \
            char *__p;                                                  \
            int __l;                                                    \
            __p = (a)[__i];                                             \
            __l = len_func;                                             \
            (r)[__i] = memcpy ((char *) malloc (__l), __p, (__l));      \
        }                                                               \
    } while (0)

Definition at line 67 of file netdb.c.

Referenced by callback().

#define array_free  ) 
 

Value:

do {                                                            \
        int __i;                                                        \
        for (__i = 0; (a)[__i]; __i++)                                  \
            free ((a)[__i]);                                            \
        free (a);                                                       \
    } while (0)

Definition at line 82 of file netdb.c.

Referenced by gethostbyaddr(), and gethostbyname().

#define DNS_MAXDOMAIN   255
 

Definition at line 142 of file netdb.c.

Referenced by gethostbynameaddr().

#define ENSROK   0
 

Definition at line 25 of file netdb.c.


Function Documentation

int ares_error_to_errno int  e  )  [static]
 

Definition at line 50 of file netdb.c.

References ares_errno_trans.

Referenced by gethostbynameaddr().

00051 {
00052     int i;
00053     for (i = 0; i < (sizeof(ares_errno_trans) / sizeof(ares_errno_trans[0])); i++)
00054         if (ares_errno_trans[i].ares_errno == e)
00055             return ares_errno_trans[i].errno2;
00056     rtl_printf ("no such ares error");
00057     //abort (); /* prevents warning */
00058     return -1;
00059 }

void callback void *  arg,
int  status,
struct hostent host
[static]
 

Definition at line 91 of file netdb.c.

References ARES_SUCCESS, array_dup, free, hostent::h_addr_list, hostent::h_addrtype, hostent::h_aliases, hostent::h_length, hostent::h_name, status, strcasecmp, and strdup.

Referenced by ares_gethostbyaddr(), ares_gethostbyname(), ares_query(), ares_search(), ares_send(), fake_hostent(), and gethostbynameaddr().

00092 {
00093     struct gethost_result *r = (struct gethost_result *) arg;
00094 
00095     if (host) {
00096         if (status == ARES_SUCCESS)
00097             if (r->cname)       /* we got back a cname, so retry the query with the cname */
00098                 if (strcasecmp (r->cname, host->h_name)) {
00099                     free (r->cname);
00100                     r->cname = strdup (host->h_name);
00101                     r->status = ARES_SUCCESS;
00102                     return;
00103                 }
00104 
00105 /* clear any old cname */
00106         if (r->cname) {
00107             free (r->cname);
00108             r->cname = 0;
00109         }
00110 
00111 /* copy alias list */
00112         array_dup (r->host.h_aliases, host->h_aliases, strlen (__p) + 1);
00113 
00114 /* copy address list */
00115         array_dup (r->host.h_addr_list, host->h_addr_list, host->h_length);
00116 
00117 /* copy tidbits */
00118         r->host.h_name = (char *) strdup (host->h_name);
00119         r->host.h_addrtype = host->h_addrtype;
00120         r->host.h_length = host->h_length;
00121     }
00122 
00123 /* get status */
00124     r->status = status;
00125 }

struct hostent* gethostbyaddr const char *  addr,
int  len,
int  type
 

Definition at line 238 of file netdb.c.

References AF_INET, array_free, free, gethostbynameaddr(), hostent::h_addr_list, hostent::h_aliases, hostent::h_name, gethost_result::host, and len.

00239 {
00240     static struct gethost_result r;
00241     static int init = 0;
00242     if (!init) {
00243         init = 1;
00244         memset (&r, 0, sizeof (r));
00245     }
00246     if (len != sizeof (struct in_addr) || type != AF_INET) {
00247         errno = EINVAL;
00248         return 0;
00249     }
00250     if (r.host.h_aliases)
00251         array_free (r.host.h_aliases);
00252     if (r.host.h_addr_list)
00253         array_free (r.host.h_addr_list);
00254     if (r.host.h_name)
00255         free (r.host.h_name);
00256     return gethostbynameaddr (0, *((struct in_addr *) addr), &r);
00257 }

Here is the call graph for this function:

struct hostent* gethostbyname const char *  name  ) 
 

Definition at line 219 of file netdb.c.

References array_free, free, gethostbynameaddr(), hostent::h_addr_list, hostent::h_aliases, hostent::h_name, gethost_result::host, name, and in_addr::s_addr.

Referenced by main().

00220 {
00221     struct in_addr addr;
00222     static struct gethost_result r;
00223     static int init = 0;
00224     if (!init) {
00225         init = 1;
00226         memset (&r, 0, sizeof (r));
00227     }
00228     addr.s_addr = 0;
00229     if (r.host.h_aliases)
00230         array_free (r.host.h_aliases);
00231     if (r.host.h_addr_list)
00232         array_free (r.host.h_addr_list);
00233     if (r.host.h_name)
00234         free (r.host.h_name);
00235     return gethostbynameaddr (name, addr, &r);
00236 }

Here is the call graph for this function:

struct hostent* gethostbynameaddr const char *  name,
struct in_addr  addr,
struct gethost_result r
[static]
 

Definition at line 145 of file netdb.c.

References __nameservers, AF_INET, ares_channel, ares_destroy(), ARES_ENOTFOUND, ares_error_to_errno(), ares_fds(), ares_gethostbyaddr(), ares_gethostbyname(), ares_init_options(), ARES_OPT_SERVERS, ares_process(), ARES_SUCCESS, ares_timeout(), callback(), gethost_result::cname, DNS_MAXDOMAIN, FD_ZERO, free, gethost_result::host, name, ares_options::nservers, NULL, in_addr::s_addr, ares_options::servers, gethost_result::status, status, and strdup.

Referenced by gethostbyaddr(), and gethostbyname().

00146 {
00147     char cname[DNS_MAXDOMAIN + 1];
00148     ares_channel channel;
00149     int status, nfds, cname_loops = 0;
00150     fd_set read_fds, write_fds;
00151     struct timeval *tvp, tv;
00152     struct ares_options o;
00153 
00154     if (!__nameservers[0].s_addr) {
00155         rtl_printf ("The nameservers have not been set - use the setnameservers() function\n");
00156         errno = EINVAL;
00157         return NULL;
00158     }
00159 
00160     o.servers = __nameservers;
00161     o.nservers = 1;
00162     if (__nameservers[1].s_addr)
00163         o.nservers++;
00164 
00165     status = ares_init_options (&channel, &o, ARES_OPT_SERVERS);
00166     if (status != ARES_SUCCESS) {
00167         errno = ares_error_to_errno (status);
00168         return NULL;
00169     }
00170 
00171     r->status = -1;
00172     if (r->cname)
00173         free (r->cname);
00174     r->cname = strdup (name);
00175     if (name) {
00176         if (strlen (name) > DNS_MAXDOMAIN) {
00177             ares_destroy (channel);
00178             errno = ENSRQUERYDOMAINTOOLONG;
00179             return NULL;
00180         }
00181         ares_gethostbyname (channel, name, AF_INET, callback, (void *) r);
00182     } else {
00183         ares_gethostbyaddr (channel, &addr, sizeof (addr), AF_INET, callback, (void *) r);
00184     }
00185 
00186     /* Wait for all queries to complete. */
00187     while (1) {
00188         FD_ZERO (&read_fds);
00189         FD_ZERO (&write_fds);
00190         nfds = ares_fds (channel, &read_fds, &write_fds);
00191         if (nfds == 0) {
00192             if (name && r->cname && r->status == ARES_SUCCESS) {
00193                 if (++cname_loops > 10) {
00194                     errno = ENSRCNAMELOOP;
00195                     ares_destroy (channel);
00196                     return NULL;
00197                 }
00198                 strncpy (cname, r->cname, DNS_MAXDOMAIN);
00199                 cname[DNS_MAXDOMAIN] = '\0';
00200                 name = cname;
00201                 ares_gethostbyname (channel, name, AF_INET, callback, (void *) r);
00202                 continue;
00203             }
00204             break;
00205         }
00206         tvp = ares_timeout (channel, NULL, &tv);
00207         select (nfds, &read_fds, &write_fds, NULL, tvp);
00208         ares_process (channel, &read_fds, &write_fds);
00209     }
00210 
00211     ares_destroy (channel);
00212     if (r->status != ARES_SUCCESS || r->status == -1) {
00213         errno = r->status == -1 ? ARES_ENOTFOUND : ares_error_to_errno (r->status);
00214         return NULL;
00215     }
00216     return &r->host;
00217 }

Here is the call graph for this function:

void getnameservers struct in_addr ns1,
struct in_addr ns2
 

Definition at line 135 of file netdb.c.

References __nameservers, and in_addr::s_addr.

00136 {
00137      ns1->s_addr = __nameservers[0].s_addr;
00138      ns2->s_addr = __nameservers[1].s_addr;
00139 }

void setnameservers const struct in_addr ns1,
const struct in_addr ns2
 

Definition at line 129 of file netdb.c.

References __nameservers, and in_addr::s_addr.

00130 {
00131     __nameservers[0].s_addr = ns1->s_addr;
00132     __nameservers[1].s_addr = ns2->s_addr;
00133 }


Variable Documentation

struct in_addr __nameservers[2] [static]
 

Definition at line 127 of file netdb.c.

Referenced by gethostbynameaddr(), getnameservers(), and setnameservers().

unsigned short ares_errno
 

Definition at line 28 of file netdb.c.

struct { ... } ares_errno_trans[] [static]
 

Referenced by ares_error_to_errno().

unsigned short errno2
 

Definition at line 28 of file netdb.c.


Generated on Wed Jan 14 12:59:07 2004 for RTL-lwIP-0.4 by doxygen 1.3.4