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

Comparing:
ircd-hybrid/trunk/src/irc_reslib.c (file contents), Revision 1592 by michael, Sat Oct 27 21:02:32 2012 UTC vs.
ircd-hybrid/trunk/src/reslib.c (file contents), Revision 3852 by michael, Thu Jun 5 13:12:45 2014 UTC

# Line 10 | Line 10
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by the University of
16 *      California, Berkeley and its contributors.
13   * 4. Neither the name of the University nor the names of its contributors
14   *    may be used to endorse or promote products derived from this software
15   *    without specific prior written permission.
# Line 82 | Line 78
78  
79   #include "stdinc.h"
80   #include "ircd_defs.h"
81 < #include "irc_res.h"
82 < #include "irc_reslib.h"
81 > #include "res.h"
82 > #include "reslib.h"
83   #include "irc_string.h"
84  
85   #define NS_TYPE_ELT             0x40 /* EDNS0 extended label type */
# Line 93 | Line 89
89   /* $Id$ */
90  
91   struct irc_ssaddr irc_nsaddr_list[IRCD_MAXNS];
92 < int irc_nscount = 0;
92 > unsigned int irc_nscount = 0;
93  
94   static const char digits[] = "0123456789";
95   static const char digitvalue[256] = {
# Line 188 | Line 184 | parse_resvconf(void)
184    if ((file = fopen("/etc/resolv.conf", "r")) == NULL)
185      return;
186  
187 <  while (fgets(input, sizeof(input), file) != NULL)
187 >  while (fgets(input, sizeof(input), file))
188    {
189      /* blow away any newline */
190 <    if ((p = strpbrk(input, "\r\n")) != NULL)
190 >    if ((p = strpbrk(input, "\r\n")))
191        *p = '\0';
192  
193      p = input;
# Line 226 | Line 222 | parse_resvconf(void)
222      /* Now arg should be right where p is pointing */
223      arg = p;
224  
225 <    if ((p = strpbrk(arg, " \t")) != NULL)
225 >    if ((p = strpbrk(arg, " \t")))
226        *p = '\0';  /* take the first word */
227  
228 <    if (!irccmp(opt, "nameserver"))
228 >    if (!strcasecmp(opt, "nameserver"))
229        add_nameserver(arg);
230    }
231  
# Line 263 | Line 259 | irc_dn_expand(const unsigned char *msg,
259  
260    if (n > 0 && dst[0] == '.')
261      dst[0] = '\0';
262 <  return(n);
263 < } /*2*/
262 >  return n;
263 > }
264  
265   /*
266   * irc_ns_name_uncompress(msg, eom, src, dst, dstsiz)
# Line 282 | Line 278 | irc_ns_name_uncompress(const unsigned ch
278    int n;
279  
280    if ((n = irc_ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
281 <    return(-1);
281 >    return -1;
282    if (irc_ns_name_ntop(tmp, dst, dstsiz) == -1)
283 <    return(-1);
284 <  return(n);
285 < } /*2*/
283 >    return -1;
284 >  return n;
285 > }
286  
287   /*
288   * irc_ns_name_unpack(msg, eom, src, dst, dstsiz)
# Line 299 | Line 295 | irc_ns_name_unpack(const unsigned char *
295                     const unsigned char *src, unsigned char *dst,
296                     size_t dstsiz)
297   {
298 <        const unsigned char *srcp, *dstlim;
299 <        unsigned char *dstp;
300 <        int n, len, checked, l;
305 <
306 <        len = -1;
307 <        checked = 0;
308 <        dstp = dst;
309 <        srcp = src;
310 <        dstlim = dst + dstsiz;
311 <        if (srcp < msg || srcp >= eom) {
312 <                errno = EMSGSIZE;
313 <                return (-1);
314 <        }
315 <        /* Fetch next label in domain name. */
316 <        while ((n = *srcp++) != 0) {
317 <                /* Check for indirection. */
318 <                switch (n & NS_CMPRSFLGS) {
319 <                case 0:
320 <                case NS_TYPE_ELT:
321 <                        /* Limit checks. */
322 <                        if ((l = labellen(srcp - 1)) < 0) {
323 <                                errno = EMSGSIZE;
324 <                                return(-1);
325 <                        }
326 <                        if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
327 <                                errno = EMSGSIZE;
328 <                                return (-1);
329 <                        }
330 <                        checked += l + 1;
331 <                        *dstp++ = n;
332 <                        memcpy(dstp, srcp, l);
333 <                        dstp += l;
334 <                        srcp += l;
335 <                        break;
298 >  const unsigned char *srcp, *dstlim;
299 >  unsigned char *dstp;
300 >  int n, len, checked, l;
301  
302 <                case NS_CMPRSFLGS:
303 <                        if (srcp >= eom) {
304 <                                errno = EMSGSIZE;
305 <                                return (-1);
306 <                        }
342 <                        if (len < 0)
343 <                                len = srcp - src + 1;
344 <                        srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
345 <                        if (srcp < msg || srcp >= eom) {  /* Out of range. */
346 <                                errno = EMSGSIZE;
347 <                                return (-1);
348 <                        }
349 <                        checked += 2;
350 <                        /*
351 <                         * Check for loops in the compressed name;
352 <                         * if we've looked at the whole message,
353 <                         * there must be a loop.
354 <                         */
355 <                        if (checked >= eom - msg) {
356 <                                errno = EMSGSIZE;
357 <                                return (-1);
358 <                        }
359 <                        break;
302 >  len = -1;
303 >  checked = 0;
304 >  dstp = dst;
305 >  srcp = src;
306 >  dstlim = dst + dstsiz;
307  
308 <                default:
309 <                        errno = EMSGSIZE;
310 <                        return (-1);                    /* flag error */
311 <                }
312 <        }
313 <        *dstp = '\0';
314 <        if (len < 0)
315 <                len = srcp - src;
316 <        return (len);
317 < } /*2*/
308 >  if (srcp < msg || srcp >= eom)
309 >  {
310 >    errno = EMSGSIZE;
311 >    return -1;
312 >  }
313 >
314 >  /* Fetch next label in domain name. */
315 >  while ((n = *srcp++) != 0)
316 >  {
317 >    /* Check for indirection. */
318 >    switch (n & NS_CMPRSFLGS)
319 >    {
320 >      case 0:
321 >      case NS_TYPE_ELT:
322 >        /* Limit checks. */
323 >        if ((l = labellen(srcp - 1)) < 0)
324 >        {
325 >          errno = EMSGSIZE;
326 >          return -1;
327 >        }
328 >
329 >        if (dstp + l + 1 >= dstlim || srcp + l >= eom)
330 >        {
331 >          errno = EMSGSIZE;
332 >          return -1;
333 >        }
334 >
335 >        checked += l + 1;
336 >        *dstp++ = n;
337 >        memcpy(dstp, srcp, l);
338 >
339 >        dstp += l;
340 >        srcp += l;
341 >        break;
342 >
343 >      case NS_CMPRSFLGS:
344 >        if (srcp >= eom)
345 >        {
346 >          errno = EMSGSIZE;
347 >          return -1;
348 >        }
349 >
350 >        if (len < 0)
351 >          len = srcp - src + 1;
352 >        srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
353 >
354 >        if (srcp < msg || srcp >= eom)
355 >        {
356 >          /* Out of range. */
357 >          errno = EMSGSIZE;
358 >          return -1;
359 >        }
360 >
361 >        checked += 2;
362 >
363 >        /*
364 >         * Check for loops in the compressed name;
365 >         * if we've looked at the whole message,
366 >         * there must be a loop.
367 >         */
368 >        if (checked >= eom - msg)
369 >        {
370 >          errno = EMSGSIZE;
371 >          return -1;
372 >        }
373 >
374 >        break;
375 >
376 >      default:
377 >        errno = EMSGSIZE;
378 >        return -1; /* Flag error */
379 >    }
380 >  }
381 >
382 >  *dstp = '\0';
383 >
384 >  if (len < 0)
385 >    len = srcp - src;
386 >
387 >  return len;
388 > }
389  
390   /*
391   * irc_ns_name_ntop(src, dst, dstsiz)
# Line 474 | Line 492 | irc_ns_name_ntop(const unsigned char *sr
492   * Skip over a compressed domain name. Return the size or -1.
493   */
494   int
495 < irc_dn_skipname(const unsigned char *ptr, const unsigned char *eom) {
495 > irc_dn_skipname(const unsigned char *ptr, const unsigned char *eom)
496 > {
497    const unsigned char *saveptr = ptr;
498  
499    if (irc_ns_name_skip(&ptr, eom) == -1)
500 <    return(-1);
501 <  return(ptr - saveptr);
502 < } /*2*/
500 >    return -1;
501 >  return ptr - saveptr;
502 > }
503  
504   /*
505   * ns_name_skip(ptrptr, eom)
# Line 502 | Line 521 | irc_ns_name_skip(const unsigned char **p
521      /* Check for indirection. */
522      switch (n & NS_CMPRSFLGS)
523      {
524 <      case 0: /* normal case, n == len */
524 >      case 0:  /* Normal case, n == len */
525          cp += n;
526          continue;
527 <      case NS_TYPE_ELT: /* EDNS0 extended label */
527 >      case NS_TYPE_ELT:  /* EDNS0 extended label */
528          if ((l = labellen(cp - 1)) < 0)
529          {
530 <          errno = EMSGSIZE; /* XXX */
531 <          return(-1);
530 >          errno = EMSGSIZE;  /* XXX */
531 >          return -1;
532          }
533  
534          cp += l;
535          continue;
536 <      case NS_CMPRSFLGS: /* indirection */
536 >      case NS_CMPRSFLGS:  /* Indirection */
537          cp++;
538          break;
539 <      default: /* illegal type */
539 >      default:  /* Illegal type */
540          errno = EMSGSIZE;
541 <        return(-1);
541 >        return -1;
542      }
543  
544      break;
# Line 528 | Line 547 | irc_ns_name_skip(const unsigned char **p
547    if (cp > eom)
548    {
549      errno = EMSGSIZE;
550 <    return (-1);
550 >    return -1;
551    }
552  
553    *ptrptr = cp;
554 <  return(0);
555 < } /*2*/
554 >  return 0;
555 > }
556  
557   unsigned int
558   irc_ns_get16(const unsigned char *src)
# Line 541 | Line 560 | irc_ns_get16(const unsigned char *src)
560    unsigned int dst;
561  
562    IRC_NS_GET16(dst, src);
563 <  return(dst);
563 >  return dst;
564   }
565  
566   unsigned long
# Line 550 | Line 569 | irc_ns_get32(const unsigned char *src)
569    unsigned long dst;
570  
571    IRC_NS_GET32(dst, src);
572 <  return(dst);
572 >  return dst;
573   }
574  
575   void
# Line 579 | Line 598 | special(int ch)
598   {
599    switch (ch)
600    {
601 <    case 0x22: /* '"'  */
602 <    case 0x2E: /* '.'  */
603 <    case 0x3B: /* ';'  */
604 <    case 0x5C: /* '\\' */
605 <    case 0x28: /* '('  */
606 <    case 0x29: /* ')'  */
601 >    case 0x22:  /* '"'  */
602 >    case 0x2E:  /* '.'  */
603 >    case 0x3B:  /* ';'  */
604 >    case 0x5C:  /* '\\' */
605 >    case 0x28:  /* '('  */
606 >    case 0x29:  /* ')'  */
607      /* Special modifiers in zone files. */
608 <    case 0x40: /* '@'  */
609 <    case 0x24: /* '$'  */
610 <      return(1);
608 >    case 0x40:  /* '@'  */
609 >    case 0x24:  /* '$'  */
610 >      return 1;
611      default:
612 <      return(0);
612 >      return 0;
613    }
614 < } /*2*/
614 > }
615  
616   static int
617   labellen(const unsigned char *lp)
# Line 602 | Line 621 | labellen(const unsigned char *lp)
621  
622    if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS)
623    {
624 <    /* should be avoided by the caller */
625 <    return(-1);
624 >    /* Should be avoided by the caller */
625 >    return -1;
626    }
627  
628    if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT)
# Line 612 | Line 631 | labellen(const unsigned char *lp)
631      {
632        if ((bitlen = *(lp + 1)) == 0)
633          bitlen = 256;
634 <      return((bitlen + 7 ) / 8 + 1);
634 >      return (bitlen + 7 ) / 8 + 1;
635      }
636  
637 <    return(-1); /* unknwon ELT */
637 >    return -1;  /* Unknwon ELT */
638    }
639  
640 <  return(l);
641 < } /*2*/
640 >  return l;
641 > }
642  
643  
644   /*
# Line 632 | Line 651 | labellen(const unsigned char *lp)
651   static int
652   printable(int ch)
653   {
654 <  return(ch > 0x20 && ch < 0x7f);
655 < } /*2*/
654 >  return ch > 0x20 && ch < 0x7f;
655 > }
656  
657   static int
658   irc_decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
# Line 837 | Line 856 | irc_ns_name_pack(const unsigned char *sr
856    dstp = dst;
857    eob = dstp + dstsiz;
858    lpp = cpp = NULL;
859 <  if (dnptrs != NULL) {
860 <    if ((msg = *dnptrs++) != NULL) {
859 >
860 >  if (dnptrs != NULL)
861 >  {
862 >    if ((msg = *dnptrs++))
863 >    {
864        for (cpp = dnptrs; *cpp != NULL; cpp++)
865 <        (void)NULL;
866 <      lpp = cpp;  /* end of list to search */
865 >        ;
866 >      lpp = cpp;  /* End of list to search */
867      }
868 <  } else
868 >  }
869 >  else
870      msg = NULL;
871  
872 <  /* make sure the domain we are about to add is legal */
872 >  /* Make sure the domain we are about to add is legal */
873    l = 0;
874 <  do {
874 >  do
875 >  {
876      int l0;
877  
878      n = *srcp;
879 <    if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
879 >    if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS)
880 >    {
881        errno = EMSGSIZE;
882 <      return (-1);
882 >      return -1;
883      }
884 <    if ((l0 = labellen(srcp)) < 0) {
884 >
885 >    if ((l0 = labellen(srcp)) < 0)
886 >    {
887        errno = EINVAL;
888 <      return(-1);
888 >      return -1;
889      }
890 +
891      l += l0 + 1;
892 <    if (l > NS_MAXCDNAME) {
892 >    if (l > NS_MAXCDNAME)
893 >    {
894        errno = EMSGSIZE;
895 <      return (-1);
895 >      return -1;
896      }
897 +
898      srcp += l0 + 1;
899    } while (n != 0);
900  
901 <  /* from here on we need to reset compression pointer array on error */
901 >  /* From here on we need to reset compression pointer array on error */
902    srcp = src;
903 <  do {
903 >  do
904 >  {
905      /* Look to see if we can use pointers. */
906      n = *srcp;
907 <    if (n != 0 && msg != NULL) {
908 <      l = irc_dn_find(srcp, msg, (const unsigned char * const *)dnptrs,
909 <            (const unsigned char * const *)lpp);
910 <      if (l >= 0) {
911 <        if (dstp + 1 >= eob) {
907 >
908 >    if (n != 0 && msg != NULL)
909 >    {
910 >      l = irc_dn_find(srcp, msg, (const unsigned char *const *)dnptrs,
911 >                      (const unsigned char *const *)lpp);
912 >
913 >      if (l >= 0)
914 >      {
915 >        if (dstp + 1 >= eob)
916            goto cleanup;
917 <        }
917 >
918          *dstp++ = (l >> 8) | NS_CMPRSFLGS;
919          *dstp++ = l % 256;
920 <        return (dstp - dst);
920 >        return dstp - dst;
921        }
922 +
923        /* Not found, save it. */
924 <      if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
925 <          (dstp - msg) < 0x4000 && first) {
924 >      if (lastdnptr != NULL && cpp < lastdnptr - 1 && (dstp - msg) < 0x4000 && first)
925 >      {
926          *cpp++ = dstp;
927          *cpp = NULL;
928          first = 0;
929        }
930      }
931 <    /* copy label to buffer */
932 <    if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
933 <      /* Should not happen. */
934 <      goto cleanup;
935 <    }
931 >
932 >    /* Copy label to buffer */
933 >    if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS)
934 >      goto cleanup;  /* Should not happen. */
935 >
936      n = labellen(srcp);
937 <    if (dstp + 1 + n >= eob) {
937 >    if (dstp + 1 + n >= eob)
938        goto cleanup;
939 <    }
939 >
940      memcpy(dstp, srcp, n + 1);
941      srcp += n + 1;
942      dstp += n + 1;
943    } while (n != 0);
944  
945 <  if (dstp > eob) {
945 >  if (dstp > eob)
946 >  {
947   cleanup:
948      if (msg != NULL)
949        *lpp = NULL;
950      errno = EMSGSIZE;
951 <    return (-1);
951 >    return -1;
952    }
953 <  return(dstp - dst);
954 < } /*2*/
953 >
954 >  return dstp - dst;
955 > }
956  
957   static int
958   irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz,
# Line 923 | Line 961 | irc_ns_name_compress(const char *src, un
961    unsigned char tmp[NS_MAXCDNAME];
962  
963    if (irc_ns_name_pton(src, tmp, sizeof tmp) == -1)
964 <    return(-1);
965 <  return(irc_ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
964 >    return -1;
965 >  return irc_ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr);
966   }
967  
968   static int

Diff Legend

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