1

I have two problems related to my implementation -

  1. I need a function which can convert a given link-layer address from text to standard format like we have a similar function at n/w layer for IP addresses inet_pton() which converts a given IP address from text to standard IPv4/IPv6 format.

  2. Is there any difference b/w Link-layer address and 48-bit mac address (in case of IPv6 specifically)?

    If no, then link layer address should always also be of 48 bit in length, if I am not wrong.

Thanks in advance. Please excuse if I am missing something trivial.

EDIT :

Ok.. I am clear on the difference b/w link layer address and ethernet mac address. There are several types of data link layer addresses, ethernet mac address is just one.

Now, this arises one more problem ... As i said in my first question I need to convert a link layer address given from command line to its standard form. The solution provided here will just work for ethernet mac address only.

Isn't there any standard function for the purpose ? What I would like to do is to create an application where user will enter values for different options present in ICMP router advertisement message as stated in 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].

One more thing I am not quite handy with C++, can you please provide a C alternative ? Thanks.

4

1 回答 1

1

Your first question, it's not that hard to write, and since MAC addresses are represented by a 6 byte array you don't need to take machine-dependency into account ( like endian-ness and stuff )

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');
    }
}

About your second question, IP ( and IPv6 specifically) is a Network Layer protocol and is above the Link Layer, thus doesn't have to do anything with the Link Layer. If by Link Layer you mean Ethernet, yes Ethernet Address is always 48bits, but there are other Link Layer protocols presents which may use other formats.

于 2012-12-10T20:08:50.303 回答