从MAC转换为IPv6

问题描述 投票:4回答:3

我想了解如何将MAC地址转换为IPv6。例如:00:01:04:76:2A:5C应该变为FE80 :: 0201:04FF:FE76:2A5C

有人可以详细说明转换吗?我们假设一个没有随机参数的自动配置用于本地机器。

ipv6
3个回答
16
投票

Conversion step by step从MAC地址(48位)到IPv6地址(128位):

  1. 取mac地址:52:74:f2:b1:a8:7f
  2. 在中间抛出ff:fe:52:74:f2:ff:fe:b1:a8:7f
  3. 重新格式化为IPv6表示法5274:f2ff:feb1:a87f
  4. 将第一个八位字节从十六进制转换为二进制:52 - > 01010010
  5. 反转索引6处的位(从0开始计数):01010010 - > 01010000
  6. 将八位字节转换回十六进制:01010000 - > 50
  7. 用新计算的一个替换第一个八位字节:5074:f2ff:feb1:a87f
  8. pre-link-local prefix:fe80 :: 5074:f2ff:feb1:a87f

1
投票

Bash / zsh函数(现在没有bc工作):

format_eui_64() {
    local macaddr="$1"
    printf "%02x%s" $(( 16#${macaddr:0:2} ^ 2#00000010 )) "${macaddr:2}" \
        | sed -E -e 's/([0-9a-zA-Z]{2})*/0x\0|/g' \
        | tr -d ':\n' \
        | xargs -d '|' \
        printf "%02x%02x:%02xff:fe%02x:%02x%02x"
}

0
投票

我让它在C工作。欢迎提出建议。

#include <stdio.h>
#include <string.h>

int toogle_kth_bit(unsigned int n, int k) { 
    return (n ^ (1 << (k-1))); 
}

/*
- take the mac address: 52:74:f2:b1:a8:7f
- throw ff:fe in the middle: 52:74:f2:ff:fe:b1:a8:7f
- reformat to IPv6 notation 5274:f2ff:feb1:a87f
- convert the first octet from hexadecimal to binary: 52 -> 01010010
- invert the bit at index 6 (counting from 0): 01010010 -> 01010000
- convert octet back to hexadecimal: 01010000 -> 50
- replace first octet with newly calculated one: 5074:f2ff:feb1:a87f
- prepend the link-local prefix: fe80::5074:f2ff:feb1:a87f
*/

void mac_to_ipv6_link_local(char* mac) {
    char mac_c[128];
    char mac_c_exp[128];
    char ipv6[128];
    char ipv6_link_local[128];
    char delim[] = ":";
    int count = 0;

    // throw ff:fe in the middle

    snprintf(mac_c, sizeof(mac_c), "%s", mac);
    char *ptr = strtok(mac_c, delim);

    while (ptr != NULL) {
        count++;

        if (count > 1) {
            snprintf(mac_c_exp, sizeof(mac_c_exp), "%s:%s", mac_c_exp, ptr);
        } else {
            snprintf(mac_c_exp, sizeof(mac_c_exp), "%s%s", mac_c_exp, ptr);
        }

        if (count == 3) {
            snprintf(mac_c_exp, sizeof(mac_c_exp), "%s:%s", mac_c_exp, "ff:fe");
        }

        ptr = strtok(NULL, delim);
    }

    // reformat to IPv6 notation

    ptr = strtok(mac_c_exp, delim);
    count = 0;

    while (ptr != NULL) {
        count++;

        if (count % 2 != 0 && count > 1) {
            snprintf(ipv6, sizeof(ipv6), "%s:%s", ipv6, ptr);
        } else {
            snprintf(ipv6, sizeof(ipv6), "%s%s", ipv6, ptr);
        }

        ptr = strtok(NULL, delim);
    }

    // convert the first octet from hexadecimal to binary

    char hex[2] = {ipv6[0], ipv6[1]};
    char inverted_hex[3];
    unsigned int number = (unsigned int)strtol(hex, NULL, 16);

    // invert the bit at index 6 (counting from 0)

    unsigned int new_num = toogle_kth_bit(number, 2);
    sprintf(inverted_hex, "%x", new_num);

    // replace first octet with newly calculated one

    ipv6[0] = inverted_hex[0];
    ipv6[1] = inverted_hex[1];

    // prepend the link-local prefix

    sprintf(ipv6_link_local, "%s%s", "fe80::", ipv6);
    printf("%s\n", ipv6_link_local);
}

int main() {
   mac_to_ipv6_link_local("52:74:f2:b1:a8:7f");
   return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.