inet_neta.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifdef SPRINTF_CHAR
  25. # define SPRINTF(x) strlen(sprintf/**/x)
  26. #else
  27. # define SPRINTF(x) ((size_t)sprintf x)
  28. #endif
  29. /*
  30. * char *
  31. * inet_neta(src, dst, size)
  32. * format a u_long network number into presentation format.
  33. * return:
  34. * pointer to dst, or NULL if an error occurred (check errno).
  35. * note:
  36. * format of ``src'' is as for inet_network().
  37. * author:
  38. * Paul Vixie (ISC), July 1996
  39. */
  40. char *
  41. inet_neta (uint32_t src, char *dst, size_t size)
  42. {
  43. char *odst = dst;
  44. char *tp;
  45. while (src & 0xffffffff) {
  46. u_char b = (src & 0xff000000) >> 24;
  47. src <<= 8;
  48. if (b) {
  49. if (size < sizeof "255.")
  50. goto emsgsize;
  51. tp = dst;
  52. dst += SPRINTF((dst, "%u", b));
  53. if (src != 0L) {
  54. *dst++ = '.';
  55. *dst = '\0';
  56. }
  57. size -= (size_t)(dst - tp);
  58. }
  59. }
  60. if (dst == odst) {
  61. if (size < sizeof "0.0.0.0")
  62. goto emsgsize;
  63. strcpy(dst, "0.0.0.0");
  64. }
  65. return (odst);
  66. emsgsize:
  67. __set_errno (EMSGSIZE);
  68. return (NULL);
  69. }