如何在 uri 中包含带(或不带)区域索引的 ipv6 地址以进行 .net 远程处理?

发布于 2024-11-13 10:23:53 字数 1203 浏览 1 评论 0原文

该应用程序发布自己的地址,供其他系统使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

且行且努力 2024-11-20 10:23:53

区域索引对其他系统没有任何意义。 (您的区域索引 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.

南风几经秋 2024-11-20 10:23:53

通过阅读 RFC 3986,我认为正确输出将类似于 tcp://[fe80::a8e8:2b42:3c07:c04a%2510]:port/name - 您必须将 % 编码为%25,否则您的区域索引将被视为百分比编码字符!另一方面,§3.2.2 说:

ABNF
这里提供的是 IPv6 文本定义的翻译
[RFC3513] 中提供了文字地址。该语法不支持
IPv6 范围寻址区域标识符。

...因此带有区域索引的 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:

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...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文