inet_net_ntop.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/socket.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #ifdef SPRINTF_CHAR
  26. # define SPRINTF(x) strlen(sprintf/**/x)
  27. #else
  28. # define SPRINTF(x) ((size_t)sprintf x)
  29. #endif
  30. static char * inet_net_ntop_ipv4 (const u_char *src, int bits,
  31. char *dst, size_t size) __THROW;
  32. /*
  33. * char *
  34. * inet_net_ntop(af, src, bits, dst, size)
  35. * convert network number from network to presentation format.
  36. * generates CIDR style result always.
  37. * return:
  38. * pointer to dst, or NULL if an error occurred (check errno).
  39. * author:
  40. * Paul Vixie (ISC), July 1996
  41. */
  42. char *
  43. inet_net_ntop (int af, const void *src, int bits, char *dst, size_t size)
  44. {
  45. switch (af) {
  46. case AF_INET:
  47. return (inet_net_ntop_ipv4(src, bits, dst, size));
  48. default:
  49. __set_errno (EAFNOSUPPORT);
  50. return (NULL);
  51. }
  52. }
  53. /*
  54. * static char *
  55. * inet_net_ntop_ipv4(src, bits, dst, size)
  56. * convert IPv4 network number from network to presentation format.
  57. * generates CIDR style result always.
  58. * return:
  59. * pointer to dst, or NULL if an error occurred (check errno).
  60. * note:
  61. * network byte order assumed. this means 192.5.5.240/28 has
  62. * 0b11110000 in its fourth octet.
  63. * author:
  64. * Paul Vixie (ISC), July 1996
  65. */
  66. static char *
  67. inet_net_ntop_ipv4 (const u_char *src, int bits, char *dst, size_t size)
  68. {
  69. char *odst = dst;
  70. char *t;
  71. u_int m;
  72. int b;
  73. if (bits < 0 || bits > 32) {
  74. __set_errno (EINVAL);
  75. return (NULL);
  76. }
  77. if (bits == 0) {
  78. if (size < sizeof "0")
  79. goto emsgsize;
  80. *dst++ = '0';
  81. size--;
  82. *dst = '\0';
  83. }
  84. /* Format whole octets. */
  85. for (b = bits / 8; b > 0; b--) {
  86. if (size < sizeof "255.")
  87. goto emsgsize;
  88. t = dst;
  89. dst += SPRINTF((dst, "%u", *src++));
  90. if (b > 1) {
  91. *dst++ = '.';
  92. *dst = '\0';
  93. }
  94. size -= (size_t)(dst - t);
  95. }
  96. /* Format partial octet. */
  97. b = bits % 8;
  98. if (b > 0) {
  99. if (size < sizeof ".255")
  100. goto emsgsize;
  101. t = dst;
  102. if (dst != odst)
  103. *dst++ = '.';
  104. m = ((1 << b) - 1) << (8 - b);
  105. dst += SPRINTF((dst, "%u", *src & m));
  106. size -= (size_t)(dst - t);
  107. }
  108. /* Format CIDR /width. */
  109. if (size < sizeof "/32")
  110. goto emsgsize;
  111. dst += SPRINTF((dst, "/%u", bits));
  112. return (odst);
  113. emsgsize:
  114. __set_errno (EMSGSIZE);
  115. return (NULL);
  116. }