NetAddress.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "OS.h"
  26. namespace crown
  27. {
  28. namespace os
  29. {
  30. /// OS level network address.
  31. class NetAddress
  32. {
  33. public:
  34. /// Initializes the address to 127.0.0.1 and port to 1000.
  35. NetAddress();
  36. /// Initializes the address from IP address (as single elemets)
  37. /// and the port number.
  38. NetAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port);
  39. /// Returns the IP address as packed 32-bit integer.
  40. uint32_t address() const;
  41. /// Returns the port number bind to the address.
  42. uint16_t port() const;
  43. /// Sets both the IP address (packed 32-bit integer) and the port number.
  44. void set(uint32_t address, uint16_t port);
  45. /// Sets both the IP address (as single elements) and the port number.
  46. void set(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port);
  47. bool operator==(const NetAddress& addr) const;
  48. NetAddress& operator=(const NetAddress& addr);
  49. void print() const;
  50. public:
  51. uint8_t m_address[4];
  52. uint16_t m_port;
  53. };
  54. //-----------------------------------------------------------------------------
  55. inline NetAddress::NetAddress() :
  56. m_port(1000)
  57. {
  58. m_address[0] = 127;
  59. m_address[1] = 0;
  60. m_address[2] = 0;
  61. m_address[3] = 1;
  62. }
  63. //-----------------------------------------------------------------------------
  64. inline NetAddress::NetAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port) :
  65. m_port(port)
  66. {
  67. m_address[0] = a;
  68. m_address[1] = b;
  69. m_address[2] = c;
  70. m_address[3] = d;
  71. }
  72. //-----------------------------------------------------------------------------
  73. inline uint32_t NetAddress::address() const
  74. {
  75. uint32_t addr = (m_address[0] << 24) | (m_address[1] << 16) | (m_address[2] << 8) | (m_address[3]);
  76. return addr;
  77. }
  78. //-----------------------------------------------------------------------------
  79. inline uint16_t NetAddress::port() const
  80. {
  81. return m_port;
  82. }
  83. //-----------------------------------------------------------------------------
  84. inline void NetAddress::set(uint32_t address, uint16_t port)
  85. {
  86. m_address[0] = address >> 24;
  87. m_address[1] = address >> 16;
  88. m_address[2] = address >> 8;
  89. m_address[3] = address;
  90. m_port = port;
  91. }
  92. //-----------------------------------------------------------------------------
  93. inline void NetAddress::set(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port)
  94. {
  95. m_address[0] = a;
  96. m_address[1] = b;
  97. m_address[2] = c;
  98. m_address[3] = d;
  99. m_port = port;
  100. }
  101. //-----------------------------------------------------------------------------
  102. inline bool NetAddress::operator==(const NetAddress& addr) const
  103. {
  104. return m_address[0] == addr.m_address[0] &&
  105. m_address[1] == addr.m_address[1] &&
  106. m_address[2] == addr.m_address[2] &&
  107. m_address[3] == addr.m_address[3] &&
  108. m_port == addr.m_port;
  109. }
  110. //-----------------------------------------------------------------------------
  111. inline NetAddress& NetAddress::operator=(const NetAddress& addr)
  112. {
  113. m_address[0] = addr.m_address[0];
  114. m_address[1] = addr.m_address[1];
  115. m_address[2] = addr.m_address[2];
  116. m_address[3] = addr.m_address[3];
  117. m_port = addr.m_port;
  118. return *this;
  119. }
  120. //-----------------------------------------------------------------------------
  121. inline void NetAddress::print() const
  122. {
  123. os::printf("NetAddress: %i.%i.%i.%i:%i\n", m_address[0], m_address[1], m_address[2], m_address[3], m_port);
  124. }
  125. } // namespace os
  126. } // namespace crown