2012-12-10 5 views
1

내 이행과 관련된 두 가지 문제가 - 나는 우리가 비슷한이 같은 표준 형식으로 텍스트에서 특정 링크 계층 주소를 변환 할 수있는 기능을 필요inet_pton() 대응

  1. 을 특정 IP 주소를 텍스트에서 표준 IPv4/IPv6 형식으로 변환하는 IP 주소 inet_pton()에 대해 n/w 계층에서 작동합니다.

  2. 링크 레이어 주소와 48 비트 MAC 주소가 다를 수 있습니까? (구체적으로 IPv6의 경우) ?

    만약 내가 틀리지 않다면, 링크 레이어 주소는 항상 48 비트 길이 여야합니다.

미리 감사드립니다. 사소한 것을 놓친다면 변명하십시오.

편집 :

좋아 .. 나는 차이에 대한 명확한 AM B 링크 계층 주소와 이더넷 MAC 주소 w /. 몇 가지 유형의 데이터 링크 계층 주소가 있습니다. 이더넷 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) –

+0

첫 번째 질문은 정확한 중복이며, 두 번째 질문은 건설적인 질문이 아닙니다. IEEE 802의 데이터 링크 계층에 대해 이야기하고 있습니까? 또는 다른 것? –

답변

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

관련 문제