Skip to content

inet_ntop()

Purpose

inet_ntop() converts a binary network address to a printable string.

It is useful for logging peer IP addresses after accept().

#include <arpa/inet.h>

Prototype

const char *inet_ntop(int af, const void *src,
                      char *dst, socklen_t size);

Minimal Example

char ip[INET_ADDRSTRLEN];

if (!inet_ntop(AF_INET, &peer.sin_addr, ip, sizeof(ip))) {
    perror("inet_ntop");
    return -1;
}

Common Pitfalls

  • Using a buffer that is too small.
  • Passing the wrong address family.
  • Passing the address of the wrong field inside sockaddr_in.