Skip to content
This repository
Browse code

Merge pull request #1822 from sipa/fix_ipv6_rpc

Do not abort if RPC listening for IPv6 fails
  • Loading branch information...
commit b3295cd283007a619e75f3d6e00bcc5a7a5efed1 2 parents d078739 + c1d7981
Gavin Andresen gavinandresen authored

Showing 1 changed file with 20 additions and 5 deletions. Show diff stats Hide diff stats

  1. +20 5 src/bitcoinrpc.cpp
25 src/bitcoinrpc.cpp
@@ -761,17 +761,19 @@ void ThreadRPCServer2(void* parg)
761 761 const bool loopback = !mapArgs.count("-rpcallowip");
762 762 asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any();
763 763 ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", 8332));
  764 + boost::system::error_code v6_only_error;
  765 + boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(io_service));
764 766
765 767 boost::signals2::signal<void ()> StopRequests;
766 768
  769 + bool fListening = false;
  770 + std::string strerr;
767 771 try
768 772 {
769   - boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(io_service));
770 773 acceptor->open(endpoint.protocol());
771 774 acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
772 775
773 776 // Try making the socket dual IPv6/IPv4 (if listening on the "any" address)
774   - boost::system::error_code v6_only_error;
775 777 acceptor->set_option(boost::asio::ip::v6_only(loopback), v6_only_error);
776 778
777 779 acceptor->bind(endpoint);
@@ -783,8 +785,16 @@ void ThreadRPCServer2(void* parg)
783 785 static_cast<void (ip::tcp::acceptor::*)()>(&ip::tcp::acceptor::close), acceptor.get())
784 786 .track(acceptor));
785 787
  788 + fListening = true;
  789 + }
  790 + catch(boost::system::system_error &e)
  791 + {
  792 + strerr = strprintf(_("An error occurred while setting up the RPC port %i for listening on IPv6, falling back to IPv4: %s"), endpoint.port(), e.what());
  793 + }
  794 +
  795 + try {
786 796 // If dual IPv6/IPv4 failed (or we're opening loopback interfaces only), open IPv4 separately
787   - if (loopback || v6_only_error)
  797 + if (!fListening || loopback || v6_only_error)
788 798 {
789 799 bindAddress = loopback ? asio::ip::address_v4::loopback() : asio::ip::address_v4::any();
790 800 endpoint.address(bindAddress);
@@ -800,12 +810,17 @@ void ThreadRPCServer2(void* parg)
800 810 StopRequests.connect(signals2::slot<void ()>(
801 811 static_cast<void (ip::tcp::acceptor::*)()>(&ip::tcp::acceptor::close), acceptor.get())
802 812 .track(acceptor));
  813 +
  814 + fListening = true;
803 815 }
804 816 }
805 817 catch(boost::system::system_error &e)
806 818 {
807   - uiInterface.ThreadSafeMessageBox(strprintf(_("An error occurred while setting up the RPC port %i for listening: %s"), endpoint.port(), e.what()),
808   - _("Error"), CClientUIInterface::OK | CClientUIInterface::MODAL);
  819 + strerr = strprintf(_("An error occurred while setting up the RPC port %i for listening on IPv4: %s"), endpoint.port(), e.what());
  820 + }
  821 +
  822 + if (!fListening) {
  823 + uiInterface.ThreadSafeMessageBox(strerr, _("Error"), CClientUIInterface::OK | CClientUIInterface::MODAL);
809 824 StartShutdown();
810 825 return;
811 826 }

0 comments on commit b3295cd

Please sign in to comment.
Something went wrong with that request. Please try again.