Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / netwerk / base / nsNetAddr.cpp
blobd0cbe69baeb601d68ae8e20fb13f8b60ca7bd730
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/. */
7 #include "nsNetAddr.h"
8 #include "nsString.h"
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) {
17 case AF_INET:
18 *aFamily = nsINetAddr::FAMILY_INET;
19 break;
20 case AF_INET6:
21 *aFamily = nsINetAddr::FAMILY_INET6;
22 break;
23 #if defined(XP_UNIX)
24 case AF_LOCAL:
25 *aFamily = nsINetAddr::FAMILY_LOCAL;
26 break;
27 #endif
28 default:
29 return NS_ERROR_UNEXPECTED;
32 return NS_OK;
35 NS_IMETHODIMP nsNetAddr::GetAddress(nsACString& aAddress) {
36 switch (mAddr.raw.family) {
37 /* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
38 case AF_INET:
39 aAddress.SetLength(kIPv4CStrBufSize);
40 mAddr.ToStringBuffer(aAddress.BeginWriting(), kIPv4CStrBufSize);
41 aAddress.SetLength(strlen(aAddress.BeginReading()));
42 break;
43 case AF_INET6:
44 aAddress.SetLength(kIPv6CStrBufSize);
45 mAddr.ToStringBuffer(aAddress.BeginWriting(), kIPv6CStrBufSize);
46 aAddress.SetLength(strlen(aAddress.BeginReading()));
47 break;
48 #if defined(XP_UNIX)
49 case AF_LOCAL:
50 aAddress.Assign(mAddr.local.path);
51 break;
52 #endif
53 // PR_AF_LOCAL falls through to default when not XP_UNIX
54 default:
55 return NS_ERROR_UNEXPECTED;
58 return NS_OK;
61 NS_IMETHODIMP nsNetAddr::GetPort(uint16_t* aPort) {
62 switch (mAddr.raw.family) {
63 case AF_INET:
64 *aPort = ntohs(mAddr.inet.port);
65 break;
66 case AF_INET6:
67 *aPort = ntohs(mAddr.inet6.port);
68 break;
69 #if defined(XP_UNIX)
70 case AF_LOCAL:
71 // There is no port number for local / connections.
72 return NS_ERROR_NOT_AVAILABLE;
73 #endif
74 default:
75 return NS_ERROR_UNEXPECTED;
78 return NS_OK;
81 NS_IMETHODIMP nsNetAddr::GetFlow(uint32_t* aFlow) {
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;
96 return NS_OK;
99 NS_IMETHODIMP nsNetAddr::GetScope(uint32_t* aScope) {
100 switch (mAddr.raw.family) {
101 case AF_INET6:
102 *aScope = ntohl(mAddr.inet6.scope_id);
103 break;
104 case AF_INET:
105 #if defined(XP_UNIX)
106 case AF_LOCAL:
107 #endif
108 // only for IPv6
109 return NS_ERROR_NOT_AVAILABLE;
110 default:
111 return NS_ERROR_UNEXPECTED;
114 return NS_OK;
117 NS_IMETHODIMP nsNetAddr::GetIsV4Mapped(bool* aIsV4Mapped) {
118 switch (mAddr.raw.family) {
119 case AF_INET6:
120 *aIsV4Mapped = IPv6ADDR_IS_V4MAPPED(&mAddr.inet6.ip);
121 break;
122 case AF_INET:
123 #if defined(XP_UNIX)
124 case AF_LOCAL:
125 #endif
126 // only for IPv6
127 return NS_ERROR_NOT_AVAILABLE;
128 default:
129 return NS_ERROR_UNEXPECTED;
132 return NS_OK;
135 NS_IMETHODIMP nsNetAddr::GetNetAddr(NetAddr* aResult) {
136 memcpy(aResult, &mAddr, sizeof(mAddr));
137 return NS_OK;