如何使用getnameinfo而不是gethostbyname?

时间:2021-11-12 12:27:12

in code :

在代码中:

if ((host = (struct hostent*) gethostbyname(address) ) == 0) // address is a string

I've got warning when cross compiling (generic arm architecture) on 4.5.x gcc :

在4.5.x gcc上进行交叉编译(通用arm架构)时,我收到了警告:

(.text+0x1558): warning: gethostbyname is obsolescent, use getnameinfo() instead.

getnameinfo is:

getnameinfo是:

int WSAAPI getnameinfo(
  __in   const struct sockaddr FAR *sa,
  __in   socklen_t salen,
  __out  char FAR *host,
  __in   DWORD hostlen,
  __out  char FAR *serv,
  __in   DWORD servlen,
  __in   int flags
);

And it got more parameters... And I'm confused with it, I just need it work as gethostbyname were working. What parameter to pass to keep it simple stupid as it was with gethostbyname?

它有更多的参数......而且我对它感到困惑,我只需要它就像gethostbyname正在工作一样。传递什么参数以保持简单愚蠢与gethostbyname一样?

Finally here is my try:

最后这是我的尝试:

struct sockaddr_in servAddr;
struct hostent *host;        /* Structure containing host information */

/* open socket */
if ((handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    return LILI_ERROR;

memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family      = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(address.ptr());
servAddr.sin_port        = htons(port);

char servInfo[NI_MAXSERV];
if ( ( host = (hostent*) getnameinfo(
                 (struct sockaddr *) &servAddr
                 ,sizeof (struct sockaddr)
                 ,address.ptr(), address.size()
                 ,servInfo, NI_MAXSERV
                 ,NI_NUMERICHOST | NI_NUMERICSERV )  ) == 0)
    return LILI_ERROR;

if (::connect(handle, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
    return LILI_ERROR;

It compiles well and no segmentation fault on start up but I can't connect my server with it :(

它编译得很好,启动时没有分段错误,但我无法连接我的服务器:(

2 个解决方案

#1


12  

gethostbyname() does a name→IP lookup. It should be replaced with getaddrinfo(), which can do the same.

gethostbyname()执行名称→IP查找。它应该替换为getaddrinfo(),它可以做同样的事情。

This means the warning is completely wrong. getnameinfo() is the replacement of gethostbyaddr(), both for IP→name lookups. The reverse.

这意味着警告完全错误。 getnameinfo()取代了gethostbyaddr(),用于IP→名称查找。相反。

name→IP: gethostbyname(), getaddrinfo()
IP→name: gethostbyaddr(), getnameinfo()

name→IP:gethostbyname(),getaddrinfo()IP→name:gethostbyaddr(),getnameinfo()

The newer functions can do more: they handle IPv6 and can translate strings like 'http' to 80 (port). In the future they can also determine if e.g. TCP should be used for the service in question or SCTP. The interface is ready.

较新的功能可以做得更多:它们处理IPv6并可以将'http'等字符串转换为80(端口)。在将来,他们还可以确定是否TCP应该用于有问题的服务或SCTP。界面准备就绪。

#2


11  

Beej's explains it pretty good. gethostbyname() does not works well with IPV6 and thus you should use getnameinfo() instead. All you have to do is to fill in the required informations, i.e.

Beej的解释非常好。 gethostbyname()与IPV6不兼容,因此您应该使用getnameinfo()。您所要做的就是填写所需的信息,即

getnameinfo(
    &sa,             // Pointer to your struct sockaddr
    sizeof sa,       // Size of this struct
    host,            // Pointer to hostname string
    sizeof host,     // Size of this string
    service,         // Pointer to service name string
    sizeof service,  // Size of this string
    0                // No flags given
);

Edit: After some research, I've found that

编辑:经过一番研究,我发现了

getnameinfo(&sa, sizeof(sa), hostname, size_hostname, NULL, NULL, 0);

should be sufficient.

应该足够了。

Edit #2 I've noticed you are trying to use the return value of getnameinfo as hostname. But that is not correct, the hostname is saved within the provided host pointer. The return value indicates whether the operation was sufficient. Also have a look at the man page.

编辑#2我注意到你正在尝试使用getnameinfo的返回值作为主机名。但这不正确,主机名保存在提供的主机指针中。返回值表示操作是否足够。另请参阅手册页。

#1


12  

gethostbyname() does a name→IP lookup. It should be replaced with getaddrinfo(), which can do the same.

gethostbyname()执行名称→IP查找。它应该替换为getaddrinfo(),它可以做同样的事情。

This means the warning is completely wrong. getnameinfo() is the replacement of gethostbyaddr(), both for IP→name lookups. The reverse.

这意味着警告完全错误。 getnameinfo()取代了gethostbyaddr(),用于IP→名称查找。相反。

name→IP: gethostbyname(), getaddrinfo()
IP→name: gethostbyaddr(), getnameinfo()

name→IP:gethostbyname(),getaddrinfo()IP→name:gethostbyaddr(),getnameinfo()

The newer functions can do more: they handle IPv6 and can translate strings like 'http' to 80 (port). In the future they can also determine if e.g. TCP should be used for the service in question or SCTP. The interface is ready.

较新的功能可以做得更多:它们处理IPv6并可以将'http'等字符串转换为80(端口)。在将来,他们还可以确定是否TCP应该用于有问题的服务或SCTP。界面准备就绪。

#2


11  

Beej's explains it pretty good. gethostbyname() does not works well with IPV6 and thus you should use getnameinfo() instead. All you have to do is to fill in the required informations, i.e.

Beej的解释非常好。 gethostbyname()与IPV6不兼容,因此您应该使用getnameinfo()。您所要做的就是填写所需的信息,即

getnameinfo(
    &sa,             // Pointer to your struct sockaddr
    sizeof sa,       // Size of this struct
    host,            // Pointer to hostname string
    sizeof host,     // Size of this string
    service,         // Pointer to service name string
    sizeof service,  // Size of this string
    0                // No flags given
);

Edit: After some research, I've found that

编辑:经过一番研究,我发现了

getnameinfo(&sa, sizeof(sa), hostname, size_hostname, NULL, NULL, 0);

should be sufficient.

应该足够了。

Edit #2 I've noticed you are trying to use the return value of getnameinfo as hostname. But that is not correct, the hostname is saved within the provided host pointer. The return value indicates whether the operation was sufficient. Also have a look at the man page.

编辑#2我注意到你正在尝试使用getnameinfo的返回值作为主机名。但这不正确,主机名保存在提供的主机指针中。返回值表示操作是否足够。另请参阅手册页。