EndPoint.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file EndPoint.h
  13. @brief The class \ref kNet::EndPoint Endpoint. Represents an endpoint of a network connection. */
  14. #if defined(KNET_UNIX) || defined(ANDROID)
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #endif
  19. #include <cstring>
  20. #include <cstdio>
  21. #include <string>
  22. namespace kNet
  23. {
  24. /// EndPoint represents a (ip, port) source or destination of a socket.
  25. struct EndPoint
  26. {
  27. /// The IPv4 address of the endpoint, stored in big-endian. ///\todo Not IPv6-capable.
  28. unsigned char ip[4];
  29. unsigned short port;
  30. EndPoint()
  31. {
  32. Reset();
  33. }
  34. /// Clears all fields of this structure to zero.
  35. void Reset()
  36. {
  37. memset(this, 0, sizeof(EndPoint));
  38. }
  39. ///\todo Not IPv6-capable.
  40. bool operator < (const EndPoint &rhs) const
  41. {
  42. if (ip[0] < rhs.ip[0]) return true;
  43. if (ip[0] > rhs.ip[0]) return false;
  44. if (ip[1] < rhs.ip[1]) return true;
  45. if (ip[1] > rhs.ip[1]) return false;
  46. if (ip[2] < rhs.ip[2]) return true;
  47. if (ip[2] > rhs.ip[2]) return false;
  48. if (ip[3] < rhs.ip[3]) return true;
  49. if (ip[3] > rhs.ip[3]) return false;
  50. if (port < rhs.port) return true;
  51. if (port > rhs.port) return false;
  52. return false;
  53. }
  54. ///\todo Not IPv6-capable.
  55. static EndPoint FromSockAddrIn(const sockaddr_in &addr)
  56. {
  57. EndPoint endPoint;
  58. #ifdef WIN32
  59. endPoint.ip[0] = addr.sin_addr.S_un.S_un_b.s_b1;
  60. endPoint.ip[1] = addr.sin_addr.S_un.S_un_b.s_b2;
  61. endPoint.ip[2] = addr.sin_addr.S_un.S_un_b.s_b3;
  62. endPoint.ip[3] = addr.sin_addr.S_un.S_un_b.s_b4;
  63. #else
  64. endPoint.ip[0] = addr.sin_addr.s_addr & 0xFF;
  65. endPoint.ip[1] = (addr.sin_addr.s_addr >> 8) & 0xFF;
  66. endPoint.ip[2] = (addr.sin_addr.s_addr >> 16) & 0xFF;
  67. endPoint.ip[3] = (addr.sin_addr.s_addr >> 24) & 0xFF;
  68. #endif
  69. endPoint.port = ntohs(addr.sin_port);
  70. return endPoint;
  71. }
  72. ///\todo Not IPv6-capable.
  73. sockaddr_in ToSockAddrIn() const
  74. {
  75. using namespace std;
  76. sockaddr_in address;
  77. memset(&address, 0, sizeof(address));
  78. address.sin_family = AF_INET;
  79. address.sin_port = htons(port);
  80. #ifdef WIN32
  81. address.sin_addr.S_un.S_un_b.s_b1 = ip[0];
  82. address.sin_addr.S_un.S_un_b.s_b2 = ip[1];
  83. address.sin_addr.S_un.S_un_b.s_b3 = ip[2];
  84. address.sin_addr.S_un.S_un_b.s_b4 = ip[3];
  85. #else
  86. address.sin_addr.s_addr = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
  87. #endif
  88. return address;
  89. }
  90. ///\todo Not IPv6-capable.
  91. std::string IPToString() const
  92. {
  93. char str[256];
  94. sprintf(str, "%d.%d.%d.%d", (unsigned int)ip[0], (unsigned int)ip[1], (unsigned int)ip[2], (unsigned int)ip[3]);
  95. return std::string(str);
  96. }
  97. ///\todo Not IPv6-capable.
  98. std::string ToString() const
  99. {
  100. char str[256];
  101. sprintf(str, "%d.%d.%d.%d:%d", (unsigned int)ip[0], (unsigned int)ip[1], (unsigned int)ip[2], (unsigned int)ip[3], (unsigned int)port);
  102. return std::string(str);
  103. }
  104. };
  105. } // ~kNet