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 status_t MarkClosed(); 71 72 static L2capEndpoint* ForPsm(uint16 psm); 73 74 bool RequiresConfiguration() 75 { 76 return fConfigurationSet; 77 } 78 79 ChannelConfiguration fConfiguration; 80 bool fConfigurationSet; 81 net_fifo fReceivingFifo; 82 83 private: 84 typedef enum { 85 // establishing a connection 86 CLOSED, 87 BOUND, 88 LISTEN, 89 CONNECTING, 90 ESTABLISHED, 91 92 // peer closes the connection 93 FINISH_RECEIVED, 94 WAIT_FOR_FINISH_ACKNOWLEDGE, 95 96 // we close the connection 97 FINISH_SENT, 98 FINISH_ACKNOWLEDGED, 99 CLOSING, 100 TIME_WAIT 101 } State; 102 103 mutex fLock; 104 State fState; 105 sem_id fAcceptSemaphore; 106 L2capEndpoint* fPeerEndpoint; 107 L2capChannel* fChannel; 108 109 }; 110 111 112 extern DoublyLinkedList<L2capEndpoint> EndpointList; 113 114 #endif // L2CAP_ENDPOINT_H 115