2017-07-06 15:00:35 +03:00
|
|
|
/* vim: et ts=2 sw=2 tw=80
|
2012-02-29 15:19:00 +04:00
|
|
|
*/
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-02-29 15:19:00 +04:00
|
|
|
|
|
|
|
#include "nsNetAddr.h"
|
|
|
|
#include "nsString.h"
|
2013-01-10 22:44:17 +04:00
|
|
|
#include "mozilla/net/DNS.h"
|
2012-12-13 18:18:33 +04:00
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
using namespace mozilla::net;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsNetAddr, nsINetAddr)
|
2012-02-29 15:19:00 +04:00
|
|
|
|
|
|
|
/* Makes a copy of |addr| */
|
2012-12-24 01:08:43 +04:00
|
|
|
nsNetAddr::nsNetAddr(NetAddr *addr) {
|
2012-02-29 15:19:00 +04:00
|
|
|
NS_ASSERTION(addr, "null addr");
|
|
|
|
mAddr = *addr;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsNetAddr::GetFamily(uint16_t *aFamily) {
|
2012-02-29 15:19:00 +04:00
|
|
|
switch (mAddr.raw.family) {
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET:
|
2012-02-29 15:19:00 +04:00
|
|
|
*aFamily = nsINetAddr::FAMILY_INET;
|
|
|
|
break;
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET6:
|
2012-02-29 15:19:00 +04:00
|
|
|
*aFamily = nsINetAddr::FAMILY_INET6;
|
|
|
|
break;
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_LOCAL:
|
2012-02-29 15:19:00 +04:00
|
|
|
*aFamily = nsINetAddr::FAMILY_LOCAL;
|
|
|
|
break;
|
2012-12-24 01:08:43 +04:00
|
|
|
#endif
|
2012-02-29 15:19:00 +04:00
|
|
|
default:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsNetAddr::GetAddress(nsACString &aAddress) {
|
|
|
|
switch (mAddr.raw.family) {
|
|
|
|
/* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET:
|
2018-08-21 17:20:48 +03:00
|
|
|
aAddress.SetLength(kIPv4CStrBufSize);
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv4CStrBufSize);
|
2012-02-29 15:19:00 +04:00
|
|
|
aAddress.SetLength(strlen(aAddress.BeginReading()));
|
|
|
|
break;
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET6:
|
2018-08-21 17:20:48 +03:00
|
|
|
aAddress.SetLength(kIPv6CStrBufSize);
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv6CStrBufSize);
|
2012-02-29 15:19:00 +04:00
|
|
|
aAddress.SetLength(strlen(aAddress.BeginReading()));
|
|
|
|
break;
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_LOCAL:
|
2012-02-29 15:19:00 +04:00
|
|
|
aAddress.Assign(mAddr.local.path);
|
|
|
|
break;
|
|
|
|
#endif
|
2014-02-11 02:57:01 +04:00
|
|
|
// PR_AF_LOCAL falls through to default when not XP_UNIX
|
2012-02-29 15:19:00 +04:00
|
|
|
default:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-02-29 15:19:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsNetAddr::GetPort(uint16_t *aPort) {
|
2012-02-29 15:19:00 +04:00
|
|
|
switch (mAddr.raw.family) {
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET:
|
|
|
|
*aPort = ntohs(mAddr.inet.port);
|
2012-02-29 15:19:00 +04:00
|
|
|
break;
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET6:
|
|
|
|
*aPort = ntohs(mAddr.inet6.port);
|
2012-02-29 15:19:00 +04:00
|
|
|
break;
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_LOCAL:
|
2012-02-29 15:19:00 +04:00
|
|
|
// There is no port number for local / connections.
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2012-12-24 01:08:43 +04:00
|
|
|
#endif
|
2012-02-29 15:19:00 +04:00
|
|
|
default:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsNetAddr::GetFlow(uint32_t *aFlow) {
|
2012-02-29 15:19:00 +04:00
|
|
|
switch (mAddr.raw.family) {
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET6:
|
|
|
|
*aFlow = ntohl(mAddr.inet6.flowinfo);
|
2012-02-29 15:19:00 +04:00
|
|
|
break;
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET:
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_LOCAL:
|
|
|
|
#endif
|
2012-02-29 15:19:00 +04:00
|
|
|
// only for IPv6
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsNetAddr::GetScope(uint32_t *aScope) {
|
2012-02-29 15:19:00 +04:00
|
|
|
switch (mAddr.raw.family) {
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET6:
|
|
|
|
*aScope = ntohl(mAddr.inet6.scope_id);
|
2012-02-29 15:19:00 +04:00
|
|
|
break;
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_INET:
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2012-12-24 01:08:43 +04:00
|
|
|
case AF_LOCAL:
|
|
|
|
#endif
|
2012-02-29 15:19:00 +04:00
|
|
|
// only for IPv6
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-10 22:44:17 +04:00
|
|
|
NS_IMETHODIMP nsNetAddr::GetIsV4Mapped(bool *aIsV4Mapped) {
|
|
|
|
switch (mAddr.raw.family) {
|
|
|
|
case AF_INET6:
|
|
|
|
*aIsV4Mapped = IPv6ADDR_IS_V4MAPPED(&mAddr.inet6.ip);
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2013-01-10 22:44:17 +04:00
|
|
|
case AF_LOCAL:
|
|
|
|
#endif
|
|
|
|
// only for IPv6
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-10 16:00:22 +04:00
|
|
|
NS_IMETHODIMP nsNetAddr::GetNetAddr(NetAddr *aResult) {
|
|
|
|
memcpy(aResult, &mAddr, sizeof(mAddr));
|
|
|
|
return NS_OK;
|
|
|
|
}
|