英文:
C++ Tracker NAT-PMP operation and other solutions
问题
I created a torrent tracker as a hobby. The question would be how to solve the seed without manual port forward?
In general, everyone writes NAT-PMP and UPNP solutions. But upnp is not a very supported solution, and I'm referring to this from the fact that it never finds anything when it searches for a device. It doesn't want to work with NAT-PMP either. My other question is, what do they usually use?
I tried the following solution. But I couldn't get it to work. Here I don't want to focus on the NAT-PMP solution, but on the solution that works.
NAT-PMP solution:
void nat_pmp_request(boost::asio::io_service& io_service, const std::string& gateway, unsigned short int local_port, unsigned short int external_port) {
udp::socket socket(io_service, udp::v4());
udp::endpoint endpoint(boost::asio::ip::address::from_string(gateway), 5351); // NAT-PMP Gateway IP and port
unsigned char request[] = {
0, 0, 0, 2, // NAT-PMP version (2)
0, 0, 0, 0, // Reserved
0, 1, // NAT-PMP opcode (port mapping request)
0, 0, // Reserved
(external_port >> 8) & 0xff, external_port & 0xff, // Desired external port in network byte order
(local_port >> 8) & 0xff, local_port & 0xff, // Local port in network byte order
0, 0, 0, 0, // Mapping lifetime (0 means the mapping lasts until it is explicitly removed)
0, 0, 0, 0 // Reserved
};
socket.send_to(boost::asio::buffer(request, sizeof(request)), endpoint);
}
# Main function
boost::asio::io_service io_service;
nat_pmp_request(io_service, "192.168.0.1", 49905, 49905);
io_service.run();
英文:
I created a torrent tracker as a hobby. The question would be how to solve the seed without manual port forward?
In general, everyone writes NAT-PMP and UPNP solutions. But upnp is not a very supported solution, and I'm referring to this from the fact that it never finds anything when it searches for a device.
It doesn't want to work with NAT-PMP either.
My other question is, what do they usually use?
I tried the following solution.
But I couldn't get it to work. Here I don't want to focus on the NAT-PMP solution, but on the solution that works.
NAT-PMP solution:
void nat_pmp_request(boost::asio::io_service& io_service, const std::string& gateway, unsigned short int local_port, unsigned short int external_port) {
udp::socket socket(io_service, udp::v4());
udp::endpoint endpoint(boost::asio::ip::address::from_string(gateway), 5351); // NAT-PMP Gateway IP and port
unsigned char request[] = {
0, 0, 0, 2, // NAT-PMP version (2)
0, 0, 0, 0, // Reserved
0, 1, // NAT-PMP opcode (port mapping request)
0, 0, // Reserved
(external_port >> 8) & 0xff, external_port & 0xff, // Desired external port in network byte order
(local_port >> 8) & 0xff, local_port & 0xff, // Local port in network byte order
0, 0, 0, 0, // Mapping lifetime (0 means the mapping lasts until it is explicitly removed)
0, 0, 0, 0 // Reserved
};
socket.send_to(boost::asio::buffer(request, sizeof(request)), endpoint);
}
# Main fuction
boost::asio::io_service io_service;
nat_pmp_request(io_service, "192.168.0.1", 49905, 49905);
io_service.run();
答案1
得分: 1
The request raw buffer seems to have too much bytes (reserved ones).
Take a look here: http://miniupnp.free.fr/nat-pmp.html
英文:
The request raw buffer seems to have too much bytes (reserved ones).
Take a look here: http://miniupnp.free.fr/nat-pmp.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论