xref: /haiku/src/add-ons/kernel/network/protocols/tcp/EndpointManager.h (revision d25503d3dbd8e3f526fd0a9bdd884b8e43c1b794)
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 
26 struct net_address_module_info;
27 struct net_domain;
28 class EndpointManager;
29 class TCPEndpoint;
30 
31 
32 struct ConnectionHashDefinition {
33 public:
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 
50 private:
51 	EndpointManager*		fManager;
52 };
53 
54 
55 class EndpointHashDefinition {
56 public:
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 
69 class EndpointManager : public DoublyLinkedListLinkImpl<EndpointManager> {
70 public:
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 			status_t		Unbind(TCPEndpoint* endpoint);
87 
88 			status_t		ReplyWithReset(tcp_segment_header& segment,
89 								net_buffer* buffer);
90 
91 			net_domain*		Domain() const { return fDomain; }
92 			net_address_module_info* AddressModule() const
93 								{ return Domain()->address_module; }
94 
95 			void			Dump() const;
96 
97 private:
98 			TCPEndpoint*	_LookupConnection(const sockaddr* local,
99 								const sockaddr* peer);
100 			status_t		_Bind(TCPEndpoint* endpoint,
101 								const sockaddr* address);
102 			status_t		_BindToAddress(WriteLocker& locker,
103 								TCPEndpoint* endpoint, const sockaddr* address);
104 			status_t		_BindToEphemeral(TCPEndpoint* endpoint,
105 								const sockaddr* address);
106 
107 	typedef BOpenHashTable<ConnectionHashDefinition> ConnectionTable;
108 	typedef MultiHashTable<EndpointHashDefinition> EndpointTable;
109 
110 	rw_lock					fLock;
111 	net_domain*				fDomain;
112 	ConnectionTable			fConnectionHash;
113 	EndpointTable			fEndpointHash;
114 	uint16					fLastPort;
115 };
116 
117 #endif	// ENDPOINT_MANAGER_H
118