1/* 2 * Copyright 2006-2009, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel D��rfler, axeld@pinc-software.de 7 * Hugo Santos, hugosantos@gmail.com 8 */ 9#ifndef ENDPOINT_MANAGER_H 10#define ENDPOINT_MANAGER_H 11 12 13#include "tcp.h" 14 15#include <AddressUtilities.h> 16 17#include <lock.h> 18#include <util/AutoLock.h> 19#include <util/DoublyLinkedList.h> 20#include <util/MultiHashTable.h> 21#include <util/OpenHashTable.h> 22 23#include <utility> 24 25 26struct net_address_module_info; 27struct net_domain; 28class EndpointManager; 29class TCPEndpoint; 30 31 32struct ConnectionHashDefinition { 33public: 34 typedef std::pair<const sockaddr*, const sockaddr*> KeyType; 35 typedef TCPEndpoint ValueType; 36 37 ConnectionHashDefinition(EndpointManager* manager); 38 ConnectionHashDefinition( 39 const ConnectionHashDefinition& definition) 40 : fManager(definition.fManager) 41 { 42 } 43 44 size_t HashKey(const KeyType& key) const; 45 size_t Hash(TCPEndpoint* endpoint) const; 46 bool Compare(const KeyType& key, 47 TCPEndpoint* endpoint) const; 48 TCPEndpoint*& GetLink(TCPEndpoint* endpoint) const; 49 50private: 51 EndpointManager* fManager; 52}; 53 54 55class EndpointHashDefinition { 56public: 57 typedef uint16 KeyType; 58 typedef TCPEndpoint ValueType; 59 60 size_t HashKey(uint16 port) const; 61 size_t Hash(TCPEndpoint* endpoint) const; 62 bool Compare(uint16 port, TCPEndpoint* endpoint) const; 63 bool CompareValues(TCPEndpoint* first, 64 TCPEndpoint* second) const; 65 TCPEndpoint*& GetLink(TCPEndpoint* endpoint) const; 66}; 67 68 69class EndpointManager : public DoublyLinkedListLinkImpl<EndpointManager> { 70public: 71 EndpointManager(net_domain* domain); 72 ~EndpointManager(); 73 74 status_t Init(); 75 76 TCPEndpoint* FindConnection(sockaddr* local, sockaddr* peer); 77 78 status_t SetConnection(TCPEndpoint* endpoint, 79 const sockaddr* local, const sockaddr* peer, 80 const sockaddr* interfaceLocal); 81 status_t SetPassive(TCPEndpoint* endpoint); 82 83 status_t Bind(TCPEndpoint* endpoint, 84 const sockaddr* address); 85 status_t BindChild(TCPEndpoint* endpoint, 86 const sockaddr* address); 87 status_t Unbind(TCPEndpoint* endpoint); 88 89 status_t ReplyWithReset(tcp_segment_header& segment, 90 net_buffer* buffer); 91 92 net_domain* Domain() const { return fDomain; } 93 net_address_module_info* AddressModule() const 94 { return Domain()->address_module; } 95 96 void Dump() const; 97 98private: 99 TCPEndpoint* _LookupConnection(const sockaddr* local, 100 const sockaddr* peer); 101 status_t _Bind(TCPEndpoint* endpoint, 102 const sockaddr* address); 103 status_t _BindToAddress(WriteLocker& locker, 104 TCPEndpoint* endpoint, const sockaddr* address); 105 status_t _BindToEphemeral(TCPEndpoint* endpoint, 106 const sockaddr* address); 107 108 typedef BOpenHashTable<ConnectionHashDefinition> ConnectionTable; 109 typedef MultiHashTable<EndpointHashDefinition> EndpointTable; 110 111 rw_lock fLock; 112 net_domain* fDomain; 113 ConnectionTable fConnectionHash; 114 EndpointTable fEndpointHash; 115 uint16 fLastPort; 116}; 117 118#endif // ENDPOINT_MANAGER_H 119