How to include ipv6 addresses with (or without) zo

2019-06-01 09:50发布

This application publishes its own address for other systems to connect to using .net remoting. It gets all the addresses of the computer with something like:

IPAddress[] IPList = Dns.GetHostEntry(Environment.MachineName).AddressList;
string ipAddress = IPList[ipIndex].ToString();
if(ipAddress.contains(":"))
{
  // ip6 address
  url = "tcp://[" + ipAddress + "]:" + port + "/" + name;
}
else
{
  url = "tcp://" + ipAddress + ":" + port + "/" + name;
}

I added the square brackets in case it was getting confused by the colons as per this rfc. I think the problem is zone indexes - ToString() returns fe80::a8e8:2b42:3c07:c04a%10 - Note the %10 zone index. Can I put the zone index into a url? According to this wikipedia page they cause problems? Do I need the zone index? Is there some better way to assemble a uri from an IP address?

edit:

As per the answer to find the valid addresses I changed the code to ignore link local addresses, loopback and multicast addresses - this excludes these in windows7 eg.

IPList[ipIndex].IsIPv6LinkLocal || IPList[ipIndex].IsIPv6Multicast || IPAddress.IsLoopback(IPList[ipIndex])

I am now assuming that if someone uses this software with an actual ipv6 address windows will not report the address as having a zone index or else the url I build will break. I was hoping for an existing .net url building class that could take an IPAddress, port, etc as input or even better for some way to connect a remoting client to an IPAddress class instead of a string url as is required in Activator.GetObject but this will do for now.

2条回答
时光不老,我们不散
2楼-- · 2019-06-01 10:07

The zone indexes won't mean anything to other systems. (Your zone index 10 might be someone else's zone index 25, eth0, en0, etc, depending on the network interface that happens to be in use for that particular network, and the platform.) Plus, since they are link-local addresses, (you can tell because they start with fe80::) they are only usable if they are being advertised to other machines on the local LAN, anyway.

I would suggest you not advertise IPv6 link-local addresses. If you must advertise them, see if there is a way to advertise them only to specific interfaces and leave off the zone index. Other systems would need to know that link-local addresses are only usable from the same interface they were received from.

Far better would be to add an IPv6 router to your network, and use only global unicast addresses.

查看更多
你好瞎i
3楼-- · 2019-06-01 10:23

From a reading of RFC 3986, I think the correct output would be something like tcp://[fe80::a8e8:2b42:3c07:c04a%2510]:port/name -- you have to encode the % as %25, otherwise your zone index is seen as a percent-encoded character! On the other hand, §3.2.2 says:

The ABNF provided here is a translation of the text definition of an IPv6 literal address provided in [RFC3513]. This syntax does not support IPv6 scoped addressing zone identifiers.

...so a URI with a zone index might not conform to that RFC, depending on how you read it.

ETA: Oh here we go, RFC 6874 updates 3986 to describe the interaction. Yes, you need to escape the percent sign.

(I'm in the middle of writing a URL parsing, manipulation, and formatting library and am currently a bit stuck on how to handle zone indexes, given that the spec is a bit cool on them and they're not much in use...)

查看更多
登录 后发表回答