C 链接层地址的inet_pton()对应项

C 链接层地址的inet_pton()对应项,c,linux,sockets,networking,mac-address,C,Linux,Sockets,Networking,Mac Address,我有两个与我的实现相关的问题- 我需要一个函数,它可以将给定的链路层地址从文本转换为标准格式,就像我们在n/w层有一个类似的函数,用于IP地址inet\u pton(),它可以将给定的IP地址从文本转换为标准的IPv4/IPv6格式 链路层地址和48位mac地址有什么区别吗 (特别是在IPv6的情况下) 如果没有,那么链接层地址的长度也应该始终为48位,如果我没有错的话 提前谢谢。如果我遗漏了一些小事,请原谅 编辑: 好的。。我很清楚b/w链路层地址和以太网mac地址的区别。有几种类型的数据链路

我有两个与我的实现相关的问题-

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

  • 链路层地址和48位mac地址有什么区别吗 (特别是在IPv6的情况下)

    如果没有,那么链接层地址的长度也应该始终为48位,如果我没有错的话

  • 提前谢谢。如果我遗漏了一些小事,请原谅

    编辑:

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

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

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

    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语言吗?
    谢谢。

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

    void str2MAC(字符串str,char*mac){
    
    对于(inti=0;i你的第一个问题,写起来并不难,而且由于MAC地址由一个6字节的数组表示,你不需要考虑机器依赖性(比如endian ness之类的)

    void str2MAC(字符串str,char*mac){
    
    对于(int i=0;i第一个问题的可能重复是完全重复,您的第二个问题不是建设性的。您是在谈论IEEE 802的数据链路层?还是其他?第一个问题的可能重复是完全重复,您的第二个问题不是建设性的。您是在谈论IEEE 802的数据链路层?或者还有别的事吗?这取决于你的观点…但通常是相反的。这取决于你的观点…但通常是相反的。
    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');
        }
    }