11

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.

CodeDezk
  • 1,230
  • 1
  • 12
  • 40
  • 1
    `resolver_.resolve()` returns an iterator to a list of entries. Your code just uses the first entry.You may find an IPv6 entry if you iterate through the list... – kenba Sep 05 '16 at 08:58
  • Thanks for the response, can you give me some reference code. – CodeDezk Sep 06 '16 at 12:48

1 Answers1

4

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;
kenba
  • 4,303
  • 1
  • 23
  • 40
  • Thanks, I will try your answer and let you know the result. – CodeDezk Sep 06 '16 at 15:09
  • `if (endpoint_iter_->protocol() == tcp::v6())` giving me error no member named.. – CodeDezk Sep 06 '16 at 17:51
  • I replaced above with `boost::asio::ip::address addr = endpoint_iter_->endpoint().address(); if(addr.is_v6()) break; ++endpoint_iter_;` – CodeDezk Sep 06 '16 at 17:58
  • Also the application should work on ipv6/ipv4 network. – CodeDezk Sep 06 '16 at 18:11
  • I'm glad that you manged to fix the example I gave you. However, if there's an IPv6 endpoint available it should find it. Have you tried using [wireshark](https://www.wireshark.org/) to see the network traffic? – kenba Sep 06 '16 at 20:38
  • @kenba Thanks for the feedback, it really help me, my situation is like the server I am trying to connect is with ipv4 address like `162.250.197.92` I need to connect to this server from both ipv6 and ipv4 network using my application. – Haris Sep 07 '16 at 05:32
  • @CodeDeck FYI I've fixed the example to compile cleanly. – kenba Sep 14 '16 at 05:17