【问题标题】:inet_pton() counterpart for link layer addressinet_pton() 对应的链路层地址
【发布时间】:2012-12-10 19:37:54
【问题描述】:

我有两个与我的实施有关的问题 -

  1. 我需要一个函数,它可以将给定的链路层地址从文本转换为标准格式,就像我们在 n/w 层有一个类似的函数,用于 IP 地址inet_pton(),它将给定的 IP 地址从文本转换为标准 IPv4/IPv6 格式。

  2. b/w 链路层地址和 48 位 mac 地址有什么区别吗 (特别是在 IPv6 的情况下)?

    如果不是,那么链路层地址也应该总是 48 位的长度,如果我没记错的话。

提前致谢。如果我遗漏了一些琐碎的事情,请原谅。

编辑:

好的.. 我很清楚黑白链路层地址和以太网 MAC 地址的区别。数据链路层地址有多种类型,以太网MAC地址只是其中一种。

现在,这又出现了一个问题……正如我在第一个问题中所说,我需要将从命令行给出的链路层地址转换为其标准形式。此处提供的解决方案仅适用于以太网 MAC 地址。

没有任何标准功能用于此目的吗?我想做的是创建一个应用程序,用户将在其中输入 ICMP 路由器广告消息中存在的不同选项的值,如RFC 4861 中所述。

Option Formats


Neighbor Discovery messages include zero or more options, some of
which may appear multiple times in the same message.  Options should
be padded when necessary to ensure that they end on their natural
64-bit boundaries.  All options are of the form:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |     Type      |    Length     |              ...              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   ~                              ...                              ~
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Fields:

  Type           8-bit identifier of the type of option.  The
                 options defined in this document are:

                       Option Name                             Type

                    Source Link-Layer Address                    1
                    Target Link-Layer Address                    2
                    Prefix Information                           3
                    Redirected Header                            4
                    MTU                                          5

  Length         8-bit unsigned integer.  The length of the option
                 (including the type and length fields) in units of
                 8 octets.  The value 0 is invalid.  Nodes MUST
                 silently discard an ND packet that contains an
                 option with length zero.

  4.6.1. Source/Target Link-layer Address


  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 |     Type      |    Length     |    Link-Layer Address ...
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


   Fields:

   Type
                 1 for Source Link-layer Address
                 2 for Target Link-layer Address

   Length         The length of the option (including the type and
                 length fields) in units of 8 octets.  For example,
                 the length for IEEE 802 addresses is 1
                 [IPv6-ETHER].

   Link-Layer Address
                 The variable length link-layer address.

                 The content and format of this field (including
                 byte and bit ordering) is expected to be specified
                 in specific documents that describe how IPv6
                 operates over different link layers.  For instance,
                 [IPv6-ETHER].

还有一件事我对 C++ 不太方便,你能提供一个 C 替代方案吗? 谢谢。

【问题讨论】:

标签: c linux sockets networking mac-address


【解决方案1】:

你的第一个问题,写起来并不难,而且由于 MAC 地址由一个 6 字节数组表示,你不需要考虑机器依赖性(比如字节序和其他东西)

void str2MAC(string str,char* mac) {
    for(int i=0;i<5;i++) {
        string b = str.substr(0,str.find(':'));
        str = str.substr(str.find(':')+1);
        mac[i] = 0;
        for(int j=0;j<b.size();b++) {
            mac[i] *= 0x10;
            mac[i] += (b[j]>'9'?b[j]-'a'+10:b[j]-'0');
        }
    }
    mac[5] = 0;
    for(int i=0;i<str.size();i++) {
        mac[5] *= 0x10;
        mac[5] += (str[i]>'9'?str[i]-'a'+10:str[i]-'0');
    }
}

关于您的第二个问题,IP(特别是 IPv6)是一种网络层协议,位于链路层之上,因此不必对链路层做任何事情。 如果链路层是指以太网,是的,以太网地址始终为 48 位,但存在其他可能使用其他格式的链路层协议。

【讨论】:

  • 嗯,这取决于你的观点......但通常相反。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2023-03-24
  • 2021-09-05
  • 2011-08-10
  • 2018-12-26
  • 2018-01-04
相关资源
最近更新 更多