NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
mac_address类 参考

MAC地址封装类 更多...

#include <mac_address.hpp>

类 mac_address 继承关系图:
[图例]

Public 类型

using bytes_type = array<byte_t, MAC_LEN>
 MAC地址字节数组类型

Public 成员函数

 mac_address () noexcept=default
 默认构造函数
 mac_address (const byte_t *bytes) noexcept
 从字节数组构造
 mac_address (const bytes_type &bytes) noexcept
 从字节数组容器构造
string to_string () const
 转换为字符串表示
bool operator== (const mac_address &other) const noexcept
 相等比较运算符
bool operator!= (const mac_address &other) const noexcept
 不等比较运算符
const bytes_typebytes () const noexcept
 获取MAC地址字节数组
Public 成员函数 继承自 istringify< mac_address >
constexpr string to_string () const
 转换为字符串

静态 Public 成员函数

static optional< mac_addressparse (string_view str)
 从字符串解析MAC地址
static optional< mac_addressparse (const ip_address &ip, const char *iface=nullptr)
 从IP地址获取MAC地址

静态 Public 属性

static constexpr size_t MAC_LEN = 6
 MAC地址字节长度

详细描述

MAC地址封装类

支持MAC地址的解析、格式化和通过ARP协议从IP地址获取MAC地址。

遵循的国际标准

本实现严格遵循以下网络寻址与协议相关标准规范:

MAC 地址标准(IEEE 802 系列):

地址表示格式标准:

MAC 地址结构

根据 IEEE 802.3,MAC 地址(48 位 EUI-48)由以下部分组成:

字段 位数 说明
OUI 24 组织唯一标识符,由 IEEE 分配给制造商
NIC Specific 24 网络接口卡特定部分,由制造商分配

**地址类型标识**(第一个字节的最低位):

位值 类型 说明
0 单播地址 发送到单个网络接口
1 多播地址 发送到一组网络接口

**本地管理标识**(第一个字节的次低位):

位值 类型 说明
0 全局唯一地址 IEEE 分配的 OUI
1 本地管理地址 由网络管理员分配

MAC 地址表示格式

根据 IEEE 802.3 §3.2.4 和 RFC 7042,MAC 地址的标准表示为:

  • 6 组十六进制数字,每组 2 位
  • 常用分隔符:冒号(:)、连字符(-)、点号(.)或无分隔符
  • 字母使用大写(推荐)或小写

**支持的格式示例**:

格式 示例 说明
冒号分隔(推荐) 00:11:22:33:44:55 本实现默认格式
连字符分隔 00-11-22-33-44-55 常见于某些操作系统
无分隔符 001122334455 紧凑格式
注解
MAC 地址格式遵循 IEEE 802.3 标准,通常表示为 6 组十六进制数字。 ARP 查询仅在本地网络有效,且需要目标主机在线。
警告
ARP 查询需要适当的系统权限(在某些系统上可能需要 root/管理员权限)。 跨网段的 IP 地址无法通过 ARP 解析 MAC 地址(需要使用默认网关的 MAC 地址)。 ARP 协议无认证机制,存在 ARP 欺骗攻击风险。
参见
https://standards.ieee.org/products-services/regauth/
https://www.rfc-editor.org/rfc/rfc826.html

在文件 mac_address.hpp84 行定义.

构造及析构函数说明

◆ mac_address() [1/3]

mac_address::mac_address ( )
defaultnoexcept

默认构造函数

创建全零的MAC地址。

被这些函数引用 mac_address(), operator!=() , 以及 operator==().

◆ mac_address() [2/3]

mac_address::mac_address ( const byte_t * bytes)
inlineexplicitnoexcept

从字节数组构造

参数
bytes指向6字节数据的指针

在文件 mac_address.hpp104 行定义.

引用了 bytes(), copy(), mac_address() , 以及 MAC_LEN.

◆ mac_address() [3/3]

mac_address::mac_address ( const bytes_type & bytes)
inlineexplicitnoexcept

从字节数组容器构造

参数
bytesMAC地址字节数组

在文件 mac_address.hpp110 行定义.

引用了 bytes().

成员函数说明

◆ bytes()

const bytes_type & mac_address::bytes ( ) const
inlinenodiscardnoexcept

获取MAC地址字节数组

返回
6字节数组的常量引用

在文件 mac_address.hpp161 行定义.

被这些函数引用 mac_address() , 以及 mac_address().

◆ operator!=()

bool mac_address::operator!= ( const mac_address & other) const
inlinenoexcept

不等比较运算符

参数
other另一个MAC地址
返回
地址不同返回true

在文件 mac_address.hpp155 行定义.

引用了 mac_address().

◆ operator==()

bool mac_address::operator== ( const mac_address & other) const
inlinenoexcept

相等比较运算符

参数
other另一个MAC地址
返回
地址相同返回true

在文件 mac_address.hpp148 行定义.

引用了 mac_address().

◆ parse() [1/2]

optional< mac_address > mac_address::parse ( const ip_address & ip,
const char * iface = nullptr )
static

从IP地址获取MAC地址

参数
ipIP地址对象
iface网络接口名称(Linux可选)
返回
成功返回MAC地址,失败返回none

通过ARP查询指定IP对应的MAC地址。

注解
仅在本地网络中有效,需要目标主机在线且可达。
此操作可能需要适当的系统权限。

◆ parse() [2/2]

optional< mac_address > mac_address::parse ( string_view str)
static

从字符串解析MAC地址

参数
strMAC地址字符串
返回
解析成功返回MAC地址对象,失败返回none

支持的格式:

  • "00:11:22:33:44:55"(冒号分隔)
  • "00-11-22-33-44-55"(连字符分隔)

◆ to_string()

string mac_address::to_string ( ) const
nodiscard

转换为字符串表示

返回
格式化的MAC地址字符串

该类的文档由以下文件生成: