doumiang2297 2018-11-20 03:05
浏览 399

如何找到机器可以使用的ipv6地址范围(前缀?),以及如何将ipv6子网转换为ip地址片段?

There are many things I don't understand about ipv6 and networking in general, which is why I need some further clarification on some the answers already posted to other questions. I'll list my questions, what I grasped from other answers, and what I'm still confused about.

  1. Say I have a VPS with a /56 ipv6 subnet (256 * residential /64 subnets) allotted to it. How can I programmatically find the range (prefix?) of the ip's I "own". How to get IPv4 and IPv6 address of local machine?. This is the answer I saw for this question: and what I think I understand is that I get the DNS hostname for the machine, then look up that same hostname to find the range. I'm wondering two things: How do I do this in Go, and

  2. How do I transfer this range ^ into a slice (array) of ipv6 addresses. For this specific use case: the ideal solution would be to only get one ipv6 address per \64 subnet, resulting in 256 seperate ips

  • 写回答

1条回答 默认 最新

  • douzhuo1853 2018-11-20 08:04
    关注

    DNS is not very helpful in determining the local IP addresses, because a DNS entry is not required to make an IP address work, nor is it required to point to (only) the machine that you happen to run your program on.

    Instead, inspect the network interfaces and their configuration:

    package main
    
    import (
        "fmt"
        "log"
        "net"
        "os"
        "text/tabwriter"
    )
    
    func main() {
        tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
    
        ifaces, err := net.Interfaces()
        if err != nil {
            log.Fatal(err)
        }
    
        for _, iface := range ifaces {
            addrs, err := iface.Addrs()
            if err != nil {
                log.Fatal(err)
            }
            for _, addr := range addrs {
                addr, ok := addr.(*net.IPNet)
                if !ok {
                    // Not an IP interface
                    continue
                }
                if addr.IP.To4() != nil {
                    // Skip IPv4 addresses
                    continue
                }
    
                fmt.Fprintf(tw, "%s\t%s\t%s\t%s
    ", 
                    iface.Name, addr.String(), addr.IP, addr.Mask)
            }
        }
        tw.Flush()
    }
    

    For my local machine the output is:

    lo          ::1/128                      ::1                       ffffffffffffffffffffffffffffffff
    enp2s0      fe80::52e5:49ff:fe3b:107a/64 fe80::52e5:49ff:fe3b:107a ffffffffffffffff0000000000000000
    docker0     fe80::42:afff:fedb:7389/64   fe80::42:afff:fedb:7389   ffffffffffffffff0000000000000000
    tun0        fe80::f22c:2d3b:a5a0:1b61/64 fe80::f22c:2d3b:a5a0:1b61 ffffffffffffffff0000000000000000
    vethd176f0c fe80::1cc1:65ff:fe39:feff/64 fe80::1cc1:65ff:fe39:feff ffffffffffffffff0000000000000000
    

    Note that these addresses are not necessarily reachable from the Internet. This all depends on how the routing of the hoster works. In any kind of cloud setup, you are almost always better off querying the providers APIs.


    To list all /64 subnets in a particular /56 subnet, you have to leave the 56 upper bits of the subnet address as they are and permute the following 64-56 = 8 bits (which happens to be the eigth byte):

    package main
    
    import (
        "fmt"
        "net"
    )
    
    func main() {
        _, subnet, _ := net.ParseCIDR("2001:db8::/56")
        fmt.Println(subnet)
    
        subnet.Mask = net.CIDRMask(64, 128) // change mask to /64
    
        for i := 0; i <= 0xff; i++ {
            subnet.IP[7] = byte(i) // permute the 8th byte
            fmt.Println("\t", subnet)
        }
    
        // Output:
        // 2001:db8::/56
        //          2001:db8::/64
        //          2001:db8:0:1::/64
        //          2001:db8:0:2::/64
        //          2001:db8:0:3::/64
        //          2001:db8:0:4::/64
        //          2001:db8:0:5::/64
        //          2001:db8:0:6::/64
        //          2001:db8:0:7::/64
        //          2001:db8:0:8::/64
        // [...]
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记