inet_ntop_ipv4.c 610 B

1234567891011121314151617181920212223242526272829303132
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #ifndef INET_ADDRSTRLEN
  6. #define INET_ADDRSTRLEN 16
  7. #endif
  8. /* include inet_ntop */
  9. const char *
  10. inet_ntop(int family, const void *addrptr, char *strptr, size_t len)
  11. {
  12. const u_char *p = (const u_char *) addrptr;
  13. if (family == AF_INET) {
  14. char temp[INET_ADDRSTRLEN];
  15. snprintf(temp, sizeof(temp), "%d.%d.%d.%d",
  16. p[0], p[1], p[2], p[3]);
  17. if (strlen(temp) >= len) {
  18. errno = ENOSPC;
  19. return (NULL);
  20. }
  21. strcpy(strptr, temp);
  22. return (strptr);
  23. }
  24. errno = EAFNOSUPPORT;
  25. return (NULL);
  26. }
  27. /* end inet_ntop */