How to find range (prefix?) of ipv6 addresses that a machine can use, and how to transform a subnet of ipv6...












0














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











share|improve this question
























  • You get all of them. And it probably does not make sense to take one IP address per subnet, but you also haven't described what you're thinking about doing.
    – Michael Hampton
    Nov 20 at 3:15










  • @MichaelHampton Am aware it seems counter intuitive, but hypothetically. Maybe a better question would be: How should I separate the ips into their respective subnets.
    – bad
    Nov 20 at 3:23






  • 1




    There's only one way to do that. You should definitely study the basics of IPv6 before you go any farther.
    – Michael Hampton
    Nov 20 at 3:26










  • A /56 subnet consists of 2^(128-56) = 2^72 = 4.7e21 addresses. You cannot compute that slice before the sun burns out. To compute the first and last address of the subnet, set the trailing 56 bits of any address within the subnet to zero or one, respectively.
    – Peter
    Nov 20 at 6:58










  • Oops, trailing 72 bits, not 56.
    – Peter
    Nov 20 at 7:06
















0














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











share|improve this question
























  • You get all of them. And it probably does not make sense to take one IP address per subnet, but you also haven't described what you're thinking about doing.
    – Michael Hampton
    Nov 20 at 3:15










  • @MichaelHampton Am aware it seems counter intuitive, but hypothetically. Maybe a better question would be: How should I separate the ips into their respective subnets.
    – bad
    Nov 20 at 3:23






  • 1




    There's only one way to do that. You should definitely study the basics of IPv6 before you go any farther.
    – Michael Hampton
    Nov 20 at 3:26










  • A /56 subnet consists of 2^(128-56) = 2^72 = 4.7e21 addresses. You cannot compute that slice before the sun burns out. To compute the first and last address of the subnet, set the trailing 56 bits of any address within the subnet to zero or one, respectively.
    – Peter
    Nov 20 at 6:58










  • Oops, trailing 72 bits, not 56.
    – Peter
    Nov 20 at 7:06














0












0








0







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











share|improve this question















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








go networking ipv6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 8:05









glglgl

65.7k788163




65.7k788163










asked Nov 20 at 3:05









bad

419




419












  • You get all of them. And it probably does not make sense to take one IP address per subnet, but you also haven't described what you're thinking about doing.
    – Michael Hampton
    Nov 20 at 3:15










  • @MichaelHampton Am aware it seems counter intuitive, but hypothetically. Maybe a better question would be: How should I separate the ips into their respective subnets.
    – bad
    Nov 20 at 3:23






  • 1




    There's only one way to do that. You should definitely study the basics of IPv6 before you go any farther.
    – Michael Hampton
    Nov 20 at 3:26










  • A /56 subnet consists of 2^(128-56) = 2^72 = 4.7e21 addresses. You cannot compute that slice before the sun burns out. To compute the first and last address of the subnet, set the trailing 56 bits of any address within the subnet to zero or one, respectively.
    – Peter
    Nov 20 at 6:58










  • Oops, trailing 72 bits, not 56.
    – Peter
    Nov 20 at 7:06


















  • You get all of them. And it probably does not make sense to take one IP address per subnet, but you also haven't described what you're thinking about doing.
    – Michael Hampton
    Nov 20 at 3:15










  • @MichaelHampton Am aware it seems counter intuitive, but hypothetically. Maybe a better question would be: How should I separate the ips into their respective subnets.
    – bad
    Nov 20 at 3:23






  • 1




    There's only one way to do that. You should definitely study the basics of IPv6 before you go any farther.
    – Michael Hampton
    Nov 20 at 3:26










  • A /56 subnet consists of 2^(128-56) = 2^72 = 4.7e21 addresses. You cannot compute that slice before the sun burns out. To compute the first and last address of the subnet, set the trailing 56 bits of any address within the subnet to zero or one, respectively.
    – Peter
    Nov 20 at 6:58










  • Oops, trailing 72 bits, not 56.
    – Peter
    Nov 20 at 7:06
















You get all of them. And it probably does not make sense to take one IP address per subnet, but you also haven't described what you're thinking about doing.
– Michael Hampton
Nov 20 at 3:15




You get all of them. And it probably does not make sense to take one IP address per subnet, but you also haven't described what you're thinking about doing.
– Michael Hampton
Nov 20 at 3:15












@MichaelHampton Am aware it seems counter intuitive, but hypothetically. Maybe a better question would be: How should I separate the ips into their respective subnets.
– bad
Nov 20 at 3:23




@MichaelHampton Am aware it seems counter intuitive, but hypothetically. Maybe a better question would be: How should I separate the ips into their respective subnets.
– bad
Nov 20 at 3:23




1




1




There's only one way to do that. You should definitely study the basics of IPv6 before you go any farther.
– Michael Hampton
Nov 20 at 3:26




There's only one way to do that. You should definitely study the basics of IPv6 before you go any farther.
– Michael Hampton
Nov 20 at 3:26












A /56 subnet consists of 2^(128-56) = 2^72 = 4.7e21 addresses. You cannot compute that slice before the sun burns out. To compute the first and last address of the subnet, set the trailing 56 bits of any address within the subnet to zero or one, respectively.
– Peter
Nov 20 at 6:58




A /56 subnet consists of 2^(128-56) = 2^72 = 4.7e21 addresses. You cannot compute that slice before the sun burns out. To compute the first and last address of the subnet, set the trailing 56 bits of any address within the subnet to zero or one, respectively.
– Peter
Nov 20 at 6:58












Oops, trailing 72 bits, not 56.
– Peter
Nov 20 at 7:06




Oops, trailing 72 bits, not 56.
– Peter
Nov 20 at 7:06












1 Answer
1






active

oldest

votes


















1














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, "%st%st%st%sn",
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
// [...]
}





share|improve this answer





















  • Thank you Peter, I really appreciate the help. This stems from my lack ofo knowledge in ipv6, but how would I transform a subnet into a random ip from within it. Say for example: 2001:db8:0:3::/64, an address from within it might look like: 2001:db8:0:3::xxxx?
    – bad
    Nov 20 at 19:46










  • You take 2001:db8:0:3::, leave the top 64 bits as they are, and set the remaining bits to any value you'd like. Note that this is not specific to IPv6; You simply apply a bitmask to the address (the subnet mask). The same applies to IPv4. For instance, to pick an address from 192.168.0.0/24, leave the top 24 bits as they are, and set the remaining 8 bits to any value you'd like.
    – Peter
    Nov 21 at 8:53













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385626%2fhow-to-find-range-prefix-of-ipv6-addresses-that-a-machine-can-use-and-how-to%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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, "%st%st%st%sn",
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
// [...]
}





share|improve this answer





















  • Thank you Peter, I really appreciate the help. This stems from my lack ofo knowledge in ipv6, but how would I transform a subnet into a random ip from within it. Say for example: 2001:db8:0:3::/64, an address from within it might look like: 2001:db8:0:3::xxxx?
    – bad
    Nov 20 at 19:46










  • You take 2001:db8:0:3::, leave the top 64 bits as they are, and set the remaining bits to any value you'd like. Note that this is not specific to IPv6; You simply apply a bitmask to the address (the subnet mask). The same applies to IPv4. For instance, to pick an address from 192.168.0.0/24, leave the top 24 bits as they are, and set the remaining 8 bits to any value you'd like.
    – Peter
    Nov 21 at 8:53


















1














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, "%st%st%st%sn",
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
// [...]
}





share|improve this answer





















  • Thank you Peter, I really appreciate the help. This stems from my lack ofo knowledge in ipv6, but how would I transform a subnet into a random ip from within it. Say for example: 2001:db8:0:3::/64, an address from within it might look like: 2001:db8:0:3::xxxx?
    – bad
    Nov 20 at 19:46










  • You take 2001:db8:0:3::, leave the top 64 bits as they are, and set the remaining bits to any value you'd like. Note that this is not specific to IPv6; You simply apply a bitmask to the address (the subnet mask). The same applies to IPv4. For instance, to pick an address from 192.168.0.0/24, leave the top 24 bits as they are, and set the remaining 8 bits to any value you'd like.
    – Peter
    Nov 21 at 8:53
















1












1








1






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, "%st%st%st%sn",
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
// [...]
}





share|improve this answer












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, "%st%st%st%sn",
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
// [...]
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 8:04









Peter

15.5k42232




15.5k42232












  • Thank you Peter, I really appreciate the help. This stems from my lack ofo knowledge in ipv6, but how would I transform a subnet into a random ip from within it. Say for example: 2001:db8:0:3::/64, an address from within it might look like: 2001:db8:0:3::xxxx?
    – bad
    Nov 20 at 19:46










  • You take 2001:db8:0:3::, leave the top 64 bits as they are, and set the remaining bits to any value you'd like. Note that this is not specific to IPv6; You simply apply a bitmask to the address (the subnet mask). The same applies to IPv4. For instance, to pick an address from 192.168.0.0/24, leave the top 24 bits as they are, and set the remaining 8 bits to any value you'd like.
    – Peter
    Nov 21 at 8:53




















  • Thank you Peter, I really appreciate the help. This stems from my lack ofo knowledge in ipv6, but how would I transform a subnet into a random ip from within it. Say for example: 2001:db8:0:3::/64, an address from within it might look like: 2001:db8:0:3::xxxx?
    – bad
    Nov 20 at 19:46










  • You take 2001:db8:0:3::, leave the top 64 bits as they are, and set the remaining bits to any value you'd like. Note that this is not specific to IPv6; You simply apply a bitmask to the address (the subnet mask). The same applies to IPv4. For instance, to pick an address from 192.168.0.0/24, leave the top 24 bits as they are, and set the remaining 8 bits to any value you'd like.
    – Peter
    Nov 21 at 8:53


















Thank you Peter, I really appreciate the help. This stems from my lack ofo knowledge in ipv6, but how would I transform a subnet into a random ip from within it. Say for example: 2001:db8:0:3::/64, an address from within it might look like: 2001:db8:0:3::xxxx?
– bad
Nov 20 at 19:46




Thank you Peter, I really appreciate the help. This stems from my lack ofo knowledge in ipv6, but how would I transform a subnet into a random ip from within it. Say for example: 2001:db8:0:3::/64, an address from within it might look like: 2001:db8:0:3::xxxx?
– bad
Nov 20 at 19:46












You take 2001:db8:0:3::, leave the top 64 bits as they are, and set the remaining bits to any value you'd like. Note that this is not specific to IPv6; You simply apply a bitmask to the address (the subnet mask). The same applies to IPv4. For instance, to pick an address from 192.168.0.0/24, leave the top 24 bits as they are, and set the remaining 8 bits to any value you'd like.
– Peter
Nov 21 at 8:53






You take 2001:db8:0:3::, leave the top 64 bits as they are, and set the remaining bits to any value you'd like. Note that this is not specific to IPv6; You simply apply a bitmask to the address (the subnet mask). The same applies to IPv4. For instance, to pick an address from 192.168.0.0/24, leave the top 24 bits as they are, and set the remaining 8 bits to any value you'd like.
– Peter
Nov 21 at 8:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385626%2fhow-to-find-range-prefix-of-ipv6-addresses-that-a-machine-can-use-and-how-to%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?