address.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #ifdef _WIN32
  18. #include <windows.h>
  19. #include <winsock2.h>
  20. #include <ws2tcpip.h>
  21. #else
  22. #include <netinet/in.h>
  23. #include <sys/socket.h>
  24. #endif
  25. #include <ostream>
  26. #include <cstring>
  27. #include "irrlichttypes.h"
  28. #include "networkexceptions.h"
  29. struct IPv6AddressBytes
  30. {
  31. u8 bytes[16];
  32. IPv6AddressBytes() { memset(bytes, 0, 16); }
  33. };
  34. class Address
  35. {
  36. public:
  37. Address();
  38. Address(u32 address, u16 port);
  39. Address(u8 a, u8 b, u8 c, u8 d, u16 port);
  40. Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
  41. bool operator==(const Address &address) const;
  42. bool operator!=(const Address &address) const { return !(*this == address); }
  43. int getFamily() const { return m_addr_family; }
  44. bool isValid() const { return m_addr_family != 0; }
  45. bool isIPv6() const { return m_addr_family == AF_INET6; }
  46. struct in_addr getAddress() const { return m_address.ipv4; }
  47. struct in6_addr getAddress6() const { return m_address.ipv6; }
  48. u16 getPort() const { return m_port; }
  49. void print(std::ostream &s) const;
  50. std::string serializeString() const;
  51. // Is this an address that binds to all interfaces (like INADDR_ANY)?
  52. bool isAny() const;
  53. // Is this an address referring to the local host?
  54. bool isLocalhost() const;
  55. // `name`: hostname or numeric IP
  56. // `fallback`: fallback IP to try gets written here
  57. // any empty name resets the IP to the "any address"
  58. // may throw ResolveError (address is unchanged in this case)
  59. void Resolve(const char *name, Address *fallback = nullptr);
  60. void setAddress(u32 address);
  61. void setAddress(u8 a, u8 b, u8 c, u8 d);
  62. void setAddress(const IPv6AddressBytes *ipv6_bytes);
  63. void setPort(u16 port);
  64. private:
  65. unsigned short m_addr_family = 0;
  66. union
  67. {
  68. struct in_addr ipv4;
  69. struct in6_addr ipv6;
  70. } m_address;
  71. // port is separate from in_addr structures
  72. u16 m_port = 0;
  73. };