This is an automated email from the ASF dual-hosted git repository.

bmahler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git


The following commit(s) were added to refs/heads/master by this push:
     new fe2f38f2e Support constructing net::MAC objects from sockaddr.sa_data.
fe2f38f2e is described below

commit fe2f38f2eae0f8be21507ab298935d53d93a07da
Author: Jason Zhou <[email protected]>
AuthorDate: Wed Jun 19 13:47:36 2024 -0400

    Support constructing net::MAC objects from sockaddr.sa_data.
    
    When using ioctl, we get a char[14] sa_data array from sockaddr that holds 
the
    information necessary to construct a net::MAC object, this patch adds 
support
    for using the sa_data field to directly create a net::MAC object.
    
    Review: https://reviews.apache.org/r/75058/
---
 3rdparty/stout/include/stout/mac.hpp | 12 ++++++++++++
 3rdparty/stout/tests/mac_tests.cpp   |  7 +++++++
 2 files changed, 19 insertions(+)

diff --git a/3rdparty/stout/include/stout/mac.hpp 
b/3rdparty/stout/include/stout/mac.hpp
index 298cc1422..eb5e4d66e 100644
--- a/3rdparty/stout/include/stout/mac.hpp
+++ b/3rdparty/stout/include/stout/mac.hpp
@@ -45,6 +45,7 @@
 #include <string>
 
 #include <stout/abort.hpp>
+#include <stout/check.hpp>
 #include <stout/error.hpp>
 #include <stout/none.hpp>
 #include <stout/result.hpp>
@@ -120,6 +121,17 @@ public:
     }
   }
 
+  // Template-based constructor for constructing from character array.
+  // This is useful for sockaddr.sa_data (char[14]).
+  template <size_t N>
+  explicit MAC(const char (&_bytes)[N])
+  {
+    CHECK_GE(N, 6u);
+    for (size_t i = 0; i < 6; i++) {
+      bytes[i] = _bytes[i];
+    }
+  }
+
   // Returns the byte at the given index. For example, for a MAC
   // address 01:23:45:67:89:ab, mac[0] = 01, mac[1] = 23 and etc.
   uint8_t operator[](size_t index) const
diff --git a/3rdparty/stout/tests/mac_tests.cpp 
b/3rdparty/stout/tests/mac_tests.cpp
index ebd50a005..83d180ff2 100644
--- a/3rdparty/stout/tests/mac_tests.cpp
+++ b/3rdparty/stout/tests/mac_tests.cpp
@@ -65,6 +65,13 @@ TEST(NetTest, ConstructMAC)
   uint8_t bytes[6] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc};
 
   EXPECT_EQ("12:34:56:78:9a:bc", stringify(net::MAC(bytes)));
+
+  // Mimicking a sockaddr.sa_data field which contains a MAC address,
+  // this is how ioctl exposes mac addresses.
+  char sa_data[14] = {0x12, 0x34, 0x56, 0x78, 0x0a, 0x14, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+  EXPECT_EQ("12:34:56:78:0a:14", stringify(net::MAC(sa_data)));
 }
 
 

Reply via email to