Skip to content

Commit 0e4d417

Browse files
Fix loopback interface for MacOS (#3799)
* Add loopback interface helper for macos * Define LOOPBACK_INTERFACE constant instead of helper function * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 85a54e2 commit 0e4d417

6 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/shared/constants.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ static const std::unordered_map<int, std::string> ROBOT_MULTICAST_CHANNELS = {
2222
{12, "ff02::c3d0:42d2:bb12"}, {13, "ff02::c3d0:42d2:bb13"},
2323
{14, "ff02::c3d0:42d2:bb14"}, {15, "ff02::c3d0:42d2:bb15"}};
2424

25+
// the name of the loopback network interface, which differs across platforms
26+
#ifdef __APPLE__
27+
static const std::string LOOPBACK_INTERFACE = "lo0";
28+
#else
29+
static const std::string LOOPBACK_INTERFACE = "lo";
30+
#endif
31+
2532
// PlotJuggler's default host and port
2633
static const std::string PLOTJUGGLER_GUI_DEFAULT_HOST = "ff02::c3d0:42d2:aaaa";
2734
static const short unsigned int PLOTJUGGLER_GUI_DEFAULT_PORT = 9870;

src/software/logger/plotjuggler_sink.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string>
55

66
#include "proto/visualization.pb.h"
7+
#include "shared/constants.h"
78
#include "software/logger/custom_logging_levels.h"
89
#include "software/networking/udp/threaded_udp_sender.h"
910

@@ -20,7 +21,7 @@ class PlotJugglerSink
2021
*
2122
* @param interface The interface to send Plotjuggler UDP packets on
2223
*/
23-
PlotJugglerSink(const std::string& interface = "lo");
24+
PlotJugglerSink(const std::string& interface = LOOPBACK_INTERFACE);
2425

2526
~PlotJugglerSink() = default;
2627

src/software/networking/udp/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cc_test(
1717
],
1818
deps = [
1919
":network_utils",
20+
"//shared:constants",
2021
"//shared/test_util:tbots_gtest_main",
2122
],
2223
)
@@ -68,6 +69,7 @@ cc_test(
6869
],
6970
deps = [
7071
":threaded_proto_udp_listener",
72+
"//shared:constants",
7173
"//shared/test_util:tbots_gtest_main",
7274
"@protobuf//:empty_cc_proto",
7375
],
@@ -90,6 +92,7 @@ cc_test(
9092
],
9193
deps = [
9294
":threaded_proto_udp_sender",
95+
"//shared:constants",
9396
"//shared/test_util:tbots_gtest_main",
9497
"@protobuf//:empty_cc_proto",
9598
],

src/software/networking/udp/network_utils_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#include <gtest/gtest.h>
44

5+
#include "shared/constants.h"
6+
57
TEST(NetworkUtilsTest, getLocalIpValidInterface)
68
{
7-
std::string interface = "lo";
9+
std::string interface = LOOPBACK_INTERFACE;
810
std::optional<std::string> local_ip = getLocalIp(interface, true);
911
EXPECT_TRUE(local_ip);
1012
EXPECT_EQ(local_ip.value(), "127.0.0.1");

src/software/networking/udp/threaded_proto_udp_listener_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <gtest/gtest.h>
44

55
#include "google/protobuf/empty.pb.h"
6+
#include "shared/constants.h"
67
#include "software/networking/tbots_network_exception.h"
78

89
TEST(ThreadedProtoUdpListenerTest, error_finding_local_ip_address)
@@ -17,12 +18,12 @@ TEST(ThreadedProtoUdpListenerTest, error_creating_socket)
1718
{
1819
// This will always fail because it requires root privileges to open this port
1920
EXPECT_THROW(ThreadedProtoUdpListener<google::protobuf::Empty>(
20-
"224.5.23.1", 1023, "lo", [](const auto&) {}, true),
21+
"224.5.23.1", 1023, LOOPBACK_INTERFACE, [](const auto&) {}, true),
2122
TbotsNetworkException);
2223
}
2324

2425
TEST(ThreadedProtoUdpListenerTest, no_error_creating_socket)
2526
{
2627
ThreadedProtoUdpListener<google::protobuf::Empty>(
27-
"224.5.23.0", 40000, "lo", [](const auto&) {}, true);
28+
"224.5.23.0", 40000, LOOPBACK_INTERFACE, [](const auto&) {}, true);
2829
}

src/software/networking/udp/threaded_proto_udp_sender_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <gtest/gtest.h>
44

55
#include "google/protobuf/empty.pb.h"
6+
#include "shared/constants.h"
67
#include "software/networking/tbots_network_exception.h"
78

89
TEST(ThreadedProtoUdpSenderTest, error_finding_local_ip_address)
@@ -14,5 +15,6 @@ TEST(ThreadedProtoUdpSenderTest, error_finding_local_ip_address)
1415

1516
TEST(ThreadedProtoUdpSenderTest, no_error_creating_socket)
1617
{
17-
ThreadedProtoUdpSender<google::protobuf::Empty>("224.5.23.1", 40000, "lo", true);
18+
ThreadedProtoUdpSender<google::protobuf::Empty>("224.5.23.1", 40000,
19+
LOOPBACK_INTERFACE, true);
1820
}

0 commit comments

Comments
 (0)