#ifndef MKLIB_H #define MKLIB_H /* Library for MeshKit */ #include #include #include #include "include/commmodel.hpp" #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) /*! Defines the length of a MAC address */ #define MAC_ADDR_BYTES 6 /*! Defines the ip version 6 data type */ typedef unsigned long long ip6_t; /*! Implements a mac address to be used as a key in a map. */ struct MacAddr { /*! The MAC address. */ mac_t addr; MacAddr(); MacAddr(mac_t mac) : addr(mac) {} MacAddr(uint8_t arr[MAC_ADDR_BYTES]); bool operator<(const MacAddr &rhs) const; bool operator==(const MacAddr &rhs) const; bool operator!=(const MacAddr &rhs) const { return !(*this == rhs); } /*! Cast to mac_t */ operator mac_t() const { return this->addr; } }; class BaseException : public std::exception { public: BaseException(const std::string &err) : mErr(err) {} ~BaseException() throw() {}; const char *what() const throw() {return mErr.c_str();} protected: std::string mErr; }; /* class FatalException : BaseException { public: FatalException(const std::string &err) : BaseException(err) { } }; */ class MKLib { public: /*! \brief Returns the string representation of a ip version 4 address. \param addr The ip version 4 address. \return A string of the form of dot-decimal notation (e.g., 192.168.0.1) representing the given ip address. */ static std::string ip2string(ip4_t addr); /*! \brief Returns the string representation of a mac address. \param addr The mac address. \return A string of the form xx:xx:xx:xx representing the given mac address. */ static std::string mac2string(mac_t addr); /*! \brief Returns an ip4_t representation of a version 4 ip address. \param A version 4 ip address in the form of dot-decimal notation (e.g., 192.168.0.1) \return An ip4_t representation of the given version 4 ip address. \throw BaseException If addr is in an invalid form. */ static ip4_t parseIP4Addr(const char *addr); /*! \brief Returns a mac_t representation of a mac address. \param A mac address in the form of xx:xx:xx:xx:xx:xx (e.g., DE:AD:BE:EF:CA:FE) \return A mac_t representation of the given mac address. \throw BaseException If addr is in an invalid form. */ static mac_t parseMACAddr(const char *addr); private: enum { /*! The longest length an ip version 4 address string. */ kIP4MaxLen = 15, /*! The longest length of a mac address string. */ kMACMaxLen = 17 }; }; /*! Represents a formatted string using the printf family formatting notation. */ class FString : public std::string { public: /*! \brief Constructs a formatted string. \details The parameter list is identical to that of printf() */ FString(char *fmt, ...); }; #endif