dn_skipname.c 950 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <errno.h>
  2. #include <resolv.h>
  3. /* Ripped from glibc 2.4 sources. */
  4. /*
  5. * ns_name_skip(ptrptr, eom)
  6. * Advance *ptrptr to skip over the compressed name it points at.
  7. * return:
  8. * 0 on success, -1 (with errno set) on failure.
  9. */
  10. int ns_name_skip(const u_char **ptrptr, const u_char *eom)
  11. {
  12. const u_char *cp;
  13. u_int n;
  14. cp = *ptrptr;
  15. while (cp < eom && (n = *cp++) != 0)
  16. {
  17. /* Check for indirection. */
  18. switch (n & NS_CMPRSFLGS) {
  19. case 0: /* normal case, n == len */
  20. cp += n;
  21. continue;
  22. case NS_CMPRSFLGS: /* indirection */
  23. cp++;
  24. break;
  25. default: /* illegal type */
  26. errno = EMSGSIZE;
  27. return (-1);
  28. }
  29. break;
  30. }
  31. if (cp > eom)
  32. {
  33. errno = EMSGSIZE;
  34. return (-1);
  35. }
  36. *ptrptr = cp;
  37. return (0);
  38. }
  39. int dn_skipname(const u_char *ptr, const u_char *eom)
  40. {
  41. const u_char *saveptr = ptr;
  42. if(ns_name_skip(&ptr, eom) == -1)
  43. return (-1);
  44. return (ptr - saveptr);
  45. }