Listening on multiple IP's

Aug 1, 2011 at 7:11 PM

Here's an odd one.  Couldn't figure out why my replacement server would not work when it said it was doing everything it should and worked on test.  Ran tcpdump.  Found the problem.

I'm listening on all addresses using

DnsServer(IPAddress.Any, 10, 10, ProcessQuery)

Server has 2 public IP's.  Problem is, queries come in one ip and out the other so are ignored by the client.

The old server does not do this.

Is there anything I can do to ensure replies are sent from the same IP they are recieved on?

I could fix and listen on 1 IP only, but I do actually want to listen on both IP's.



Aug 6, 2011 at 2:44 PM

Interesting point.

This is the way how the socket class of the framework works. When sending and receiving udp datarams on a socket bind to IPAddress.Any, where is no way to detect to which local address the datagram was sent. And when sending using this socket, the source address is selected by the system depending on the target of the datagram.

I will look forward to find some solution for that, for the moment, you could use two server instances, each listening on one single ip.


Aug 8, 2011 at 11:54 AM

Yes, I read up and see that .net automatically selects the 'best' interface to send out via.

I did consider the 2 server route, but in the end it was easier to NAT the other IP to the single IP using iptables.  Not something I could do on windows though.  In certain instances it would be good to also log which IP was being used.  If there were a way to make packets go out of the interface they came in on and perhaps also tell what interface the query came in on, that would be good.  It's not something that can't be worked around in most cases though I think.

Jul 6, 2012 at 9:58 PM

Did you by any chance find a solution for this ?

Jul 7, 2012 at 9:31 AM


Not natively, no.  What I did as I'm running under Linux is use IPtables to re-write the packets that some in via the other IP, so mark them inbound, re-write them with the original IP they were received on outbound.  The other way which I did not get to was to start 2 or more servers, 1 for each IP.  I'm doing that with IPv6 now, but find yet more issues.  Mono listens on ALL IP's including IPv4 if you start a listener on ipv6!  So, even more fudging required.  That's a mono issue though, not with this  code.