1 // Copyright 2016 The Fuchsia Authors 2 // 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file or at 5 // https://opensource.org/licenses/MIT 6 7 #pragma once 8 9 #include <stdint.h> 10 11 #include <lib/user_copy/user_ptr.h> 12 #include <object/dispatcher.h> 13 #include <object/handle.h> 14 #include <object/mbuf.h> 15 16 #include <zircon/rights.h> 17 #include <zircon/types.h> 18 19 #include <fbl/canary.h> 20 #include <fbl/intrusive_single_list.h> 21 #include <fbl/mutex.h> 22 #include <fbl/ref_counted.h> 23 24 class SocketDispatcher final : 25 public PeeredDispatcher<SocketDispatcher, ZX_DEFAULT_SOCKET_RIGHTS> { 26 public: 27 static zx_status_t Create(uint32_t flags, fbl::RefPtr<Dispatcher>* dispatcher0, 28 fbl::RefPtr<Dispatcher>* dispatcher1, zx_rights_t* rights); 29 30 ~SocketDispatcher() final; 31 32 // Dispatcher implementation. get_type()33 zx_obj_type_t get_type() const final { return ZX_OBJ_TYPE_SOCKET; } 34 35 // Socket methods. 36 zx_status_t Write(user_in_ptr<const void> src, size_t len, size_t* written); 37 38 zx_status_t WriteControl(user_in_ptr<const void> src, size_t len); 39 40 // Shut this endpoint of the socket down for reading, writing, or both. 41 zx_status_t Shutdown(uint32_t how); 42 43 zx_status_t HalfClose(); 44 45 zx_status_t Read(user_out_ptr<void> dst, size_t len, size_t* nread); 46 47 zx_status_t ReadControl(user_out_ptr<void> dst, size_t len, size_t* nread); 48 49 // On success, the share queue takes ownership of |h|. On failure, 50 // |h| is closed. 51 zx_status_t Share(HandleOwner h); 52 53 // On success, a HandleOwner is returned via h 54 zx_status_t Accept(HandleOwner* h); 55 56 // Property methods. 57 size_t GetReadThreshold() const; 58 zx_status_t SetReadThreshold(size_t value); 59 size_t GetWriteThreshold() const; 60 zx_status_t SetWriteThreshold(size_t value); 61 62 void GetInfo(zx_info_socket_t* info) const; 63 64 zx_status_t CheckShareable(SocketDispatcher* to_send); 65 66 struct ControlMsg { 67 static constexpr size_t kSize = 1024; 68 char msg[kSize]; 69 }; 70 71 // PeeredDispatcher implementation. 72 void on_zero_handles_locked() TA_REQ(get_lock()); 73 void OnPeerZeroHandlesLocked() TA_REQ(get_lock()); 74 75 private: 76 // |control_msg| may be null. 77 SocketDispatcher(fbl::RefPtr<PeerHolder<SocketDispatcher>> holder, 78 zx_signals_t starting_signals, uint32_t flags, 79 ktl::unique_ptr<ControlMsg> control_msg); 80 void Init(fbl::RefPtr<SocketDispatcher> other); 81 zx_status_t WriteSelfLocked(user_in_ptr<const void> src, size_t len, size_t* nwritten) TA_REQ(get_lock()); 82 zx_status_t WriteControlSelfLocked(user_in_ptr<const void> src, size_t len) TA_REQ(get_lock()); 83 zx_status_t UserSignalSelfLocked(uint32_t clear_mask, uint32_t set_mask) TA_REQ(get_lock()); 84 zx_status_t ShutdownOtherLocked(uint32_t how) TA_REQ(get_lock()); 85 zx_status_t ShareSelfLocked(HandleOwner h) TA_REQ(get_lock()); 86 is_full()87 bool is_full() const TA_REQ(get_lock()) { return data_.is_full(); } is_empty()88 bool is_empty() const TA_REQ(get_lock()) { return data_.is_empty(); } 89 90 fbl::Canary<fbl::magic("SOCK")> canary_; 91 92 const uint32_t flags_; 93 94 // The shared |get_lock()| protects all members below. 95 MBufChain data_ TA_GUARDED(get_lock()); 96 ktl::unique_ptr<ControlMsg> control_msg_ TA_GUARDED(get_lock()); 97 size_t control_msg_len_ TA_GUARDED(get_lock()); 98 HandleOwner accept_queue_ TA_GUARDED(get_lock()); 99 size_t read_threshold_; 100 size_t write_threshold_; 101 bool read_disabled_ TA_GUARDED(get_lock()); 102 }; 103