xref: /haiku/src/add-ons/kernel/network/protocols/l2cap/L2capEndpoint.h (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef L2CAP_ENDPOINT_H
6 #define L2CAP_ENDPOINT_H
7 
8 #include <sys/stat.h>
9 
10 #include <lock.h>
11 #include <util/DoublyLinkedList.h>
12 #include <util/OpenHashTable.h>
13 
14 #include <net_protocol.h>
15 #include <net_socket.h>
16 #include <ProtocolUtilities.h>
17 
18 #include "l2cap_internal.h"
19 
20 extern net_stack_module_info* gStackModule;
21 
22 class L2capEndpoint : public net_protocol,
23 	public ProtocolSocket,
24 	public DoublyLinkedListLinkImpl<L2capEndpoint> {
25 
26 public:
27 	L2capEndpoint(net_socket* socket);
28 	virtual ~L2capEndpoint();
29 
30 	status_t Init();
31 	void Uninit();
32 
33 	status_t Open();
34 	status_t Close();
35 	status_t Free();
36 
37 	bool Lock()
38 	{
39 		return mutex_lock(&fLock) == B_OK;
40 	}
41 
42 	void Unlock()
43 	{
44 		mutex_unlock(&fLock);
45 	}
46 
47 	status_t	Bind(const struct sockaddr* _address);
48 	status_t	Unbind();
49 	status_t	Listen(int backlog);
50 	status_t	Connect(const struct sockaddr* address);
51 	status_t	Accept(net_socket** _acceptedSocket);
52 
53 	ssize_t		Send(const iovec* vecs, size_t vecCount,
54 					ancillary_data_container* ancillaryData);
55 	ssize_t		Receive(const iovec* vecs, size_t vecCount,
56 					ancillary_data_container** _ancillaryData,
57 					struct sockaddr* _address, socklen_t* _addressLength);
58 
59 	ssize_t ReadData(size_t numBytes, uint32 flags, net_buffer** _buffer);
60 	ssize_t SendData(net_buffer* buffer);
61 	ssize_t Sendable();
62 	ssize_t Receivable();
63 
64 	status_t SetReceiveBufferSize(size_t size);
65 	status_t GetPeerCredentials(ucred* credentials);
66 
67 	status_t Shutdown(int direction);
68 
69 	void 		BindNewEnpointToChannel(L2capChannel* channel);
70 	void 		BindToChannel(L2capChannel* channel);
71 	status_t	MarkEstablished();
72 	status_t	MarkClosed();
73 
74 	static L2capEndpoint* ForPsm(uint16 psm);
75 
76 	bool RequiresConfiguration()
77 	{
78 		return fConfigurationSet;
79 	}
80 
81 	ChannelConfiguration 	fConfiguration;
82 	bool 					fConfigurationSet;
83 	net_fifo		fReceivingFifo;
84 
85 private:
86 	typedef enum {
87 		// establishing a connection
88 		CLOSED,
89 		BOUND,
90 		LISTEN,
91 		CONNECTING,
92 		ESTABLISHED,
93 
94 		// peer closes the connection
95 		FINISH_RECEIVED,
96 		WAIT_FOR_FINISH_ACKNOWLEDGE,
97 
98 		// we close the connection
99 		FINISH_SENT,
100 		FINISH_ACKNOWLEDGED,
101 		CLOSING,
102 		TIME_WAIT
103 	} State;
104 
105 	mutex			fLock;
106 	State    		fState;
107 	sem_id			fEstablishSemaphore;
108 	L2capEndpoint*	fPeerEndpoint;
109 	L2capChannel* 	fChannel;
110 
111 };
112 
113 
114 extern DoublyLinkedList<L2capEndpoint> EndpointList;
115 
116 #endif	// L2CAP_ENDPOINT_H
117