Is there an alternative to inet_ntop / InetNtop in Windows XP?
Asked Answered
T

6

8

I'm trying to compile beej's guide to network programming examples, but Windows XP doesn't have such a function. I'm using mingw, if it makes any difference.

Todo answered 13/10, 2009 at 16:44 Comment(2)
inet_ntop is also missing in Winsock implementation on Windows Phone 8.1. How MS could hope for a success of such a crippled platform?Zamora
Yep, and also missing for Windows CE 7.Yolondayon
N
7

If you're only dealing with IPv4 addresses, you can use inet_ntoa. It's available on Windows 2000 or later. Otherwise you'll have to either require Vista and later, or write your own inet_ntop function.

You could also look at boost - the boost::asio has an inet_ntop implementation that works in Windows: boost::asio::detail::socket_ops::inet_ntop. You can see the source code here.

Notate answered 13/10, 2009 at 18:43 Comment(2)
inet_ntoa is available on every version of windows with WinSock2 on it, you can't trust MSDN when it comes to minimum version, they seem to have forgotten about Win9x and NT4Taction
MinGW headers don't contain a declaration of inet_ntop. So even on Windows Vista and later you can't use it :(Goldplate
T
10

From the WinSock layer:

Tessitura answered 14/10, 2009 at 8:3 Comment(1)
Note that WSAAddressToString will append the port number as well if your address structure has one. So to just get the IP, you may have to create a new address with no port.Hyperbola
N
7

If you're only dealing with IPv4 addresses, you can use inet_ntoa. It's available on Windows 2000 or later. Otherwise you'll have to either require Vista and later, or write your own inet_ntop function.

You could also look at boost - the boost::asio has an inet_ntop implementation that works in Windows: boost::asio::detail::socket_ops::inet_ntop. You can see the source code here.

Notate answered 13/10, 2009 at 18:43 Comment(2)
inet_ntoa is available on every version of windows with WinSock2 on it, you can't trust MSDN when it comes to minimum version, they seem to have forgotten about Win9x and NT4Taction
MinGW headers don't contain a declaration of inet_ntop. So even on Windows Vista and later you can't use it :(Goldplate
M
3

There is also inet_ntop function in POSIX compliant libc for Windows (PlibC) library that was created for porting POSIX applications to Windows. There is no notes about it in online documentation, but it exists in file inet_ntop.c at least since 2008 (according to file date).

const char * inet_ntop(int af, const void *src, char *dst, size_t size)
Mylo answered 7/6, 2013 at 9:46 Comment(0)
Y
2

You might want to use something Jeroen Massar provided here, excerpt from his post follows:

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
    if (af == AF_INET)
    {
        struct sockaddr_in in;
        memset(&in, 0, sizeof(in));
        in.sin_family = AF_INET;
        memcpy(&in.sin_addr, src, sizeof(struct in_addr));
        getnameinfo((struct sockaddr *)&in, sizeof(struct
            sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
        return dst;
    }
    else if (af == AF_INET6)
    {
        struct sockaddr_in6 in;
        memset(&in, 0, sizeof(in));
        in.sin6_family = AF_INET6;
        memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
        getnameinfo((struct sockaddr *)&in, sizeof(struct
            sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
        return dst;
    }
    return NULL;
}

Yolondayon answered 9/4, 2021 at 18:46 Comment(1)
It works, you are really a lifesaver, thanks. after trying ways to fix the compiling error of inet_ntop in vs2008, this one is the perfect answer.Tenerife
M
1

you can use winsocket with mingw-64 on windows 7

#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>

linkwith

gcc showip.c -lws2_32

Target: x86_64-w64-mingw32 Thread model: win32 gcc version 6.3.0 (GCC)

Mailer answered 5/4, 2017 at 16:34 Comment(0)
G
0

There is no header file for the function getnameinfo in Xp Sp3 because it uses the lowest operating system of Vista. I will replace it with the following method, which is only applicable to ipv4:

QString ipv4ToString(const in_addr& addr) {
    char ipstr[INET_ADDRSTRLEN];
    unsigned char* bytes = (unsigned char*)&addr;
    _snprintf(ipstr, INET_ADDRSTRLEN, "%u.%u.%u.%u", bytes[0], bytes[1], bytes[2], bytes[3]);
    return QString::fromLocal8Bit(ipstr);
}
Gabelle answered 23/4 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.