ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/res.c
(Generate patch)

Comparing:
ircd-hybrid/trunk/src/irc_res.c (file contents), Revision 3268 by michael, Sat Apr 5 19:21:55 2014 UTC vs.
ircd-hybrid/branches/8.2.x/src/res.c (file contents), Revision 4235 by michael, Tue Jul 15 17:32:14 2014 UTC

# Line 19 | Line 19
19   *  USA
20   */
21  
22 < /*! \file irc_res.c
22 > /*! \file res.c
23   * \brief ircd resolver functions
24   * \version $Id$
25   */
# Line 56 | Line 56
56   #include "fdlist.h"
57   #include "s_bsd.h"
58   #include "log.h"
59 < #include "s_misc.h"
59 > #include "misc.h"
60   #include "send.h"
61   #include "memory.h"
62   #include "mempool.h"
63 < #include "irc_res.h"
64 < #include "irc_reslib.h"
63 > #include "res.h"
64 > #include "reslib.h"
65  
66   #if (CHAR_BIT != 8)
67   #error this code needs to be able to address individual octets
# Line 70 | Line 70
70   static PF res_readreply;
71  
72   #define MAXPACKET      1024  /* rfc sez 512 but we expand names so ... */
73 #define RES_MAXALIASES 35    /* maximum aliases allowed */
74 #define RES_MAXADDRS   35    /* maximum addresses allowed */
73   #define AR_TTL         600   /* TTL in seconds for dns cache entries */
74  
75   /* RFC 1104/1105 wasn't very helpful about what these fields
# Line 104 | Line 102 | struct reslist
102    time_t ttl;
103    char type;
104    char retries;            /* retry counter */
105 <  char sends;              /* number of sends (>1 means resent) */
105 >  unsigned int sends;      /* number of sends (>1 means resent) */
106    char resend;             /* send flag. 0 == dont resend */
107    time_t sentat;
108    time_t timeout;
# Line 126 | Line 124 | static void do_query_number(dns_callback
124                              const struct irc_ssaddr *,
125                              struct reslist *);
126   static void query_name(const char *, int, int, struct reslist *);
127 < static int send_res_msg(const char *, int, int);
127 > static int send_res_msg(const char *, int, unsigned int);
128   static void resend_query(struct reslist *);
129   static int proc_answer(struct reslist *, HEADER *, char *, char *);
130   static struct reslist *find_id(int);
# Line 153 | Line 151 | res_ourserver(const struct irc_ssaddr *i
151    const struct sockaddr_in *v4;
152    const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
153  
154 <  for (int i = 0; i < irc_nscount; ++i)
154 >  for (unsigned int i = 0; i < irc_nscount; ++i)
155    {
156      const struct irc_ssaddr *srv = &irc_nsaddr_list[i];
157   #ifdef IPV6
# Line 195 | Line 193 | res_ourserver(const struct irc_ssaddr *i
193   * there too long without being resolved.
194   */
195   static time_t
196 < timeout_query_list(time_t now)
196 > timeout_query_list(void)
197   {
198    dlink_node *ptr = NULL, *ptr_next = NULL;
199 <  struct reslist *request;
199 >  struct reslist *request = NULL;
200    time_t next_time = 0;
201    time_t timeout   = 0;
202  
# Line 207 | Line 205 | timeout_query_list(time_t now)
205      request = ptr->data;
206      timeout = request->sentat + request->timeout;
207  
208 <    if (now >= timeout)
208 >    if (CurrentTime >= timeout)
209      {
210        if (--request->retries <= 0)
211        {
# Line 217 | Line 215 | timeout_query_list(time_t now)
215        }
216        else
217        {
218 <        request->sentat = now;
218 >        request->sentat = CurrentTime;
219          request->timeout += request->timeout;
220          resend_query(request);
221        }
# Line 227 | Line 225 | timeout_query_list(time_t now)
225        next_time = timeout;
226    }
227  
228 <  return (next_time > now) ? next_time : (now + AR_TTL);
228 >  return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL);
229   }
230  
231   /*
# Line 236 | Line 234 | timeout_query_list(time_t now)
234   static void
235   timeout_resolver(void *notused)
236   {
237 <  timeout_query_list(CurrentTime);
237 >  timeout_query_list();
238   }
239  
240   /*
# Line 255 | Line 253 | start_resolver(void)
253        return;
254  
255      /* At the moment, the resolver FD data is global .. */
256 <    comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ,
259 <        res_readreply, NULL, 0);
260 <    eventAdd("timeout_resolver", timeout_resolver, NULL, 1);
256 >    comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0);
257    }
258   }
259  
# Line 267 | Line 263 | start_resolver(void)
263   void
264   init_resolver(void)
265   {
266 +  static struct event event_timeout_resolver =
267 +  {
268 +    .name = "timeout_resolver",
269 +    .handler = timeout_resolver,
270 +    .when = 1
271 +  };
272 +
273    dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS);
274    memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
275    start_resolver();
276 +
277 +  event_add(&event_timeout_resolver, NULL);
278   }
279  
280   /*
# Line 279 | Line 284 | void
284   restart_resolver(void)
285   {
286    fd_close(&ResolverFileDescriptor);
282  eventDelete(timeout_resolver, NULL); /* -ddosen */
287    start_resolver();
288   }
289  
# Line 305 | Line 309 | make_request(dns_callback_fnc callback,
309   {
310    struct reslist *request = mp_pool_get(dns_pool);
311  
308  memset(request, 0, sizeof(*request));
312    request->sentat       = CurrentTime;
313    request->retries      = 2;
314    request->resend       = 1;
315 <  request->timeout      = 4;    /* start at 4 and exponential inc. */
315 >  request->timeout      = 4;  /* Start at 4 and exponential inc. */
316    request->state        = REQ_IDLE;
317    request->callback     = callback;
318    request->callback_ctx = ctx;
# Line 344 | Line 347 | delete_resolver_queries(const void *vptr
347   * nameservers or -1 if no successful sends.
348   */
349   static int
350 < send_res_msg(const char *msg, int len, int rcount)
350 > send_res_msg(const char *msg, int len, unsigned int rcount)
351   {
352    int sent = 0;
353 <  int max_queries = IRCD_MIN(irc_nscount, rcount);
353 >  unsigned int max_queries = IRCD_MIN(irc_nscount, rcount);
354  
355    /* RES_PRIMARY option is not implemented
356     * if (res.options & RES_PRIMARY || 0 == max_queries)
# Line 355 | Line 358 | send_res_msg(const char *msg, int len, i
358    if (max_queries == 0)
359      max_queries = 1;
360  
361 <  for (int i = 0; i < max_queries; ++i)
361 >  for (unsigned int i = 0; i < max_queries; ++i)
362    {
363      if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
364          (struct sockaddr*)&(irc_nsaddr_list[i]),
# Line 432 | Line 435 | do_query_name(dns_callback_fnc callback,
435    if (request == NULL)
436    {
437      request       = make_request(callback, ctx);
438 <    request->name = MyMalloc(strlen(host_name) + 1);
438 >    request->name = MyCalloc(strlen(host_name) + 1);
439      request->type = type;
440      strcpy(request->name, host_name);
441   #ifdef IPV6
# Line 455 | Line 458 | do_query_number(dns_callback_fnc callbac
458                  const struct irc_ssaddr *addr,
459                  struct reslist *request)
460   {
461 <  char ipbuf[128];
459 <  const unsigned char *cp;
461 >  char ipbuf[128] = "";
462  
463    if (addr->ss.ss_family == AF_INET)
464    {
465      const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
466 <    cp = (const unsigned char *)&v4->sin_addr.s_addr;
466 >    const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr;
467  
468      snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
469               (unsigned int)(cp[3]), (unsigned int)(cp[2]),
# Line 471 | Line 473 | do_query_number(dns_callback_fnc callbac
473    else if (addr->ss.ss_family == AF_INET6)
474    {
475      const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
476 <    cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
476 >    const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
477  
478      snprintf(ipbuf, sizeof(ipbuf),
479               "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
# Line 499 | Line 501 | do_query_number(dns_callback_fnc callbac
501      request       = make_request(callback, ctx);
502      request->type = T_PTR;
503      memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
504 <    request->name = MyMalloc(HOSTLEN + 1);
504 >    request->name = MyCalloc(HOSTLEN + 1);
505    }
506  
507    query_name(ipbuf, C_IN, T_PTR, request);
# Line 600 | Line 602 | proc_answer(struct reslist *request, HEA
602      n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
603          hostbuf, sizeof(hostbuf));
604  
605 <    if (n < 0 /* broken message */ || n == 0 /* no more answers left */)
605 >    if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
606        return 0;
607  
608      hostbuf[HOSTLEN] = '\0';
# Line 609 | Line 611 | proc_answer(struct reslist *request, HEA
611       * this code was not working on alpha due to that
612       * (spotted by rodder/jailbird/dianora)
613       */
614 <    current += (size_t) n;
614 >    current += (size_t)n;
615  
616      if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
617        break;
# Line 636 | Line 638 | proc_answer(struct reslist *request, HEA
638            return 0;
639  
640          /*
641 <         * check for invalid rd_length or too many addresses
641 >         * Check for invalid rd_length or too many addresses
642           */
643          if (rd_length != sizeof(struct in_addr))
644            return 0;
# Line 667 | Line 669 | proc_answer(struct reslist *request, HEA
669            return 0;
670  
671          n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
672 <            current, hostbuf, sizeof(hostbuf));
673 <        if (n < 0 /* broken message */ || n == 0 /* no more answers left */)
672 >                          current, hostbuf, sizeof(hostbuf));
673 >        if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
674            return 0;
675  
676          strlcpy(request->name, hostbuf, HOSTLEN + 1);
# Line 739 | Line 741 | res_readreply(fde_t *fd, void *data)
741      return;
742  
743    /*
744 <   * convert DNS reply reader from Network byte order to CPU byte order.
744 >   * Convert DNS reply reader from Network byte order to CPU byte order.
745     */
746    header = (HEADER *)buf;
747    header->ancount = ntohs(header->ancount);
# Line 748 | Line 750 | res_readreply(fde_t *fd, void *data)
750    header->arcount = ntohs(header->arcount);
751  
752    /*
753 <   * check against possibly fake replies
753 >   * Check against possibly fake replies
754     */
755    if (!res_ourserver(&lsin))
756      return;
757  
758    /*
759 <   * response for an id which we have already received an answer for
759 >   * Response for an id which we have already received an answer for
760     * just ignore this response.
761     */
762    if (!(request = find_id(header->id)))
# Line 800 | Line 802 | res_readreply(fde_t *fd, void *data)
802        if (request->name == NULL)
803        {
804          /*
805 <         * got a PTR response with no name, something bogus is happening
805 >         * Got a PTR response with no name, something bogus is happening
806           * don't bother trying again, the client address doesn't resolve
807           */
808          (*request->callback)(request->callback_ctx, NULL, NULL);
# Line 809 | Line 811 | res_readreply(fde_t *fd, void *data)
811        }
812  
813        /*
814 <       * Lookup the 'authoritative' name that we were given for the
813 <       * ip#.
814 <       *
814 >       * Lookup the 'authoritative' name that we were given for the ip#.
815         */
816   #ifdef IPV6
817        if (request->addr.ss.ss_family == AF_INET6)
# Line 824 | Line 824 | res_readreply(fde_t *fd, void *data)
824      else
825      {
826        /*
827 <       * got a name and address response, client resolved
827 >       * Got a name and address response, client resolved
828         */
829        (*request->callback)(request->callback_ctx, &request->addr, request->name);
830        rem_request(request);
# Line 837 | Line 837 | res_readreply(fde_t *fd, void *data)
837       */
838      assert(0);
839  
840 +    (*request->callback)(request->callback_ctx, NULL, NULL);
841      /* XXX don't leak it */
842      rem_request(request);
843    }
# Line 847 | Line 848 | report_dns_servers(struct Client *source
848   {
849    char ipaddr[HOSTIPLEN + 1] = "";
850  
851 <  for (int i = 0; i < irc_nscount; ++i)
851 >  for (unsigned int i = 0; i < irc_nscount; ++i)
852    {
853      getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
854                  irc_nsaddr_list[i].ss_len, ipaddr,

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)