address.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef _WIN32_WINNT
  19. #define _WIN32_WINNT 0x0501
  20. #endif
  21. #include <windows.h>
  22. #include <winsock2.h>
  23. #include <ws2tcpip.h>
  24. #else
  25. #include <netinet/in.h>
  26. #include <sys/socket.h>
  27. #endif
  28. #include <ostream>
  29. #include <cstring>
  30. #include "irrlichttypes.h"
  31. #include "networkexceptions.h"
  32. class IPv6AddressBytes
  33. {
  34. public:
  35. u8 bytes[16];
  36. IPv6AddressBytes() { memset(bytes, 0, 16); }
  37. };
  38. class Address
  39. {
  40. public:
  41. Address();
  42. Address(u32 address, u16 port);
  43. Address(u8 a, u8 b, u8 c, u8 d, u16 port);
  44. Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
  45. bool operator==(const Address &address);
  46. bool operator!=(const Address &address);
  47. // Resolve() may throw ResolveError (address is unchanged in this case)
  48. void Resolve(const char *name);
  49. struct sockaddr_in getAddress() const;
  50. unsigned short getPort() const;
  51. void setAddress(u32 address);
  52. void setAddress(u8 a, u8 b, u8 c, u8 d);
  53. void setAddress(const IPv6AddressBytes *ipv6_bytes);
  54. struct sockaddr_in6 getAddress6() const;
  55. int getFamily() const;
  56. bool isIPv6() const;
  57. bool isZero() const;
  58. void setPort(unsigned short port);
  59. void print(std::ostream *s) const;
  60. std::string serializeString() const;
  61. private:
  62. unsigned int m_addr_family = 0;
  63. union
  64. {
  65. struct sockaddr_in ipv4;
  66. struct sockaddr_in6 ipv6;
  67. } m_address;
  68. u16 m_port = 0; // Port is separate from sockaddr structures
  69. };