diff --git a/src/dns/bind/bind_resolver.cc b/src/dns/bind/bind_resolver.cc index ce6c1145ac8..fd5dcdd2ac3 100644 --- a/src/dns/bind/bind_resolver.cc +++ b/src/dns/bind/bind_resolver.cc @@ -46,7 +46,11 @@ BindResolver::BindResolver(boost::asio::io_service &io, sock_.open(boost::asio::ip::udp::v4(), ec); assert(ec.value() == 0); sock_.bind(local_ep, ec); - assert(ec.value() == 0); + if (ec.value() != 0) { + local_ep.port(0); + sock_.bind(local_ep, ec); + assert(ec.value() == 0); + } AsyncRead(); } diff --git a/src/dns/test/dns_bind_test.cc b/src/dns/test/dns_bind_test.cc index 7856aed1efa..82dcc62ee7e 100644 --- a/src/dns/test/dns_bind_test.cc +++ b/src/dns/test/dns_bind_test.cc @@ -1061,6 +1061,17 @@ TEST_F(DnsBindTest, DnsUpdateErrorParse) { int main(int argc, char **argv) { Dns::Init(); + + // Bind to the client port first so that a new client port is used + boost::system::error_code ec; + boost::asio::ip::udp::socket sock(*Dns::GetEventManager()->io_service()); + boost::asio::ip::udp::endpoint ep(boost::asio::ip::address:: + from_string("0.0.0.0", ec), + ContrailPorts::ContrailDnsClientUdpPort()); + sock.open(boost::asio::ip::udp::v4(), ec); + assert(ec.value() == 0); + sock.bind(ep, ec); + ::testing::InitGoogleTest(&argc, argv); int error = RUN_ALL_TESTS(); TaskScheduler::GetInstance()->Terminate(); diff --git a/src/vnsw/agent/oper/mirror_table.cc b/src/vnsw/agent/oper/mirror_table.cc index fd64aa551e4..9f276527147 100644 --- a/src/vnsw/agent/oper/mirror_table.cc +++ b/src/vnsw/agent/oper/mirror_table.cc @@ -303,7 +303,11 @@ void MirrorTable::MirrorSockInit(void) { assert(ec.value() == 0); udp_sock_->bind(ep, ec); - assert(ec.value() == 0); + if (ec.value() != 0) { + ep.port(0); + udp_sock_->bind(ep, ec); + assert(ec.value() == 0); + } ip::udp::endpoint sock_ep = udp_sock_->local_endpoint(ec); assert(ec.value() == 0);