media/mtransport/transportlayerprsock.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 // Original author: ekr@rtfm.com
michael@0 8
michael@0 9 #include "logging.h"
michael@0 10 #include "nspr.h"
michael@0 11 #include "prerror.h"
michael@0 12 #include "prio.h"
michael@0 13
michael@0 14 #include "nsCOMPtr.h"
michael@0 15 #include "nsASocketHandler.h"
michael@0 16 #include "nsISocketTransportService.h"
michael@0 17 #include "nsNetCID.h"
michael@0 18 #include "nsServiceManagerUtils.h"
michael@0 19 #include "nsXPCOM.h"
michael@0 20
michael@0 21 #include "transportflow.h"
michael@0 22 #include "transportlayerprsock.h"
michael@0 23
michael@0 24 namespace mozilla {
michael@0 25
michael@0 26 MOZ_MTLOG_MODULE("mtransport")
michael@0 27
michael@0 28 nsresult TransportLayerPrsock::InitInternal() {
michael@0 29 // Get the transport service as a transport service
michael@0 30 nsresult rv;
michael@0 31 stservice_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
michael@0 32
michael@0 33 if (!NS_SUCCEEDED(rv)) {
michael@0 34 MOZ_MTLOG(ML_ERROR, "Couldn't get socket transport service");
michael@0 35 return rv;
michael@0 36 }
michael@0 37
michael@0 38 return NS_OK;
michael@0 39 }
michael@0 40
michael@0 41 void TransportLayerPrsock::Import(PRFileDesc *fd, nsresult *result) {
michael@0 42 if (state_ != TS_INIT) {
michael@0 43 *result = NS_ERROR_NOT_INITIALIZED;
michael@0 44 return;
michael@0 45 }
michael@0 46
michael@0 47 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Importing()");
michael@0 48 fd_ = fd;
michael@0 49 handler_ = new SocketHandler(this, fd);
michael@0 50
michael@0 51 nsresult rv = stservice_->AttachSocket(fd_, handler_);
michael@0 52 if (!NS_SUCCEEDED(rv)) {
michael@0 53 *result = rv;
michael@0 54 return;
michael@0 55 }
michael@0 56
michael@0 57 TL_SET_STATE(TS_OPEN);
michael@0 58
michael@0 59 *result = NS_OK;
michael@0 60 }
michael@0 61
michael@0 62 int TransportLayerPrsock::SendPacket(const unsigned char *data, size_t len) {
michael@0 63 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "SendPacket(" << len << ")");
michael@0 64 if (state_ != TS_OPEN) {
michael@0 65 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Can't send packet on closed interface");
michael@0 66 return TE_INTERNAL;
michael@0 67 }
michael@0 68
michael@0 69 int32_t status;
michael@0 70 status = PR_Write(fd_, data, len);
michael@0 71 if (status >= 0) {
michael@0 72 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Wrote " << len << " bytes");
michael@0 73 return status;
michael@0 74 }
michael@0 75
michael@0 76 PRErrorCode err = PR_GetError();
michael@0 77 if (err == PR_WOULD_BLOCK_ERROR) {
michael@0 78 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Write blocked");
michael@0 79 return TE_WOULDBLOCK;
michael@0 80 }
michael@0 81
michael@0 82
michael@0 83 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Write error; channel closed");
michael@0 84 TL_SET_STATE(TS_ERROR);
michael@0 85 return TE_ERROR;
michael@0 86 }
michael@0 87
michael@0 88 void TransportLayerPrsock::OnSocketReady(PRFileDesc *fd, int16_t outflags) {
michael@0 89 if (!(outflags & PR_POLL_READ)) {
michael@0 90 return;
michael@0 91 }
michael@0 92
michael@0 93 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "OnSocketReady(flags=" << outflags << ")");
michael@0 94
michael@0 95 unsigned char buf[1600];
michael@0 96 int32_t rv = PR_Read(fd, buf, sizeof(buf));
michael@0 97
michael@0 98 if (rv > 0) {
michael@0 99 // Successful read
michael@0 100 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read " << rv << " bytes");
michael@0 101 SignalPacketReceived(this, buf, rv);
michael@0 102 } else if (rv == 0) {
michael@0 103 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read 0 bytes; channel closed");
michael@0 104 TL_SET_STATE(TS_CLOSED);
michael@0 105 } else {
michael@0 106 PRErrorCode err = PR_GetError();
michael@0 107
michael@0 108 if (err != PR_WOULD_BLOCK_ERROR) {
michael@0 109 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read error; channel closed");
michael@0 110 TL_SET_STATE(TS_ERROR);
michael@0 111 }
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 NS_IMPL_ISUPPORTS0(TransportLayerPrsock::SocketHandler)
michael@0 116 } // close namespace

mercurial