[llvm-jitlink] Fix use of getaddrinfo(3) when connecting remote executor via TCP socket

Since llvm-jitlink moved from gethostbyname to getaddrinfo in D95477, it seems to no longer connect to llvm-jitlink-executor via TCP. I can reproduce this behavior on both, Debian 10 and macOS 10.15.7:

```
> llvm-jitlink-executor listen=localhost:10819
--
> llvm-jitlink --oop-executor-connect=localhost:10819 /path/to/obj.o
Failed to resolve localhost:10819
```

Reviewed By: rzurob

Differential Revision: https://reviews.llvm.org/D98579
This commit is contained in:
Stefan Gränitz 2021-03-22 11:18:49 +01:00
parent 20d93267e1
commit 4a8161fe40
1 changed files with 13 additions and 13 deletions

View File

@ -704,28 +704,28 @@ LLVMJITLinkRemoteTargetProcessControl::ConnectToExecutor() {
addrinfo Hints{};
Hints.ai_family = AF_INET;
Hints.ai_socktype = SOCK_STREAM;
Hints.ai_protocol = PF_INET;
Hints.ai_flags = AI_NUMERICSERV;
if (getaddrinfo(HostName.c_str(), PortStr.str().c_str(), &Hints, &AI) != 0)
return make_error<StringError>("Failed to resolve " + HostName + ":" +
Twine(Port),
if (int EC =
getaddrinfo(HostName.c_str(), PortStr.str().c_str(), &Hints, &AI))
return make_error<StringError>(formatv("Failed to resolve {0}:{1} ({2})",
HostName, Port, gai_strerror(EC)),
inconvertibleErrorCode());
int SockFD = socket(PF_INET, SOCK_STREAM, 0);
sockaddr_in ServAddr;
memset(&ServAddr, 0, sizeof(ServAddr));
ServAddr.sin_family = PF_INET;
ServAddr.sin_port = htons(Port);
// getaddrinfo returns a list of address structures. Go through the list
// to find one we can connect to.
int SockFD;
int ConnectRC = -1;
for (addrinfo *Server = AI; Server; Server = Server->ai_next) {
memmove(&Server->ai_addr, &ServAddr.sin_addr.s_addr, Server->ai_addrlen);
ConnectRC = connect(SockFD, reinterpret_cast<sockaddr *>(&ServAddr),
sizeof(ServAddr));
// If socket fails, maybe it's because the address family is not supported.
// Skip to the next addrinfo structure.
if ((SockFD = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0)
continue;
ConnectRC = connect(SockFD, Server->ai_addr, Server->ai_addrlen);
if (ConnectRC == 0)
break;
close(SockFD);
}
freeaddrinfo(AI);
if (ConnectRC == -1)