xorp

mac.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/mac.hh,v 1.32 2008/10/10 00:38:20 pavlin Exp $
00023 
00024 #ifndef __LIBXORP_MAC_HH__
00025 #define __LIBXORP_MAC_HH__
00026 
00027 #include "libxorp/xorp.h"
00028 #include "libxorp/ether_compat.h"
00029 #include "libxorp/exceptions.hh"
00030 
00031 
00035 class Mac {
00036 public:
00040     Mac();
00041 
00048     explicit Mac(const uint8_t* from_uint8);
00049 
00057     Mac(const char* from_cstring) throw (InvalidString);
00058 
00065     Mac(const struct ether_addr& from_ether_addr);
00066 
00073     Mac(const struct sockaddr& from_sockaddr);
00074 
00081     size_t copy_out(uint8_t* to_uint8) const;
00082 
00089     size_t copy_out(struct ether_addr& to_ether_addr) const;
00090 
00097     size_t copy_out(struct sockaddr& to_sockaddr) const;
00098 
00106     size_t copy_in(const uint8_t* from_uint8);
00107 
00114     size_t copy_in(const struct ether_addr& from_ether_addr);
00115 
00122     size_t copy_in(const struct sockaddr& from_sockaddr);
00123 
00131     size_t copy_in(const char* from_cstring) throw (InvalidString);
00132 
00140     bool operator<(const Mac& other) const;
00141 
00149     bool operator==(const Mac& other) const;
00150 
00158     bool operator!=(const Mac& other) const;
00159 
00166     string str() const;
00167 
00174     const uint8_t* addr() const { return _addr; }
00175 
00181     static size_t addr_bytelen() { return (ADDR_BYTELEN); }
00182 
00188     static uint32_t addr_bitlen() { return (ADDR_BITLEN); }
00189 
00195     bool is_zero() const    { return *this == ZERO(); }
00196 
00202     bool is_unicast() const;
00203 
00209     bool is_multicast() const;
00210 
00214     static const uint32_t ADDR_BITLEN = 48;
00215 
00219     static const uint32_t ADDR_BYTELEN = ADDR_BITLEN / 8;
00220 
00224     static const uint8_t MULTICAST_BIT = 0x1;
00225 
00229     static const Mac& ZERO();
00230     static const Mac& ALL_ONES();
00231     static const Mac& BROADCAST();
00232     static const Mac& STP_MULTICAST();
00233     static const Mac& LLDP_MULTICAST();
00234     static const Mac& GMRP_MULTICAST();
00235     static const Mac& GVRP_MULTICAST();
00236 
00237 private:
00238     uint8_t _addr[ADDR_BYTELEN];    // The address value (in network-order)
00239 };
00240 
00241 struct MacConstants {
00242     static const Mac zero,
00243     all_ones,
00244     broadcast,
00245     stp_multicast,
00246     lldp_multicast,
00247     gmrp_multicast,
00248     gvrp_multicast;
00249 };
00250 
00251 inline const Mac& Mac::ZERO() {
00252     return MacConstants::zero;
00253 }
00254 
00255 inline const Mac& Mac::ALL_ONES() {
00256     return MacConstants::all_ones;
00257 }
00258 
00259 inline const Mac& Mac::BROADCAST() {
00260     return MacConstants::broadcast;
00261 }
00262 
00263 inline const Mac& Mac::STP_MULTICAST() {
00264     return MacConstants::stp_multicast;
00265 }
00266 
00267 inline const Mac& Mac::LLDP_MULTICAST() {
00268     return MacConstants::lldp_multicast;
00269 }
00270 
00271 inline const Mac& Mac::GMRP_MULTICAST() {
00272     return MacConstants::gmrp_multicast;
00273 }
00274 
00275 inline const Mac& Mac::GVRP_MULTICAST() {
00276     return MacConstants::gvrp_multicast;
00277 }
00278 
00279 #endif // __LIBXORP_MAC_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations