开发者

Loop back packets through the switch

开发者 https://www.devze.com 2023-03-27 02:16 出处:网络
I\'m working on a program that needs to send data to itself too. in our topology, we have a managed switch that has mirror capability to duplicate data from all ports to one port for monitoring purpos

I'm working on a program that needs to send data to itself too. in our topology, we have a managed switch that has mirror capability to duplicate data from all ports to one port for monitoring purposes. when I send a packet from a PC to itself, the packet doesn't go outside the computer and windows IP Stack loops it back locally, so we can't have a duplicate packet of开发者_如何学Python it for our monitor port. Our program writen in C#. How can we loop back the data through the switch instead of windows stack? Is it possible at all? Thanks in advance


A convoluted way, if possible, would be to create an IP alias for the network card that needs to be routed through the router. So you would then send to this external address which goes to your gateway / router and is then sent back to your IP alias. Your router must support multiple IPs from different subnets on one port to do this.

Router 
  ip1: 192.168.0.1
  ip2: 192.168.1.1
PC A (your machine) 
  ip1: 192.168.0.2 with gateway 192.168.0.1
  ip2(alias): 192.168.1.10 with gateway 192.168.1.1

So you send traffic from PC A ip1 to PC A ip2, this should be routed through the gateway.

Obviously, this requires much more than just code, it requires config changes, ip Aliasing, etc... I don't think this is what you want, but it could be a solution. I hope someone can come up with a better idea (maybe by fiddling with the Arp / Ip route tables?).

--

Second solution: In the route table of OS, you can fiddle with the gateway of the ip local address (assuming you machine is .0.2 and router/gateway is .0.1 :

Route DELETE 192.168.0.2
Route ADD 192.168.0.2 MASK 255.255.255.255 192.168.0.1 METRIC ?? IF ???

Using this, I caused my machine to contact the gateway for ip packets destined for my own machine. I was able to confirm this with a traceroute, now using 2 steps. You must give the right Metric and Interface (IF) parameter to the Route ADD command.


You could do it with two NICs in the PC and a NAT Router (Basically any off the shelf consumer router would work).

Setup the two NICs the the PC with IP address on different networks with no route between them. Attached the WAN port of the NAT router to NIC1 and one of the switch ports of the NAT router to the switch. Plug the the NIC2 on the PC to the switch. Then on the NAT router setup a forwarding rule for whatever port you use so it will forward traffic to NIC2's address.

In your application you will send the data to the NAT's WAN IP from NIC1 and receive the response on NIC2.

     NIC1 --------------------  NAT -------------- Switch -------- NIC2
 192.168.0.2        192.168.0.1 <-> 192.168.1.1                  192.168.1.2
 255.255.255.0                                                     255.255.255.0
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号