inet_ntop

inet_ntop function

The inet_ntop function converts an Internet Protocol (IP) address structure to a string. This function is more robust and extensible than the IPv6-compatible AddressTo-String conversion functions.

Syntax

PCSTR inet_ntop(
    INT Family,
    const void *pAddr,
    PSTR pStringBuf,
    socklen_t StringBufSize
);

Parameters

Parameter Description
Family The address family. Possible values are specified in the Winsock2.h header.
pAddr A pointer to a buffer containing the IP address structure.
pStringBuf A pointer to a buffer to store the string representation of the IP address.
StringBufSize The size, in bytes, of the buffer pointed to by pStringBuf.

Return value

If the function succeeds, it returns a pointer to the buffer pointed to by pStringBuf.

If the function fails, it returns NULL. To get extended error information, call WSAGetLastError.

Remarks

The inet_ntop function converts an IP address structure to a presentation string. This function supports both IPv4 and IPv6 addresses. The Family parameter specifies the address family.

For IPv4 addresses, the pAddr parameter points to an in_addr structure. For IPv6 addresses, it points to an in6_addr structure.

The pStringBuf parameter must point to a buffer large enough to hold the resulting string. For IPv4, a buffer of 16 bytes is sufficient. For IPv6, a buffer of 40 bytes is recommended.

Note

Prior to Windows Vista, the inet_ntop function was not available. For backward compatibility on earlier versions of Windows, you may need to use the inet_ntoa function for IPv4 addresses and other methods for IPv6.

Example

// Example of using inet_ntop for IPv4
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

int main() {
    WSADATA wsaData;
    int iResult;

    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    struct in_addr ipv4Addr;
    char ipStringBuf[INET_ADDRSTRLEN];
    PCSTR pszAddress;

    // Convert IPv4 address "192.168.1.1" to string
    inet_pton(AF_INET, "192.168.1.1", &ipv4Addr);
    pszAddress = inet_ntop(AF_INET, &ipv4Addr, ipStringBuf, INET_ADDRSTRLEN);

    if (pszAddress != NULL) {
        printf("IPv4 Address: %s\n", pszAddress);
    } else {
        printf("inet_ntop failed with error: %d\n", WSAGetLastError());
    }

    WSACleanup();
    return 0;
}
// Example of using inet_ntop for IPv6
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

int main() {
    WSADATA wsaData;
    int iResult;

    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    struct in6_addr ipv6Addr;
    char ipStringBuf[INET6_ADDRSTRLEN];
    PCSTR pszAddress;

    // Convert IPv6 address "2001:db8::1" to string
    inet_pton(AF_INET6, "2001:db8::1", &ipv6Addr);
    pszAddress = inet_ntop(AF_INET6, &ipv6Addr, ipStringBuf, INET6_ADDRSTRLEN);

    if (pszAddress != NULL) {
        printf("IPv6 Address: %s\n", pszAddress);
    } else {
        printf("inet_ntop failed with error: %d\n", WSAGetLastError());
    }

    WSACleanup();
    return 0;
}