2012-12-10 30 views
1

我不得不与我实现了两个问题 -inet_pton()对应为链路层地址

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

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

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

在此先感谢。请原谅我是否缺少一些微不足道的东西。

编辑:

好吧..我上的区别清楚的B/W链路层地址和以太网MAC地址。有几种类型的数据链路层地址,以太网mac地址只是一个。

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

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

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的替代品吗? 谢谢。

+0

[C++将mac id字符串转换为uint8 \ _t数组]可能的重复(http://stackoverflow.com/questions/276099/c-converting-a-mac-id-string-into-an- array-of-uint8-t) – 2012-12-10 19:44:32

+0

第一个问题是一个确切的重复,你的第二个问题不是建设性的。你在谈论IEEE 802的数据链路层吗?或者是其他东西? – 2012-12-10 19:48:28

回答

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位,但也有其他链路层协议提出可能使用其他格式。

+0

我认为你在这里混淆了“以上”和“波纹管”...... –

+0

那么这个还是取决于你的观点......但是通常是相反的。 – Goz

+0

@NikolaiNFetissov嗯,你是对的。编辑。谢谢 – user59169

相关问题