知道在具有多个 NIC 的计算机上通过 NAT 发出的 http 请求的内部 IP?
想象一个 Windows 机器,其中:
- 托管 WCF 服务
- 有多个 NIC
- 位于 NAT
之后 当用户向该服务(在 WCF 基础结构之上)发出请求时,他使用 NAT 分配给目标计算机的外部地址。
我必须在 WCF 服务中编写一些代码,该代码必须知道计算机拥有的几个 NIC 中的哪一个用于实际处理网络流量。此代码如何识别 NIC 不太重要 - 它可能是其 MAC 地址(最好),也可能是 NIC 的(内部)IP 地址。
我该怎么做呢?
编辑1
我将尝试提供问题上下文。有两个代理。两者都公开相同的 WCF 服务。此外,可以指示其中一个代理开始按以下方式探测第二个代理的网络:
- 要求代理 A 向代理 B 探测网络
- 代理 A 与代理 B 协商要使用的 UDP 端口使用代理 B 公开的 WCF 服务进行探测。
- 协商结束后,代理 A 会启动一些基于 UDP 的自定义协议,其中代理 B 充当服务器 - 即它绑定到在上一项中协商的 UDP 端口。
绑定到 UDP 端口需要两部分 - IP 地址和 UDP 端口,其中 IP 地址可以是特定 IP 地址或 *(绑定到与机器关联的所有 IP 地址)。后一种选择对我们不利——我将省略原因。这给我们留下了前一个选项——绑定到特定的IP地址。但是,当代理 B 位于 NAT 之后时,用于与 WCF 服务通信的 IP 地址是 NAT 分配给代理的外部 IP 地址。另一方面,绑定需要相应的内部 IP 地址 - 如何获取它?
Imagine a Windows box, which:
- hosts a WCF service
- has multiple NICs
- sits behind NAT
When a user issues a request to the service (on top of the WCF infrastructure), he uses the external address assigned to the target machine by the NAT.
I have to write some piece of code inside the WCF service, which must know which of the several NICs that the machine has was used to actually handle the network traffic. How does this code identify the NIC is less important - it could be its MAC address (the best) or it could be the (internal) IP address of the NIC.
How can I do it?
EDIT1
I will try to supply the question context. There are two agents. Both expose the same WCF service. In addition, one of the agents can be instructed to start probing the network towards the second agent in the following fashion:
- Agent A is asked to probe the network to agent B
- Agent A negotiates with agent B the UDP port to utilize for the sake of probing using the WCF service exposed by the agent B.
- Once negotiation is over, the agent A starts some custom protocol over UDP, where the agent B acts as the server - i.e. it binds to the UDP port negotiated in the previous item.
Binding to a UDP port requires two pieces - the IP address and UDP port, where the IP address can either be a specific IP address or * (to bind to all the IP addresses associated with the machine). The latter option is not good for us - I will omit the reasons. This leaves us the former option - binding to the specific IP address. However, when the agent B is behind NAT, the IP address used to talk to the WCF service is the external IP address assigned to the agent by the NAT. Binding, on the other hand, requires the respective internal IP address - how to get it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查 WCF 操作内的 OperationContext.Current.Channel.LocalAddress (它是 EndpointAddress)吗?
作为旁注,获取远程地址可以通过以下方式完成:
--larsw
Can you check the OperationContext.Current.Channel.LocalAddress (it's an EndpointAddress) inside a WCF operation?
As a side note, getting the remote address can be done with:
--larsw
获取 MAC 使用
System.Net.NetworkInformation.NetworkInterface.GetPhysicalAddress();
所有网卡:
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
要找出真正的侦听 IP 地址,您可以编写一段代码来侦听每个地址上的端口,并从代理模拟器对其执行 ping 操作,以查看该地址是否有效。
干杯,
吉拉德
To get the MAC use
System.Net.NetworkInformation.NetworkInterface.GetPhysicalAddress();
All Nics:
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
to find out what is the real listening ip address you can write a code that listen to your port on each address and ping it from an agent emulator to see that the address is valid.
Cheers,
Gilad