如何在 uri 中包含带(或不带)区域索引的 ipv6 地址以进行 .net 远程处理?
该应用程序发布自己的地址,供其他系统使用 .net 远程处理进行连接。它获取计算机的所有地址,如下所示:
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;
}
我添加了方括号,以防它被冒号混淆,按照 此 rfc。我认为问题是区域索引 - ToString()
返回 fe80::a8e8:2b42:3c07:c04a%10
- 请注意 %10
区域索引。我可以将区域索引放入 url 中吗?根据 此维基百科页面,它们会引起问题吗?我需要区域索引吗?有没有更好的方法从 IP 地址组装 uri?
编辑:
根据查找有效地址的答案,我更改了代码以忽略链接本地地址、环回和多播地址 - 这排除了 windows7 中的这些地址,例如。
IPList[ipIndex].IsIPv6LinkLocal || IPList[ipIndex].IsIPv6Multicast || IPAddress.IsLoopback(IPList[ipIndex])
我现在假设,如果有人使用带有实际 ipv6 地址的软件,Windows 不会将该地址报告为具有区域索引,否则我构建的 url 将被破坏。我希望有一个现有的 .net url 构建类,它可以将 IPAddress、端口等作为输入,甚至更好地以某种方式将远程客户端连接到 IPAddress 类,而不是 Activator.GetObject 中所需的字符串 url,但是现在就这样了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
区域索引对其他系统没有任何意义。 (您的区域索引 10 可能是其他人的区域索引 25、eth0、en0 等,具体取决于该特定网络和平台所使用的网络接口。)另外,因为它们是 link-local 地址,(你可以看出,因为它们以
fe80 开头::
)无论如何,它们只有在被通告到本地 LAN 上的其他计算机时才可用。我建议您不要公布 IPv6 链路本地地址。如果您必须通告它们,请查看是否有办法仅将它们通告到特定接口并省略区域索引。其他系统需要知道链路本地地址只能从接收它们的同一接口使用。
更好的方法是在您的网络中添加一个 IPv6 路由器,并仅使用 全球单播地址。
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.
通过阅读 RFC 3986,我认为正确输出将类似于
tcp://[fe80::a8e8:2b42:3c07:c04a%2510]:port/name
- 您必须将%
编码为%25
,否则您的区域索引将被视为百分比编码字符!另一方面,§3.2.2 说:...因此带有区域索引的 URI 可能不符合该 RFC,具体取决于您如何阅读它。
ETA:噢,我们开始了,RFC 6874 更新了 3986 条来描述交互。是的,您需要转义百分号。
(我正在编写一个 URL 解析、操作和格式化库,目前是有点卡在如何处理区域索引上,因为规范对它们有点酷而且它们没有太多使用......)
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:...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...)