/src/mozilla-central/dom/network/TCPSocketParent.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et 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 file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "TCPSocketParent.h" |
8 | | #include "jsapi.h" |
9 | | #include "jsfriendapi.h" |
10 | | #include "nsJSUtils.h" |
11 | | #include "mozilla/Unused.h" |
12 | | #include "mozilla/net/NeckoCommon.h" |
13 | | #include "mozilla/net/PNeckoParent.h" |
14 | | #include "mozilla/dom/ContentParent.h" |
15 | | #include "mozilla/dom/ScriptSettings.h" |
16 | | #include "mozilla/dom/TabParent.h" |
17 | | #include "mozilla/HoldDropJSObjects.h" |
18 | | #include "nsISocketTransportService.h" |
19 | | #include "nsISocketTransport.h" |
20 | | #include "nsIScriptSecurityManager.h" |
21 | | #include "nsNetUtil.h" |
22 | | |
23 | | namespace IPC { |
24 | | |
25 | | //Defined in TCPSocketChild.cpp |
26 | | extern bool |
27 | | DeserializeArrayBuffer(JSContext* aCx, |
28 | | const InfallibleTArray<uint8_t>& aBuffer, |
29 | | JS::MutableHandle<JS::Value> aVal); |
30 | | |
31 | | } // namespace IPC |
32 | | |
33 | | namespace mozilla { |
34 | | |
35 | | namespace net { |
36 | | // |
37 | | // set MOZ_LOG=TCPSocket:5 |
38 | | // |
39 | | extern LazyLogModule gTCPSocketLog; |
40 | 0 | #define TCPSOCKET_LOG(args) MOZ_LOG(gTCPSocketLog, LogLevel::Debug, args) |
41 | | #define TCPSOCKET_LOG_ENABLED() MOZ_LOG_TEST(gTCPSocketLog, LogLevel::Debug) |
42 | | } // namespace net |
43 | | |
44 | | using namespace net; |
45 | | |
46 | | namespace dom { |
47 | | |
48 | | static void |
49 | | FireInteralError(mozilla::net::PTCPSocketParent* aActor, uint32_t aLineNo) |
50 | 0 | { |
51 | 0 | mozilla::Unused << |
52 | 0 | aActor->SendCallback(NS_LITERAL_STRING("onerror"), |
53 | 0 | TCPError(NS_LITERAL_STRING("InvalidStateError"), NS_LITERAL_STRING("Internal error")), |
54 | 0 | static_cast<uint32_t>(TCPReadyState::Connecting)); |
55 | 0 | } |
56 | | |
57 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPSocketParentBase) |
58 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
59 | 0 | NS_INTERFACE_MAP_END |
60 | | |
61 | | NS_IMPL_CYCLE_COLLECTION(TCPSocketParentBase, mSocket) |
62 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPSocketParentBase) |
63 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPSocketParentBase) |
64 | | |
65 | | TCPSocketParentBase::TCPSocketParentBase() |
66 | | : mIPCOpen(false) |
67 | 0 | { |
68 | 0 | } |
69 | | |
70 | | TCPSocketParentBase::~TCPSocketParentBase() |
71 | 0 | { |
72 | 0 | } |
73 | | |
74 | | void |
75 | | TCPSocketParentBase::ReleaseIPDLReference() |
76 | 0 | { |
77 | 0 | MOZ_ASSERT(mIPCOpen); |
78 | 0 | mIPCOpen = false; |
79 | 0 | this->Release(); |
80 | 0 | } |
81 | | |
82 | | void |
83 | | TCPSocketParentBase::AddIPDLReference() |
84 | 0 | { |
85 | 0 | MOZ_ASSERT(!mIPCOpen); |
86 | 0 | mIPCOpen = true; |
87 | 0 | this->AddRef(); |
88 | 0 | } |
89 | | |
90 | | NS_IMETHODIMP_(MozExternalRefCountType) TCPSocketParent::Release(void) |
91 | 0 | { |
92 | 0 | nsrefcnt refcnt = TCPSocketParentBase::Release(); |
93 | 0 | if (refcnt == 1 && mIPCOpen) { |
94 | 0 | mozilla::Unused << PTCPSocketParent::SendRequestDelete(); |
95 | 0 | return 1; |
96 | 0 | } |
97 | 0 | return refcnt; |
98 | 0 | } |
99 | | |
100 | | mozilla::ipc::IPCResult |
101 | | TCPSocketParent::RecvOpen(const nsString& aHost, const uint16_t& aPort, const bool& aUseSSL, |
102 | | const bool& aUseArrayBuffers) |
103 | 0 | { |
104 | 0 | mSocket = new TCPSocket(nullptr, aHost, aPort, aUseSSL, aUseArrayBuffers); |
105 | 0 | mSocket->SetSocketBridgeParent(this); |
106 | 0 | NS_ENSURE_SUCCESS(mSocket->Init(), IPC_OK()); |
107 | 0 | return IPC_OK(); |
108 | 0 | } |
109 | | |
110 | | mozilla::ipc::IPCResult |
111 | | TCPSocketParent::RecvOpenBind(const nsCString& aRemoteHost, |
112 | | const uint16_t& aRemotePort, |
113 | | const nsCString& aLocalAddr, |
114 | | const uint16_t& aLocalPort, |
115 | | const bool& aUseSSL, |
116 | | const bool& aReuseAddrPort, |
117 | | const bool& aUseArrayBuffers, |
118 | | const nsCString& aFilter) |
119 | 0 | { |
120 | 0 | nsresult rv; |
121 | 0 | nsCOMPtr<nsISocketTransportService> sts = |
122 | 0 | do_GetService("@mozilla.org/network/socket-transport-service;1", &rv); |
123 | 0 | if (NS_FAILED(rv)) { |
124 | 0 | FireInteralError(this, __LINE__); |
125 | 0 | return IPC_OK(); |
126 | 0 | } |
127 | 0 |
|
128 | 0 | nsCOMPtr<nsISocketTransport> socketTransport; |
129 | 0 | if (aUseSSL) { |
130 | 0 | const char* socketTypes[1]; |
131 | 0 | socketTypes[0] = "ssl"; |
132 | 0 | rv = sts->CreateTransport(socketTypes, 1, |
133 | 0 | aRemoteHost, aRemotePort, |
134 | 0 | nullptr, getter_AddRefs(socketTransport)); |
135 | 0 | } else { |
136 | 0 | rv = sts->CreateTransport(nullptr, 0, |
137 | 0 | aRemoteHost, aRemotePort, |
138 | 0 | nullptr, getter_AddRefs(socketTransport)); |
139 | 0 | } |
140 | 0 |
|
141 | 0 | if (NS_FAILED(rv)) { |
142 | 0 | FireInteralError(this, __LINE__); |
143 | 0 | return IPC_OK(); |
144 | 0 | } |
145 | 0 |
|
146 | 0 | // in most cases aReuseAddrPort is false, but ICE TCP needs |
147 | 0 | // sockets options set that allow addr/port reuse |
148 | 0 | socketTransport->SetReuseAddrPort(aReuseAddrPort); |
149 | 0 |
|
150 | 0 | PRNetAddr prAddr; |
151 | 0 | if (PR_SUCCESS != PR_InitializeNetAddr(PR_IpAddrAny, aLocalPort, &prAddr)) { |
152 | 0 | FireInteralError(this, __LINE__); |
153 | 0 | return IPC_OK(); |
154 | 0 | } |
155 | 0 | if (PR_SUCCESS != PR_StringToNetAddr(aLocalAddr.BeginReading(), &prAddr)) { |
156 | 0 | FireInteralError(this, __LINE__); |
157 | 0 | return IPC_OK(); |
158 | 0 | } |
159 | 0 |
|
160 | 0 | mozilla::net::NetAddr addr; |
161 | 0 | PRNetAddrToNetAddr(&prAddr, &addr); |
162 | 0 | rv = socketTransport->Bind(&addr); |
163 | 0 | if (NS_FAILED(rv)) { |
164 | 0 | FireInteralError(this, __LINE__); |
165 | 0 | return IPC_OK(); |
166 | 0 | } |
167 | 0 |
|
168 | 0 | if (!aFilter.IsEmpty()) { |
169 | 0 | nsAutoCString contractId(NS_NETWORK_TCP_SOCKET_FILTER_HANDLER_PREFIX); |
170 | 0 | contractId.Append(aFilter); |
171 | 0 | nsCOMPtr<nsISocketFilterHandler> filterHandler = |
172 | 0 | do_GetService(contractId.get()); |
173 | 0 | if (!filterHandler) { |
174 | 0 | NS_ERROR("Content doesn't have a valid filter"); |
175 | 0 | FireInteralError(this, __LINE__); |
176 | 0 | return IPC_OK(); |
177 | 0 | } |
178 | 0 | rv = filterHandler->NewFilter(getter_AddRefs(mFilter)); |
179 | 0 | if (NS_FAILED(rv)) { |
180 | 0 | NS_ERROR("Cannot create filter that content specified"); |
181 | 0 | FireInteralError(this, __LINE__); |
182 | 0 | return IPC_OK(); |
183 | 0 | } |
184 | 0 | } |
185 | 0 |
|
186 | 0 | mSocket = new TCPSocket(nullptr, NS_ConvertUTF8toUTF16(aRemoteHost), aRemotePort, aUseSSL, aUseArrayBuffers); |
187 | 0 | mSocket->SetSocketBridgeParent(this); |
188 | 0 | rv = mSocket->InitWithUnconnectedTransport(socketTransport); |
189 | 0 | NS_ENSURE_SUCCESS(rv, IPC_OK()); |
190 | 0 | return IPC_OK(); |
191 | 0 | } |
192 | | |
193 | | mozilla::ipc::IPCResult |
194 | | TCPSocketParent::RecvStartTLS() |
195 | 0 | { |
196 | 0 | NS_ENSURE_TRUE(mSocket, IPC_OK()); |
197 | 0 | ErrorResult rv; |
198 | 0 | mSocket->UpgradeToSecure(rv); |
199 | 0 | if (NS_WARN_IF(rv.Failed())) { |
200 | 0 | rv.SuppressException(); |
201 | 0 | } |
202 | 0 |
|
203 | 0 | return IPC_OK(); |
204 | 0 | } |
205 | | |
206 | | mozilla::ipc::IPCResult |
207 | | TCPSocketParent::RecvSuspend() |
208 | 0 | { |
209 | 0 | NS_ENSURE_TRUE(mSocket, IPC_OK()); |
210 | 0 | mSocket->Suspend(); |
211 | 0 | return IPC_OK(); |
212 | 0 | } |
213 | | |
214 | | mozilla::ipc::IPCResult |
215 | | TCPSocketParent::RecvResume() |
216 | 0 | { |
217 | 0 | NS_ENSURE_TRUE(mSocket, IPC_OK()); |
218 | 0 | ErrorResult rv; |
219 | 0 | mSocket->Resume(rv); |
220 | 0 | if (NS_WARN_IF(rv.Failed())) { |
221 | 0 | rv.SuppressException(); |
222 | 0 | } |
223 | 0 |
|
224 | 0 | return IPC_OK(); |
225 | 0 | } |
226 | | |
227 | | mozilla::ipc::IPCResult |
228 | | TCPSocketParent::RecvData(const SendableData& aData, |
229 | | const uint32_t& aTrackingNumber) |
230 | 0 | { |
231 | 0 | ErrorResult rv; |
232 | 0 |
|
233 | 0 | if (mFilter) { |
234 | 0 | mozilla::net::NetAddr addr; // dummy value |
235 | 0 | bool allowed; |
236 | 0 | MOZ_ASSERT(aData.type() == SendableData::TArrayOfuint8_t, |
237 | 0 | "Unsupported data type for filtering"); |
238 | 0 | const InfallibleTArray<uint8_t>& data(aData.get_ArrayOfuint8_t()); |
239 | 0 | nsresult nsrv = mFilter->FilterPacket(&addr, data.Elements(), |
240 | 0 | data.Length(), |
241 | 0 | nsISocketFilter::SF_OUTGOING, |
242 | 0 | &allowed); |
243 | 0 |
|
244 | 0 | // Reject sending of unallowed data |
245 | 0 | if (NS_WARN_IF(NS_FAILED(nsrv)) || !allowed) { |
246 | 0 | TCPSOCKET_LOG(("%s: Dropping outgoing TCP packet", __FUNCTION__)); |
247 | 0 | return IPC_FAIL_NO_REASON(this); |
248 | 0 | } |
249 | 0 | } |
250 | 0 |
|
251 | 0 | switch (aData.type()) { |
252 | 0 | case SendableData::TArrayOfuint8_t: { |
253 | 0 | AutoSafeJSContext autoCx; |
254 | 0 | JS::Rooted<JS::Value> val(autoCx); |
255 | 0 | const nsTArray<uint8_t>& buffer = aData.get_ArrayOfuint8_t(); |
256 | 0 | bool ok = IPC::DeserializeArrayBuffer(autoCx, buffer, &val); |
257 | 0 | NS_ENSURE_TRUE(ok, IPC_OK()); |
258 | 0 | RootedSpiderMonkeyInterface<ArrayBuffer> data(autoCx); |
259 | 0 | data.Init(&val.toObject()); |
260 | 0 | Optional<uint32_t> byteLength(buffer.Length()); |
261 | 0 | mSocket->SendWithTrackingNumber(autoCx, data, 0, byteLength, aTrackingNumber, rv); |
262 | 0 | break; |
263 | 0 | } |
264 | 0 |
|
265 | 0 | case SendableData::TnsCString: { |
266 | 0 | const nsCString& strData = aData.get_nsCString(); |
267 | 0 | mSocket->SendWithTrackingNumber(strData, aTrackingNumber, rv); |
268 | 0 | break; |
269 | 0 | } |
270 | 0 |
|
271 | 0 | default: |
272 | 0 | MOZ_CRASH("unexpected SendableData type"); |
273 | 0 | } |
274 | 0 | NS_ENSURE_SUCCESS(rv.StealNSResult(), IPC_OK()); |
275 | 0 | return IPC_OK(); |
276 | 0 | } |
277 | | |
278 | | mozilla::ipc::IPCResult |
279 | | TCPSocketParent::RecvClose() |
280 | 0 | { |
281 | 0 | NS_ENSURE_TRUE(mSocket, IPC_OK()); |
282 | 0 | mSocket->Close(); |
283 | 0 | return IPC_OK(); |
284 | 0 | } |
285 | | |
286 | | void |
287 | | TCPSocketParent::FireErrorEvent(const nsAString& aName, const nsAString& aType, TCPReadyState aReadyState) |
288 | 0 | { |
289 | 0 | SendEvent(NS_LITERAL_STRING("error"), TCPError(nsString(aName), nsString(aType)), aReadyState); |
290 | 0 | } |
291 | | |
292 | | void |
293 | | TCPSocketParent::FireEvent(const nsAString& aType, TCPReadyState aReadyState) |
294 | 0 | { |
295 | 0 | return SendEvent(aType, mozilla::void_t(), aReadyState); |
296 | 0 | } |
297 | | |
298 | | void |
299 | | TCPSocketParent::FireArrayBufferDataEvent(nsTArray<uint8_t>& aBuffer, TCPReadyState aReadyState) |
300 | 0 | { |
301 | 0 | InfallibleTArray<uint8_t> arr; |
302 | 0 | arr.SwapElements(aBuffer); |
303 | 0 |
|
304 | 0 | if (mFilter) { |
305 | 0 | bool allowed; |
306 | 0 | mozilla::net::NetAddr addr; |
307 | 0 | nsresult nsrv = mFilter->FilterPacket(&addr, arr.Elements(), arr.Length(), |
308 | 0 | nsISocketFilter::SF_INCOMING, |
309 | 0 | &allowed); |
310 | 0 | // receiving unallowed data, drop it. |
311 | 0 | if (NS_WARN_IF(NS_FAILED(nsrv)) || !allowed) { |
312 | 0 | TCPSOCKET_LOG(("%s: Dropping incoming TCP packet", __FUNCTION__)); |
313 | 0 | return; |
314 | 0 | } |
315 | 0 | } |
316 | 0 |
|
317 | 0 | SendableData data(arr); |
318 | 0 | SendEvent(NS_LITERAL_STRING("data"), data, aReadyState); |
319 | 0 | } |
320 | | |
321 | | void |
322 | | TCPSocketParent::FireStringDataEvent(const nsACString& aData, TCPReadyState aReadyState) |
323 | 0 | { |
324 | 0 | SendableData data((nsCString(aData))); |
325 | 0 |
|
326 | 0 | MOZ_ASSERT(!mFilter, "Socket filtering doesn't support nsCString"); |
327 | 0 |
|
328 | 0 | SendEvent(NS_LITERAL_STRING("data"), data, aReadyState); |
329 | 0 | } |
330 | | |
331 | | void |
332 | | TCPSocketParent::SendEvent(const nsAString& aType, CallbackData aData, TCPReadyState aReadyState) |
333 | 0 | { |
334 | 0 | if (mIPCOpen) { |
335 | 0 | mozilla::Unused << PTCPSocketParent::SendCallback(nsString(aType), |
336 | 0 | aData, |
337 | 0 | static_cast<uint32_t>(aReadyState)); |
338 | 0 | } |
339 | 0 | } |
340 | | |
341 | | void |
342 | | TCPSocketParent::SetSocket(TCPSocket *socket) |
343 | 0 | { |
344 | 0 | mSocket = socket; |
345 | 0 | } |
346 | | |
347 | | nsresult |
348 | | TCPSocketParent::GetHost(nsAString& aHost) |
349 | 0 | { |
350 | 0 | if (!mSocket) { |
351 | 0 | NS_ERROR("No internal socket instance mSocket!"); |
352 | 0 | return NS_ERROR_FAILURE; |
353 | 0 | } |
354 | 0 | mSocket->GetHost(aHost); |
355 | 0 | return NS_OK; |
356 | 0 | } |
357 | | |
358 | | nsresult |
359 | | TCPSocketParent::GetPort(uint16_t* aPort) |
360 | 0 | { |
361 | 0 | if (!mSocket) { |
362 | 0 | NS_ERROR("No internal socket instance mSocket!"); |
363 | 0 | return NS_ERROR_FAILURE; |
364 | 0 | } |
365 | 0 | *aPort = mSocket->Port(); |
366 | 0 | return NS_OK; |
367 | 0 | } |
368 | | |
369 | | void |
370 | | TCPSocketParent::ActorDestroy(ActorDestroyReason why) |
371 | 0 | { |
372 | 0 | if (mSocket) { |
373 | 0 | mSocket->Close(); |
374 | 0 | } |
375 | 0 | mSocket = nullptr; |
376 | 0 | } |
377 | | |
378 | | mozilla::ipc::IPCResult |
379 | | TCPSocketParent::RecvRequestDelete() |
380 | 0 | { |
381 | 0 | mozilla::Unused << Send__delete__(this); |
382 | 0 | return IPC_OK(); |
383 | 0 | } |
384 | | |
385 | | } // namespace dom |
386 | | } // namespace mozilla |