'inet_pton': identifier not found
Asked Answered
A

8

32

I'm trying to include the following code in my program but the error ('inet_pton': identifier not found) will appeared.

// IPv4:

struct sockaddr_in ip4addr;
int s;

ip4addr.sin_family = AF_INET;
ip4addr.sin_port = htons(3490);
inet_pton(AF_INET, "10.0.0.1", &ip4addr.sin_addr);

s = socket(PF_INET, SOCK_STREAM, 0);
bind(s, (struct sockaddr*)&ip4addr, sizeof ip4addr);

Output

 error C3861: 'inet_pton': identifier not found

the including header

 #include <stdio.h>
 #include <stdlib.h>
 #include "udpDefine.h"
 #include <windows.h>

any helping may be missed some headers or lib.

Ashmead answered 27/3, 2013 at 13:36 Comment(7)
Quick google search turns up #include <arpa/inet.h>Theadora
#include <arpa/inet.h>Mesics
@H2CO3 fatal error C1083: Cannot open include file: 'arpa/inet.h': No such file or directory. are i need to install additional headers or what?Ashmead
have you path to the include files defined?Fusible
@abdo.eng2006210 That header file is part of the standard library (at least according to POSIX). If you don't have it, then Windows simply doesn't provide this function, which is not a surprise - Windows isn't POSIX-compliant.Mesics
@H2CO3 my operating system is windows 7. is the <arpa/inet.h> not provided under windows or it's missed from my system.Ashmead
@abdo.eng2006210 I don't know, I don't use/program for Windows, all I can say I really wouldn't be surprised if it was nonexistent. I don't know why you would consider your system special.Mesics
F
42

the function

int inet_pton(int af, const char *src, void *dst);

is declared in header file:

#include <arpa/inet.h>

if this is Windows (Vista or later) there is Winsock analog to this ANSI version:

INT WSAAPI InetPton(
  _In_   INT  Family,
  _In_   PCTSTR pszAddrString,
  _Out_  PVOID pAddrBuf
);

try #include <Ws2tcpip.h> add Ws2_32.lib

Fusible answered 27/3, 2013 at 13:40 Comment(4)
i included the <winsock.h> and changed the statement to InetPton(AF_INET, "192.168.10.9", &address.sin_addr); and the error was ( error C3861: 'InetPton': identifier not found )Ashmead
@abdo.eng2006210 add lib Ws2_32.libFusible
@abdo.eng2006210 include Ws2tcpip.h instead of winsockFusible
Note that the definitions in Ws2tcpip.h are inside an "#if (NTDDI_VERSION >= NTDDI_VISTA) / ... / #endif" block. You might need to set NTDDI_VERSION and _WIN32_WINNT accordingly depending on your build environment.Colum
I
17

In windows XP (and later) you can use these functions:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>    

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


int inet_pton(int af, const char *src, void *dst)
{
  struct sockaddr_storage ss;
  int size = sizeof(ss);
  char src_copy[INET6_ADDRSTRLEN+1];

  ZeroMemory(&ss, sizeof(ss));
  /* stupid non-const API */
  strncpy (src_copy, src, INET6_ADDRSTRLEN+1);
  src_copy[INET6_ADDRSTRLEN] = 0;

  if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {
    switch(af) {
      case AF_INET:
    *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
    return 1;
      case AF_INET6:
    *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
    return 1;
    }
  }
  return 0;
}

const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
{
  struct sockaddr_storage ss;
  unsigned long s = size;

  ZeroMemory(&ss, sizeof(ss));
  ss.ss_family = af;

  switch(af) {
    case AF_INET:
      ((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src;
      break;
    case AF_INET6:
      ((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src;
      break;
    default:
      return NULL;
  }
  /* cannot direclty use &size because of strict aliasing rules */
  return (WSAAddressToString((struct sockaddr *)&ss, sizeof(ss), NULL, dst, &s) == 0)?
          dst : NULL;
}

Link with ws2_32 library.

Ilona answered 28/12, 2013 at 16:52 Comment(1)
The +1 in INET6_ADDRSTRLEN+1 (or INET_ADDRSTRLEN+1) is not needed. The comments in the Microsoft headers indicate the define includes the terminating NULL.Complemental
T
4

Windows users can also find the answer here:

https://learn.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-inetptonw

The InetPton function converts an IPv4 or IPv6 Internet network address in its standard text presentation form into its numeric binary form. The ANSI version of this function is inet_pton.

The InetPton function is supported on Windows Vista and later.

When UNICODE or _UNICODE is not defined, InetPton is defined to InetPtonA, the ANSI version of this function. The ANSI version of this function is always defined as inet_pton.

Header      ws2tcpip.h
Library     Ws2_32.lib
DLL         Ws2_32.dll

You need to use the ws2tcpip.h header and add Ws2_32.lib to your linker.

Thusly answered 19/7, 2018 at 7:55 Comment(2)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable.Answers that are little more than a link may be deleted.Allotropy
@Allotropy Edited.Caseous
B
2

In my case the function couldn't be found, because there was a "#define WINVER 0x0502" somewere in a header file.

On Windows systems the version 0x600 (= Vista) is as least required for this function.

Burkhardt answered 25/10, 2016 at 14:33 Comment(0)
S
1

Function inet_pton() is in header file <ws2tcpip.h> in windows (on Windows Vista and later) and in <arpa/inet.h> in linux. So below code snippet may help:

#ifdef _WIN32
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#endif
Shreveport answered 11/9, 2023 at 9:49 Comment(0)
H
0

In WS2tcpip.h, this api inet_pton is defined:

#if (NTDDI_VERSION >= NTDDI_VISTA)
WINSOCK_API_LINKAGE
INT
WSAAPI
inet_pton(
    _In_                                      INT             Family,
    _In_                                      PCSTR           pszAddrString,
    _When_(Family == AF_INET, _Out_writes_bytes_(sizeof(IN_ADDR)))
    _When_(Family == AF_INET6, _Out_writes_bytes_(sizeof(IN6_ADDR)))
                                              PVOID           pAddrBuf
    );
Heartthrob answered 30/7, 2019 at 3:34 Comment(0)
N
-1

Decalare this function Explicitly in your code.

int inet_pton(int af, const char *src, void *dst)
{
  struct sockaddr_storage ss;
  int size = sizeof(ss);
  char src_copy[INET6_ADDRSTRLEN+1];

  ZeroMemory(&ss, sizeof(ss));
  /* stupid non-const API */
  strncpy (src_copy, src, INET6_ADDRSTRLEN+1);
  src_copy[INET6_ADDRSTRLEN] = 0;

  if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {
    switch(af) {
      case AF_INET:
    *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
    return 1;
      case AF_INET6:
    *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
    return 1;
    }
  }
  return 0;
} 
Nga answered 20/5, 2020 at 7:55 Comment(0)
O
-6

To fix this problem just add the following to your code after all #includes

#pragma comment(lib, "ws2_32.lib")
Objective answered 21/3, 2015 at 8:25 Comment(1)
Not always works, there might be many reasons for this problem, not only onePostmistress

© 2022 - 2024 — McMap. All rights reserved.