nsSocketTransport2.cpp 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsSocketTransport2.h"
  6. #include "mozilla/Attributes.h"
  7. #include "nsIOService.h"
  8. #include "nsStreamUtils.h"
  9. #include "nsNetSegmentUtils.h"
  10. #include "nsNetAddr.h"
  11. #include "nsTransportUtils.h"
  12. #include "nsProxyInfo.h"
  13. #include "nsNetCID.h"
  14. #include "nsNetUtil.h"
  15. #include "nsAutoPtr.h"
  16. #include "nsCOMPtr.h"
  17. #include "plstr.h"
  18. #include "prerr.h"
  19. #include "NetworkActivityMonitor.h"
  20. #include "NSSErrorsService.h"
  21. #include "mozilla/dom/ToJSValue.h"
  22. #include "mozilla/net/NeckoChild.h"
  23. #include "nsThreadUtils.h"
  24. #include "nsISocketProviderService.h"
  25. #include "nsISocketProvider.h"
  26. #include "nsISSLSocketControl.h"
  27. #include "nsIPipe.h"
  28. #include "nsIClassInfoImpl.h"
  29. #include "nsURLHelper.h"
  30. #include "nsIDNSService.h"
  31. #include "nsIDNSRecord.h"
  32. #include "nsICancelable.h"
  33. #include <algorithm>
  34. #include "nsPrintfCString.h"
  35. #include "xpcpublic.h"
  36. #if defined(XP_WIN)
  37. #include "ShutdownLayer.h"
  38. #endif
  39. /* Following inclusions required for keepalive config not supported by NSPR. */
  40. #include "private/pprio.h"
  41. #if defined(XP_WIN)
  42. #include <winsock2.h>
  43. #include <mstcpip.h>
  44. #elif defined(XP_UNIX)
  45. #include <errno.h>
  46. #include <netinet/tcp.h>
  47. #endif
  48. /* End keepalive config inclusions. */
  49. #define SUCCESSFUL_CONNECTING_TO_IPV4_ADDRESS 0
  50. #define UNSUCCESSFUL_CONNECTING_TO_IPV4_ADDRESS 1
  51. #define SUCCESSFUL_CONNECTING_TO_IPV6_ADDRESS 2
  52. #define UNSUCCESSFUL_CONNECTING_TO_IPV6_ADDRESS 3
  53. //-----------------------------------------------------------------------------
  54. static NS_DEFINE_CID(kSocketProviderServiceCID, NS_SOCKETPROVIDERSERVICE_CID);
  55. static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
  56. //-----------------------------------------------------------------------------
  57. namespace mozilla {
  58. namespace net {
  59. class nsSocketEvent : public Runnable
  60. {
  61. public:
  62. nsSocketEvent(nsSocketTransport *transport, uint32_t type,
  63. nsresult status = NS_OK, nsISupports *param = nullptr)
  64. : mTransport(transport)
  65. , mType(type)
  66. , mStatus(status)
  67. , mParam(param)
  68. {}
  69. NS_IMETHOD Run() override
  70. {
  71. mTransport->OnSocketEvent(mType, mStatus, mParam);
  72. return NS_OK;
  73. }
  74. private:
  75. RefPtr<nsSocketTransport> mTransport;
  76. uint32_t mType;
  77. nsresult mStatus;
  78. nsCOMPtr<nsISupports> mParam;
  79. };
  80. //-----------------------------------------------------------------------------
  81. //#define TEST_CONNECT_ERRORS
  82. #ifdef TEST_CONNECT_ERRORS
  83. #include <stdlib.h>
  84. static PRErrorCode RandomizeConnectError(PRErrorCode code)
  85. {
  86. //
  87. // To test out these errors, load http://www.yahoo.com/. It should load
  88. // correctly despite the random occurrence of these errors.
  89. //
  90. int n = rand();
  91. if (n > RAND_MAX/2) {
  92. struct {
  93. PRErrorCode err_code;
  94. const char *err_name;
  95. }
  96. errors[] = {
  97. //
  98. // These errors should be recoverable provided there is another
  99. // IP address in mDNSRecord.
  100. //
  101. { PR_CONNECT_REFUSED_ERROR, "PR_CONNECT_REFUSED_ERROR" },
  102. { PR_CONNECT_TIMEOUT_ERROR, "PR_CONNECT_TIMEOUT_ERROR" },
  103. //
  104. // This error will cause this socket transport to error out;
  105. // however, if the consumer is HTTP, then the HTTP transaction
  106. // should be restarted when this error occurs.
  107. //
  108. { PR_CONNECT_RESET_ERROR, "PR_CONNECT_RESET_ERROR" },
  109. };
  110. n = n % (sizeof(errors)/sizeof(errors[0]));
  111. code = errors[n].err_code;
  112. SOCKET_LOG(("simulating NSPR error %d [%s]\n", code, errors[n].err_name));
  113. }
  114. return code;
  115. }
  116. #endif
  117. //-----------------------------------------------------------------------------
  118. nsresult
  119. ErrorAccordingToNSPR(PRErrorCode errorCode)
  120. {
  121. nsresult rv = NS_ERROR_FAILURE;
  122. switch (errorCode) {
  123. case PR_WOULD_BLOCK_ERROR:
  124. rv = NS_BASE_STREAM_WOULD_BLOCK;
  125. break;
  126. case PR_CONNECT_ABORTED_ERROR:
  127. case PR_CONNECT_RESET_ERROR:
  128. rv = NS_ERROR_NET_RESET;
  129. break;
  130. case PR_END_OF_FILE_ERROR: // XXX document this correlation
  131. rv = NS_ERROR_NET_INTERRUPT;
  132. break;
  133. case PR_CONNECT_REFUSED_ERROR:
  134. // We lump the following NSPR codes in with PR_CONNECT_REFUSED_ERROR. We
  135. // could get better diagnostics by adding distinct XPCOM error codes for
  136. // each of these, but there are a lot of places in Gecko that check
  137. // specifically for NS_ERROR_CONNECTION_REFUSED, all of which would need to
  138. // be checked.
  139. case PR_NETWORK_UNREACHABLE_ERROR:
  140. case PR_HOST_UNREACHABLE_ERROR:
  141. case PR_ADDRESS_NOT_AVAILABLE_ERROR:
  142. // Treat EACCES as a soft error since (at least on Linux) connect() returns
  143. // EACCES when an IPv6 connection is blocked by a firewall. See bug 270784.
  144. case PR_NO_ACCESS_RIGHTS_ERROR:
  145. rv = NS_ERROR_CONNECTION_REFUSED;
  146. break;
  147. case PR_ADDRESS_NOT_SUPPORTED_ERROR:
  148. rv = NS_ERROR_SOCKET_ADDRESS_NOT_SUPPORTED;
  149. break;
  150. case PR_IO_TIMEOUT_ERROR:
  151. case PR_CONNECT_TIMEOUT_ERROR:
  152. rv = NS_ERROR_NET_TIMEOUT;
  153. break;
  154. case PR_OUT_OF_MEMORY_ERROR:
  155. // These really indicate that the descriptor table filled up, or that the
  156. // kernel ran out of network buffers - but nobody really cares which part of
  157. // the system ran out of memory.
  158. case PR_PROC_DESC_TABLE_FULL_ERROR:
  159. case PR_SYS_DESC_TABLE_FULL_ERROR:
  160. case PR_INSUFFICIENT_RESOURCES_ERROR:
  161. rv = NS_ERROR_OUT_OF_MEMORY;
  162. break;
  163. case PR_ADDRESS_IN_USE_ERROR:
  164. rv = NS_ERROR_SOCKET_ADDRESS_IN_USE;
  165. break;
  166. // These filename-related errors can arise when using Unix-domain sockets.
  167. case PR_FILE_NOT_FOUND_ERROR:
  168. rv = NS_ERROR_FILE_NOT_FOUND;
  169. break;
  170. case PR_IS_DIRECTORY_ERROR:
  171. rv = NS_ERROR_FILE_IS_DIRECTORY;
  172. break;
  173. case PR_LOOP_ERROR:
  174. rv = NS_ERROR_FILE_UNRESOLVABLE_SYMLINK;
  175. break;
  176. case PR_NAME_TOO_LONG_ERROR:
  177. rv = NS_ERROR_FILE_NAME_TOO_LONG;
  178. break;
  179. case PR_NO_DEVICE_SPACE_ERROR:
  180. rv = NS_ERROR_FILE_NO_DEVICE_SPACE;
  181. break;
  182. case PR_NOT_DIRECTORY_ERROR:
  183. rv = NS_ERROR_FILE_NOT_DIRECTORY;
  184. break;
  185. case PR_READ_ONLY_FILESYSTEM_ERROR:
  186. rv = NS_ERROR_FILE_READ_ONLY;
  187. break;
  188. default:
  189. if (psm::IsNSSErrorCode(errorCode)) {
  190. rv = psm::GetXPCOMFromNSSError(errorCode);
  191. }
  192. break;
  193. // NSPR's socket code can return these, but they're not worth breaking out
  194. // into their own error codes, distinct from NS_ERROR_FAILURE:
  195. //
  196. // PR_BAD_DESCRIPTOR_ERROR
  197. // PR_INVALID_ARGUMENT_ERROR
  198. // PR_NOT_SOCKET_ERROR
  199. // PR_NOT_TCP_SOCKET_ERROR
  200. // These would indicate a bug internal to the component.
  201. //
  202. // PR_PROTOCOL_NOT_SUPPORTED_ERROR
  203. // This means that we can't use the given "protocol" (like
  204. // IPPROTO_TCP or IPPROTO_UDP) with a socket of the given type. As
  205. // above, this indicates an internal bug.
  206. //
  207. // PR_IS_CONNECTED_ERROR
  208. // This indicates that we've applied a system call like 'bind' or
  209. // 'connect' to a socket that is already connected. The socket
  210. // components manage each file descriptor's state, and in some cases
  211. // handle this error result internally. We shouldn't be returning
  212. // this to our callers.
  213. //
  214. // PR_IO_ERROR
  215. // This is so vague that NS_ERROR_FAILURE is just as good.
  216. }
  217. SOCKET_LOG(("ErrorAccordingToNSPR [in=%d out=%x]\n", errorCode, rv));
  218. return rv;
  219. }
  220. //-----------------------------------------------------------------------------
  221. // socket input stream impl
  222. //-----------------------------------------------------------------------------
  223. nsSocketInputStream::nsSocketInputStream(nsSocketTransport *trans)
  224. : mTransport(trans)
  225. , mReaderRefCnt(0)
  226. , mCondition(NS_OK)
  227. , mCallbackFlags(0)
  228. , mByteCount(0)
  229. {
  230. }
  231. nsSocketInputStream::~nsSocketInputStream()
  232. {
  233. }
  234. // called on the socket transport thread...
  235. //
  236. // condition : failure code if socket has been closed
  237. //
  238. void
  239. nsSocketInputStream::OnSocketReady(nsresult condition)
  240. {
  241. SOCKET_LOG(("nsSocketInputStream::OnSocketReady [this=%p cond=%x]\n",
  242. this, condition));
  243. NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  244. nsCOMPtr<nsIInputStreamCallback> callback;
  245. {
  246. MutexAutoLock lock(mTransport->mLock);
  247. // update condition, but be careful not to erase an already
  248. // existing error condition.
  249. if (NS_SUCCEEDED(mCondition))
  250. mCondition = condition;
  251. // ignore event if only waiting for closure and not closed.
  252. if (NS_FAILED(mCondition) || !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
  253. callback = mCallback;
  254. mCallback = nullptr;
  255. mCallbackFlags = 0;
  256. }
  257. }
  258. if (callback)
  259. callback->OnInputStreamReady(this);
  260. }
  261. NS_IMPL_QUERY_INTERFACE(nsSocketInputStream,
  262. nsIInputStream,
  263. nsIAsyncInputStream)
  264. NS_IMETHODIMP_(MozExternalRefCountType)
  265. nsSocketInputStream::AddRef()
  266. {
  267. ++mReaderRefCnt;
  268. return mTransport->AddRef();
  269. }
  270. NS_IMETHODIMP_(MozExternalRefCountType)
  271. nsSocketInputStream::Release()
  272. {
  273. if (--mReaderRefCnt == 0)
  274. Close();
  275. return mTransport->Release();
  276. }
  277. NS_IMETHODIMP
  278. nsSocketInputStream::Close()
  279. {
  280. return CloseWithStatus(NS_BASE_STREAM_CLOSED);
  281. }
  282. NS_IMETHODIMP
  283. nsSocketInputStream::Available(uint64_t *avail)
  284. {
  285. SOCKET_LOG(("nsSocketInputStream::Available [this=%p]\n", this));
  286. *avail = 0;
  287. PRFileDesc *fd;
  288. {
  289. MutexAutoLock lock(mTransport->mLock);
  290. if (NS_FAILED(mCondition))
  291. return mCondition;
  292. fd = mTransport->GetFD_Locked();
  293. if (!fd)
  294. return NS_OK;
  295. }
  296. // cannot hold lock while calling NSPR. (worried about the fact that PSM
  297. // synchronously proxies notifications over to the UI thread, which could
  298. // mistakenly try to re-enter this code.)
  299. int32_t n = PR_Available(fd);
  300. // PSM does not implement PR_Available() so do a best approximation of it
  301. // with MSG_PEEK
  302. if ((n == -1) && (PR_GetError() == PR_NOT_IMPLEMENTED_ERROR)) {
  303. char c;
  304. n = PR_Recv(fd, &c, 1, PR_MSG_PEEK, 0);
  305. SOCKET_LOG(("nsSocketInputStream::Available [this=%p] "
  306. "using PEEK backup n=%d]\n", this, n));
  307. }
  308. nsresult rv;
  309. {
  310. MutexAutoLock lock(mTransport->mLock);
  311. mTransport->ReleaseFD_Locked(fd);
  312. if (n >= 0)
  313. *avail = n;
  314. else {
  315. PRErrorCode code = PR_GetError();
  316. if (code == PR_WOULD_BLOCK_ERROR)
  317. return NS_OK;
  318. mCondition = ErrorAccordingToNSPR(code);
  319. }
  320. rv = mCondition;
  321. }
  322. if (NS_FAILED(rv))
  323. mTransport->OnInputClosed(rv);
  324. return rv;
  325. }
  326. NS_IMETHODIMP
  327. nsSocketInputStream::Read(char *buf, uint32_t count, uint32_t *countRead)
  328. {
  329. SOCKET_LOG(("nsSocketInputStream::Read [this=%p count=%u]\n", this, count));
  330. *countRead = 0;
  331. PRFileDesc* fd = nullptr;
  332. {
  333. MutexAutoLock lock(mTransport->mLock);
  334. if (NS_FAILED(mCondition))
  335. return (mCondition == NS_BASE_STREAM_CLOSED) ? NS_OK : mCondition;
  336. fd = mTransport->GetFD_Locked();
  337. if (!fd)
  338. return NS_BASE_STREAM_WOULD_BLOCK;
  339. }
  340. SOCKET_LOG((" calling PR_Read [count=%u]\n", count));
  341. // cannot hold lock while calling NSPR. (worried about the fact that PSM
  342. // synchronously proxies notifications over to the UI thread, which could
  343. // mistakenly try to re-enter this code.)
  344. int32_t n = PR_Read(fd, buf, count);
  345. SOCKET_LOG((" PR_Read returned [n=%d]\n", n));
  346. nsresult rv = NS_OK;
  347. {
  348. MutexAutoLock lock(mTransport->mLock);
  349. #ifdef ENABLE_SOCKET_TRACING
  350. if (n > 0)
  351. mTransport->TraceInBuf(buf, n);
  352. #endif
  353. mTransport->ReleaseFD_Locked(fd);
  354. if (n > 0)
  355. mByteCount += (*countRead = n);
  356. else if (n < 0) {
  357. PRErrorCode code = PR_GetError();
  358. if (code == PR_WOULD_BLOCK_ERROR)
  359. return NS_BASE_STREAM_WOULD_BLOCK;
  360. mCondition = ErrorAccordingToNSPR(code);
  361. }
  362. rv = mCondition;
  363. }
  364. if (NS_FAILED(rv))
  365. mTransport->OnInputClosed(rv);
  366. // only send this notification if we have indeed read some data.
  367. // see bug 196827 for an example of why this is important.
  368. if (n > 0)
  369. mTransport->SendStatus(NS_NET_STATUS_RECEIVING_FROM);
  370. return rv;
  371. }
  372. NS_IMETHODIMP
  373. nsSocketInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
  374. uint32_t count, uint32_t *countRead)
  375. {
  376. // socket stream is unbuffered
  377. return NS_ERROR_NOT_IMPLEMENTED;
  378. }
  379. NS_IMETHODIMP
  380. nsSocketInputStream::IsNonBlocking(bool *nonblocking)
  381. {
  382. *nonblocking = true;
  383. return NS_OK;
  384. }
  385. NS_IMETHODIMP
  386. nsSocketInputStream::CloseWithStatus(nsresult reason)
  387. {
  388. SOCKET_LOG(("nsSocketInputStream::CloseWithStatus [this=%p reason=%x]\n", this, reason));
  389. // may be called from any thread
  390. nsresult rv;
  391. {
  392. MutexAutoLock lock(mTransport->mLock);
  393. if (NS_SUCCEEDED(mCondition))
  394. rv = mCondition = reason;
  395. else
  396. rv = NS_OK;
  397. }
  398. if (NS_FAILED(rv))
  399. mTransport->OnInputClosed(rv);
  400. return NS_OK;
  401. }
  402. NS_IMETHODIMP
  403. nsSocketInputStream::AsyncWait(nsIInputStreamCallback *callback,
  404. uint32_t flags,
  405. uint32_t amount,
  406. nsIEventTarget *target)
  407. {
  408. SOCKET_LOG(("nsSocketInputStream::AsyncWait [this=%p]\n", this));
  409. bool hasError = false;
  410. {
  411. MutexAutoLock lock(mTransport->mLock);
  412. if (callback && target) {
  413. //
  414. // build event proxy
  415. //
  416. mCallback = NS_NewInputStreamReadyEvent(callback, target);
  417. }
  418. else
  419. mCallback = callback;
  420. mCallbackFlags = flags;
  421. hasError = NS_FAILED(mCondition);
  422. } // unlock mTransport->mLock
  423. if (hasError) {
  424. // OnSocketEvent will call OnInputStreamReady with an error code after
  425. // going through the event loop. We do this because most socket callers
  426. // do not expect AsyncWait() to synchronously execute the OnInputStreamReady
  427. // callback.
  428. mTransport->PostEvent(nsSocketTransport::MSG_INPUT_PENDING);
  429. } else {
  430. mTransport->OnInputPending();
  431. }
  432. return NS_OK;
  433. }
  434. //-----------------------------------------------------------------------------
  435. // socket output stream impl
  436. //-----------------------------------------------------------------------------
  437. nsSocketOutputStream::nsSocketOutputStream(nsSocketTransport *trans)
  438. : mTransport(trans)
  439. , mWriterRefCnt(0)
  440. , mCondition(NS_OK)
  441. , mCallbackFlags(0)
  442. , mByteCount(0)
  443. {
  444. }
  445. nsSocketOutputStream::~nsSocketOutputStream()
  446. {
  447. }
  448. // called on the socket transport thread...
  449. //
  450. // condition : failure code if socket has been closed
  451. //
  452. void
  453. nsSocketOutputStream::OnSocketReady(nsresult condition)
  454. {
  455. SOCKET_LOG(("nsSocketOutputStream::OnSocketReady [this=%p cond=%x]\n",
  456. this, condition));
  457. NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  458. nsCOMPtr<nsIOutputStreamCallback> callback;
  459. {
  460. MutexAutoLock lock(mTransport->mLock);
  461. // update condition, but be careful not to erase an already
  462. // existing error condition.
  463. if (NS_SUCCEEDED(mCondition))
  464. mCondition = condition;
  465. // ignore event if only waiting for closure and not closed.
  466. if (NS_FAILED(mCondition) || !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
  467. callback = mCallback;
  468. mCallback = nullptr;
  469. mCallbackFlags = 0;
  470. }
  471. }
  472. if (callback)
  473. callback->OnOutputStreamReady(this);
  474. }
  475. NS_IMPL_QUERY_INTERFACE(nsSocketOutputStream,
  476. nsIOutputStream,
  477. nsIAsyncOutputStream)
  478. NS_IMETHODIMP_(MozExternalRefCountType)
  479. nsSocketOutputStream::AddRef()
  480. {
  481. ++mWriterRefCnt;
  482. return mTransport->AddRef();
  483. }
  484. NS_IMETHODIMP_(MozExternalRefCountType)
  485. nsSocketOutputStream::Release()
  486. {
  487. if (--mWriterRefCnt == 0)
  488. Close();
  489. return mTransport->Release();
  490. }
  491. NS_IMETHODIMP
  492. nsSocketOutputStream::Close()
  493. {
  494. return CloseWithStatus(NS_BASE_STREAM_CLOSED);
  495. }
  496. NS_IMETHODIMP
  497. nsSocketOutputStream::Flush()
  498. {
  499. return NS_OK;
  500. }
  501. NS_IMETHODIMP
  502. nsSocketOutputStream::Write(const char *buf, uint32_t count, uint32_t *countWritten)
  503. {
  504. SOCKET_LOG(("nsSocketOutputStream::Write [this=%p count=%u]\n", this, count));
  505. *countWritten = 0;
  506. // A write of 0 bytes can be used to force the initial SSL handshake, so do
  507. // not reject that.
  508. PRFileDesc* fd = nullptr;
  509. {
  510. MutexAutoLock lock(mTransport->mLock);
  511. if (NS_FAILED(mCondition))
  512. return mCondition;
  513. fd = mTransport->GetFD_Locked();
  514. if (!fd)
  515. return NS_BASE_STREAM_WOULD_BLOCK;
  516. }
  517. SOCKET_LOG((" calling PR_Write [count=%u]\n", count));
  518. // cannot hold lock while calling NSPR. (worried about the fact that PSM
  519. // synchronously proxies notifications over to the UI thread, which could
  520. // mistakenly try to re-enter this code.)
  521. int32_t n = PR_Write(fd, buf, count);
  522. SOCKET_LOG((" PR_Write returned [n=%d]\n", n));
  523. nsresult rv = NS_OK;
  524. {
  525. MutexAutoLock lock(mTransport->mLock);
  526. #ifdef ENABLE_SOCKET_TRACING
  527. if (n > 0)
  528. mTransport->TraceOutBuf(buf, n);
  529. #endif
  530. mTransport->ReleaseFD_Locked(fd);
  531. if (n > 0)
  532. mByteCount += (*countWritten = n);
  533. else if (n < 0) {
  534. PRErrorCode code = PR_GetError();
  535. if (code == PR_WOULD_BLOCK_ERROR)
  536. return NS_BASE_STREAM_WOULD_BLOCK;
  537. mCondition = ErrorAccordingToNSPR(code);
  538. }
  539. rv = mCondition;
  540. }
  541. if (NS_FAILED(rv))
  542. mTransport->OnOutputClosed(rv);
  543. // only send this notification if we have indeed written some data.
  544. // see bug 196827 for an example of why this is important.
  545. if (n > 0)
  546. mTransport->SendStatus(NS_NET_STATUS_SENDING_TO);
  547. return rv;
  548. }
  549. NS_IMETHODIMP
  550. nsSocketOutputStream::WriteSegments(nsReadSegmentFun reader, void *closure,
  551. uint32_t count, uint32_t *countRead)
  552. {
  553. // socket stream is unbuffered
  554. return NS_ERROR_NOT_IMPLEMENTED;
  555. }
  556. nsresult
  557. nsSocketOutputStream::WriteFromSegments(nsIInputStream *input,
  558. void *closure,
  559. const char *fromSegment,
  560. uint32_t offset,
  561. uint32_t count,
  562. uint32_t *countRead)
  563. {
  564. nsSocketOutputStream *self = (nsSocketOutputStream *) closure;
  565. return self->Write(fromSegment, count, countRead);
  566. }
  567. NS_IMETHODIMP
  568. nsSocketOutputStream::WriteFrom(nsIInputStream *stream, uint32_t count, uint32_t *countRead)
  569. {
  570. return stream->ReadSegments(WriteFromSegments, this, count, countRead);
  571. }
  572. NS_IMETHODIMP
  573. nsSocketOutputStream::IsNonBlocking(bool *nonblocking)
  574. {
  575. *nonblocking = true;
  576. return NS_OK;
  577. }
  578. NS_IMETHODIMP
  579. nsSocketOutputStream::CloseWithStatus(nsresult reason)
  580. {
  581. SOCKET_LOG(("nsSocketOutputStream::CloseWithStatus [this=%p reason=%x]\n", this, reason));
  582. // may be called from any thread
  583. nsresult rv;
  584. {
  585. MutexAutoLock lock(mTransport->mLock);
  586. if (NS_SUCCEEDED(mCondition))
  587. rv = mCondition = reason;
  588. else
  589. rv = NS_OK;
  590. }
  591. if (NS_FAILED(rv))
  592. mTransport->OnOutputClosed(rv);
  593. return NS_OK;
  594. }
  595. NS_IMETHODIMP
  596. nsSocketOutputStream::AsyncWait(nsIOutputStreamCallback *callback,
  597. uint32_t flags,
  598. uint32_t amount,
  599. nsIEventTarget *target)
  600. {
  601. SOCKET_LOG(("nsSocketOutputStream::AsyncWait [this=%p]\n", this));
  602. {
  603. MutexAutoLock lock(mTransport->mLock);
  604. if (callback && target) {
  605. //
  606. // build event proxy
  607. //
  608. mCallback = NS_NewOutputStreamReadyEvent(callback, target);
  609. }
  610. else
  611. mCallback = callback;
  612. mCallbackFlags = flags;
  613. }
  614. mTransport->OnOutputPending();
  615. return NS_OK;
  616. }
  617. //-----------------------------------------------------------------------------
  618. // socket transport impl
  619. //-----------------------------------------------------------------------------
  620. nsSocketTransport::nsSocketTransport()
  621. : mTypes(nullptr)
  622. , mTypeCount(0)
  623. , mPort(0)
  624. , mProxyPort(0)
  625. , mOriginPort(0)
  626. , mProxyTransparent(false)
  627. , mProxyTransparentResolvesHost(false)
  628. , mHttpsProxy(false)
  629. , mConnectionFlags(0)
  630. , mState(STATE_CLOSED)
  631. , mAttached(false)
  632. , mInputClosed(true)
  633. , mOutputClosed(true)
  634. , mResolving(false)
  635. , mNetAddrIsSet(false)
  636. , mSelfAddrIsSet(false)
  637. , mNetAddrPreResolved(false)
  638. , mLock("nsSocketTransport.mLock")
  639. , mFD(this)
  640. , mFDref(0)
  641. , mFDconnected(false)
  642. , mSocketTransportService(gSocketTransportService)
  643. , mInput(this)
  644. , mOutput(this)
  645. , mQoSBits(0x00)
  646. , mKeepaliveEnabled(false)
  647. , mKeepaliveIdleTimeS(-1)
  648. , mKeepaliveRetryIntervalS(-1)
  649. , mKeepaliveProbeCount(-1)
  650. , mDoNotRetryToConnect(false)
  651. {
  652. SOCKET_LOG(("creating nsSocketTransport @%p\n", this));
  653. mTimeouts[TIMEOUT_CONNECT] = UINT16_MAX; // no timeout
  654. mTimeouts[TIMEOUT_READ_WRITE] = UINT16_MAX; // no timeout
  655. }
  656. nsSocketTransport::~nsSocketTransport()
  657. {
  658. SOCKET_LOG(("destroying nsSocketTransport @%p\n", this));
  659. CleanupTypes();
  660. }
  661. void
  662. nsSocketTransport::CleanupTypes()
  663. {
  664. // cleanup socket type info
  665. if (mTypes) {
  666. for (uint32_t i = 0; i < mTypeCount; ++i) {
  667. PL_strfree(mTypes[i]);
  668. }
  669. free(mTypes);
  670. mTypes = nullptr;
  671. }
  672. mTypeCount = 0;
  673. }
  674. nsresult
  675. nsSocketTransport::Init(const char **types, uint32_t typeCount,
  676. const nsACString &host, uint16_t port,
  677. const nsACString &hostRoute, uint16_t portRoute,
  678. nsIProxyInfo *givenProxyInfo)
  679. {
  680. nsCOMPtr<nsProxyInfo> proxyInfo;
  681. if (givenProxyInfo) {
  682. proxyInfo = do_QueryInterface(givenProxyInfo);
  683. NS_ENSURE_ARG(proxyInfo);
  684. }
  685. // init socket type info
  686. mOriginHost = host;
  687. mOriginPort = port;
  688. if (!hostRoute.IsEmpty()) {
  689. mHost = hostRoute;
  690. mPort = portRoute;
  691. } else {
  692. mHost = host;
  693. mPort = port;
  694. }
  695. if (proxyInfo) {
  696. mHttpsProxy = proxyInfo->IsHTTPS();
  697. }
  698. const char *proxyType = nullptr;
  699. mProxyInfo = proxyInfo;
  700. if (proxyInfo) {
  701. mProxyPort = proxyInfo->Port();
  702. mProxyHost = proxyInfo->Host();
  703. // grab proxy type (looking for "socks" for example)
  704. proxyType = proxyInfo->Type();
  705. if (proxyType && (proxyInfo->IsHTTP() ||
  706. proxyInfo->IsHTTPS() ||
  707. proxyInfo->IsDirect() ||
  708. !strcmp(proxyType, "unknown"))) {
  709. proxyType = nullptr;
  710. }
  711. }
  712. SOCKET_LOG(("nsSocketTransport::Init [this=%p host=%s:%hu origin=%s:%d proxy=%s:%hu]\n",
  713. this, mHost.get(), mPort, mOriginHost.get(), mOriginPort,
  714. mProxyHost.get(), mProxyPort));
  715. // include proxy type as a socket type if proxy type is not "http"
  716. mTypeCount = typeCount + (proxyType != nullptr);
  717. if (!mTypeCount)
  718. return NS_OK;
  719. // if we have socket types, then the socket provider service had
  720. // better exist!
  721. nsresult rv;
  722. nsCOMPtr<nsISocketProviderService> spserv =
  723. do_GetService(kSocketProviderServiceCID, &rv);
  724. if (NS_FAILED(rv)) return rv;
  725. mTypes = (char **) malloc(mTypeCount * sizeof(char *));
  726. if (!mTypes)
  727. return NS_ERROR_OUT_OF_MEMORY;
  728. // now verify that each socket type has a registered socket provider.
  729. for (uint32_t i = 0, type = 0; i < mTypeCount; ++i) {
  730. // store socket types
  731. if (i == 0 && proxyType)
  732. mTypes[i] = PL_strdup(proxyType);
  733. else
  734. mTypes[i] = PL_strdup(types[type++]);
  735. if (!mTypes[i]) {
  736. mTypeCount = i;
  737. return NS_ERROR_OUT_OF_MEMORY;
  738. }
  739. nsCOMPtr<nsISocketProvider> provider;
  740. rv = spserv->GetSocketProvider(mTypes[i], getter_AddRefs(provider));
  741. if (NS_FAILED(rv)) {
  742. NS_WARNING("no registered socket provider");
  743. return rv;
  744. }
  745. // note if socket type corresponds to a transparent proxy
  746. // XXX don't hardcode SOCKS here (use proxy info's flags instead).
  747. if ((strcmp(mTypes[i], "socks") == 0) ||
  748. (strcmp(mTypes[i], "socks4") == 0)) {
  749. mProxyTransparent = true;
  750. if (proxyInfo->Flags() & nsIProxyInfo::TRANSPARENT_PROXY_RESOLVES_HOST) {
  751. // we want the SOCKS layer to send the hostname
  752. // and port to the proxy and let it do the DNS.
  753. mProxyTransparentResolvesHost = true;
  754. }
  755. }
  756. }
  757. return NS_OK;
  758. }
  759. nsresult
  760. nsSocketTransport::InitPreResolved(const char **socketTypes, uint32_t typeCount,
  761. const nsACString &host, uint16_t port,
  762. const nsACString &hostRoute, uint16_t portRoute,
  763. nsIProxyInfo *proxyInfo,
  764. const mozilla::net::NetAddr* addr)
  765. {
  766. nsresult rv = Init(socketTypes, typeCount, host, port, hostRoute, portRoute, proxyInfo);
  767. if (NS_WARN_IF(NS_FAILED(rv))) {
  768. return rv;
  769. }
  770. mNetAddr = *addr;
  771. mNetAddrPreResolved = true;
  772. return NS_OK;
  773. }
  774. nsresult
  775. nsSocketTransport::InitWithFilename(const char *filename)
  776. {
  777. #if defined(XP_UNIX)
  778. size_t filenameLength = strlen(filename);
  779. if (filenameLength > sizeof(mNetAddr.local.path) - 1)
  780. return NS_ERROR_FILE_NAME_TOO_LONG;
  781. mHost.Assign(filename);
  782. mPort = 0;
  783. mTypeCount = 0;
  784. mNetAddr.local.family = AF_LOCAL;
  785. memcpy(mNetAddr.local.path, filename, filenameLength);
  786. mNetAddr.local.path[filenameLength] = '\0';
  787. mNetAddrIsSet = true;
  788. return NS_OK;
  789. #else
  790. return NS_ERROR_SOCKET_ADDRESS_NOT_SUPPORTED;
  791. #endif
  792. }
  793. nsresult
  794. nsSocketTransport::InitWithConnectedSocket(PRFileDesc *fd, const NetAddr *addr)
  795. {
  796. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  797. NS_ASSERTION(!mFD.IsInitialized(), "already initialized");
  798. char buf[kNetAddrMaxCStrBufSize];
  799. NetAddrToString(addr, buf, sizeof(buf));
  800. mHost.Assign(buf);
  801. uint16_t port;
  802. if (addr->raw.family == AF_INET)
  803. port = addr->inet.port;
  804. else if (addr->raw.family == AF_INET6)
  805. port = addr->inet6.port;
  806. else
  807. port = 0;
  808. mPort = ntohs(port);
  809. memcpy(&mNetAddr, addr, sizeof(NetAddr));
  810. mPollFlags = (PR_POLL_READ | PR_POLL_WRITE | PR_POLL_EXCEPT);
  811. mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
  812. mState = STATE_TRANSFERRING;
  813. SetSocketName(fd);
  814. mNetAddrIsSet = true;
  815. {
  816. MutexAutoLock lock(mLock);
  817. mFD = fd;
  818. mFDref = 1;
  819. mFDconnected = 1;
  820. }
  821. // make sure new socket is non-blocking
  822. PRSocketOptionData opt;
  823. opt.option = PR_SockOpt_Nonblocking;
  824. opt.value.non_blocking = true;
  825. PR_SetSocketOption(fd, &opt);
  826. SOCKET_LOG(("nsSocketTransport::InitWithConnectedSocket [this=%p addr=%s:%hu]\n",
  827. this, mHost.get(), mPort));
  828. // jump to InitiateSocket to get ourselves attached to the STS poll list.
  829. return PostEvent(MSG_RETRY_INIT_SOCKET);
  830. }
  831. nsresult
  832. nsSocketTransport::InitWithConnectedSocket(PRFileDesc* aFD,
  833. const NetAddr* aAddr,
  834. nsISupports* aSecInfo)
  835. {
  836. mSecInfo = aSecInfo;
  837. return InitWithConnectedSocket(aFD, aAddr);
  838. }
  839. nsresult
  840. nsSocketTransport::PostEvent(uint32_t type, nsresult status, nsISupports *param)
  841. {
  842. SOCKET_LOG(("nsSocketTransport::PostEvent [this=%p type=%u status=%x param=%p]\n",
  843. this, type, status, param));
  844. nsCOMPtr<nsIRunnable> event = new nsSocketEvent(this, type, status, param);
  845. if (!event)
  846. return NS_ERROR_OUT_OF_MEMORY;
  847. return mSocketTransportService->Dispatch(event, NS_DISPATCH_NORMAL);
  848. }
  849. void
  850. nsSocketTransport::SendStatus(nsresult status)
  851. {
  852. SOCKET_LOG(("nsSocketTransport::SendStatus [this=%p status=%x]\n", this, status));
  853. nsCOMPtr<nsITransportEventSink> sink;
  854. uint64_t progress;
  855. {
  856. MutexAutoLock lock(mLock);
  857. sink = mEventSink;
  858. switch (status) {
  859. case NS_NET_STATUS_SENDING_TO:
  860. progress = mOutput.ByteCount();
  861. break;
  862. case NS_NET_STATUS_RECEIVING_FROM:
  863. progress = mInput.ByteCount();
  864. break;
  865. default:
  866. progress = 0;
  867. break;
  868. }
  869. }
  870. if (sink) {
  871. sink->OnTransportStatus(this, status, progress, -1);
  872. }
  873. }
  874. nsresult
  875. nsSocketTransport::ResolveHost()
  876. {
  877. SOCKET_LOG(("nsSocketTransport::ResolveHost [this=%p %s:%d%s]\n",
  878. this, SocketHost().get(), SocketPort(),
  879. mConnectionFlags & nsSocketTransport::BYPASS_CACHE ?
  880. " bypass cache" : ""));
  881. nsresult rv;
  882. if (mNetAddrPreResolved) {
  883. mState = STATE_RESOLVING;
  884. return PostEvent(MSG_DNS_LOOKUP_COMPLETE, NS_OK, nullptr);
  885. }
  886. if (!mProxyHost.IsEmpty()) {
  887. if (!mProxyTransparent || mProxyTransparentResolvesHost) {
  888. #if defined(XP_UNIX)
  889. MOZ_ASSERT(!mNetAddrIsSet || mNetAddr.raw.family != AF_LOCAL,
  890. "Unix domain sockets can't be used with proxies");
  891. #endif
  892. // When not resolving mHost locally, we still want to ensure that
  893. // it only contains valid characters. See bug 304904 for details.
  894. // Sometimes the end host is not yet known and mHost is *
  895. if (!net_IsValidHostName(mHost) &&
  896. !mHost.Equals(NS_LITERAL_CSTRING("*"))) {
  897. SOCKET_LOG((" invalid hostname %s\n", mHost.get()));
  898. return NS_ERROR_UNKNOWN_HOST;
  899. }
  900. }
  901. if (mProxyTransparentResolvesHost) {
  902. // Name resolution is done on the server side. Just pretend
  903. // client resolution is complete, this will get picked up later.
  904. // since we don't need to do DNS now, we bypass the resolving
  905. // step by initializing mNetAddr to an empty address, but we
  906. // must keep the port. The SOCKS IO layer will use the hostname
  907. // we send it when it's created, rather than the empty address
  908. // we send with the connect call.
  909. mState = STATE_RESOLVING;
  910. mNetAddr.raw.family = AF_INET;
  911. mNetAddr.inet.port = htons(SocketPort());
  912. mNetAddr.inet.ip = htonl(INADDR_ANY);
  913. return PostEvent(MSG_DNS_LOOKUP_COMPLETE, NS_OK, nullptr);
  914. }
  915. }
  916. nsCOMPtr<nsIDNSService> dns = do_GetService(kDNSServiceCID, &rv);
  917. if (NS_FAILED(rv)) return rv;
  918. mResolving = true;
  919. uint32_t dnsFlags = 0;
  920. if (mConnectionFlags & nsSocketTransport::BYPASS_CACHE)
  921. dnsFlags = nsIDNSService::RESOLVE_BYPASS_CACHE;
  922. if (mConnectionFlags & nsSocketTransport::DISABLE_IPV6)
  923. dnsFlags |= nsIDNSService::RESOLVE_DISABLE_IPV6;
  924. if (mConnectionFlags & nsSocketTransport::DISABLE_IPV4)
  925. dnsFlags |= nsIDNSService::RESOLVE_DISABLE_IPV4;
  926. NS_ASSERTION(!(dnsFlags & nsIDNSService::RESOLVE_DISABLE_IPV6) ||
  927. !(dnsFlags & nsIDNSService::RESOLVE_DISABLE_IPV4),
  928. "Setting both RESOLVE_DISABLE_IPV6 and RESOLVE_DISABLE_IPV4");
  929. SendStatus(NS_NET_STATUS_RESOLVING_HOST);
  930. if (!SocketHost().Equals(mOriginHost)) {
  931. SOCKET_LOG(("nsSocketTransport %p origin %s doing dns for %s\n",
  932. this, mOriginHost.get(), SocketHost().get()));
  933. }
  934. rv = dns->AsyncResolveExtended(SocketHost(), dnsFlags, mNetworkInterfaceId, this,
  935. nullptr, getter_AddRefs(mDNSRequest));
  936. if (NS_SUCCEEDED(rv)) {
  937. SOCKET_LOG((" advancing to STATE_RESOLVING\n"));
  938. mState = STATE_RESOLVING;
  939. }
  940. return rv;
  941. }
  942. nsresult
  943. nsSocketTransport::BuildSocket(PRFileDesc *&fd, bool &proxyTransparent, bool &usingSSL)
  944. {
  945. SOCKET_LOG(("nsSocketTransport::BuildSocket [this=%p]\n", this));
  946. nsresult rv;
  947. proxyTransparent = false;
  948. usingSSL = false;
  949. if (mTypeCount == 0) {
  950. fd = PR_OpenTCPSocket(mNetAddr.raw.family);
  951. rv = fd ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
  952. }
  953. else {
  954. #if defined(XP_UNIX)
  955. MOZ_ASSERT(!mNetAddrIsSet || mNetAddr.raw.family != AF_LOCAL,
  956. "Unix domain sockets can't be used with socket types");
  957. #endif
  958. fd = nullptr;
  959. nsCOMPtr<nsISocketProviderService> spserv =
  960. do_GetService(kSocketProviderServiceCID, &rv);
  961. if (NS_FAILED(rv)) return rv;
  962. // by setting host to mOriginHost, instead of mHost we send the
  963. // SocketProvider (e.g. PSM) the origin hostname but can still do DNS
  964. // on an explicit alternate service host name
  965. const char *host = mOriginHost.get();
  966. int32_t port = (int32_t) mOriginPort;
  967. nsCOMPtr<nsIProxyInfo> proxyInfo = mProxyInfo;
  968. uint32_t controlFlags = 0;
  969. uint32_t i;
  970. for (i=0; i<mTypeCount; ++i) {
  971. nsCOMPtr<nsISocketProvider> provider;
  972. SOCKET_LOG((" pushing io layer [%u:%s]\n", i, mTypes[i]));
  973. rv = spserv->GetSocketProvider(mTypes[i], getter_AddRefs(provider));
  974. if (NS_FAILED(rv))
  975. break;
  976. if (mProxyTransparentResolvesHost)
  977. controlFlags |= nsISocketProvider::PROXY_RESOLVES_HOST;
  978. if (mConnectionFlags & nsISocketTransport::ANONYMOUS_CONNECT)
  979. controlFlags |= nsISocketProvider::ANONYMOUS_CONNECT;
  980. if (mConnectionFlags & nsISocketTransport::NO_PERMANENT_STORAGE)
  981. controlFlags |= nsISocketProvider::NO_PERMANENT_STORAGE;
  982. if (mConnectionFlags & nsISocketTransport::MITM_OK)
  983. controlFlags |= nsISocketProvider::MITM_OK;
  984. if (mConnectionFlags & nsISocketTransport::BE_CONSERVATIVE)
  985. controlFlags |= nsISocketProvider::BE_CONSERVATIVE;
  986. nsCOMPtr<nsISupports> secinfo;
  987. if (i == 0) {
  988. // if this is the first type, we'll want the
  989. // service to allocate a new socket
  990. // when https proxying we want to just connect to the proxy as if
  991. // it were the end host (i.e. expect the proxy's cert)
  992. rv = provider->NewSocket(mNetAddr.raw.family,
  993. mHttpsProxy ? mProxyHost.get() : host,
  994. mHttpsProxy ? mProxyPort : port,
  995. proxyInfo, mOriginAttributes,
  996. controlFlags, &fd,
  997. getter_AddRefs(secinfo));
  998. if (NS_SUCCEEDED(rv) && !fd) {
  999. NS_NOTREACHED("NewSocket succeeded but failed to create a PRFileDesc");
  1000. rv = NS_ERROR_UNEXPECTED;
  1001. }
  1002. }
  1003. else {
  1004. // the socket has already been allocated,
  1005. // so we just want the service to add itself
  1006. // to the stack (such as pushing an io layer)
  1007. rv = provider->AddToSocket(mNetAddr.raw.family,
  1008. host, port, proxyInfo,
  1009. mOriginAttributes, controlFlags, fd,
  1010. getter_AddRefs(secinfo));
  1011. }
  1012. // controlFlags = 0; not used below this point...
  1013. if (NS_FAILED(rv))
  1014. break;
  1015. // if the service was ssl or starttls, we want to hold onto the socket info
  1016. bool isSSL = (strcmp(mTypes[i], "ssl") == 0);
  1017. if (isSSL || (strcmp(mTypes[i], "starttls") == 0)) {
  1018. // remember security info and give notification callbacks to PSM...
  1019. nsCOMPtr<nsIInterfaceRequestor> callbacks;
  1020. {
  1021. MutexAutoLock lock(mLock);
  1022. mSecInfo = secinfo;
  1023. callbacks = mCallbacks;
  1024. SOCKET_LOG((" [secinfo=%x callbacks=%x]\n", mSecInfo.get(), mCallbacks.get()));
  1025. }
  1026. // don't call into PSM while holding mLock!!
  1027. nsCOMPtr<nsISSLSocketControl> secCtrl(do_QueryInterface(secinfo));
  1028. if (secCtrl)
  1029. secCtrl->SetNotificationCallbacks(callbacks);
  1030. // remember if socket type is SSL so we can ProxyStartSSL if need be.
  1031. usingSSL = isSSL;
  1032. }
  1033. else if ((strcmp(mTypes[i], "socks") == 0) ||
  1034. (strcmp(mTypes[i], "socks4") == 0)) {
  1035. // since socks is transparent, any layers above
  1036. // it do not have to worry about proxy stuff
  1037. proxyInfo = nullptr;
  1038. proxyTransparent = true;
  1039. }
  1040. }
  1041. if (NS_FAILED(rv)) {
  1042. SOCKET_LOG((" error pushing io layer [%u:%s rv=%x]\n", i, mTypes[i], rv));
  1043. if (fd) {
  1044. CloseSocket(fd);
  1045. }
  1046. }
  1047. }
  1048. return rv;
  1049. }
  1050. nsresult
  1051. nsSocketTransport::InitiateSocket()
  1052. {
  1053. SOCKET_LOG(("nsSocketTransport::InitiateSocket [this=%p]\n", this));
  1054. nsresult rv;
  1055. bool isLocal;
  1056. IsLocal(&isLocal);
  1057. if (gIOService->IsNetTearingDown()) {
  1058. return NS_ERROR_ABORT;
  1059. }
  1060. if (gIOService->IsOffline()) {
  1061. if (!isLocal)
  1062. return NS_ERROR_OFFLINE;
  1063. } else if (!isLocal) {
  1064. #ifdef DEBUG
  1065. // all IP networking has to be done from the parent
  1066. if (NS_SUCCEEDED(mCondition) &&
  1067. ((mNetAddr.raw.family == AF_INET) || (mNetAddr.raw.family == AF_INET6))) {
  1068. MOZ_ASSERT(!IsNeckoChild());
  1069. }
  1070. #endif
  1071. if (NS_SUCCEEDED(mCondition) &&
  1072. xpc::AreNonLocalConnectionsDisabled() &&
  1073. !(IsIPAddrAny(&mNetAddr) || IsIPAddrLocal(&mNetAddr))) {
  1074. nsAutoCString ipaddr;
  1075. RefPtr<nsNetAddr> netaddr = new nsNetAddr(&mNetAddr);
  1076. netaddr->GetAddress(ipaddr);
  1077. fprintf_stderr(stderr,
  1078. "FATAL ERROR: Non-local network connections are disabled and a connection "
  1079. "attempt to %s (%s) was made.\nYou should only access hostnames "
  1080. "available via the test networking proxy (if running mochitests) "
  1081. "or from a test-specific httpd.js server (if running xpcshell tests). "
  1082. "Browser services should be disabled or redirected to a local server.\n",
  1083. mHost.get(), ipaddr.get());
  1084. MOZ_CRASH("Attempting to connect to non-local address!");
  1085. }
  1086. }
  1087. // Hosts/Proxy Hosts that are Local IP Literals should not be speculatively
  1088. // connected - Bug 853423.
  1089. if (mConnectionFlags & nsISocketTransport::DISABLE_RFC1918 &&
  1090. IsIPAddrLocal(&mNetAddr)) {
  1091. if (SOCKET_LOG_ENABLED()) {
  1092. nsAutoCString netAddrCString;
  1093. netAddrCString.SetCapacity(kIPv6CStrBufSize);
  1094. if (!NetAddrToString(&mNetAddr,
  1095. netAddrCString.BeginWriting(),
  1096. kIPv6CStrBufSize))
  1097. netAddrCString = NS_LITERAL_CSTRING("<IP-to-string failed>");
  1098. SOCKET_LOG(("nsSocketTransport::InitiateSocket skipping "
  1099. "speculative connection for host [%s:%d] proxy "
  1100. "[%s:%d] with Local IP address [%s]",
  1101. mHost.get(), mPort, mProxyHost.get(), mProxyPort,
  1102. netAddrCString.get()));
  1103. }
  1104. mCondition = NS_ERROR_CONNECTION_REFUSED;
  1105. OnSocketDetached(nullptr);
  1106. return mCondition;
  1107. }
  1108. //
  1109. // find out if it is going to be ok to attach another socket to the STS.
  1110. // if not then we have to wait for the STS to tell us that it is ok.
  1111. // the notification is asynchronous, which means that when we could be
  1112. // in a race to call AttachSocket once notified. for this reason, when
  1113. // we get notified, we just re-enter this function. as a result, we are
  1114. // sure to ask again before calling AttachSocket. in this way we deal
  1115. // with the race condition. though it isn't the most elegant solution,
  1116. // it is far simpler than trying to build a system that would guarantee
  1117. // FIFO ordering (which wouldn't even be that valuable IMO). see bug
  1118. // 194402 for more info.
  1119. //
  1120. if (!mSocketTransportService->CanAttachSocket()) {
  1121. nsCOMPtr<nsIRunnable> event =
  1122. new nsSocketEvent(this, MSG_RETRY_INIT_SOCKET);
  1123. if (!event)
  1124. return NS_ERROR_OUT_OF_MEMORY;
  1125. return mSocketTransportService->NotifyWhenCanAttachSocket(event);
  1126. }
  1127. //
  1128. // if we already have a connected socket, then just attach and return.
  1129. //
  1130. if (mFD.IsInitialized()) {
  1131. rv = mSocketTransportService->AttachSocket(mFD, this);
  1132. if (NS_SUCCEEDED(rv))
  1133. mAttached = true;
  1134. return rv;
  1135. }
  1136. //
  1137. // create new socket fd, push io layers, etc.
  1138. //
  1139. PRFileDesc *fd;
  1140. bool proxyTransparent;
  1141. bool usingSSL;
  1142. rv = BuildSocket(fd, proxyTransparent, usingSSL);
  1143. if (NS_FAILED(rv)) {
  1144. SOCKET_LOG((" BuildSocket failed [rv=%x]\n", rv));
  1145. return rv;
  1146. }
  1147. // Attach network activity monitor
  1148. NetworkActivityMonitor::AttachIOLayer(fd);
  1149. PRStatus status;
  1150. // Make the socket non-blocking...
  1151. PRSocketOptionData opt;
  1152. opt.option = PR_SockOpt_Nonblocking;
  1153. opt.value.non_blocking = true;
  1154. status = PR_SetSocketOption(fd, &opt);
  1155. NS_ASSERTION(status == PR_SUCCESS, "unable to make socket non-blocking");
  1156. // disable the nagle algorithm - if we rely on it to coalesce writes into
  1157. // full packets the final packet of a multi segment POST/PUT or pipeline
  1158. // sequence is delayed a full rtt
  1159. opt.option = PR_SockOpt_NoDelay;
  1160. opt.value.no_delay = true;
  1161. PR_SetSocketOption(fd, &opt);
  1162. // if the network.tcp.sendbuffer preference is set, use it to size SO_SNDBUF
  1163. // The Windows default of 8KB is too small and as of vista sp1, autotuning
  1164. // only applies to receive window
  1165. int32_t sndBufferSize;
  1166. mSocketTransportService->GetSendBufferSize(&sndBufferSize);
  1167. if (sndBufferSize > 0) {
  1168. opt.option = PR_SockOpt_SendBufferSize;
  1169. opt.value.send_buffer_size = sndBufferSize;
  1170. PR_SetSocketOption(fd, &opt);
  1171. }
  1172. if (mQoSBits) {
  1173. opt.option = PR_SockOpt_IpTypeOfService;
  1174. opt.value.tos = mQoSBits;
  1175. PR_SetSocketOption(fd, &opt);
  1176. }
  1177. #if defined(XP_WIN)
  1178. // The linger is turned off by default. This is not a hard close, but
  1179. // closesocket should return immediately and operating system tries to send
  1180. // remaining data for certain, implementation specific, amount of time.
  1181. // https://msdn.microsoft.com/en-us/library/ms739165.aspx
  1182. //
  1183. // Turn the linger option on an set the interval to 0. This will cause hard
  1184. // close of the socket.
  1185. opt.option = PR_SockOpt_Linger;
  1186. opt.value.linger.polarity = 1;
  1187. opt.value.linger.linger = 0;
  1188. PR_SetSocketOption(fd, &opt);
  1189. #endif
  1190. // inform socket transport about this newly created socket...
  1191. rv = mSocketTransportService->AttachSocket(fd, this);
  1192. if (NS_FAILED(rv)) {
  1193. CloseSocket(fd);
  1194. return rv;
  1195. }
  1196. mAttached = true;
  1197. // assign mFD so that we can properly handle OnSocketDetached before we've
  1198. // established a connection.
  1199. {
  1200. MutexAutoLock lock(mLock);
  1201. mFD = fd;
  1202. mFDref = 1;
  1203. mFDconnected = false;
  1204. }
  1205. SOCKET_LOG((" advancing to STATE_CONNECTING\n"));
  1206. mState = STATE_CONNECTING;
  1207. mPollTimeout = mTimeouts[TIMEOUT_CONNECT];
  1208. SendStatus(NS_NET_STATUS_CONNECTING_TO);
  1209. if (SOCKET_LOG_ENABLED()) {
  1210. char buf[kNetAddrMaxCStrBufSize];
  1211. NetAddrToString(&mNetAddr, buf, sizeof(buf));
  1212. SOCKET_LOG((" trying address: %s\n", buf));
  1213. }
  1214. //
  1215. // Initiate the connect() to the host...
  1216. //
  1217. PRNetAddr prAddr;
  1218. {
  1219. if (mBindAddr) {
  1220. MutexAutoLock lock(mLock);
  1221. NetAddrToPRNetAddr(mBindAddr.get(), &prAddr);
  1222. status = PR_Bind(fd, &prAddr);
  1223. if (status != PR_SUCCESS) {
  1224. return NS_ERROR_FAILURE;
  1225. }
  1226. mBindAddr = nullptr;
  1227. }
  1228. }
  1229. NetAddrToPRNetAddr(&mNetAddr, &prAddr);
  1230. #ifdef XP_WIN
  1231. // Find the real tcp socket and set non-blocking once again!
  1232. // Bug 1158189.
  1233. PRFileDesc *bottom = PR_GetIdentitiesLayer(fd, PR_NSPR_IO_LAYER);
  1234. if (bottom) {
  1235. PROsfd osfd = PR_FileDesc2NativeHandle(bottom);
  1236. u_long nonblocking = 1;
  1237. if (ioctlsocket(osfd, FIONBIO, &nonblocking) != 0) {
  1238. NS_WARNING("Socket could not be set non-blocking!");
  1239. return NS_ERROR_FAILURE;
  1240. }
  1241. }
  1242. #endif
  1243. status = PR_Connect(fd, &prAddr, NS_SOCKET_CONNECT_TIMEOUT);
  1244. if (status == PR_SUCCESS) {
  1245. //
  1246. // we are connected!
  1247. //
  1248. OnSocketConnected();
  1249. }
  1250. else {
  1251. PRErrorCode code = PR_GetError();
  1252. #if defined(TEST_CONNECT_ERRORS)
  1253. code = RandomizeConnectError(code);
  1254. #endif
  1255. //
  1256. // If the PR_Connect(...) would block, then poll for a connection.
  1257. //
  1258. if ((PR_WOULD_BLOCK_ERROR == code) || (PR_IN_PROGRESS_ERROR == code))
  1259. mPollFlags = (PR_POLL_EXCEPT | PR_POLL_WRITE);
  1260. //
  1261. // If the socket is already connected, then return success...
  1262. //
  1263. else if (PR_IS_CONNECTED_ERROR == code) {
  1264. //
  1265. // we are connected!
  1266. //
  1267. OnSocketConnected();
  1268. if (mSecInfo && !mProxyHost.IsEmpty() && proxyTransparent && usingSSL) {
  1269. // if the connection phase is finished, and the ssl layer has
  1270. // been pushed, and we were proxying (transparently; ie. nothing
  1271. // has to happen in the protocol layer above us), it's time for
  1272. // the ssl to start doing it's thing.
  1273. nsCOMPtr<nsISSLSocketControl> secCtrl =
  1274. do_QueryInterface(mSecInfo);
  1275. if (secCtrl) {
  1276. SOCKET_LOG((" calling ProxyStartSSL()\n"));
  1277. secCtrl->ProxyStartSSL();
  1278. }
  1279. // XXX what if we were forced to poll on the socket for a successful
  1280. // connection... wouldn't we need to call ProxyStartSSL after a call
  1281. // to PR_ConnectContinue indicates that we are connected?
  1282. //
  1283. // XXX this appears to be what the old socket transport did. why
  1284. // isn't this broken?
  1285. }
  1286. }
  1287. //
  1288. // A SOCKS request was rejected; get the actual error code from
  1289. // the OS error
  1290. //
  1291. else if (PR_UNKNOWN_ERROR == code &&
  1292. mProxyTransparent &&
  1293. !mProxyHost.IsEmpty()) {
  1294. code = PR_GetOSError();
  1295. rv = ErrorAccordingToNSPR(code);
  1296. }
  1297. //
  1298. // The connection was refused...
  1299. //
  1300. else {
  1301. rv = ErrorAccordingToNSPR(code);
  1302. if ((rv == NS_ERROR_CONNECTION_REFUSED) && !mProxyHost.IsEmpty())
  1303. rv = NS_ERROR_PROXY_CONNECTION_REFUSED;
  1304. }
  1305. }
  1306. return rv;
  1307. }
  1308. bool
  1309. nsSocketTransport::RecoverFromError()
  1310. {
  1311. NS_ASSERTION(NS_FAILED(mCondition), "there should be something wrong");
  1312. SOCKET_LOG(("nsSocketTransport::RecoverFromError [this=%p state=%x cond=%x]\n",
  1313. this, mState, mCondition));
  1314. if (mDoNotRetryToConnect) {
  1315. SOCKET_LOG(("nsSocketTransport::RecoverFromError do not retry because "
  1316. "mDoNotRetryToConnect is set [this=%p]\n",
  1317. this));
  1318. return false;
  1319. }
  1320. #if defined(XP_UNIX)
  1321. // Unix domain connections don't have multiple addresses to try,
  1322. // so the recovery techniques here don't apply.
  1323. if (mNetAddrIsSet && mNetAddr.raw.family == AF_LOCAL)
  1324. return false;
  1325. #endif
  1326. // can only recover from errors in these states
  1327. if (mState != STATE_RESOLVING && mState != STATE_CONNECTING)
  1328. return false;
  1329. nsresult rv;
  1330. // OK to check this outside mLock
  1331. NS_ASSERTION(!mFDconnected, "socket should not be connected");
  1332. // all connection failures need to be reported to DNS so that the next
  1333. // time we will use a different address if available.
  1334. if (mState == STATE_CONNECTING && mDNSRecord) {
  1335. mDNSRecord->ReportUnusable(SocketPort());
  1336. }
  1337. // can only recover from these errors
  1338. if (mCondition != NS_ERROR_CONNECTION_REFUSED &&
  1339. mCondition != NS_ERROR_PROXY_CONNECTION_REFUSED &&
  1340. mCondition != NS_ERROR_NET_TIMEOUT &&
  1341. mCondition != NS_ERROR_UNKNOWN_HOST &&
  1342. mCondition != NS_ERROR_UNKNOWN_PROXY_HOST)
  1343. return false;
  1344. bool tryAgain = false;
  1345. if (mConnectionFlags & (DISABLE_IPV6 | DISABLE_IPV4) &&
  1346. mCondition == NS_ERROR_UNKNOWN_HOST &&
  1347. mState == STATE_RESOLVING &&
  1348. !mProxyTransparentResolvesHost) {
  1349. SOCKET_LOG((" trying lookup again with both ipv4/ipv6 enabled\n"));
  1350. mConnectionFlags &= ~(DISABLE_IPV6 | DISABLE_IPV4);
  1351. tryAgain = true;
  1352. }
  1353. // try next ip address only if past the resolver stage...
  1354. if (mState == STATE_CONNECTING && mDNSRecord) {
  1355. nsresult rv = mDNSRecord->GetNextAddr(SocketPort(), &mNetAddr);
  1356. if (NS_SUCCEEDED(rv)) {
  1357. SOCKET_LOG((" trying again with next ip address\n"));
  1358. tryAgain = true;
  1359. }
  1360. else if (mConnectionFlags & (DISABLE_IPV6 | DISABLE_IPV4)) {
  1361. // Drop state to closed. This will trigger new round of DNS
  1362. // resolving bellow.
  1363. // XXX Could be optimized to only switch the flags to save duplicate
  1364. // connection attempts.
  1365. SOCKET_LOG((" failed to connect all ipv4-only or ipv6-only hosts,"
  1366. " trying lookup/connect again with both ipv4/ipv6\n"));
  1367. mState = STATE_CLOSED;
  1368. mConnectionFlags &= ~(DISABLE_IPV6 | DISABLE_IPV4);
  1369. tryAgain = true;
  1370. }
  1371. }
  1372. // prepare to try again.
  1373. if (tryAgain) {
  1374. uint32_t msg;
  1375. if (mState == STATE_CONNECTING) {
  1376. mState = STATE_RESOLVING;
  1377. msg = MSG_DNS_LOOKUP_COMPLETE;
  1378. }
  1379. else {
  1380. mState = STATE_CLOSED;
  1381. msg = MSG_ENSURE_CONNECT;
  1382. }
  1383. rv = PostEvent(msg, NS_OK);
  1384. if (NS_FAILED(rv))
  1385. tryAgain = false;
  1386. }
  1387. return tryAgain;
  1388. }
  1389. // called on the socket thread only
  1390. void
  1391. nsSocketTransport::OnMsgInputClosed(nsresult reason)
  1392. {
  1393. SOCKET_LOG(("nsSocketTransport::OnMsgInputClosed [this=%p reason=%x]\n",
  1394. this, reason));
  1395. NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  1396. mInputClosed = true;
  1397. // check if event should affect entire transport
  1398. if (NS_FAILED(reason) && (reason != NS_BASE_STREAM_CLOSED))
  1399. mCondition = reason; // XXX except if NS_FAILED(mCondition), right??
  1400. else if (mOutputClosed)
  1401. mCondition = NS_BASE_STREAM_CLOSED; // XXX except if NS_FAILED(mCondition), right??
  1402. else {
  1403. if (mState == STATE_TRANSFERRING)
  1404. mPollFlags &= ~PR_POLL_READ;
  1405. mInput.OnSocketReady(reason);
  1406. }
  1407. }
  1408. // called on the socket thread only
  1409. void
  1410. nsSocketTransport::OnMsgOutputClosed(nsresult reason)
  1411. {
  1412. SOCKET_LOG(("nsSocketTransport::OnMsgOutputClosed [this=%p reason=%x]\n",
  1413. this, reason));
  1414. NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  1415. mOutputClosed = true;
  1416. // check if event should affect entire transport
  1417. if (NS_FAILED(reason) && (reason != NS_BASE_STREAM_CLOSED))
  1418. mCondition = reason; // XXX except if NS_FAILED(mCondition), right??
  1419. else if (mInputClosed)
  1420. mCondition = NS_BASE_STREAM_CLOSED; // XXX except if NS_FAILED(mCondition), right??
  1421. else {
  1422. if (mState == STATE_TRANSFERRING)
  1423. mPollFlags &= ~PR_POLL_WRITE;
  1424. mOutput.OnSocketReady(reason);
  1425. }
  1426. }
  1427. void
  1428. nsSocketTransport::OnSocketConnected()
  1429. {
  1430. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  1431. SOCKET_LOG((" advancing to STATE_TRANSFERRING\n"));
  1432. mPollFlags = (PR_POLL_READ | PR_POLL_WRITE | PR_POLL_EXCEPT);
  1433. mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
  1434. mState = STATE_TRANSFERRING;
  1435. // Set the m*AddrIsSet flags only when state has reached TRANSFERRING
  1436. // because we need to make sure its value does not change due to failover
  1437. mNetAddrIsSet = true;
  1438. // assign mFD (must do this within the transport lock), but take care not
  1439. // to trample over mFDref if mFD is already set.
  1440. {
  1441. MutexAutoLock lock(mLock);
  1442. NS_ASSERTION(mFD.IsInitialized(), "no socket");
  1443. NS_ASSERTION(mFDref == 1, "wrong socket ref count");
  1444. SetSocketName(mFD);
  1445. mFDconnected = true;
  1446. }
  1447. // Ensure keepalive is configured correctly if previously enabled.
  1448. if (mKeepaliveEnabled) {
  1449. nsresult rv = SetKeepaliveEnabledInternal(true);
  1450. if (NS_WARN_IF(NS_FAILED(rv))) {
  1451. SOCKET_LOG((" SetKeepaliveEnabledInternal failed rv[0x%x]", rv));
  1452. }
  1453. }
  1454. SendStatus(NS_NET_STATUS_CONNECTED_TO);
  1455. }
  1456. void
  1457. nsSocketTransport::SetSocketName(PRFileDesc *fd)
  1458. {
  1459. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  1460. if (mSelfAddrIsSet) {
  1461. return;
  1462. }
  1463. PRNetAddr prAddr;
  1464. memset(&prAddr, 0, sizeof(prAddr));
  1465. if (PR_GetSockName(fd, &prAddr) == PR_SUCCESS) {
  1466. PRNetAddrToNetAddr(&prAddr, &mSelfAddr);
  1467. mSelfAddrIsSet = true;
  1468. }
  1469. }
  1470. PRFileDesc *
  1471. nsSocketTransport::GetFD_Locked()
  1472. {
  1473. mLock.AssertCurrentThreadOwns();
  1474. // mFD is not available to the streams while disconnected.
  1475. if (!mFDconnected)
  1476. return nullptr;
  1477. if (mFD.IsInitialized())
  1478. mFDref++;
  1479. return mFD;
  1480. }
  1481. class ThunkPRClose : public Runnable
  1482. {
  1483. public:
  1484. explicit ThunkPRClose(PRFileDesc *fd) : mFD(fd) {}
  1485. NS_IMETHOD Run() override
  1486. {
  1487. nsSocketTransport::CloseSocket(mFD);
  1488. return NS_OK;
  1489. }
  1490. private:
  1491. PRFileDesc *mFD;
  1492. };
  1493. void
  1494. STS_PRCloseOnSocketTransport(PRFileDesc *fd)
  1495. {
  1496. if (gSocketTransportService) {
  1497. // Can't PR_Close() a socket off STS thread. Thunk it to STS to die
  1498. // FIX - Should use RUN_ON_THREAD once it's generally available
  1499. // RUN_ON_THREAD(gSocketThread,WrapRunnableNM(&PR_Close, mFD);
  1500. gSocketTransportService->Dispatch(new ThunkPRClose(fd), NS_DISPATCH_NORMAL);
  1501. } else {
  1502. // something horrible has happened
  1503. NS_ASSERTION(gSocketTransportService, "No STS service");
  1504. }
  1505. }
  1506. void
  1507. nsSocketTransport::ReleaseFD_Locked(PRFileDesc *fd)
  1508. {
  1509. mLock.AssertCurrentThreadOwns();
  1510. NS_ASSERTION(mFD == fd, "wrong fd");
  1511. SOCKET_LOG(("JIMB: ReleaseFD_Locked: mFDref = %d\n", mFDref));
  1512. if (--mFDref == 0) {
  1513. if (gIOService->IsNetTearingDown() &&
  1514. ((PR_IntervalNow() - gIOService->NetTearingDownStarted()) >
  1515. gSocketTransportService->MaxTimeForPrClosePref())) {
  1516. // If shutdown last to long, let the socket leak and do not close it.
  1517. SOCKET_LOG(("Intentional leak"));
  1518. } else if (PR_GetCurrentThread() == gSocketThread) {
  1519. SOCKET_LOG(("nsSocketTransport: calling PR_Close [this=%p]\n", this));
  1520. CloseSocket(mFD);
  1521. } else {
  1522. // Can't PR_Close() a socket off STS thread. Thunk it to STS to die
  1523. STS_PRCloseOnSocketTransport(mFD);
  1524. }
  1525. mFD = nullptr;
  1526. }
  1527. }
  1528. //-----------------------------------------------------------------------------
  1529. // socket event handler impl
  1530. void
  1531. nsSocketTransport::OnSocketEvent(uint32_t type, nsresult status, nsISupports *param)
  1532. {
  1533. SOCKET_LOG(("nsSocketTransport::OnSocketEvent [this=%p type=%u status=%x param=%p]\n",
  1534. this, type, status, param));
  1535. if (NS_FAILED(mCondition)) {
  1536. // block event since we're apparently already dead.
  1537. SOCKET_LOG((" blocking event [condition=%x]\n", mCondition));
  1538. //
  1539. // notify input/output streams in case either has a pending notify.
  1540. //
  1541. mInput.OnSocketReady(mCondition);
  1542. mOutput.OnSocketReady(mCondition);
  1543. return;
  1544. }
  1545. switch (type) {
  1546. case MSG_ENSURE_CONNECT:
  1547. SOCKET_LOG((" MSG_ENSURE_CONNECT\n"));
  1548. //
  1549. // ensure that we have created a socket, attached it, and have a
  1550. // connection.
  1551. //
  1552. if (mState == STATE_CLOSED) {
  1553. // Unix domain sockets are ready to connect; mNetAddr is all we
  1554. // need. Internet address families require a DNS lookup (or possibly
  1555. // several) before we can connect.
  1556. #if defined(XP_UNIX)
  1557. if (mNetAddrIsSet && mNetAddr.raw.family == AF_LOCAL)
  1558. mCondition = InitiateSocket();
  1559. else
  1560. #endif
  1561. mCondition = ResolveHost();
  1562. } else {
  1563. SOCKET_LOG((" ignoring redundant event\n"));
  1564. }
  1565. break;
  1566. case MSG_DNS_LOOKUP_COMPLETE:
  1567. if (mDNSRequest) // only send this if we actually resolved anything
  1568. SendStatus(NS_NET_STATUS_RESOLVED_HOST);
  1569. SOCKET_LOG((" MSG_DNS_LOOKUP_COMPLETE\n"));
  1570. mDNSRequest = nullptr;
  1571. if (param) {
  1572. mDNSRecord = static_cast<nsIDNSRecord *>(param);
  1573. mDNSRecord->GetNextAddr(SocketPort(), &mNetAddr);
  1574. }
  1575. // status contains DNS lookup status
  1576. if (NS_FAILED(status)) {
  1577. // When using a HTTP proxy, NS_ERROR_UNKNOWN_HOST means the HTTP
  1578. // proxy host is not found, so we fixup the error code.
  1579. // For SOCKS proxies (mProxyTransparent == true), the socket
  1580. // transport resolves the real host here, so there's no fixup
  1581. // (see bug 226943).
  1582. if ((status == NS_ERROR_UNKNOWN_HOST) && !mProxyTransparent &&
  1583. !mProxyHost.IsEmpty())
  1584. mCondition = NS_ERROR_UNKNOWN_PROXY_HOST;
  1585. else
  1586. mCondition = status;
  1587. }
  1588. else if (mState == STATE_RESOLVING) {
  1589. mCondition = InitiateSocket();
  1590. }
  1591. break;
  1592. case MSG_RETRY_INIT_SOCKET:
  1593. mCondition = InitiateSocket();
  1594. break;
  1595. case MSG_INPUT_CLOSED:
  1596. SOCKET_LOG((" MSG_INPUT_CLOSED\n"));
  1597. OnMsgInputClosed(status);
  1598. break;
  1599. case MSG_INPUT_PENDING:
  1600. SOCKET_LOG((" MSG_INPUT_PENDING\n"));
  1601. OnMsgInputPending();
  1602. break;
  1603. case MSG_OUTPUT_CLOSED:
  1604. SOCKET_LOG((" MSG_OUTPUT_CLOSED\n"));
  1605. OnMsgOutputClosed(status);
  1606. break;
  1607. case MSG_OUTPUT_PENDING:
  1608. SOCKET_LOG((" MSG_OUTPUT_PENDING\n"));
  1609. OnMsgOutputPending();
  1610. break;
  1611. case MSG_TIMEOUT_CHANGED:
  1612. SOCKET_LOG((" MSG_TIMEOUT_CHANGED\n"));
  1613. mPollTimeout = mTimeouts[(mState == STATE_TRANSFERRING)
  1614. ? TIMEOUT_READ_WRITE : TIMEOUT_CONNECT];
  1615. break;
  1616. default:
  1617. SOCKET_LOG((" unhandled event!\n"));
  1618. }
  1619. if (NS_FAILED(mCondition)) {
  1620. SOCKET_LOG((" after event [this=%p cond=%x]\n", this, mCondition));
  1621. if (!mAttached) // need to process this error ourselves...
  1622. OnSocketDetached(nullptr);
  1623. }
  1624. else if (mPollFlags == PR_POLL_EXCEPT)
  1625. mPollFlags = 0; // make idle
  1626. }
  1627. //-----------------------------------------------------------------------------
  1628. // socket handler impl
  1629. void
  1630. nsSocketTransport::OnSocketReady(PRFileDesc *fd, int16_t outFlags)
  1631. {
  1632. SOCKET_LOG(("nsSocketTransport::OnSocketReady [this=%p outFlags=%hd]\n",
  1633. this, outFlags));
  1634. if (outFlags == -1) {
  1635. SOCKET_LOG(("socket timeout expired\n"));
  1636. mCondition = NS_ERROR_NET_TIMEOUT;
  1637. return;
  1638. }
  1639. if (mState == STATE_TRANSFERRING) {
  1640. // if waiting to write and socket is writable or hit an exception.
  1641. if ((mPollFlags & PR_POLL_WRITE) && (outFlags & ~PR_POLL_READ)) {
  1642. // assume that we won't need to poll any longer (the stream will
  1643. // request that we poll again if it is still pending).
  1644. mPollFlags &= ~PR_POLL_WRITE;
  1645. mOutput.OnSocketReady(NS_OK);
  1646. }
  1647. // if waiting to read and socket is readable or hit an exception.
  1648. if ((mPollFlags & PR_POLL_READ) && (outFlags & ~PR_POLL_WRITE)) {
  1649. // assume that we won't need to poll any longer (the stream will
  1650. // request that we poll again if it is still pending).
  1651. mPollFlags &= ~PR_POLL_READ;
  1652. mInput.OnSocketReady(NS_OK);
  1653. }
  1654. // Update poll timeout in case it was changed
  1655. mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
  1656. }
  1657. else if ((mState == STATE_CONNECTING) && !gIOService->IsNetTearingDown()) {
  1658. // We do not need to do PR_ConnectContinue when we are already
  1659. // shutting down.
  1660. PRStatus status = PR_ConnectContinue(fd, outFlags);
  1661. if (status == PR_SUCCESS) {
  1662. //
  1663. // we are connected!
  1664. //
  1665. OnSocketConnected();
  1666. }
  1667. else {
  1668. PRErrorCode code = PR_GetError();
  1669. #if defined(TEST_CONNECT_ERRORS)
  1670. code = RandomizeConnectError(code);
  1671. #endif
  1672. //
  1673. // If the connect is still not ready, then continue polling...
  1674. //
  1675. if ((PR_WOULD_BLOCK_ERROR == code) || (PR_IN_PROGRESS_ERROR == code)) {
  1676. // Set up the select flags for connect...
  1677. mPollFlags = (PR_POLL_EXCEPT | PR_POLL_WRITE);
  1678. // Update poll timeout in case it was changed
  1679. mPollTimeout = mTimeouts[TIMEOUT_CONNECT];
  1680. }
  1681. //
  1682. // The SOCKS proxy rejected our request. Find out why.
  1683. //
  1684. else if (PR_UNKNOWN_ERROR == code &&
  1685. mProxyTransparent &&
  1686. !mProxyHost.IsEmpty()) {
  1687. code = PR_GetOSError();
  1688. mCondition = ErrorAccordingToNSPR(code);
  1689. }
  1690. else {
  1691. //
  1692. // else, the connection failed...
  1693. //
  1694. mCondition = ErrorAccordingToNSPR(code);
  1695. if ((mCondition == NS_ERROR_CONNECTION_REFUSED) && !mProxyHost.IsEmpty())
  1696. mCondition = NS_ERROR_PROXY_CONNECTION_REFUSED;
  1697. SOCKET_LOG((" connection failed! [reason=%x]\n", mCondition));
  1698. }
  1699. }
  1700. }
  1701. else if ((mState == STATE_CONNECTING) && gIOService->IsNetTearingDown()) {
  1702. // We do not need to do PR_ConnectContinue when we are already
  1703. // shutting down.
  1704. SOCKET_LOG(("We are in shutdown so skip PR_ConnectContinue and set "
  1705. "and error.\n"));
  1706. mCondition = NS_ERROR_ABORT;
  1707. }
  1708. else {
  1709. NS_ERROR("unexpected socket state");
  1710. mCondition = NS_ERROR_UNEXPECTED;
  1711. }
  1712. if (mPollFlags == PR_POLL_EXCEPT)
  1713. mPollFlags = 0; // make idle
  1714. }
  1715. // called on the socket thread only
  1716. void
  1717. nsSocketTransport::OnSocketDetached(PRFileDesc *fd)
  1718. {
  1719. SOCKET_LOG(("nsSocketTransport::OnSocketDetached [this=%p cond=%x]\n",
  1720. this, mCondition));
  1721. NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  1722. // if we didn't initiate this detach, then be sure to pass an error
  1723. // condition up to our consumers. (e.g., STS is shutting down.)
  1724. if (NS_SUCCEEDED(mCondition)) {
  1725. if (gIOService->IsOffline()) {
  1726. mCondition = NS_ERROR_OFFLINE;
  1727. }
  1728. else {
  1729. mCondition = NS_ERROR_ABORT;
  1730. }
  1731. }
  1732. // If we are not shutting down try again.
  1733. if (!gIOService->IsNetTearingDown() && RecoverFromError())
  1734. mCondition = NS_OK;
  1735. else {
  1736. mState = STATE_CLOSED;
  1737. // make sure there isn't any pending DNS request
  1738. if (mDNSRequest) {
  1739. mDNSRequest->Cancel(NS_ERROR_ABORT);
  1740. mDNSRequest = nullptr;
  1741. }
  1742. //
  1743. // notify input/output streams
  1744. //
  1745. mInput.OnSocketReady(mCondition);
  1746. mOutput.OnSocketReady(mCondition);
  1747. }
  1748. // break any potential reference cycle between the security info object
  1749. // and ourselves by resetting its notification callbacks object. see
  1750. // bug 285991 for details.
  1751. nsCOMPtr<nsISSLSocketControl> secCtrl = do_QueryInterface(mSecInfo);
  1752. if (secCtrl)
  1753. secCtrl->SetNotificationCallbacks(nullptr);
  1754. // finally, release our reference to the socket (must do this within
  1755. // the transport lock) possibly closing the socket. Also release our
  1756. // listeners to break potential refcount cycles.
  1757. // We should be careful not to release mEventSink and mCallbacks while
  1758. // we're locked, because releasing it might require acquiring the lock
  1759. // again, so we just null out mEventSink and mCallbacks while we're
  1760. // holding the lock, and let the stack based objects' destuctors take
  1761. // care of destroying it if needed.
  1762. nsCOMPtr<nsIInterfaceRequestor> ourCallbacks;
  1763. nsCOMPtr<nsITransportEventSink> ourEventSink;
  1764. {
  1765. MutexAutoLock lock(mLock);
  1766. if (mFD.IsInitialized()) {
  1767. ReleaseFD_Locked(mFD);
  1768. // flag mFD as unusable; this prevents other consumers from
  1769. // acquiring a reference to mFD.
  1770. mFDconnected = false;
  1771. }
  1772. // We must release mCallbacks and mEventSink to avoid memory leak
  1773. // but only when RecoverFromError() above failed. Otherwise we lose
  1774. // link with UI and security callbacks on next connection attempt
  1775. // round. That would lead e.g. to a broken certificate exception page.
  1776. if (NS_FAILED(mCondition)) {
  1777. mCallbacks.swap(ourCallbacks);
  1778. mEventSink.swap(ourEventSink);
  1779. }
  1780. }
  1781. }
  1782. void
  1783. nsSocketTransport::IsLocal(bool *aIsLocal)
  1784. {
  1785. {
  1786. MutexAutoLock lock(mLock);
  1787. #if defined(XP_UNIX)
  1788. // Unix-domain sockets are always local.
  1789. if (mNetAddr.raw.family == PR_AF_LOCAL)
  1790. {
  1791. *aIsLocal = true;
  1792. return;
  1793. }
  1794. #endif
  1795. *aIsLocal = IsLoopBackAddress(&mNetAddr);
  1796. }
  1797. }
  1798. //-----------------------------------------------------------------------------
  1799. // xpcom api
  1800. NS_IMPL_ISUPPORTS(nsSocketTransport,
  1801. nsISocketTransport,
  1802. nsITransport,
  1803. nsIDNSListener,
  1804. nsIClassInfo,
  1805. nsIInterfaceRequestor)
  1806. NS_IMPL_CI_INTERFACE_GETTER(nsSocketTransport,
  1807. nsISocketTransport,
  1808. nsITransport,
  1809. nsIDNSListener,
  1810. nsIInterfaceRequestor)
  1811. NS_IMETHODIMP
  1812. nsSocketTransport::OpenInputStream(uint32_t flags,
  1813. uint32_t segsize,
  1814. uint32_t segcount,
  1815. nsIInputStream **aResult)
  1816. {
  1817. SOCKET_LOG(("nsSocketTransport::OpenInputStream [this=%p flags=%x]\n",
  1818. this, flags));
  1819. NS_ENSURE_TRUE(!mInput.IsReferenced(), NS_ERROR_UNEXPECTED);
  1820. nsresult rv;
  1821. nsCOMPtr<nsIAsyncInputStream> pipeIn;
  1822. nsCOMPtr<nsIInputStream> result;
  1823. if (!(flags & OPEN_UNBUFFERED) || (flags & OPEN_BLOCKING)) {
  1824. // XXX if the caller wants blocking, then the caller also gets buffered!
  1825. //bool openBuffered = !(flags & OPEN_UNBUFFERED);
  1826. bool openBlocking = (flags & OPEN_BLOCKING);
  1827. net_ResolveSegmentParams(segsize, segcount);
  1828. // create a pipe
  1829. nsCOMPtr<nsIAsyncOutputStream> pipeOut;
  1830. rv = NS_NewPipe2(getter_AddRefs(pipeIn), getter_AddRefs(pipeOut),
  1831. !openBlocking, true, segsize, segcount);
  1832. if (NS_FAILED(rv)) return rv;
  1833. // async copy from socket to pipe
  1834. rv = NS_AsyncCopy(&mInput, pipeOut, mSocketTransportService,
  1835. NS_ASYNCCOPY_VIA_WRITESEGMENTS, segsize);
  1836. if (NS_FAILED(rv)) return rv;
  1837. result = pipeIn;
  1838. }
  1839. else
  1840. result = &mInput;
  1841. // flag input stream as open
  1842. mInputClosed = false;
  1843. rv = PostEvent(MSG_ENSURE_CONNECT);
  1844. if (NS_FAILED(rv)) {
  1845. return rv;
  1846. }
  1847. result.forget(aResult);
  1848. return NS_OK;
  1849. }
  1850. NS_IMETHODIMP
  1851. nsSocketTransport::OpenOutputStream(uint32_t flags,
  1852. uint32_t segsize,
  1853. uint32_t segcount,
  1854. nsIOutputStream **aResult)
  1855. {
  1856. SOCKET_LOG(("nsSocketTransport::OpenOutputStream [this=%p flags=%x]\n",
  1857. this, flags));
  1858. NS_ENSURE_TRUE(!mOutput.IsReferenced(), NS_ERROR_UNEXPECTED);
  1859. nsresult rv;
  1860. nsCOMPtr<nsIAsyncOutputStream> pipeOut;
  1861. nsCOMPtr<nsIOutputStream> result;
  1862. if (!(flags & OPEN_UNBUFFERED) || (flags & OPEN_BLOCKING)) {
  1863. // XXX if the caller wants blocking, then the caller also gets buffered!
  1864. //bool openBuffered = !(flags & OPEN_UNBUFFERED);
  1865. bool openBlocking = (flags & OPEN_BLOCKING);
  1866. net_ResolveSegmentParams(segsize, segcount);
  1867. // create a pipe
  1868. nsCOMPtr<nsIAsyncInputStream> pipeIn;
  1869. rv = NS_NewPipe2(getter_AddRefs(pipeIn), getter_AddRefs(pipeOut),
  1870. true, !openBlocking, segsize, segcount);
  1871. if (NS_FAILED(rv)) return rv;
  1872. // async copy from socket to pipe
  1873. rv = NS_AsyncCopy(pipeIn, &mOutput, mSocketTransportService,
  1874. NS_ASYNCCOPY_VIA_READSEGMENTS, segsize);
  1875. if (NS_FAILED(rv)) return rv;
  1876. result = pipeOut;
  1877. }
  1878. else
  1879. result = &mOutput;
  1880. // flag output stream as open
  1881. mOutputClosed = false;
  1882. rv = PostEvent(MSG_ENSURE_CONNECT);
  1883. if (NS_FAILED(rv)) return rv;
  1884. result.forget(aResult);
  1885. return NS_OK;
  1886. }
  1887. NS_IMETHODIMP
  1888. nsSocketTransport::Close(nsresult reason)
  1889. {
  1890. if (NS_SUCCEEDED(reason))
  1891. reason = NS_BASE_STREAM_CLOSED;
  1892. mDoNotRetryToConnect = true;
  1893. mInput.CloseWithStatus(reason);
  1894. mOutput.CloseWithStatus(reason);
  1895. return NS_OK;
  1896. }
  1897. NS_IMETHODIMP
  1898. nsSocketTransport::GetSecurityInfo(nsISupports **secinfo)
  1899. {
  1900. MutexAutoLock lock(mLock);
  1901. NS_IF_ADDREF(*secinfo = mSecInfo);
  1902. return NS_OK;
  1903. }
  1904. NS_IMETHODIMP
  1905. nsSocketTransport::GetSecurityCallbacks(nsIInterfaceRequestor **callbacks)
  1906. {
  1907. MutexAutoLock lock(mLock);
  1908. NS_IF_ADDREF(*callbacks = mCallbacks);
  1909. return NS_OK;
  1910. }
  1911. NS_IMETHODIMP
  1912. nsSocketTransport::SetSecurityCallbacks(nsIInterfaceRequestor *callbacks)
  1913. {
  1914. nsCOMPtr<nsIInterfaceRequestor> threadsafeCallbacks;
  1915. NS_NewNotificationCallbacksAggregation(callbacks, nullptr,
  1916. NS_GetCurrentThread(),
  1917. getter_AddRefs(threadsafeCallbacks));
  1918. nsCOMPtr<nsISupports> secinfo;
  1919. {
  1920. MutexAutoLock lock(mLock);
  1921. mCallbacks = threadsafeCallbacks;
  1922. SOCKET_LOG(("Reset callbacks for secinfo=%p callbacks=%p\n",
  1923. mSecInfo.get(), mCallbacks.get()));
  1924. secinfo = mSecInfo;
  1925. }
  1926. // don't call into PSM while holding mLock!!
  1927. nsCOMPtr<nsISSLSocketControl> secCtrl(do_QueryInterface(secinfo));
  1928. if (secCtrl)
  1929. secCtrl->SetNotificationCallbacks(threadsafeCallbacks);
  1930. return NS_OK;
  1931. }
  1932. NS_IMETHODIMP
  1933. nsSocketTransport::SetEventSink(nsITransportEventSink *sink,
  1934. nsIEventTarget *target)
  1935. {
  1936. nsCOMPtr<nsITransportEventSink> temp;
  1937. if (target) {
  1938. nsresult rv = net_NewTransportEventSinkProxy(getter_AddRefs(temp),
  1939. sink, target);
  1940. if (NS_FAILED(rv))
  1941. return rv;
  1942. sink = temp.get();
  1943. }
  1944. MutexAutoLock lock(mLock);
  1945. mEventSink = sink;
  1946. return NS_OK;
  1947. }
  1948. NS_IMETHODIMP
  1949. nsSocketTransport::IsAlive(bool *result)
  1950. {
  1951. *result = false;
  1952. nsresult conditionWhileLocked = NS_OK;
  1953. PRFileDescAutoLock fd(this, &conditionWhileLocked);
  1954. if (NS_FAILED(conditionWhileLocked) || !fd.IsInitialized()) {
  1955. return NS_OK;
  1956. }
  1957. // XXX do some idle-time based checks??
  1958. char c;
  1959. int32_t rval = PR_Recv(fd, &c, 1, PR_MSG_PEEK, 0);
  1960. if ((rval > 0) || (rval < 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR))
  1961. *result = true;
  1962. return NS_OK;
  1963. }
  1964. NS_IMETHODIMP
  1965. nsSocketTransport::GetHost(nsACString &host)
  1966. {
  1967. host = SocketHost();
  1968. return NS_OK;
  1969. }
  1970. NS_IMETHODIMP
  1971. nsSocketTransport::GetPort(int32_t *port)
  1972. {
  1973. *port = (int32_t) SocketPort();
  1974. return NS_OK;
  1975. }
  1976. NS_IMETHODIMP
  1977. nsSocketTransport::GetNetworkInterfaceId(nsACString_internal &aNetworkInterfaceId)
  1978. {
  1979. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  1980. aNetworkInterfaceId = mNetworkInterfaceId;
  1981. return NS_OK;
  1982. }
  1983. NS_IMETHODIMP
  1984. nsSocketTransport::SetNetworkInterfaceId(const nsACString_internal &aNetworkInterfaceId)
  1985. {
  1986. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  1987. mNetworkInterfaceId = aNetworkInterfaceId;
  1988. return NS_OK;
  1989. }
  1990. NS_IMETHODIMP
  1991. nsSocketTransport::GetScriptableOriginAttributes(JSContext* aCx,
  1992. JS::MutableHandle<JS::Value> aOriginAttributes)
  1993. {
  1994. if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aOriginAttributes))) {
  1995. return NS_ERROR_FAILURE;
  1996. }
  1997. return NS_OK;
  1998. }
  1999. NS_IMETHODIMP
  2000. nsSocketTransport::SetScriptableOriginAttributes(JSContext* aCx,
  2001. JS::Handle<JS::Value> aOriginAttributes)
  2002. {
  2003. MutexAutoLock lock(mLock);
  2004. NS_ENSURE_FALSE(mFD.IsInitialized(), NS_ERROR_FAILURE);
  2005. NeckoOriginAttributes attrs;
  2006. if (!aOriginAttributes.isObject() || !attrs.Init(aCx, aOriginAttributes)) {
  2007. return NS_ERROR_INVALID_ARG;
  2008. }
  2009. mOriginAttributes = attrs;
  2010. return NS_OK;
  2011. }
  2012. nsresult
  2013. nsSocketTransport::GetOriginAttributes(NeckoOriginAttributes* aOriginAttributes)
  2014. {
  2015. NS_ENSURE_ARG(aOriginAttributes);
  2016. *aOriginAttributes = mOriginAttributes;
  2017. return NS_OK;
  2018. }
  2019. nsresult
  2020. nsSocketTransport::SetOriginAttributes(const NeckoOriginAttributes& aOriginAttributes)
  2021. {
  2022. MutexAutoLock lock(mLock);
  2023. NS_ENSURE_FALSE(mFD.IsInitialized(), NS_ERROR_FAILURE);
  2024. mOriginAttributes = aOriginAttributes;
  2025. return NS_OK;
  2026. }
  2027. NS_IMETHODIMP
  2028. nsSocketTransport::GetPeerAddr(NetAddr *addr)
  2029. {
  2030. // once we are in the connected state, mNetAddr will not change.
  2031. // so if we can verify that we are in the connected state, then
  2032. // we can freely access mNetAddr from any thread without being
  2033. // inside a critical section.
  2034. if (!mNetAddrIsSet) {
  2035. SOCKET_LOG(("nsSocketTransport::GetPeerAddr [this=%p state=%d] "
  2036. "NOT_AVAILABLE because not yet connected.", this, mState));
  2037. return NS_ERROR_NOT_AVAILABLE;
  2038. }
  2039. memcpy(addr, &mNetAddr, sizeof(NetAddr));
  2040. return NS_OK;
  2041. }
  2042. NS_IMETHODIMP
  2043. nsSocketTransport::GetSelfAddr(NetAddr *addr)
  2044. {
  2045. // once we are in the connected state, mSelfAddr will not change.
  2046. // so if we can verify that we are in the connected state, then
  2047. // we can freely access mSelfAddr from any thread without being
  2048. // inside a critical section.
  2049. if (!mSelfAddrIsSet) {
  2050. SOCKET_LOG(("nsSocketTransport::GetSelfAddr [this=%p state=%d] "
  2051. "NOT_AVAILABLE because not yet connected.", this, mState));
  2052. return NS_ERROR_NOT_AVAILABLE;
  2053. }
  2054. memcpy(addr, &mSelfAddr, sizeof(NetAddr));
  2055. return NS_OK;
  2056. }
  2057. NS_IMETHODIMP
  2058. nsSocketTransport::Bind(NetAddr *aLocalAddr)
  2059. {
  2060. NS_ENSURE_ARG(aLocalAddr);
  2061. MutexAutoLock lock(mLock);
  2062. if (mAttached) {
  2063. return NS_ERROR_FAILURE;
  2064. }
  2065. mBindAddr = new NetAddr();
  2066. memcpy(mBindAddr.get(), aLocalAddr, sizeof(NetAddr));
  2067. return NS_OK;
  2068. }
  2069. NS_IMETHODIMP
  2070. nsSocketTransport::GetScriptablePeerAddr(nsINetAddr * *addr)
  2071. {
  2072. NetAddr rawAddr;
  2073. nsresult rv;
  2074. rv = GetPeerAddr(&rawAddr);
  2075. if (NS_FAILED(rv))
  2076. return rv;
  2077. NS_ADDREF(*addr = new nsNetAddr(&rawAddr));
  2078. return NS_OK;
  2079. }
  2080. NS_IMETHODIMP
  2081. nsSocketTransport::GetScriptableSelfAddr(nsINetAddr * *addr)
  2082. {
  2083. NetAddr rawAddr;
  2084. nsresult rv;
  2085. rv = GetSelfAddr(&rawAddr);
  2086. if (NS_FAILED(rv))
  2087. return rv;
  2088. NS_ADDREF(*addr = new nsNetAddr(&rawAddr));
  2089. return NS_OK;
  2090. }
  2091. NS_IMETHODIMP
  2092. nsSocketTransport::GetTimeout(uint32_t type, uint32_t *value)
  2093. {
  2094. NS_ENSURE_ARG_MAX(type, nsISocketTransport::TIMEOUT_READ_WRITE);
  2095. *value = (uint32_t) mTimeouts[type];
  2096. return NS_OK;
  2097. }
  2098. NS_IMETHODIMP
  2099. nsSocketTransport::SetTimeout(uint32_t type, uint32_t value)
  2100. {
  2101. NS_ENSURE_ARG_MAX(type, nsISocketTransport::TIMEOUT_READ_WRITE);
  2102. // truncate overly large timeout values.
  2103. mTimeouts[type] = (uint16_t) std::min<uint32_t>(value, UINT16_MAX);
  2104. PostEvent(MSG_TIMEOUT_CHANGED);
  2105. return NS_OK;
  2106. }
  2107. NS_IMETHODIMP
  2108. nsSocketTransport::SetQoSBits(uint8_t aQoSBits)
  2109. {
  2110. // Don't do any checking here of bits. Why? Because as of RFC-4594
  2111. // several different Class Selector and Assured Forwarding values
  2112. // have been defined, but that isn't to say more won't be added later.
  2113. // In that case, any checking would be an impediment to interoperating
  2114. // with newer QoS definitions.
  2115. mQoSBits = aQoSBits;
  2116. return NS_OK;
  2117. }
  2118. NS_IMETHODIMP
  2119. nsSocketTransport::GetQoSBits(uint8_t *aQoSBits)
  2120. {
  2121. *aQoSBits = mQoSBits;
  2122. return NS_OK;
  2123. }
  2124. NS_IMETHODIMP
  2125. nsSocketTransport::GetRecvBufferSize(uint32_t *aSize)
  2126. {
  2127. PRFileDescAutoLock fd(this);
  2128. if (!fd.IsInitialized())
  2129. return NS_ERROR_NOT_CONNECTED;
  2130. nsresult rv = NS_OK;
  2131. PRSocketOptionData opt;
  2132. opt.option = PR_SockOpt_RecvBufferSize;
  2133. if (PR_GetSocketOption(fd, &opt) == PR_SUCCESS)
  2134. *aSize = opt.value.recv_buffer_size;
  2135. else
  2136. rv = NS_ERROR_FAILURE;
  2137. return rv;
  2138. }
  2139. NS_IMETHODIMP
  2140. nsSocketTransport::GetSendBufferSize(uint32_t *aSize)
  2141. {
  2142. PRFileDescAutoLock fd(this);
  2143. if (!fd.IsInitialized())
  2144. return NS_ERROR_NOT_CONNECTED;
  2145. nsresult rv = NS_OK;
  2146. PRSocketOptionData opt;
  2147. opt.option = PR_SockOpt_SendBufferSize;
  2148. if (PR_GetSocketOption(fd, &opt) == PR_SUCCESS)
  2149. *aSize = opt.value.send_buffer_size;
  2150. else
  2151. rv = NS_ERROR_FAILURE;
  2152. return rv;
  2153. }
  2154. NS_IMETHODIMP
  2155. nsSocketTransport::SetRecvBufferSize(uint32_t aSize)
  2156. {
  2157. PRFileDescAutoLock fd(this);
  2158. if (!fd.IsInitialized())
  2159. return NS_ERROR_NOT_CONNECTED;
  2160. nsresult rv = NS_OK;
  2161. PRSocketOptionData opt;
  2162. opt.option = PR_SockOpt_RecvBufferSize;
  2163. opt.value.recv_buffer_size = aSize;
  2164. if (PR_SetSocketOption(fd, &opt) != PR_SUCCESS)
  2165. rv = NS_ERROR_FAILURE;
  2166. return rv;
  2167. }
  2168. NS_IMETHODIMP
  2169. nsSocketTransport::SetSendBufferSize(uint32_t aSize)
  2170. {
  2171. PRFileDescAutoLock fd(this);
  2172. if (!fd.IsInitialized())
  2173. return NS_ERROR_NOT_CONNECTED;
  2174. nsresult rv = NS_OK;
  2175. PRSocketOptionData opt;
  2176. opt.option = PR_SockOpt_SendBufferSize;
  2177. opt.value.send_buffer_size = aSize;
  2178. if (PR_SetSocketOption(fd, &opt) != PR_SUCCESS)
  2179. rv = NS_ERROR_FAILURE;
  2180. return rv;
  2181. }
  2182. NS_IMETHODIMP
  2183. nsSocketTransport::OnLookupComplete(nsICancelable *request,
  2184. nsIDNSRecord *rec,
  2185. nsresult status)
  2186. {
  2187. // flag host lookup complete for the benefit of the ResolveHost method.
  2188. mResolving = false;
  2189. nsresult rv = PostEvent(MSG_DNS_LOOKUP_COMPLETE, status, rec);
  2190. // if posting a message fails, then we should assume that the socket
  2191. // transport has been shutdown. this should never happen! if it does
  2192. // it means that the socket transport service was shutdown before the
  2193. // DNS service.
  2194. if (NS_FAILED(rv))
  2195. NS_WARNING("unable to post DNS lookup complete message");
  2196. return NS_OK;
  2197. }
  2198. // nsIInterfaceRequestor
  2199. NS_IMETHODIMP
  2200. nsSocketTransport::GetInterface(const nsIID &iid, void **result)
  2201. {
  2202. if (iid.Equals(NS_GET_IID(nsIDNSRecord))) {
  2203. return mDNSRecord ?
  2204. mDNSRecord->QueryInterface(iid, result) : NS_ERROR_NO_INTERFACE;
  2205. }
  2206. return this->QueryInterface(iid, result);
  2207. }
  2208. NS_IMETHODIMP
  2209. nsSocketTransport::GetInterfaces(uint32_t *count, nsIID * **array)
  2210. {
  2211. return NS_CI_INTERFACE_GETTER_NAME(nsSocketTransport)(count, array);
  2212. }
  2213. NS_IMETHODIMP
  2214. nsSocketTransport::GetScriptableHelper(nsIXPCScriptable **_retval)
  2215. {
  2216. *_retval = nullptr;
  2217. return NS_OK;
  2218. }
  2219. NS_IMETHODIMP
  2220. nsSocketTransport::GetContractID(char * *aContractID)
  2221. {
  2222. *aContractID = nullptr;
  2223. return NS_OK;
  2224. }
  2225. NS_IMETHODIMP
  2226. nsSocketTransport::GetClassDescription(char * *aClassDescription)
  2227. {
  2228. *aClassDescription = nullptr;
  2229. return NS_OK;
  2230. }
  2231. NS_IMETHODIMP
  2232. nsSocketTransport::GetClassID(nsCID * *aClassID)
  2233. {
  2234. *aClassID = nullptr;
  2235. return NS_OK;
  2236. }
  2237. NS_IMETHODIMP
  2238. nsSocketTransport::GetFlags(uint32_t *aFlags)
  2239. {
  2240. *aFlags = nsIClassInfo::THREADSAFE;
  2241. return NS_OK;
  2242. }
  2243. NS_IMETHODIMP
  2244. nsSocketTransport::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
  2245. {
  2246. return NS_ERROR_NOT_AVAILABLE;
  2247. }
  2248. NS_IMETHODIMP
  2249. nsSocketTransport::GetConnectionFlags(uint32_t *value)
  2250. {
  2251. *value = mConnectionFlags;
  2252. return NS_OK;
  2253. }
  2254. NS_IMETHODIMP
  2255. nsSocketTransport::SetConnectionFlags(uint32_t value)
  2256. {
  2257. mConnectionFlags = value;
  2258. mIsPrivate = value & nsISocketTransport::NO_PERMANENT_STORAGE;
  2259. return NS_OK;
  2260. }
  2261. void
  2262. nsSocketTransport::OnKeepaliveEnabledPrefChange(bool aEnabled)
  2263. {
  2264. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  2265. // The global pref toggles keepalive as a system feature; it only affects
  2266. // an individual socket if keepalive has been specifically enabled for it.
  2267. // So, ensure keepalive is configured correctly if previously enabled.
  2268. if (mKeepaliveEnabled) {
  2269. nsresult rv = SetKeepaliveEnabledInternal(aEnabled);
  2270. if (NS_WARN_IF(NS_FAILED(rv))) {
  2271. SOCKET_LOG((" SetKeepaliveEnabledInternal [%s] failed rv[0x%x]",
  2272. aEnabled ? "enable" : "disable", rv));
  2273. }
  2274. }
  2275. }
  2276. nsresult
  2277. nsSocketTransport::SetKeepaliveEnabledInternal(bool aEnable)
  2278. {
  2279. MOZ_ASSERT(mKeepaliveIdleTimeS > 0 &&
  2280. mKeepaliveIdleTimeS <= kMaxTCPKeepIdle);
  2281. MOZ_ASSERT(mKeepaliveRetryIntervalS > 0 &&
  2282. mKeepaliveRetryIntervalS <= kMaxTCPKeepIntvl);
  2283. MOZ_ASSERT(mKeepaliveProbeCount > 0 &&
  2284. mKeepaliveProbeCount <= kMaxTCPKeepCount);
  2285. PRFileDescAutoLock fd(this);
  2286. if (NS_WARN_IF(!fd.IsInitialized())) {
  2287. return NS_ERROR_NOT_INITIALIZED;
  2288. }
  2289. // Only enable if keepalives are globally enabled, but ensure other
  2290. // options are set correctly on the fd.
  2291. bool enable = aEnable && mSocketTransportService->IsKeepaliveEnabled();
  2292. nsresult rv = fd.SetKeepaliveVals(enable,
  2293. mKeepaliveIdleTimeS,
  2294. mKeepaliveRetryIntervalS,
  2295. mKeepaliveProbeCount);
  2296. if (NS_WARN_IF(NS_FAILED(rv))) {
  2297. SOCKET_LOG((" SetKeepaliveVals failed rv[0x%x]", rv));
  2298. return rv;
  2299. }
  2300. rv = fd.SetKeepaliveEnabled(enable);
  2301. if (NS_WARN_IF(NS_FAILED(rv))) {
  2302. SOCKET_LOG((" SetKeepaliveEnabled failed rv[0x%x]", rv));
  2303. return rv;
  2304. }
  2305. return NS_OK;
  2306. }
  2307. NS_IMETHODIMP
  2308. nsSocketTransport::GetKeepaliveEnabled(bool *aResult)
  2309. {
  2310. MOZ_ASSERT(aResult);
  2311. *aResult = mKeepaliveEnabled;
  2312. return NS_OK;
  2313. }
  2314. nsresult
  2315. nsSocketTransport::EnsureKeepaliveValsAreInitialized()
  2316. {
  2317. nsresult rv = NS_OK;
  2318. int32_t val = -1;
  2319. if (mKeepaliveIdleTimeS == -1) {
  2320. rv = mSocketTransportService->GetKeepaliveIdleTime(&val);
  2321. if (NS_WARN_IF(NS_FAILED(rv))) {
  2322. return rv;
  2323. }
  2324. mKeepaliveIdleTimeS = val;
  2325. }
  2326. if (mKeepaliveRetryIntervalS == -1) {
  2327. rv = mSocketTransportService->GetKeepaliveRetryInterval(&val);
  2328. if (NS_WARN_IF(NS_FAILED(rv))) {
  2329. return rv;
  2330. }
  2331. mKeepaliveRetryIntervalS = val;
  2332. }
  2333. if (mKeepaliveProbeCount == -1) {
  2334. rv = mSocketTransportService->GetKeepaliveProbeCount(&val);
  2335. if (NS_WARN_IF(NS_FAILED(rv))) {
  2336. return rv;
  2337. }
  2338. mKeepaliveProbeCount = val;
  2339. }
  2340. return NS_OK;
  2341. }
  2342. NS_IMETHODIMP
  2343. nsSocketTransport::SetKeepaliveEnabled(bool aEnable)
  2344. {
  2345. #if defined(XP_WIN) || defined(XP_UNIX) || defined(XP_MACOSX)
  2346. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  2347. if (aEnable == mKeepaliveEnabled) {
  2348. SOCKET_LOG(("nsSocketTransport::SetKeepaliveEnabled [%p] already %s.",
  2349. this, aEnable ? "enabled" : "disabled"));
  2350. return NS_OK;
  2351. }
  2352. nsresult rv = NS_OK;
  2353. if (aEnable) {
  2354. rv = EnsureKeepaliveValsAreInitialized();
  2355. if (NS_WARN_IF(NS_FAILED(rv))) {
  2356. SOCKET_LOG((" SetKeepaliveEnabled [%p] "
  2357. "error [0x%x] initializing keepalive vals",
  2358. this, rv));
  2359. return rv;
  2360. }
  2361. }
  2362. SOCKET_LOG(("nsSocketTransport::SetKeepaliveEnabled [%p] "
  2363. "%s, idle time[%ds] retry interval[%ds] packet count[%d]: "
  2364. "globally %s.",
  2365. this, aEnable ? "enabled" : "disabled",
  2366. mKeepaliveIdleTimeS, mKeepaliveRetryIntervalS,
  2367. mKeepaliveProbeCount,
  2368. mSocketTransportService->IsKeepaliveEnabled() ?
  2369. "enabled" : "disabled"));
  2370. // Set mKeepaliveEnabled here so that state is maintained; it is possible
  2371. // that we're in between fds, e.g. the 1st IP address failed, so we're about
  2372. // to retry on a 2nd from the DNS record.
  2373. mKeepaliveEnabled = aEnable;
  2374. rv = SetKeepaliveEnabledInternal(aEnable);
  2375. if (NS_WARN_IF(NS_FAILED(rv))) {
  2376. SOCKET_LOG((" SetKeepaliveEnabledInternal failed rv[0x%x]", rv));
  2377. return rv;
  2378. }
  2379. return NS_OK;
  2380. #else /* !(defined(XP_WIN) || defined(XP_UNIX) || defined(XP_MACOSX)) */
  2381. SOCKET_LOG(("nsSocketTransport::SetKeepaliveEnabled unsupported platform"));
  2382. return NS_ERROR_NOT_IMPLEMENTED;
  2383. #endif
  2384. }
  2385. NS_IMETHODIMP
  2386. nsSocketTransport::SetKeepaliveVals(int32_t aIdleTime,
  2387. int32_t aRetryInterval)
  2388. {
  2389. #if defined(XP_WIN) || defined(XP_UNIX) || defined(XP_MACOSX)
  2390. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  2391. if (NS_WARN_IF(aIdleTime <= 0 || kMaxTCPKeepIdle < aIdleTime)) {
  2392. return NS_ERROR_INVALID_ARG;
  2393. }
  2394. if (NS_WARN_IF(aRetryInterval <= 0 ||
  2395. kMaxTCPKeepIntvl < aRetryInterval)) {
  2396. return NS_ERROR_INVALID_ARG;
  2397. }
  2398. if (aIdleTime == mKeepaliveIdleTimeS &&
  2399. aRetryInterval == mKeepaliveRetryIntervalS) {
  2400. SOCKET_LOG(("nsSocketTransport::SetKeepaliveVals [%p] idle time "
  2401. "already %ds and retry interval already %ds.",
  2402. this, mKeepaliveIdleTimeS,
  2403. mKeepaliveRetryIntervalS));
  2404. return NS_OK;
  2405. }
  2406. mKeepaliveIdleTimeS = aIdleTime;
  2407. mKeepaliveRetryIntervalS = aRetryInterval;
  2408. nsresult rv = NS_OK;
  2409. if (mKeepaliveProbeCount == -1) {
  2410. int32_t val = -1;
  2411. nsresult rv = mSocketTransportService->GetKeepaliveProbeCount(&val);
  2412. if (NS_WARN_IF(NS_FAILED(rv))) {
  2413. return rv;
  2414. }
  2415. mKeepaliveProbeCount = val;
  2416. }
  2417. SOCKET_LOG(("nsSocketTransport::SetKeepaliveVals [%p] "
  2418. "keepalive %s, idle time[%ds] retry interval[%ds] "
  2419. "packet count[%d]",
  2420. this, mKeepaliveEnabled ? "enabled" : "disabled",
  2421. mKeepaliveIdleTimeS, mKeepaliveRetryIntervalS,
  2422. mKeepaliveProbeCount));
  2423. PRFileDescAutoLock fd(this);
  2424. if (NS_WARN_IF(!fd.IsInitialized())) {
  2425. return NS_ERROR_NULL_POINTER;
  2426. }
  2427. rv = fd.SetKeepaliveVals(mKeepaliveEnabled,
  2428. mKeepaliveIdleTimeS,
  2429. mKeepaliveRetryIntervalS,
  2430. mKeepaliveProbeCount);
  2431. if (NS_WARN_IF(NS_FAILED(rv))) {
  2432. return rv;
  2433. }
  2434. return NS_OK;
  2435. #else
  2436. SOCKET_LOG(("nsSocketTransport::SetKeepaliveVals unsupported platform"));
  2437. return NS_ERROR_NOT_IMPLEMENTED;
  2438. #endif
  2439. }
  2440. #ifdef ENABLE_SOCKET_TRACING
  2441. #include <stdio.h>
  2442. #include <ctype.h>
  2443. #include "prenv.h"
  2444. static void
  2445. DumpBytesToFile(const char *path, const char *header, const char *buf, int32_t n)
  2446. {
  2447. FILE *fp = fopen(path, "a");
  2448. fprintf(fp, "\n%s [%d bytes]\n", header, n);
  2449. const unsigned char *p;
  2450. while (n) {
  2451. p = (const unsigned char *) buf;
  2452. int32_t i, row_max = std::min(16, n);
  2453. for (i = 0; i < row_max; ++i)
  2454. fprintf(fp, "%02x ", *p++);
  2455. for (i = row_max; i < 16; ++i)
  2456. fprintf(fp, " ");
  2457. p = (const unsigned char *) buf;
  2458. for (i = 0; i < row_max; ++i, ++p) {
  2459. if (isprint(*p))
  2460. fprintf(fp, "%c", *p);
  2461. else
  2462. fprintf(fp, ".");
  2463. }
  2464. fprintf(fp, "\n");
  2465. buf += row_max;
  2466. n -= row_max;
  2467. }
  2468. fprintf(fp, "\n");
  2469. fclose(fp);
  2470. }
  2471. void
  2472. nsSocketTransport::TraceInBuf(const char *buf, int32_t n)
  2473. {
  2474. char *val = PR_GetEnv("NECKO_SOCKET_TRACE_LOG");
  2475. if (!val || !*val)
  2476. return;
  2477. nsAutoCString header;
  2478. header.AssignLiteral("Reading from: ");
  2479. header.Append(mHost);
  2480. header.Append(':');
  2481. header.AppendInt(mPort);
  2482. DumpBytesToFile(val, header.get(), buf, n);
  2483. }
  2484. void
  2485. nsSocketTransport::TraceOutBuf(const char *buf, int32_t n)
  2486. {
  2487. char *val = PR_GetEnv("NECKO_SOCKET_TRACE_LOG");
  2488. if (!val || !*val)
  2489. return;
  2490. nsAutoCString header;
  2491. header.AssignLiteral("Writing to: ");
  2492. header.Append(mHost);
  2493. header.Append(':');
  2494. header.AppendInt(mPort);
  2495. DumpBytesToFile(val, header.get(), buf, n);
  2496. }
  2497. #endif
  2498. static void LogNSPRError(const char* aPrefix, const void *aObjPtr)
  2499. {
  2500. #if defined(DEBUG)
  2501. PRErrorCode errCode = PR_GetError();
  2502. int errLen = PR_GetErrorTextLength();
  2503. nsAutoCString errStr;
  2504. if (errLen > 0) {
  2505. errStr.SetLength(errLen);
  2506. PR_GetErrorText(errStr.BeginWriting());
  2507. }
  2508. NS_WARNING(nsPrintfCString(
  2509. "%s [%p] NSPR error[0x%x] %s.",
  2510. aPrefix ? aPrefix : "nsSocketTransport", aObjPtr, errCode,
  2511. errLen > 0 ? errStr.BeginReading() : "<no error text>").get());
  2512. #endif
  2513. }
  2514. nsresult
  2515. nsSocketTransport::PRFileDescAutoLock::SetKeepaliveEnabled(bool aEnable)
  2516. {
  2517. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  2518. MOZ_ASSERT(!(aEnable && !gSocketTransportService->IsKeepaliveEnabled()),
  2519. "Cannot enable keepalive if global pref is disabled!");
  2520. if (aEnable && !gSocketTransportService->IsKeepaliveEnabled()) {
  2521. return NS_ERROR_ILLEGAL_VALUE;
  2522. }
  2523. PRSocketOptionData opt;
  2524. opt.option = PR_SockOpt_Keepalive;
  2525. opt.value.keep_alive = aEnable;
  2526. PRStatus status = PR_SetSocketOption(mFd, &opt);
  2527. if (NS_WARN_IF(status != PR_SUCCESS)) {
  2528. LogNSPRError("nsSocketTransport::PRFileDescAutoLock::SetKeepaliveEnabled",
  2529. mSocketTransport);
  2530. return ErrorAccordingToNSPR(PR_GetError());
  2531. }
  2532. return NS_OK;
  2533. }
  2534. static void LogOSError(const char *aPrefix, const void *aObjPtr)
  2535. {
  2536. #if defined(DEBUG)
  2537. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  2538. #ifdef XP_WIN
  2539. DWORD errCode = WSAGetLastError();
  2540. LPVOID errMessage;
  2541. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2542. FORMAT_MESSAGE_FROM_SYSTEM |
  2543. FORMAT_MESSAGE_IGNORE_INSERTS,
  2544. NULL,
  2545. errCode,
  2546. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2547. (LPTSTR) &errMessage,
  2548. 0, NULL);
  2549. #else
  2550. int errCode = errno;
  2551. char *errMessage = strerror(errno);
  2552. #endif
  2553. NS_WARNING(nsPrintfCString(
  2554. "%s [%p] OS error[0x%x] %s",
  2555. aPrefix ? aPrefix : "nsSocketTransport", aObjPtr, errCode,
  2556. errMessage ? errMessage : "<no error text>").get());
  2557. #ifdef XP_WIN
  2558. LocalFree(errMessage);
  2559. #endif
  2560. #endif
  2561. }
  2562. /* XXX PR_SetSockOpt does not support setting keepalive values, so native
  2563. * handles and platform specific apis (setsockopt, WSAIOCtl) are used in this
  2564. * file. Requires inclusion of NSPR private/pprio.h, and platform headers.
  2565. */
  2566. nsresult
  2567. nsSocketTransport::PRFileDescAutoLock::SetKeepaliveVals(bool aEnabled,
  2568. int aIdleTime,
  2569. int aRetryInterval,
  2570. int aProbeCount)
  2571. {
  2572. #if defined(XP_WIN) || defined(XP_UNIX) || defined(XP_MACOSX)
  2573. MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
  2574. if (NS_WARN_IF(aIdleTime <= 0 || kMaxTCPKeepIdle < aIdleTime)) {
  2575. return NS_ERROR_INVALID_ARG;
  2576. }
  2577. if (NS_WARN_IF(aRetryInterval <= 0 ||
  2578. kMaxTCPKeepIntvl < aRetryInterval)) {
  2579. return NS_ERROR_INVALID_ARG;
  2580. }
  2581. if (NS_WARN_IF(aProbeCount <= 0 || kMaxTCPKeepCount < aProbeCount)) {
  2582. return NS_ERROR_INVALID_ARG;
  2583. }
  2584. PROsfd sock = PR_FileDesc2NativeHandle(mFd);
  2585. if (NS_WARN_IF(sock == -1)) {
  2586. LogNSPRError("nsSocketTransport::PRFileDescAutoLock::SetKeepaliveVals",
  2587. mSocketTransport);
  2588. return ErrorAccordingToNSPR(PR_GetError());
  2589. }
  2590. #endif
  2591. #if defined(XP_WIN)
  2592. // Windows allows idle time and retry interval to be set; NOT ping count.
  2593. struct tcp_keepalive keepalive_vals = {
  2594. (u_long)aEnabled,
  2595. // Windows uses msec.
  2596. (u_long)(aIdleTime * 1000UL),
  2597. (u_long)(aRetryInterval * 1000UL)
  2598. };
  2599. DWORD bytes_returned;
  2600. int err = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &keepalive_vals,
  2601. sizeof(keepalive_vals), NULL, 0, &bytes_returned, NULL,
  2602. NULL);
  2603. if (NS_WARN_IF(err)) {
  2604. LogOSError("nsSocketTransport WSAIoctl failed", mSocketTransport);
  2605. return NS_ERROR_UNEXPECTED;
  2606. }
  2607. return NS_OK;
  2608. #elif defined(XP_UNIX)
  2609. // Not all *nix OSes support the following setsockopt() options
  2610. // build errors will tell us if they are not.
  2611. #ifdef TCP_KEEPIDLE
  2612. // Idle time until first keepalive probe; interval between ack'd probes; seconds.
  2613. int err = setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE,
  2614. &aIdleTime, sizeof(aIdleTime));
  2615. if (NS_WARN_IF(err)) {
  2616. LogOSError("nsSocketTransport Failed setting TCP_KEEPIDLE",
  2617. mSocketTransport);
  2618. return NS_ERROR_UNEXPECTED;
  2619. }
  2620. #endif
  2621. #ifdef TCP_KEEPINTVL
  2622. // Interval between unack'd keepalive probes; seconds.
  2623. err = setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL,
  2624. &aRetryInterval, sizeof(aRetryInterval));
  2625. if (NS_WARN_IF(err)) {
  2626. LogOSError("nsSocketTransport Failed setting TCP_KEEPINTVL",
  2627. mSocketTransport);
  2628. return NS_ERROR_UNEXPECTED;
  2629. }
  2630. #endif
  2631. #ifdef TCP_KEEPCNT
  2632. // Number of unack'd keepalive probes before connection times out.
  2633. err = setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT,
  2634. &aProbeCount, sizeof(aProbeCount));
  2635. if (NS_WARN_IF(err)) {
  2636. LogOSError("nsSocketTransport Failed setting TCP_KEEPCNT",
  2637. mSocketTransport);
  2638. return NS_ERROR_UNEXPECTED;
  2639. }
  2640. #endif
  2641. return NS_OK;
  2642. #else
  2643. MOZ_ASSERT(false, "nsSocketTransport::PRFileDescAutoLock::SetKeepaliveVals "
  2644. "called on unsupported platform!");
  2645. return NS_ERROR_UNEXPECTED;
  2646. #endif
  2647. }
  2648. void
  2649. nsSocketTransport::CloseSocket(PRFileDesc *aFd)
  2650. {
  2651. #if defined(XP_WIN)
  2652. AttachShutdownLayer(aFd);
  2653. #endif
  2654. PR_Close(aFd);
  2655. }
  2656. } // namespace net
  2657. } // namespace mozilla