6

OK. I know similar questions were asked before, but this is different.

I noticed from responses to similar questions that I can use Socket.bind to specify a certain network interface for outgoing connections. This page is an official instruction on that.

Now, my machine has two NICs eth0 and eth1, and the system routing table sets eth0 as the outgoing interface for connecting to a server S.

Then I tried the following:

Socket so = new Socket();
so.bind(new InetSocketAddress("ip.address.of.eth1", 0));
so.connect(new InetSocketAddress("ip.address.of.S", 80));

I used WireShark to capture the packets, and noticed that the "Source Address" field of IP header was indeed ip.address.of.eth1. But by checking Ethernet headers, I noticed that the source MAC address is actually the MAC address of eth0, that is, the packets were actually still sent out via eth0!

Could anyone help explain why it had this behavior? Is it expected? Thanks a lot!

leolong
  • 172
  • 1
  • 6

1 Answers1

5

This is the result of the 'weak end system model'. It's too broad to describe here but it is discussed in RFC 1122.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks a lot for the information. Considering the spec of weak end system model "A host MUST NOT restrict itself to sending (non-source-routed) IP datagrams only through the physical interface that corresponds to the IP source address of the datagrams", I guess there is no way to do it in Java then, right? – leolong Dec 15 '14 at 10:08
  • The *host* 'MUST NOT' do it at all. Nothing to do with Java whatsoever. – user207421 Dec 15 '14 at 10:20