nsNetAddr.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* vim: et ts=2 sw=2 tw=80
  2. */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "nsNetAddr.h"
  7. #include "nsString.h"
  8. #include "mozilla/net/DNS.h"
  9. using namespace mozilla::net;
  10. NS_IMPL_ISUPPORTS(nsNetAddr, nsINetAddr)
  11. /* Makes a copy of |addr| */
  12. nsNetAddr::nsNetAddr(NetAddr* addr)
  13. {
  14. NS_ASSERTION(addr, "null addr");
  15. mAddr = *addr;
  16. }
  17. NS_IMETHODIMP nsNetAddr::GetFamily(uint16_t *aFamily)
  18. {
  19. switch(mAddr.raw.family) {
  20. case AF_INET:
  21. *aFamily = nsINetAddr::FAMILY_INET;
  22. break;
  23. case AF_INET6:
  24. *aFamily = nsINetAddr::FAMILY_INET6;
  25. break;
  26. #if defined(XP_UNIX)
  27. case AF_LOCAL:
  28. *aFamily = nsINetAddr::FAMILY_LOCAL;
  29. break;
  30. #endif
  31. default:
  32. return NS_ERROR_UNEXPECTED;
  33. }
  34. return NS_OK;
  35. }
  36. NS_IMETHODIMP nsNetAddr::GetAddress(nsACString & aAddress)
  37. {
  38. switch(mAddr.raw.family) {
  39. /* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
  40. case AF_INET:
  41. aAddress.SetCapacity(kIPv4CStrBufSize);
  42. NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv4CStrBufSize);
  43. aAddress.SetLength(strlen(aAddress.BeginReading()));
  44. break;
  45. case AF_INET6:
  46. aAddress.SetCapacity(kIPv6CStrBufSize);
  47. NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv6CStrBufSize);
  48. aAddress.SetLength(strlen(aAddress.BeginReading()));
  49. break;
  50. #if defined(XP_UNIX)
  51. case AF_LOCAL:
  52. aAddress.Assign(mAddr.local.path);
  53. break;
  54. #endif
  55. // PR_AF_LOCAL falls through to default when not XP_UNIX
  56. default:
  57. return NS_ERROR_UNEXPECTED;
  58. }
  59. return NS_OK;
  60. }
  61. NS_IMETHODIMP nsNetAddr::GetPort(uint16_t *aPort)
  62. {
  63. switch(mAddr.raw.family) {
  64. case AF_INET:
  65. *aPort = ntohs(mAddr.inet.port);
  66. break;
  67. case AF_INET6:
  68. *aPort = ntohs(mAddr.inet6.port);
  69. break;
  70. #if defined(XP_UNIX)
  71. case AF_LOCAL:
  72. // There is no port number for local / connections.
  73. return NS_ERROR_NOT_AVAILABLE;
  74. #endif
  75. default:
  76. return NS_ERROR_UNEXPECTED;
  77. }
  78. return NS_OK;
  79. }
  80. NS_IMETHODIMP nsNetAddr::GetFlow(uint32_t *aFlow)
  81. {
  82. switch(mAddr.raw.family) {
  83. case AF_INET6:
  84. *aFlow = ntohl(mAddr.inet6.flowinfo);
  85. break;
  86. case AF_INET:
  87. #if defined(XP_UNIX)
  88. case AF_LOCAL:
  89. #endif
  90. // only for IPv6
  91. return NS_ERROR_NOT_AVAILABLE;
  92. default:
  93. return NS_ERROR_UNEXPECTED;
  94. }
  95. return NS_OK;
  96. }
  97. NS_IMETHODIMP nsNetAddr::GetScope(uint32_t *aScope)
  98. {
  99. switch(mAddr.raw.family) {
  100. case AF_INET6:
  101. *aScope = ntohl(mAddr.inet6.scope_id);
  102. break;
  103. case AF_INET:
  104. #if defined(XP_UNIX)
  105. case AF_LOCAL:
  106. #endif
  107. // only for IPv6
  108. return NS_ERROR_NOT_AVAILABLE;
  109. default:
  110. return NS_ERROR_UNEXPECTED;
  111. }
  112. return NS_OK;
  113. }
  114. NS_IMETHODIMP nsNetAddr::GetIsV4Mapped(bool *aIsV4Mapped)
  115. {
  116. switch(mAddr.raw.family) {
  117. case AF_INET6:
  118. *aIsV4Mapped = IPv6ADDR_IS_V4MAPPED(&mAddr.inet6.ip);
  119. break;
  120. case AF_INET:
  121. #if defined(XP_UNIX)
  122. case AF_LOCAL:
  123. #endif
  124. // only for IPv6
  125. return NS_ERROR_NOT_AVAILABLE;
  126. default:
  127. return NS_ERROR_UNEXPECTED;
  128. }
  129. return NS_OK;
  130. }
  131. NS_IMETHODIMP nsNetAddr::GetNetAddr(NetAddr *aResult) {
  132. memcpy(aResult, &mAddr, sizeof(mAddr));
  133. return NS_OK;
  134. }