Merge pull request #9214 from OatmealDome/macos-bind

Top: Set port when connecting a socket to find the default interface
This commit is contained in:
Léo Lam 2020-11-02 22:17:38 +01:00 committed by GitHub
commit cbaf8f82ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -223,8 +223,10 @@ static std::optional<DefaultInterface> GetSystemDefaultInterface()
sockaddr_in addr{};
socklen_t length = sizeof(addr);
addr.sin_family = AF_INET;
// The address is irrelevant -- no packet is actually sent. This just needs to be a public IP.
// The address and port are irrelevant -- no packet is actually sent. These just need to be set
// to a valid IP and port.
addr.sin_addr.s_addr = inet_addr(8, 8, 8, 8);
addr.sin_port = htons(53);
if (connect(sock, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr)) == -1)
return {};
if (getsockname(sock, reinterpret_cast<sockaddr*>(&addr), &length) == -1)