Source code

Revision control

Other Tools

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