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

ares_query.c

Go to the documentation of this file.
00001 /* Copyright 1998 by the Massachusetts Institute of Technology.
00002  *
00003  * Permission to use, copy, modify, and distribute this
00004  * software and its documentation for any purpose and without
00005  * fee is hereby granted, provided that the above copyright
00006  * notice appear in all copies and that both that copyright
00007  * notice and this permission notice appear in supporting
00008  * documentation, and that the name of M.I.T. not be used in
00009  * advertising or publicity pertaining to distribution of the
00010  * software without specific, written prior permission.
00011  * M.I.T. makes no representations about the suitability of
00012  * this software for any purpose.  It is provided "as is"
00013  * without express or implied warranty.
00014  *
00015  * CHANGELOG: this file has been modified by Sergio Perez Alcañiz <serpeal@disca.upv.es> 
00016  *            Departamento de Informática de Sistemas y Computadores          
00017  *            Universidad Politécnica de Valencia                             
00018  *            Valencia (Spain)    
00019  *            Date: April 2003                                          
00020  *
00021  */
00022 
00023 #include "ares_private.h"
00024 #include "ares_dns.h"
00025 
00026 struct qquery {
00027   ares_callback callback;
00028   void *arg;
00029 };
00030 
00031 static void qcallback(void *arg, int status, unsigned char *abuf, int alen);
00032 
00033 void ares_query(ares_channel channel, const char *name, int dnsclass,
00034                 int type, ares_callback callback, void *arg)
00035 {
00036   struct qquery *qquery;
00037   unsigned char *qbuf;
00038   int qlen, rd, status;
00039 
00040   /* Compose the query. */
00041   rd = !(channel->flags & ARES_FLAG_NORECURSE);
00042   status = ares_mkquery(name, dnsclass, type, channel->next_id, rd, &qbuf,
00043                         &qlen);
00044   channel->next_id++;
00045   if (status != ARES_SUCCESS)
00046     {
00047       callback(arg, status, NULL, 0);
00048       return;
00049     }
00050 
00051   /* Allocate and fill in the query structure. */
00052   qquery = malloc(sizeof(struct qquery));
00053   if (!qquery)
00054     {
00055       ares_free_string(qbuf);
00056       callback(arg, ARES_ENOMEM, NULL, 0);
00057       return;
00058     }
00059   qquery->callback = callback;
00060   qquery->arg = arg;
00061 
00062   /* Send it off.  qcallback will be called when we get an answer. */
00063   ares_send(channel, qbuf, qlen, qcallback, qquery);
00064   ares_free_string(qbuf);
00065 }
00066 
00067 static void qcallback(void *arg, int status, unsigned char *abuf, int alen)
00068 {
00069   struct qquery *qquery = (struct qquery *) arg;
00070   unsigned int ancount;
00071   int rcode;
00072 
00073   if (status != ARES_SUCCESS)
00074     qquery->callback(qquery->arg, status, abuf, alen);
00075   else
00076     {
00077       /* Pull the response code and answer count from the packet. */
00078       rcode = DNS_HEADER_RCODE(abuf);
00079       ancount = DNS_HEADER_ANCOUNT(abuf);
00080 
00081       /* Convert errors. */
00082       switch (rcode)
00083         {
00084         case NOERROR:
00085           status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA;
00086           break;
00087         case FORMERR:
00088           status = ARES_EFORMERR;
00089           break;
00090         case SERVFAIL:
00091           status = ARES_ESERVFAIL;
00092           break;
00093         case NXDOMAIN:
00094           status = ARES_ENOTFOUND;
00095           break;
00096         case NOTIMP:
00097           status = ARES_ENOTIMP;
00098           break;
00099         case REFUSED:
00100           status = ARES_EREFUSED;
00101           break;
00102         }
00103       qquery->callback(qquery->arg, status, abuf, alen);
00104     }
00105   free(qquery);
00106 }

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