upnp_control.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2004-2017 Savoir-faire Linux Inc.
  3. *
  4. * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "upnp_control.h"
  24. #include <memory>
  25. #include "logger.h"
  26. #include "ip_utils.h"
  27. #include "upnp_context.h"
  28. #include "upnp_igd.h"
  29. namespace ring { namespace upnp {
  30. Controller::Controller()
  31. {
  32. try {
  33. upnpContext_ = getUPnPContext();
  34. } catch (std::runtime_error& e) {
  35. RING_ERR("UPnP context error: %s", e.what());
  36. }
  37. }
  38. Controller::~Controller()
  39. {
  40. /* remove all mappings */
  41. removeMappings();
  42. if (listToken_ and upnpContext_)
  43. upnpContext_->removeIGDListener(listToken_);
  44. }
  45. bool
  46. Controller::hasValidIGD(std::chrono::seconds timeout)
  47. {
  48. return upnpContext_ and upnpContext_->hasValidIGD(timeout);
  49. }
  50. void
  51. Controller::setIGDListener(IGDFoundCallback&& cb)
  52. {
  53. if (not upnpContext_)
  54. return;
  55. if (listToken_)
  56. upnpContext_->removeIGDListener(listToken_);
  57. listToken_ = cb ? upnpContext_->addIGDListener(std::move(cb)) : 0;
  58. }
  59. bool
  60. Controller::addAnyMapping(uint16_t port_desired,
  61. uint16_t port_local,
  62. PortType type,
  63. bool use_same_port,
  64. bool unique,
  65. uint16_t *port_used)
  66. {
  67. if (not upnpContext_)
  68. return false;
  69. Mapping mapping = upnpContext_->addAnyMapping(port_desired, port_local, type,
  70. use_same_port, unique);
  71. if (mapping) {
  72. auto usedPort = mapping.getPortExternal();
  73. if (port_used)
  74. *port_used = usedPort;
  75. /* add to map */
  76. auto& instanceMappings = type == PortType::UDP ? udpMappings_ : tcpMappings_;
  77. instanceMappings.emplace(usedPort, std::move(mapping));
  78. return true;
  79. }
  80. return false;
  81. }
  82. bool
  83. Controller::addAnyMapping(uint16_t port_desired,
  84. PortType type,
  85. bool unique,
  86. uint16_t *port_used)
  87. {
  88. return addAnyMapping(port_desired, port_desired, type, true, unique,
  89. port_used);
  90. }
  91. void
  92. Controller::removeMappings(PortType type) {
  93. if (not upnpContext_)
  94. return;
  95. auto& instanceMappings = type == PortType::UDP ? udpMappings_ : tcpMappings_;
  96. for (auto iter = instanceMappings.begin(); iter != instanceMappings.end(); ){
  97. auto& mapping = iter->second;
  98. upnpContext_->removeMapping(mapping);
  99. iter = instanceMappings.erase(iter);
  100. }
  101. }
  102. void
  103. Controller::removeMappings()
  104. {
  105. removeMappings(PortType::UDP);
  106. removeMappings(PortType::TCP);
  107. }
  108. IpAddr
  109. Controller::getLocalIP() const
  110. {
  111. if (upnpContext_)
  112. return upnpContext_->getLocalIP();
  113. return {}; // empty address
  114. }
  115. IpAddr
  116. Controller::getExternalIP() const
  117. {
  118. if (upnpContext_)
  119. return upnpContext_->getExternalIP();
  120. return {}; // empty address
  121. }
  122. }} // namespace ring::upnp