nsap_addr.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 1996-1999 by Internet Software Consortium.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  9. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  10. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  11. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  13. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  14. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  15. * SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/param.h>
  19. #include <sys/socket.h>
  20. #include <netinet/in.h>
  21. #include <arpa/inet.h>
  22. #include <arpa/nameser.h>
  23. #include <ctype.h>
  24. #include <resolv.h>
  25. static char
  26. xtob(int c) {
  27. return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
  28. }
  29. u_int
  30. inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
  31. u_char c, nib;
  32. u_int len = 0;
  33. while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
  34. if (c == '.' || c == '+' || c == '/')
  35. continue;
  36. if (!isascii(c))
  37. return (0);
  38. c = toupper(c);
  39. if (isxdigit(c)) {
  40. nib = xtob(c);
  41. c = *ascii++;
  42. if (c != '\0') {
  43. c = toupper(c);
  44. if (isxdigit(c)) {
  45. *binary++ = (nib << 4) | xtob(c);
  46. len++;
  47. } else
  48. return (0);
  49. }
  50. else
  51. return (0);
  52. }
  53. else
  54. return (0);
  55. }
  56. return (len);
  57. }
  58. char *
  59. inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
  60. int nib;
  61. int i;
  62. static char tmpbuf[255*2 + 128];
  63. char *start;
  64. if (ascii)
  65. start = ascii;
  66. else {
  67. ascii = tmpbuf;
  68. start = tmpbuf;
  69. }
  70. if (binlen > 255)
  71. binlen = 255;
  72. for (i = 0; i < binlen; i++) {
  73. nib = *binary >> 4;
  74. *ascii++ = nib + (nib < 10 ? '0' : '7');
  75. nib = *binary++ & 0x0f;
  76. *ascii++ = nib + (nib < 10 ? '0' : '7');
  77. if (((i % 2) == 0 && (i + 1) < binlen))
  78. *ascii++ = '.';
  79. }
  80. *ascii = '\0';
  81. return (start);
  82. }