Skip to content

Commit 04e2ec9

Browse files
authored
libvncclient: replace gethostbyname() with getaddrinfo()
man gethostbyname: "The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() functions are obsolete. Applications should use getaddrinfo(3), getnameinfo(3), and gai_strerror(3) instead." Furthermore, the man page states that `gethostbyname()` is MT-Unsafe, probably due to the fact that it retuns a pointer to static data. Thus, by using `getaddrinfo()`, we improve the multithreading soundness of LibVNCClient.
1 parent 1d1153f commit 04e2ec9

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

libvncclient/sockets.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ SetDSCP(rfbSocket sock, int dscp)
812812
rfbBool
813813
StringToIPAddr(const char *str, unsigned int *addr)
814814
{
815-
struct hostent *hp;
815+
struct addrinfo hints, *res;
816816

817817
if (strcmp(str,"") == 0) {
818818
*addr = htonl(INADDR_LOOPBACK); /* local */
@@ -824,17 +824,20 @@ StringToIPAddr(const char *str, unsigned int *addr)
824824
if (*addr != -1)
825825
return TRUE;
826826

827-
hp = gethostbyname(str);
827+
memset(&hints, 0, sizeof(hints));
828+
hints.ai_family = AF_INET;
829+
hints.ai_socktype = SOCK_STREAM;
830+
831+
if (getaddrinfo(str, NULL, &hints, &res) == 0) {
832+
*addr = (((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
833+
freeaddrinfo(res);
828834

829-
if (hp) {
830-
*addr = *(unsigned int *)hp->h_addr;
831835
return TRUE;
832836
}
833837

834838
return FALSE;
835839
}
836840

837-
838841
/*
839842
* Test if the other end of a socket is on the same machine.
840843
*/

0 commit comments

Comments
 (0)