1

実装に関連する2つの問題があります-

  1. 特定のリンク層アドレスをテキストから標準形式に変換できる機能が必要です。これはinet_pton()、特定の IP アドレスをテキストから標準の IPv4/IPv6 形式に変換する IP アドレスの n/w 層に同様の機能があるようにです。

  2. b/w リンク層アドレスと 48 ビット MAC アドレスに違いはありますか (特に IPv6 の場合)?

    いいえの場合、私が間違っていなければ、リンク層アドレスも常に 48 ビットの長さにする必要があります。

前もって感謝します。些細なことが欠けている場合はご容赦ください。

編集 :

わかりました..白黒リンク層アドレスとイーサネットMACアドレスの違いは明らかです。データリンク層アドレスにはいくつかの種類がありますが、イーサネット MAC アドレスはその 1 つにすぎません。

ここで、もう 1 つの問題が発生します... 最初の質問で述べたように、コマンド ラインから指定されたリンク層アドレスを標準形式に変換する必要があります。ここで提供される解決策は、イーサネット 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].

もう 1 つ、私は C++ があまり得意ではありません。C の代替案を教えてください。ありがとう。

4

1 に答える 1

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

2番目の質問についてですが、IP(特にIPv6)はネットワーク層プロトコルであり、リンク層の上にあるため、リンク層で何もする必要はありません。リンク層がイーサネットを意味する場合、はいイーサネットアドレスは常に48ビットですが、他の形式を使用する可能性のある他のリンク層プロトコルが存在します。

于 2012-12-10T20:08:50.303 に答える