我有一个使用Boost ASIO的TCP服务器。我注意到,在Linux上使用链路范围的IPv6地址时,无法创建
我相信问题出在作用域ID没有正确设置,但我无法找出如何解决这个问题。
我正在使用Ubuntu 11.04 LTS开发,并使用Ubuntu提供的Boost 1.40.0库。以下是示例代码,演示了该问题。
在这台机器上,我已经为eth0配置了以下内容:
这让我想起了使用ping6时看到相同的“无效参数”错误。解决方法是将接口附加到IPv6地址后传递。
这似乎无法在使用Boost Asio时从字符串创建IP地址。
boost ::asio ::ip ::tcp ::acceptor
而会抛出异常。使用全局IPv6地址或IPv4地址则可以正常工作。我相信问题出在作用域ID没有正确设置,但我无法找出如何解决这个问题。
我正在使用Ubuntu 11.04 LTS开发,并使用Ubuntu提供的Boost 1.40.0库。以下是示例代码,演示了该问题。
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <string>
/* To Compile:
g++ -Wall -o ./asio-ipv6 ./asio-ipv6.cpp -lboost_system
*/
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){};
//throws
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;
//Will throw if a link local IPv6 address is used
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;
}
在这台机器上,我已经为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会导致创建一个接收器时抛出"无效参数"异常。
$ ./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 #Won't work
connect: Invalid argument
$ ping6 fe80::219:b9ff:fe2b:3a53%eth0 #Pass the interface to use to ping6
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!
我的问题是如何使用Boost ASIO侦听链接作用域的IPv6地址?boost::asio::ip::address_v6
类具有scope_id()成员函数,但我不确定如何将作用域ID作为无符号长整型获取,或者这是否是问题所在。