upnp_control.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef UPNP_H_
  21. #define UPNP_H_
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <memory>
  26. #include <chrono>
  27. #include "noncopyable.h"
  28. #include "upnp_igd.h"
  29. namespace ring {
  30. class IpAddr;
  31. }
  32. namespace ring { namespace upnp {
  33. class UPnPContext;
  34. class Controller {
  35. public:
  36. /* constructor */
  37. Controller();
  38. /* destructor */
  39. ~Controller();
  40. /**
  41. * Return whether or not this controller has a valid IGD.
  42. * @param timeout Time to wait until a valid IGD is found.
  43. * If timeout is not given or 0, the function pool (non-blocking).
  44. */
  45. bool hasValidIGD(std::chrono::seconds timeout = {});
  46. /**
  47. * Set or clear a listener for valid IGDs.
  48. * For simplicity there is one listener per controller.
  49. */
  50. void setIGDListener(IGDFoundCallback&& cb = {});
  51. /**
  52. * tries to add mapping from and to the port_desired
  53. * if unique == true, makes sure the client is not using this port already
  54. * if the mapping fails, tries other available ports until success
  55. *
  56. * tries to use a random port between 1024 < > 65535 if desired port fails
  57. *
  58. * maps port_desired to port_local; if use_same_port == true, makes sure that
  59. * that the extranl and internal ports are the same
  60. */
  61. bool addAnyMapping(uint16_t port_desired,
  62. uint16_t port_local,
  63. PortType type,
  64. bool use_same_port,
  65. bool unique,
  66. uint16_t *port_used);
  67. /**
  68. * addAnyMapping with the local port being the same as the external port
  69. */
  70. bool addAnyMapping(uint16_t port_desired,
  71. PortType type,
  72. bool unique,
  73. uint16_t *port_used);
  74. /**
  75. * removes all mappings added by this instance
  76. */
  77. void removeMappings();
  78. /**
  79. * tries to get the external ip of the IGD (router)
  80. */
  81. IpAddr getExternalIP() const;
  82. /**
  83. * tries to get the local ip of the IGD (router)
  84. */
  85. IpAddr getLocalIP() const;
  86. private:
  87. /**
  88. * All UPnP commands require an initialized upnpContext
  89. */
  90. std::shared_ptr<UPnPContext> upnpContext_;
  91. /**
  92. * list of mappings created by this instance
  93. * the key is the external port number, as there can only be one mapping
  94. * at a time for each external port
  95. */
  96. PortMapLocal udpMappings_;
  97. PortMapLocal tcpMappings_;
  98. /**
  99. * IGD listener token
  100. */
  101. size_t listToken_ {0};
  102. /**
  103. * Try to remove all mappings of the given type
  104. */
  105. void removeMappings(PortType type);
  106. };
  107. }} // namespace ring::upnp
  108. #endif /* UPNP_H_ */