Add a class for fetching the qbone interface address
PiperOrigin-RevId: 363155336
Change-Id: Iec202aea44bc0d273b44402312b5ba51f1056410
diff --git a/quic/qbone/bonnet/qbone_tunnel_info.cc b/quic/qbone/bonnet/qbone_tunnel_info.cc
new file mode 100644
index 0000000..372cd2b
--- /dev/null
+++ b/quic/qbone/bonnet/qbone_tunnel_info.cc
@@ -0,0 +1,37 @@
+// Copyright (c) 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "quic/qbone/bonnet/qbone_tunnel_info.h"
+
+namespace quic {
+
+QuicIpAddress QboneTunnelInfo::GetAddress() {
+ QuicIpAddress no_address;
+
+ NetlinkInterface::LinkInfo link_info{};
+ if (!netlink_->GetLinkInfo(ifname_, &link_info)) {
+ return no_address;
+ }
+
+ std::vector<NetlinkInterface::AddressInfo> addresses;
+ if (!netlink_->GetAddresses(link_info.index, 0, &addresses, nullptr)) {
+ return no_address;
+ }
+
+ quic::QuicIpAddress link_local_subnet;
+ if (!link_local_subnet.FromString("FE80::")) {
+ return no_address;
+ }
+
+ for (const auto& address : addresses) {
+ if (address.interface_address.IsInitialized() &&
+ !link_local_subnet.InSameSubnet(address.interface_address, 10)) {
+ return address.interface_address;
+ }
+ }
+
+ return no_address;
+}
+
+} // namespace quic
diff --git a/quic/qbone/bonnet/qbone_tunnel_info.h b/quic/qbone/bonnet/qbone_tunnel_info.h
new file mode 100644
index 0000000..6ca6ae8
--- /dev/null
+++ b/quic/qbone/bonnet/qbone_tunnel_info.h
@@ -0,0 +1,29 @@
+// Copyright (c) 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef QUICHE_QUIC_QBONE_BONNET_QBONE_TUNNEL_INFO_H_
+#define QUICHE_QUIC_QBONE_BONNET_QBONE_TUNNEL_INFO_H_
+
+#include "quic/platform/api/quic_ip_address.h"
+#include "quic/qbone/platform/netlink_interface.h"
+
+namespace quic {
+
+class QboneTunnelInfo {
+ public:
+ QboneTunnelInfo(std::string ifname, NetlinkInterface* netlink)
+ : ifname_(std::move(ifname)), netlink_(netlink) {}
+
+ // Returns the current QBONE tunnel address. Callers must use IsInitialized()
+ // to ensure the returned address is valid.
+ QuicIpAddress GetAddress();
+
+ private:
+ const std::string ifname_;
+ NetlinkInterface* netlink_;
+};
+
+} // namespace quic
+
+#endif // QUICHE_QUIC_QBONE_BONNET_QBONE_TUNNEL_INFO_H_
diff --git a/quic/qbone/platform/netlink_interface.h b/quic/qbone/platform/netlink_interface.h
index eabfe07..0794b1d 100644
--- a/quic/qbone/platform/netlink_interface.h
+++ b/quic/qbone/platform/netlink_interface.h
@@ -5,6 +5,8 @@
#ifndef QUICHE_QUIC_QBONE_PLATFORM_NETLINK_INTERFACE_H_
#define QUICHE_QUIC_QBONE_PLATFORM_NETLINK_INTERFACE_H_
+#include <linux/rtnetlink.h>
+
#include "quic/platform/api/quic_ip_address.h"
#include "quic/qbone/platform/ip_range.h"