-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
network: support interface binding on Android
Signed-off-by: Mike Schore <mike.schore@gmail.com>
- Loading branch information
Showing
6 changed files
with
150 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "library/common/network/src_addr_socket_option_impl.h" | ||
|
||
#include "envoy/config/core/v3/base.pb.h" | ||
|
||
#include "source/common/common/assert.h" | ||
|
||
namespace Envoy { | ||
namespace Network { | ||
|
||
SrcAddrSocketOptionImpl::SrcAddrSocketOptionImpl( | ||
Network::Address::InstanceConstSharedPtr source_address) | ||
: source_address_(std::move(source_address)) { | ||
ASSERT(source_address_->type() == Network::Address::Type::Ip); | ||
} | ||
|
||
bool SrcAddrSocketOptionImpl::setOption( | ||
Network::Socket& socket, envoy::config::core::v3::SocketOption::SocketState state) const { | ||
|
||
if (state == envoy::config::core::v3::SocketOption::STATE_PREBIND) { | ||
socket.connectionInfoProvider().setLocalAddress(source_address_); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Inserts an address, already in network order, to a byte array. | ||
*/ | ||
template <typename T> void addressIntoVector(std::vector<uint8_t>& vec, const T& address) { | ||
const uint8_t* byte_array = reinterpret_cast<const uint8_t*>(&address); | ||
vec.insert(vec.end(), byte_array, byte_array + sizeof(T)); | ||
} | ||
|
||
void SrcAddrSocketOptionImpl::hashKey(std::vector<uint8_t>& key) const { | ||
|
||
// Note: we're assuming that there cannot be a conflict between IPv6 addresses here. If an IPv4 | ||
// address is mapped into an IPv6 address using an IPv4-Mapped IPv6 Address (RFC4921), then it's | ||
// possible the hashes will be different despite the IP address used by the connection being | ||
// the same. | ||
if (source_address_->ip()->version() == Network::Address::IpVersion::v4) { | ||
// note raw_address is already in network order | ||
uint32_t raw_address = source_address_->ip()->ipv4()->address(); | ||
addressIntoVector(key, raw_address); | ||
} else if (source_address_->ip()->version() == Network::Address::IpVersion::v6) { | ||
// note raw_address is already in network order | ||
absl::uint128 raw_address = source_address_->ip()->ipv6()->address(); | ||
addressIntoVector(key, raw_address); | ||
} | ||
} | ||
|
||
absl::optional<Network::Socket::Option::Details> SrcAddrSocketOptionImpl::getOptionDetails( | ||
const Network::Socket&, envoy::config::core::v3::SocketOption::SocketState) const { | ||
// no details for this option. | ||
return absl::nullopt; | ||
} | ||
|
||
} // namespace Network | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include "envoy/config/core/v3/base.pb.h" | ||
#include "envoy/network/address.h" | ||
#include "envoy/network/listen_socket.h" | ||
|
||
namespace Envoy { | ||
namespace Network { | ||
|
||
/** | ||
* This is a "synthetic" socket option implementation, which sets the source IP/port of a socket | ||
* using a provided IP address (and maybe port) during bind. | ||
* | ||
* Based on the OriginalSrcSocketOption extension. | ||
*/ | ||
class SrcAddrSocketOptionImpl : public Network::Socket::Option { | ||
public: | ||
/** | ||
* Constructs a socket option which will set the socket to use source @c source_address | ||
*/ | ||
SrcAddrSocketOptionImpl(Network::Address::InstanceConstSharedPtr source_address); | ||
~SrcAddrSocketOptionImpl() override = default; | ||
|
||
/** | ||
* Updates the source address of the socket to match `source_address_`. | ||
* Adds socket options to the socket to allow this to work. | ||
*/ | ||
bool setOption(Network::Socket& socket, | ||
envoy::config::core::v3::SocketOption::SocketState state) const override; | ||
|
||
/** | ||
* Appends a key which uniquely identifies the address being tracked. | ||
*/ | ||
void hashKey(std::vector<uint8_t>& key) const override; | ||
|
||
absl::optional<Details> | ||
getOptionDetails(const Network::Socket& socket, | ||
envoy::config::core::v3::SocketOption::SocketState state) const override; | ||
|
||
private: | ||
Network::Address::InstanceConstSharedPtr source_address_; | ||
}; | ||
|
||
} // namespace Network | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters