Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit c5715bc

Browse files
authored
Merge pull request #56 from pidarped/bug/WEAV-3155
Bind connection to interface/src address only if destination address is not link-local
2 parents c47a9cc + 0ff3aae commit c5715bc

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/inet/TCPEndPoint.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -396,34 +396,39 @@ INET_ERROR TCPEndPoint::Connect(IPAddress addr, uint16_t port, InterfaceId intf)
396396
{
397397
// Try binding to the interface
398398

399-
#ifdef SO_BINDTODEVICE
400-
struct ::ifreq ifr;
401-
memset(&ifr, 0, sizeof(ifr));
402-
403-
res = GetInterfaceName(intf, ifr.ifr_name, sizeof(ifr.ifr_name));
404-
if (res != INET_NO_ERROR)
405-
return res;
399+
// If destination is link-local then there is no need to bind to
400+
// interface or address on the interface.
406401

407-
// Attempt to bind to the interface using SO_BINDTODEVICE which requires privileged access.
408-
// If the permission is denied(EACCES) because Weave is running in a context
409-
// that does not have privileged access, choose a source address on the
410-
// interface to bind the connetion to.
411-
int r = setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
412-
if (r < 0 && errno != EACCES)
413-
{
414-
return res = Weave::System::MapErrorPOSIX(errno);
415-
}
416-
417-
if (r < 0)
418-
#endif // SO_BINDTODEVICE
402+
if (!addr.IsIPv6LinkLocal())
419403
{
404+
#ifdef SO_BINDTODEVICE
405+
struct ::ifreq ifr;
406+
memset(&ifr, 0, sizeof(ifr));
420407

421-
// Attempting to initiate a connection via a specific interface is not allowed.
422-
// The only way to do this is to bind the local to an address on the desired
423-
// interface.
424-
res = BindSrcAddrFromIntf(addrType, intf);
408+
res = GetInterfaceName(intf, ifr.ifr_name, sizeof(ifr.ifr_name));
425409
if (res != INET_NO_ERROR)
426410
return res;
411+
412+
// Attempt to bind to the interface using SO_BINDTODEVICE which requires privileged access.
413+
// If the permission is denied(EACCES) because Weave is running in a context
414+
// that does not have privileged access, choose a source address on the
415+
// interface to bind the connetion to.
416+
int r = setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
417+
if (r < 0 && errno != EACCES)
418+
{
419+
return res = Weave::System::MapErrorPOSIX(errno);
420+
}
421+
422+
if (r < 0)
423+
#endif // SO_BINDTODEVICE
424+
{
425+
// Attempting to initiate a connection via a specific interface is not allowed.
426+
// The only way to do this is to bind the local to an address on the desired
427+
// interface.
428+
res = BindSrcAddrFromIntf(addrType, intf);
429+
if (res != INET_NO_ERROR)
430+
return res;
431+
}
427432
}
428433
}
429434

0 commit comments

Comments
 (0)