portconfig.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef ISC_SERVER_COMMON_PORTCONFIG_H
  15. #define ISC_SERVER_COMMON_PORTCONFIG_H
  16. #include <utility>
  17. #include <string>
  18. #include <stdint.h>
  19. #include <vector>
  20. #include <cc/data.h>
  21. /*
  22. * Some forward declarations.
  23. */
  24. namespace asiolink {
  25. class DNSService;
  26. }
  27. namespace isc {
  28. namespace server_common {
  29. /**
  30. * \brief Utilities to configure ports and addresses.
  31. *
  32. * Here are some utilities to help a server to parse configuration of addresses
  33. * and ports and install the configuration.
  34. */
  35. namespace portconfig {
  36. /**
  37. * \brief An address-port pair.
  38. *
  39. * It is just a pair of string for an address and unsigned integer for port
  40. * number. Anything more fancy would be an overkill, it is used only to pass
  41. * the addresses and ports around as intermediate results.
  42. */
  43. typedef std::pair<std::string, uint16_t> AddressPair;
  44. /// \brief Bunch of address pairs
  45. typedef std::vector<AddressPair> AddressList;
  46. /**
  47. * \brief
  48. *
  49. * This parses a list of address-port configurations and returns them. The
  50. * configuration looks like this:
  51. *
  52. * \verbatim
  53. [
  54. {
  55. "address": "192.0.2.1",
  56. "port": 13
  57. },
  58. {
  59. "address": "::",
  60. "port": 80
  61. }
  62. ]
  63. * \endverbatim
  64. * \param addresses The configuration element to parse (the list). Empty list,
  65. * null element and null pointer all mean empty list of addresses.
  66. * \param elemName The name of the element, used to create descriptions for
  67. * exceptions.
  68. * \return Vector of parsed address-port pairs found in the configuration.
  69. * \throw isc::data::TypeError if something in the configuration is of a wrong
  70. * type (string passed to a port, element in the list that isn't hash,
  71. * etc).
  72. * \throw asiolink::IOError if the provided address string can't be parsed.
  73. * \throw BadValue for other invalid configurations (missing port or address
  74. * element in the hash, port number out of range).
  75. * \throw std::bad_alloc when allocation fails.
  76. */
  77. AddressList
  78. parseAddresses(isc::data::ConstElementPtr addresses,
  79. const std::string& elemName);
  80. /**
  81. * \brief Changes current listening addresses and ports.
  82. *
  83. * Removes all sockets we currently listen on and starts listening on the
  84. * addresses and ports requested in newAddresses.
  85. *
  86. * If it fails to set up the new addresses, it attempts to roll back to the
  87. * previous addresses (but it still propagates the exception). If the rollback
  88. * fails as well, it aborts the application (it assumes if it can't listen
  89. * on the new addresses nor on the old ones, the application is useless anyway
  90. * and should be restarted by Boss, not to mention that the internal state is
  91. * probably broken).
  92. *
  93. * \param newAddresses are the addresses you want to listen on.
  94. * \param addressStore is the place you store your current addresses. It is
  95. * used when there's a need for rollback. The newAddresses are copied here
  96. * when the change is successful.
  97. * \param dnsService is the DNSService object we use now. The requests from
  98. * the new sockets are handled using this dnsService (and all current
  99. * sockets on the service are closed first).
  100. * \throw asiolink::IOError when initialization or closing of socket fails.
  101. * \throw std::bad_alloc when allocation fails.
  102. */
  103. void
  104. installListenAddresses(const AddressList& newAddresses,
  105. AddressList& addressStore,
  106. asiolink::DNSService& dnsService);
  107. }
  108. }
  109. }
  110. #endif