tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsUDPSocket.h (3344B)


      1 /* vim:set ts=2 sw=2 et cindent: */
      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 
      6 #ifndef nsUDPSocket_h__
      7 #define nsUDPSocket_h__
      8 
      9 #include "nsIUDPSocket.h"
     10 #include "mozilla/Mutex.h"
     11 #include "mozilla/net/DNS.h"
     12 #include "nsIOutputStream.h"
     13 #include "nsASocketHandler.h"
     14 #include "nsCycleCollectionParticipant.h"
     15 
     16 //-----------------------------------------------------------------------------
     17 
     18 namespace mozilla {
     19 namespace net {
     20 
     21 class nsSocketTransportService;
     22 
     23 class nsUDPSocket final : public nsASocketHandler, public nsIUDPSocket {
     24 public:
     25  NS_DECL_THREADSAFE_ISUPPORTS
     26  NS_DECL_NSIUDPSOCKET
     27 
     28  // nsASocketHandler methods:
     29  virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags) override;
     30  virtual void OnSocketDetached(PRFileDesc* fd) override;
     31  virtual void IsLocal(bool* aIsLocal) override;
     32  virtual nsresult GetRemoteAddr(NetAddr* addr) override;
     33 
     34  uint64_t ByteCountSent() override { return mByteWriteCount; }
     35  uint64_t ByteCountReceived() override { return mByteReadCount; }
     36 
     37  nsUDPSocket();
     38 
     39 private:
     40  virtual ~nsUDPSocket();
     41 
     42  void OnMsgClose();
     43  void OnMsgAttach();
     44 
     45  // try attaching our socket (mFD) to the STS's poll list.
     46  nsresult TryAttach();
     47 
     48  friend class SetSocketOptionRunnable;
     49  nsresult SetSocketOption(const PRSocketOptionData& aOpt);
     50  nsresult JoinMulticastInternal(const PRNetAddr& aAddr,
     51                                 const PRNetAddr& aIface);
     52  nsresult LeaveMulticastInternal(const PRNetAddr& aAddr,
     53                                  const PRNetAddr& aIface);
     54  nsresult SetMulticastInterfaceInternal(const PRNetAddr& aIface);
     55 
     56  void CloseSocket();
     57 
     58  // lock protects access to mListener;
     59  // so mListener is not cleared while being used/locked.
     60  Mutex mLock MOZ_UNANNOTATED{"nsUDPSocket.mLock"};
     61  PRFileDesc* mFD{nullptr};
     62  NetAddr mAddr;
     63  OriginAttributes mOriginAttributes;
     64  nsCOMPtr<nsIUDPSocketListener> mListener;
     65  nsCOMPtr<nsIUDPSocketSyncListener> mSyncListener;
     66  nsCOMPtr<nsIEventTarget> mListenerTarget;
     67  bool mAttached{false};
     68  RefPtr<nsSocketTransportService> mSts;
     69 
     70  uint64_t mByteReadCount{0};
     71  uint64_t mByteWriteCount{0};
     72 };
     73 
     74 //-----------------------------------------------------------------------------
     75 
     76 class nsUDPMessage : public nsIUDPMessage {
     77 public:
     78  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     79  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage)
     80  NS_DECL_NSIUDPMESSAGE
     81 
     82  nsUDPMessage(NetAddr* aAddr, nsIOutputStream* aOutputStream,
     83               FallibleTArray<uint8_t>&& aData);
     84 
     85 private:
     86  virtual ~nsUDPMessage();
     87 
     88  NetAddr mAddr;
     89  nsCOMPtr<nsIOutputStream> mOutputStream;
     90  FallibleTArray<uint8_t> mData;
     91  JS::Heap<JSObject*> mJsobj;
     92 };
     93 
     94 //-----------------------------------------------------------------------------
     95 
     96 class nsUDPOutputStream : public nsIOutputStream {
     97 public:
     98  NS_DECL_THREADSAFE_ISUPPORTS
     99  NS_DECL_NSIOUTPUTSTREAM
    100 
    101  nsUDPOutputStream(nsUDPSocket* aSocket, PRFileDesc* aFD,
    102                    PRNetAddr& aPrClientAddr);
    103 
    104 private:
    105  virtual ~nsUDPOutputStream() = default;
    106 
    107  RefPtr<nsUDPSocket> mSocket;
    108  PRFileDesc* mFD;
    109  PRNetAddr mPrClientAddr;
    110  bool mIsClosed;
    111 };
    112 
    113 }  // namespace net
    114 }  // namespace mozilla
    115 
    116 #endif  // nsUDPSocket_h__