If you have more than one IP address configured on one of your network adapters, which one will be used when the packet is issued? On Linux, use the Iproute2 tool to set the SRC parameter to force the package source address:
IP route add 1.2.3.4/32 via 4.3.2.1 src 3.3.3.3
But on windows, everything is automatically chosen by internal logic, and its principles are as follows:
Single NIC, the selection and default gateway in the same network segment of the most matching IP address as the source;
Multi-NIC, first select the default gateway as the network card to send the network card, however, in accordance with the single network card principle to select an IP address as the source.
So If your destination address is 1.2.3.4, the default gateway is 4.3.2.1, with two IP addresses on the local connection: 4.3.2.2 and 3.3 3.3, it is clear that 4.3.2.2 will be selected as the source IP address, so when the contract is awarded, 4.3.2.2 will be the source IP address of the packet.
However, sometimes in order to implement some of their strategies or tips, I do not want the system to automatically encapsulate the source IP address for me, such as obviously when using OpenVPN. So what do we do? In the "OpenVPN client's Source address selection problem" http://blog.csdn.net/dog250/article/details/9823205, I've solved this problem through the bridge interface, In the case of no dual NIC or multiple network card, I mean in the single card (more in line with the common situation), arbitrary selection of the source IP address will be a more general problem. But how to do it? In fact, it is not difficult.
First, let's look at what the so-called default gateway essentially means. The default gateway is actually a more specific "next hop", it is actually a next hop address, the purpose of this address is to throw the packet to the next jump (nonsense), in fact, it is only an auxiliary role, the real effect is the MAC address that it resolves to, because the MAC address will be really encapsulated in the Ethernet frame, The default gateway is only used to get this MAC address, so if you can specify a MAC address statically, the default gateway can be arbitrary. So there was a way:
1. Generate a false default gateway for a route and the source IP address to be selected on the same network segment, i.e. false next hop;
2. For the false gateway IP address to establish a static to the true gateway of the MAC address ARP mapping;
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Network/lyjs/
As a result, the selection process of the source IP address will fully conform to the choice logic of windows, and we will bypass its limitations from the outside of the kernel, so that we can select any address configured on the local connection as the source IP address.
In order to implement the manual configuration process into an automatic process, programming is essential, but I do not want to invoke that complex API (to achieve a very simple function have to do a lot of preparation ...), can only hope that the script, but the Windows command line function is too weak, very difficult. Ask colleagues, Baidu, Google, over and over again to try, toss for too long, finally write a batch script, found that Windows XP script function is still very powerful, not to mention the power shell. The script is as follows (no echo off):
Set destination=%1
set mask=%2
set origw=%3
set source=%4
:: Get default gateway (default gateway can be found automatically, too loud, not advocated)
::: GETGW
:: Set origw=
:: for/f "delims=: tokens=2"%%i in (' "ipconfig | find/i "Default gateway" | Findstr [0-9]. "') Do set origw=%%i
:: Gets the MAC address of the default gateway
: Getmac
set mac=
for/f "skip=3 tokens=2"%%i in (' Arp-a%origw% ') does Set mac=%%i
echo%mac%|findstr \-
:: Here's the problem, the fact that Ping fails (such as a gateway ban ping) doesn't matter, all we need is its MAC address
:: If the MAC address is not allowed to resolve, then do not be the default gateway. So the right approach should be to get a counter, continuous
:: two times to get Mac failure even if it fails.
if errorlevel 1 (
ping%origw%-N 1
if errorlevel 1 goto end
goto getmac
)
set gw=
set i=
:: Generate fake gateway address (very simple algorithm, want to use the source IP address minus 1)
for/f "delims=. Tokens=4 "%%i in (the ' echo%source% ') do set/a host=%%i-1
set i=
set j=
set k=
for/f" delims=. tokens=1,2,3 "%%i in (' echo%source% ') do set gw=%%i.%%j.%%k.%host%
:: Set the arp map of the fake gateway Arp-s%gw%%mac%
:: Add route
route add%destination% mask%mask%%gw%
: End
Saving the above script as "Route2.bat" can be used directly.
Actually, select and default gateway does not belong to the same network segment of the IP as the source IP address for the host is not a general practice, if so, when the packet returned, the original choice of the source IP address as the destination address, the default gateway in the network segment when routing problems, The packet may be discarded because the destination address is unreachable, unless it points to the same network segment address of the host on which the IP address resides.
In addition, the above script in the static establishment of ARP mapping when there is a problem, that is not and Windows "dead Gateway detection" function, so that the gateway can be notofy after the failure, or the gateway did not fail, just changed the machine, changed the Mac, Now you need to update that Mac map.