Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS boost asio connect from ipv6 network

I am trying to connect a dvr using boost asio library in ios. The application works fine in emulator with in ipv4 network. But when I submit the application on Appstore apple rejected the application as it's not work on ipv6 network. And I can see in the apple site that application should support ipv6 network. https://developer.apple.com/news/?id=05042016a

So I think the problem comes at the section where I am trying to connect to DVR using boost library, where the ip address of the DVR is pulled from DB(hard-coded) and below is the relevant part of the code.

        boost::asio::io_service io_service_;
        tcp::resolver::iterator endpoint_iter_;
        tcp::resolver resolver_; //to healp resolving hostname and ip

        stringstream strstream;//create a stringstream
        strstream << port;//add number to the stream

        endpoint_iter_ = resolver_.resolve(tcp::resolver::query(ip.c_str(),strstream.str()));
        start_connect(endpoint_iter_);

        // Start the deadline actor. You will note that we're not setting any
        // particular deadline here. Instead, the connect and input actors will
        // update the deadline prior to each asynchronous operation.
        deadline_timer_.async_wait(boost::bind(&dvr_obj::check_deadline, this));
        //starting thread for dvr connection
        io_service_.reset();
        thread_ =  new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));

start_connect method

void start_connect(tcp::resolver::iterator endpoint_iter)
{
    try
    {
        if (endpoint_iter != tcp::resolver::iterator())
        {

            drill_debug_info("trying to connect %s \n",name.c_str());

            // Set a deadline for the connect operation.
            deadline_timer_.expires_from_now(boost::posix_time::seconds(10));

            // Start the asynchronous connect operation.
            socket_.async_connect(endpoint_iter->endpoint(),
                                  boost::bind(&dvr_obj::handle_connect,
                                              this, _1, endpoint_iter));
        }
        else
        {
            // There are no more endpoints to try. Shut down the client.

            connectivity = false;
        }
    } catch (int e) {

        connectivity = false;
    }
}

So I am confused how to change above code to work on IPV6 network. Could not find any solution in Internet.

like image 218
CodeDezk Avatar asked Sep 05 '16 07:09

CodeDezk


1 Answers

You can iterate through the endpoints to find an IPv6 endpoint using the code below:

endpoint_iter_ = resolver_.resolve(tcp::resolver::query(ip.c_str(),strstream.str()));

while (endpoint_iter_ != tcp::resolver::iterator())
{
  if (endpoint_iter_->endpoint().protocol() == tcp::v6())
    break;
  ++endpoint_iter_;
}

if (endpoint_iter_ != tcp::resolver::iterator())
{
   start_connect(endpoint_iter_);
   ...
}
else
   std::cerr << "IPv6 host not found" << std::endl;
like image 128
kenba Avatar answered Oct 13 '22 17:10

kenba