I have a TCP server that uses Boost ASIO. I noticed that when using link-bound IPv6 addresses on Linux, I cannot create boost::asio::ip::tcp::acceptorexceptions without exception. Using a global IPv6 address or an IPv4 address will work fine.
I am sure that the problem is that the area identifier is not set correctly, but I cannot figure out how to fix this problem.
I am developing on Ubuntu 11.04 LTS using the boost 1.40.0 library provided by ubuntu. Here's a very dumb version of the server code that I have shows the problem:
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <string>
typedef boost::shared_ptr<boost::asio::ip::tcp::socket> TcpSocketPtr;
typedef boost::shared_ptr<boost::asio::ip::tcp::acceptor> TcpAcceptorPtr;
typedef boost::shared_ptr<boost::asio::ip::tcp::endpoint> TcpEndpointPtr;
class AsioServer{
public:
AsioServer(boost::asio::io_service& io): io_(io){};
void accept(const std::string& ipString,unsigned short port){
boost::asio::ip::address addr = boost::asio::ip::address::from_string(ipString);
std::cout << "Valid IP address " << ipString << std::endl;
this->endpoint_.reset(new boost::asio::ip::tcp::endpoint(addr,port));
std::cout << "Created endpoint" << std::endl;
acceptor_.reset(new boost::asio::ip::tcp::acceptor(this->io_,*(this->endpoint_)));
std::cout << "About to accept on " << *(this->endpoint_) << std::endl;
this->socket_.reset(new boost::asio::ip::tcp::socket(this->io_));
this->acceptor_->async_accept(*socket_ ,boost::bind(&AsioServer::handle_accept,this,boost::asio::placeholders::error));
}
private:
boost::asio::io_service& io_;
TcpSocketPtr socket_;
TcpAcceptorPtr acceptor_;
TcpEndpointPtr endpoint_;
void handle_accept(const boost::system::error_code &ec){
if(!ec){
std::cout << "Accepted connection!" << std::endl;
}
else{
std::cout << "Error accepting connection" << std::endl;
}
};
};
int main(int argc, char* argv[]){
boost::asio::io_service io;
std::string ipString("0.0.0.0");
if(argc > 1){
ipString.assign(argv[1]);
}
std::cout << "IP Set to " << ipString << std::endl;
AsioServer server(io);
try{
server.accept(ipString,4444);
}
catch(const std::exception& e){
std::cout << "Error caught: " << e.what() << std::endl;
}
io.run();
std::cout << "Done!" << std::endl;
return 0;
}
On this machine, the following is configured for eth0:
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0c:29:10:cf:0e
inet addr:192.168.97.162 Bcast:192.168.97.255 Mask:255.255.255.0
inet6 addr: 2620:1c:8000:190:20c:29ff:fe10:cf0e/64 Scope:Global
inet6 addr: fe80::20c:29ff:fe10:cf0e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:112470185 errors:2 dropped:1 overruns:0 frame:0
TX packets:5900249 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2187237130 (2.1 GB) TX bytes:1640094885 (1.6 GB)
Interrupt:19 Base address:0x2000
192.168.97.162, 0.0.0.0, 127.0.0.1, :: 1 2620: 1c: 8000: 190: 20c: 29ff: fe10: cf0e , fe80:: 20c: 29ff: fe10: cf0e "Invalid Argument".
$ ./asio-ipv6 fe80::20c:29ff:fe10:cf0e
IP Set to fe80::20c:29ff:fe10:cf0e
Valid IP address fe80::20c:29ff:fe10:cf0e
Created endpoint
Error caught: Invalid argument
Done!
ping6 " ". , , IPv6.
$ ping6 fe80::219:b9ff:fe2b:3a53
connect: Invalid argument
$ ping6 fe80::219:b9ff:fe2b:3a53%eth0
PING fe80::219:b9ff:fe2b:3a53%eth0(fe80::219:b9ff:fe2b:3a53) 56 data bytes
Boost Asio, IP- .
$ ./asio-ipv6 fe80::20c:29ff:fe10:cf0e%eth0
IP Set to fe80::20c:29ff:fe10:cf0e%eth0
Error caught: Invalid argument
Done!
: IPv6 , Boost ASIO? boost::asio::ip::address_v6 scope_id(), , unsigned long .