@@ -395,34 +395,39 @@ INET_ERROR TCPEndPoint::Connect(IPAddress addr, uint16_t port, InterfaceId intf)
395395 {
396396 // Try binding to the interface
397397
398- #ifdef SO_BINDTODEVICE
399- struct ifreq ifr;
400- memset (&ifr, 0 , sizeof (ifr));
401-
402- res = GetInterfaceName (intf, ifr.ifr_name , sizeof (ifr.ifr_name ));
403- if (res != INET_NO_ERROR)
404- return res;
398+ // If destination is link-local then there is no need to bind to
399+ // interface or address on the interface.
405400
406- // Attempt to bind to the interface using SO_BINDTODEVICE which requires privileged access.
407- // If the permission is denied(EACCES) because Weave is running in a context
408- // that does not have privileged access, choose a source address on the
409- // interface to bind the connetion to.
410- int r = setsockopt (mSocket , SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof (ifr));
411- if (r < 0 && errno != EACCES)
412- {
413- return res = Weave::System::MapErrorPOSIX (errno);
414- }
415-
416- if (r < 0 )
417- #endif // SO_BINDTODEVICE
401+ if (!addr.IsIPv6LinkLocal ())
418402 {
403+ #ifdef SO_BINDTODEVICE
404+ struct ifreq ifr;
405+ memset (&ifr, 0 , sizeof (ifr));
419406
420- // Attempting to initiate a connection via a specific interface is not allowed.
421- // The only way to do this is to bind the local to an address on the desired
422- // interface.
423- res = BindSrcAddrFromIntf (addrType, intf);
407+ res = GetInterfaceName (intf, ifr.ifr_name , sizeof (ifr.ifr_name ));
424408 if (res != INET_NO_ERROR)
425409 return res;
410+
411+ // Attempt to bind to the interface using SO_BINDTODEVICE which requires privileged access.
412+ // If the permission is denied(EACCES) because Weave is running in a context
413+ // that does not have privileged access, choose a source address on the
414+ // interface to bind the connetion to.
415+ int r = setsockopt (mSocket , SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof (ifr));
416+ if (r < 0 && errno != EACCES)
417+ {
418+ return res = Weave::System::MapErrorPOSIX (errno);
419+ }
420+
421+ if (r < 0 )
422+ #endif // SO_BINDTODEVICE
423+ {
424+ // Attempting to initiate a connection via a specific interface is not allowed.
425+ // The only way to do this is to bind the local to an address on the desired
426+ // interface.
427+ res = BindSrcAddrFromIntf (addrType, intf);
428+ if (res != INET_NO_ERROR)
429+ return res;
430+ }
426431 }
427432 }
428433
0 commit comments