LCOV - code coverage report
Current view: top level - net - mac_address.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 52 60 86.7 %
Date: 2026-06-11 01:56:02 Functions: 22 26 84.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #ifndef ctrlplane_mac_address_h
       6             : #define ctrlplane_mac_address_h
       7             : 
       8             : #include <string>
       9             : #include <boost/system/error_code.hpp>
      10             : 
      11             : #include <net/ethernet.h>
      12             : #include <sys/socket.h>
      13             : 
      14             : class MacAddress {
      15             : public:
      16             :     MacAddress();
      17             :     explicit MacAddress(const uint8_t *data);
      18             : 
      19     3161558 :     MacAddress(const MacAddress &rhs) {
      20     3161558 :         addr_ = rhs.addr_;
      21     3161558 :     }
      22             : 
      23       17971 :     explicit MacAddress(const struct ether_addr &a) {
      24       17971 :         addr_ = a;
      25       17971 :     }
      26           2 :     explicit MacAddress(const struct ether_addr *a) {
      27           2 :         addr_ = *a;
      28           2 :     }
      29             : 
      30             :     MacAddress(unsigned int a, unsigned int b, unsigned int c,
      31             :                unsigned int d, unsigned int e, unsigned int f);
      32             : 
      33             :     explicit MacAddress(const std::string &s,
      34             :                         boost::system::error_code *error = NULL);
      35             : 
      36             :     bool IsBroadcast() const;
      37             :     bool IsMulticast() const;
      38             :     bool IsZero() const;
      39             :     int CompareTo(const MacAddress &rhs, int len = 0) const;
      40             : 
      41             :     bool operator==(const MacAddress *rhs) const {
      42             :         return CompareTo(*rhs) == 0;
      43             :     }
      44        4642 :     bool operator==(const MacAddress &rhs) const {
      45        4642 :         return CompareTo(rhs) == 0;
      46             :     }
      47     1537883 :     bool operator<(const MacAddress &rhs) const {
      48     1537883 :         return CompareTo(rhs) < 0;
      49             :     }
      50             :     bool operator>(const MacAddress &rhs) const {
      51             :         return CompareTo(rhs) > 0;
      52             :     }
      53        4226 :     bool operator!=(const MacAddress &rhs) const {
      54        4226 :         return !operator==(rhs);
      55             :     }
      56             : 
      57       21263 :     static size_t size() {
      58       21263 :         return sizeof(struct ether_addr);
      59             :     }
      60             : 
      61           0 :     static size_t bit_len() {
      62           0 :         return (sizeof(struct ether_addr) * 8);
      63             :     }
      64             : 
      65         596 :     u_int8_t &operator[](size_t i) {
      66         596 :         return ((u_int8_t *)&addr_)[i];
      67             :     }
      68             : 
      69         534 :     u_int8_t operator[](size_t i) const {
      70         534 :         return ((u_int8_t *)&addr_)[i];
      71             :     }
      72             : 
      73             :     MacAddress &operator=(const u_int8_t *c);
      74             : 
      75             :     MacAddress &operator=(const struct sockaddr *sa);
      76             : 
      77           0 :     MacAddress &operator=(const struct sockaddr &sa) {
      78           0 :         return operator=(&sa);
      79             :     }
      80             : 
      81           1 :     MacAddress &operator=(const struct ether_addr &ea) {
      82           1 :         addr_ = ea;
      83           1 :         return *this;
      84             :     }
      85             : 
      86      111883 :     MacAddress &operator=(const MacAddress &rhs) {
      87      111883 :         addr_ = rhs.addr_;
      88      111883 :         return *this;
      89             :     }
      90             : 
      91             :     bool ToArray(u_int8_t *p, size_t s) const;
      92             : 
      93             :     operator ether_addr() {
      94             :         return addr_;
      95             :     }
      96             : 
      97           1 :     operator ether_addr&() {
      98           1 :         return addr_;
      99             :     }
     100             : 
     101           1 :     operator sockaddr() const {
     102           1 :         struct sockaddr sa = { 0 };
     103           1 :         ToArray((u_int8_t *)sa.sa_data, sizeof(sa));
     104           1 :         return sa;
     105             :     }
     106             : 
     107          29 :     operator const ether_addr&() const {
     108          29 :         return addr_;
     109             :     }
     110             : 
     111           0 :     operator const u_int8_t *() const {
     112           0 :         return (const u_int8_t *)&addr_;
     113             :     }
     114             : 
     115             :     operator u_int8_t *() {
     116             :         return (u_int8_t *)&addr_;
     117             :     }
     118             : 
     119          78 :     operator const int8_t *() const {
     120          78 :         return (const int8_t *)&addr_;
     121             :     }
     122             : 
     123         338 :     operator int8_t *() {
     124         338 :         return (int8_t *)&addr_;
     125             :     }
     126             : 
     127          22 :     u_int8_t &last_octet() {
     128          22 :         return ((u_int8_t *)&addr_)[5];
     129             :     }
     130             : 
     131          28 :     void Zero() {
     132          28 :         addr_ = kZeroMac;
     133          28 :     }
     134             : 
     135           1 :     void Broadcast() {
     136           1 :         addr_ = kBroadcastMac;
     137           1 :     }
     138             : 
     139             :     void Multicast() {
     140             :         addr_ = kMulticastMac;
     141             :     }
     142             : 
     143      843108 :     const uint8_t *GetData() const { return (uint8_t *)&addr_; }
     144             : 
     145             :     std::string ToString() const;
     146             :     static MacAddress FromString(const std::string &str,
     147             :         boost::system::error_code *error = NULL);
     148             : 
     149             :     static const MacAddress kZeroMac;
     150             :     static const MacAddress kBroadcastMac;
     151             :     static const MacAddress kMulticastMac;
     152     1013944 :     static const MacAddress &BroadcastMac() {
     153     1013944 :         return kBroadcastMac;
     154             :     }
     155           0 :     static const MacAddress &MulticastMac() {
     156           0 :         return kMulticastMac;
     157             :     }
     158       16336 :     static const MacAddress &ZeroMac() {
     159       16336 :         return kZeroMac;
     160             :     }
     161             : 
     162             : private:
     163             :     struct ether_addr addr_;
     164             : };
     165             : 
     166             : #endif

Generated by: LCOV version 1.14