1 /* vim: et ts=2 sw=2 tw=80
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/. */
9 #include "mozilla/net/DNS.h"
11 using namespace mozilla::net
;
13 NS_IMPL_ISUPPORTS(nsNetAddr
, nsINetAddr
)
15 NS_IMETHODIMP
nsNetAddr::GetFamily(uint16_t* aFamily
) {
16 switch (mAddr
.raw
.family
) {
18 *aFamily
= nsINetAddr::FAMILY_INET
;
21 *aFamily
= nsINetAddr::FAMILY_INET6
;
25 *aFamily
= nsINetAddr::FAMILY_LOCAL
;
29 return NS_ERROR_UNEXPECTED
;
35 NS_IMETHODIMP
nsNetAddr::GetAddress(nsACString
& aAddress
) {
36 switch (mAddr
.raw
.family
) {
37 /* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
39 aAddress
.SetLength(kIPv4CStrBufSize
);
40 mAddr
.ToStringBuffer(aAddress
.BeginWriting(), kIPv4CStrBufSize
);
41 aAddress
.SetLength(strlen(aAddress
.BeginReading()));
44 aAddress
.SetLength(kIPv6CStrBufSize
);
45 mAddr
.ToStringBuffer(aAddress
.BeginWriting(), kIPv6CStrBufSize
);
46 aAddress
.SetLength(strlen(aAddress
.BeginReading()));
50 aAddress
.Assign(mAddr
.local
.path
);
53 // PR_AF_LOCAL falls through to default when not XP_UNIX
55 return NS_ERROR_UNEXPECTED
;
61 NS_IMETHODIMP
nsNetAddr::GetPort(uint16_t* aPort
) {
62 switch (mAddr
.raw
.family
) {
64 *aPort
= ntohs(mAddr
.inet
.port
);
67 *aPort
= ntohs(mAddr
.inet6
.port
);
71 // There is no port number for local / connections.
72 return NS_ERROR_NOT_AVAILABLE
;
75 return NS_ERROR_UNEXPECTED
;
81 NS_IMETHODIMP
nsNetAddr::GetFlow(uint32_t* aFlow
) {
82 switch (mAddr
.raw
.family
) {
84 *aFlow
= ntohl(mAddr
.inet6
.flowinfo
);
91 return NS_ERROR_NOT_AVAILABLE
;
93 return NS_ERROR_UNEXPECTED
;
99 NS_IMETHODIMP
nsNetAddr::GetScope(uint32_t* aScope
) {
100 switch (mAddr
.raw
.family
) {
102 *aScope
= ntohl(mAddr
.inet6
.scope_id
);
109 return NS_ERROR_NOT_AVAILABLE
;
111 return NS_ERROR_UNEXPECTED
;
117 NS_IMETHODIMP
nsNetAddr::GetIsV4Mapped(bool* aIsV4Mapped
) {
118 switch (mAddr
.raw
.family
) {
120 *aIsV4Mapped
= IPv6ADDR_IS_V4MAPPED(&mAddr
.inet6
.ip
);
127 return NS_ERROR_NOT_AVAILABLE
;
129 return NS_ERROR_UNEXPECTED
;
135 NS_IMETHODIMP
nsNetAddr::GetNetAddr(NetAddr
* aResult
) {
136 memcpy(aResult
, &mAddr
, sizeof(mAddr
));