首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链路层地址的inet_pton()对应

链路层地址的inet_pton()对应
EN

Stack Overflow用户
提问于 2012-12-10 19:37:54
回答 1查看 1.1K关注 0票数 2

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

  1. 我需要一个功能,可以将给定的链路层地址从文本转换为标准格式,就像我们在n/w层对IP地址具有类似的功能一样,inet_pton()将给定的IP地址从文本转换为标准的IPv6 4/IPv6 6格式。
  2. 有什么不同的b/w链路层地址和48位mac地址(在IPv6的情况下)? 如果没有,那么链路层地址也应该总是48位的长度,如果我没有错。

提前谢谢。如果我错过了一些琐碎的东西,请原谅。

编辑:

好的..。我很清楚b/w链路层地址和以太网mac地址的不同。有几种类型的数据链路层地址,以太网mac地址只是其中之一。

现在,这又出现了一个问题..。正如我在第一个问题中所说的,我需要将从命令行提供的链接层地址转换为它的标准形式。这里提供的解决方案只适用于以太网mac地址。

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

代码语言:javascript
复制
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选项吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-10 20:08:50

第一个问题,编写起来并不难,而且由于MAC地址是由一个6字节数组表示的,所以不需要考虑机器依赖性(比如endian-ness等等)。

代码语言:javascript
复制
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位,但是还有其他链路层协议可以使用其他格式。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13807918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档