inet_ntop()

PROTOTYPE

#include <sockets.h>

const char *inet_ntop(int family, const void *addrptr, char strptr, size_t len);

DESCRIPTION

inet_ntop() converts an IPv4 or IPv6 address from an integer form in network byte order to a string form (Internet dotted notation for IPv4 or colon delimited for IPv6). family is AF_INET or AF_INET6. addrptr points to a buffer where the input is stored. strptr points to a buffer where the output is stored. len is the size of this buffer.

If successful, inet_ntop() returns strptr. Otherwise, it sets errno and returns NULL.

ERROR CODES

EAFNOSUPPORT family is not supported.
ENOSPC Not enough space in the destination string.

EXAMPLE

  struct sockaddr_in6 from;
  char echo_buf[UDP_BUF_SIZE], address[INET6_ADDRSTRLEN];
  int sock;
  ssize_t length;
  socklen_t addrlen;

  ...

  /*-----------------------------------------------------------------*/
  /* Wait for a received datagram.                                   */
  /*-----------------------------------------------------------------*/
  addrlen = sizeof(struct sockaddr_in6);
  length = recvfrom(sock, echo_buf, UDP_BUF_SIZE, 0, &from, &addrlen);
  if (length < 0)
    perror("recvfrom() failed");

  /*-------------------------------------------------------------------*/
  /* Convert the remote address into presentation form.                */
  /*-------------------------------------------------------------------*/
  if (inet_ntop(AF_INET6, &from->sin6_addr, address, INET6_ADDRSTRLEN))
    printf("UDP Echo: %d bytes from %s/%d.\n",
           address, from->sin6_scope_id);