2012-12-10 29 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

相關問題