C++追踪器NAT-PMP操作及其他解决方案

huangapple go评论52阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月17日 21:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定