1 : /* vim: et ts=2 sw=2 tw=80
2 : */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Derrick Rice <derrick.rice@gmail.com>
20 : *
21 : * Contributor(s):
22 : *
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsNetAddr.h"
39 : #include "nsString.h"
40 :
41 : #include "prnetdb.h"
42 :
43 60 : NS_IMPL_ISUPPORTS1(nsNetAddr, nsINetAddr)
44 :
45 : /* Makes a copy of |addr| */
46 4 : nsNetAddr::nsNetAddr(PRNetAddr* addr)
47 : {
48 4 : NS_ASSERTION(addr, "null addr");
49 4 : mAddr = *addr;
50 4 : }
51 :
52 : /* readonly attribute unsigned short family; */
53 8 : NS_IMETHODIMP nsNetAddr::GetFamily(PRUint16 *aFamily)
54 : {
55 8 : switch(mAddr.raw.family) {
56 : case PR_AF_INET:
57 8 : *aFamily = nsINetAddr::FAMILY_INET;
58 8 : break;
59 : case PR_AF_INET6:
60 0 : *aFamily = nsINetAddr::FAMILY_INET6;
61 0 : break;
62 : case PR_AF_LOCAL:
63 0 : *aFamily = nsINetAddr::FAMILY_LOCAL;
64 0 : break;
65 : default:
66 0 : return NS_ERROR_UNEXPECTED;
67 : }
68 :
69 8 : return NS_OK;
70 : }
71 :
72 : /* readonly attribute AUTF8String address; */
73 6 : NS_IMETHODIMP nsNetAddr::GetAddress(nsACString & aAddress)
74 : {
75 6 : switch(mAddr.raw.family) {
76 : /* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
77 : case PR_AF_INET:
78 6 : if(!aAddress.SetCapacity(16))
79 0 : return NS_ERROR_OUT_OF_MEMORY;
80 6 : PR_NetAddrToString(&mAddr, aAddress.BeginWriting(), 16);
81 6 : aAddress.SetLength(strlen(aAddress.BeginReading()));
82 6 : break;
83 : case PR_AF_INET6:
84 0 : if(!aAddress.SetCapacity(46))
85 0 : return NS_ERROR_OUT_OF_MEMORY;
86 0 : PR_NetAddrToString(&mAddr, aAddress.BeginWriting(), 46);
87 0 : aAddress.SetLength(strlen(aAddress.BeginReading()));
88 0 : break;
89 : #if defined(XP_UNIX) || defined(XP_OS2)
90 : case PR_AF_LOCAL:
91 0 : aAddress.Assign(mAddr.local.path);
92 0 : break;
93 : #endif
94 : // PR_AF_LOCAL falls through to default when not XP_UNIX || XP_OS2
95 : default:
96 0 : return NS_ERROR_UNEXPECTED;
97 : }
98 :
99 6 : return NS_OK;
100 : }
101 :
102 : /* readonly attribute unsigned short port; */
103 6 : NS_IMETHODIMP nsNetAddr::GetPort(PRUint16 *aPort)
104 : {
105 6 : switch(mAddr.raw.family) {
106 : case PR_AF_INET:
107 6 : *aPort = PR_ntohs(mAddr.inet.port);
108 6 : break;
109 : case PR_AF_INET6:
110 0 : *aPort = PR_ntohs(mAddr.ipv6.port);
111 0 : break;
112 : case PR_AF_LOCAL:
113 : // There is no port number for local / connections.
114 0 : return NS_ERROR_NOT_AVAILABLE;
115 : default:
116 0 : return NS_ERROR_UNEXPECTED;
117 : }
118 :
119 6 : return NS_OK;
120 : }
121 :
122 : /* readonly attribute unsigned long flow; */
123 0 : NS_IMETHODIMP nsNetAddr::GetFlow(PRUint32 *aFlow)
124 : {
125 0 : switch(mAddr.raw.family) {
126 : case PR_AF_INET6:
127 0 : *aFlow = PR_ntohl(mAddr.ipv6.flowinfo);
128 : break;
129 : case PR_AF_INET:
130 : case PR_AF_LOCAL:
131 : // only for IPv6
132 0 : return NS_ERROR_NOT_AVAILABLE;
133 : default:
134 0 : return NS_ERROR_UNEXPECTED;
135 : }
136 :
137 0 : return NS_OK;
138 : }
139 :
140 : /* readonly attribute unsigned long scope; */
141 0 : NS_IMETHODIMP nsNetAddr::GetScope(PRUint32 *aScope)
142 : {
143 0 : switch(mAddr.raw.family) {
144 : case PR_AF_INET6:
145 0 : *aScope = PR_ntohl(mAddr.ipv6.scope_id);
146 : break;
147 : case PR_AF_INET:
148 : case PR_AF_LOCAL:
149 : // only for IPv6
150 0 : return NS_ERROR_NOT_AVAILABLE;
151 : default:
152 0 : return NS_ERROR_UNEXPECTED;
153 : }
154 :
155 0 : return NS_OK;
156 : }
157 :
|