4

Possible Duplicate:
Is there an alternative to inet_ntop / InetNtop in Windows XP?

I am newbie to socket programming. I wrote my first code and its giving a linker error. I googled for the issue but could not find a solution. So, i am trying stack exchange. (for the first time :-) )

Here is the code and i am using windows XP

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif


#include<stdio.h>
#include<string.h>
#include<winsock2.h>
#include<Ws2tcpip.h>

#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46
#endif

int main(int argc, char *argv[])
{
    WSADATA wsaData;

if(WSAStartup(MAKEWORD(1,1), &wsaData) != 0){
    fprintf(stderr, "WSAStartup failed.\n");
    return 1;
}

int status;
struct addrinfo hints;
struct addrinfo *res, *p;
char ipstr[INET6_ADDRSTRLEN];

if(argc != 2){
    fprintf(stderr,"usage: Shows IP address of hostname.\n");
    return 1;
}

memset(&hints, 0 , sizeof hints);

hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

if((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0){
    fprintf(stderr, "getaddrinfo : %s \n", gai_strerror(status));
    return 2;
}

printf("IP addresses for %s:\n\n", argv[1]);

for(p = res; p != NULL; p = p->ai_next){
    void *addr;
    char *ipver;

    if(p->ai_family == AF_INET){ //IPv4
        struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
        addr = &(ipv4->sin_addr);
        ipver = "IPv4";
    }
    else{ //IPv6
        struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
        addr = &(ipv6->sin6_addr);
        ipver = "IPv6";
    }

    inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
    printf("%s : %s\n",ipver, ipstr);
}

freeaddrinfo(res);

WSACleanup();

return 0;
}

I am compiling it using the following command:

gcc file.c "C:\Program Files\CodeBlocks\MinGW\lib\ws2_32.a" -o final.exe

It is giving the following error:

(.text+0x21d): undefined reference to 'inet_ntop'
collect2: ld returned 1 exit status

And yes, i am using beej guide to learn socket programming. If you want to suggest me some other nice tutorial or book then please go ahead. Thanks.

Community
  • 1
  • 1
user1746485
  • 57
  • 1
  • 2
  • 1
    MSDN documents this as being available from Windows Vista on: http://msdn.microsoft.com/en-us/library/windows/desktop/cc805843%28v=vs.85%29.aspx – alk Oct 15 '12 at 12:52
  • This is a duplicate of [Is there an alternative to inet_ntop / InetNtop in Windows XP?](http://stackoverflow.com/q/1561469/772981). Probably despite googling you should use internal StackOverflow searching. A query "winsock windows inet_ntop" gave me the correct link quickly. – Jarekczek Oct 16 '12 at 07:56
  • -1: I don't know how you googled, because both the stackoverflow topic and others say clearly that this does not work on Windows. [Another article from Google](http://memset.wordpress.com/2010/10/09/inet_ntop-for-win32/). I searched for "inet_ntop windows". – Jarekczek Oct 16 '12 at 08:06
  • @Jarekczek: It's not a complete duplicate, the other guy was asking just how to do it on Windows, he probably meant Visual Studio. But this question is specificaly about MinGW, which does not contain this function in any of its headers, so you can't use it no matter the system version. – Youda008 Dec 04 '16 at 11:37

1 Answers1

0

If getaddrinfo() works, getnameinfo() will probably work too. You can use that to convert your struct sockaddr back to a numerical representation.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • In other words: seems like `inet_ntop` is not supported on Windows. I can't link it either. None of my mingw32 libraries contain it. I see there is [InetNtop](http://msdn.microsoft.com/en-us/library/windows/desktop/cc805843%28v=vs.85%29.aspx) but from Vista onwards. – Jarekczek Oct 15 '12 at 17:23
  • @Jarekczek Right, but `getnameinfo()` is a feasible alternative if called with the right flags (the "numeric" ones). – glglgl Oct 16 '12 at 07:20