xref: /haiku/src/add-ons/kernel/network/protocols/unix/UnixStreamEndpoint.h (revision 94c66b276e92f206678ca2e2c816d2665946afdd)
1 /*
2  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef UNIX_STREAM_ENDPOINT_H
6 #define UNIX_STREAM_ENDPOINT_H
7 
8 #include <sys/stat.h>
9 
10 #include <Referenceable.h>
11 
12 #include <util/DoublyLinkedList.h>
13 #include <util/OpenHashTable.h>
14 
15 #include "unix.h"
16 #include "UnixEndpoint.h"
17 
18 class UnixStreamEndpoint;
19 class UnixFifo;
20 
21 
22 enum class unix_stream_endpoint_state {
23 	NotConnected,
24 	Listening,
25 	Connected,
26 	Closed
27 };
28 
29 
30 typedef AutoLocker<UnixStreamEndpoint> UnixStreamEndpointLocker;
31 
32 
33 class UnixStreamEndpoint final : public UnixEndpoint, public BReferenceable {
34 public:
35 								UnixStreamEndpoint(net_socket* socket);
36 	virtual						~UnixStreamEndpoint() override;
37 
38 			status_t			Init() override;
39 			void				Uninit() override;
40 
41 			status_t			Open() override;
42 			status_t			Close() override;
43 			status_t			Free() override;
44 
45 			status_t			Bind(const struct sockaddr* _address) override;
46 			status_t			Unbind() override;
47 			status_t			Listen(int backlog) override;
48 			status_t			Connect(const struct sockaddr* address) override;
49 			status_t			Accept(net_socket** _acceptedSocket) override;
50 
51 			ssize_t				Send(const iovec* vecs, size_t vecCount,
52 									ancillary_data_container* ancillaryData,
53 									const struct sockaddr* address,
54 									socklen_t addressLength, int flags) override;
55 			ssize_t				Receive(const iovec* vecs, size_t vecCount,
56 									ancillary_data_container** _ancillaryData,
57 									struct sockaddr* _address,
58 									socklen_t* _addressLength, int flags) override;
59 
60 			ssize_t				Sendable() override;
61 			ssize_t				Receivable() override;
62 
63 			status_t			SetReceiveBufferSize(size_t size) override;
64 			status_t			GetPeerCredentials(ucred* credentials) override;
65 
66 			status_t			Shutdown(int direction) override;
67 
68 	bool IsBound() const
69 	{
70 		return !fIsChild && fAddress.IsValid();
71 	}
72 
73 private:
74 			void				_Spawn(UnixStreamEndpoint* connectingEndpoint,
75 									UnixStreamEndpoint* listeningEndpoint, UnixFifo* fifo);
76 			void				_Disconnect();
77 			status_t			_LockConnectedEndpoints(UnixStreamEndpointLocker& locker,
78 									UnixStreamEndpointLocker& peerLocker);
79 
80 			status_t			_Unbind();
81 
82 			void				_UnsetReceiveFifo();
83 			void				_StopListening();
84 
85 private:
86 	UnixStreamEndpoint*			fPeerEndpoint;
87 	UnixFifo*					fReceiveFifo;
88 	unix_stream_endpoint_state	fState;
89 	sem_id						fAcceptSemaphore;
90 	ucred						fCredentials;
91 	bool						fIsChild;
92 	bool						fWasConnected;
93 };
94 
95 #endif	// UNIX_STREAM_ENDPOINT_H
96