xref: /haiku/src/add-ons/kernel/network/protocols/l2cap/L2capEndpoint.h (revision e36a1b58e6daf3efeec46621114691ef499faafc)
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 Sendable();
61 	ssize_t Receivable();
62 
63 	status_t SetReceiveBufferSize(size_t size);
64 	status_t GetPeerCredentials(ucred* credentials);
65 
66 	status_t Shutdown(int direction);
67 
68 	void 		BindToChannel(L2capChannel* channel);
69 	status_t	MarkEstablished();
70 
71 	static L2capEndpoint* ForPsm(uint16 psm);
72 
73 	bool RequiresConfiguration()
74 	{
75 		return fConfigurationSet;
76 	}
77 
78 	ChannelConfiguration 	configuration;
79 	net_fifo 				fReceivingFifo;
80 	bool 					fConfigurationSet;
81 
82 private:
83 	typedef enum {
84 		// establishing a connection
85 		CLOSED,
86 		LISTEN,
87 		SYNCHRONIZE_SENT,
88 		SYNCHRONIZE_RECEIVED,
89 		ESTABLISHED,
90 
91 		// peer closes the connection
92 		FINISH_RECEIVED,
93 		WAIT_FOR_FINISH_ACKNOWLEDGE,
94 
95 		// we close the connection
96 		FINISH_SENT,
97 		FINISH_ACKNOWLEDGED,
98 		CLOSING,
99 		TIME_WAIT
100 	} State;
101 
102 	mutex			fLock;
103 	State    		fState;
104 	L2capEndpoint*	fPeerEndpoint;
105 	sem_id			fAcceptSemaphore;
106 	L2capChannel* 	fChannel;
107 };
108 
109 
110 extern DoublyLinkedList<L2capEndpoint> EndpointList;
111 
112 #endif	// L2CAP_ENDPOINT_H
113