xref: /haiku/src/add-ons/kernel/network/protocols/tcp/EndpointManager.h (revision d9cebac2b77547b7064f22497514eecd2d047160)
1 /*
2  * Copyright 2006-2007, 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/DoublyLinkedList.h>
19 #include <util/MultiHashTable.h>
20 #include <util/OpenHashTable.h>
21 
22 #include <utility>
23 
24 
25 struct net_address_module_info;
26 struct net_domain;
27 class EndpointManager;
28 class TCPEndpoint;
29 
30 struct ConnectionHashDefinition {
31 public:
32 	typedef std::pair<const sockaddr *, const sockaddr *> KeyType;
33 	typedef TCPEndpoint ValueType;
34 
35 	ConnectionHashDefinition(EndpointManager *manager);
36 
37 	ConnectionHashDefinition(const ConnectionHashDefinition& definition)
38 		: fManager(definition.fManager)
39 	{
40 	}
41 
42 	size_t HashKey(const KeyType &key) const;
43 	size_t Hash(TCPEndpoint *endpoint) const;
44 	bool Compare(const KeyType &key, TCPEndpoint *endpoint) const;
45 	HashTableLink<TCPEndpoint> *GetLink(TCPEndpoint *endpoint) const;
46 
47 private:
48 	EndpointManager *fManager;
49 };
50 
51 
52 class EndpointHashDefinition {
53 public:
54 	typedef uint16 KeyType;
55 	typedef TCPEndpoint ValueType;
56 
57 	size_t HashKey(uint16 port) const;
58 	size_t Hash(TCPEndpoint *endpoint) const;
59 	bool Compare(uint16 port, TCPEndpoint *endpoint) const;
60 	bool CompareValues(TCPEndpoint *first, TCPEndpoint *second) const;
61 	HashTableLink<TCPEndpoint> *GetLink(TCPEndpoint *endpoint) const;
62 };
63 
64 
65 class EndpointManager : public DoublyLinkedListLinkImpl<EndpointManager> {
66 	public:
67 		EndpointManager(net_domain *domain);
68 		~EndpointManager();
69 
70 		status_t InitCheck() const;
71 
72 		TCPEndpoint *FindConnection(sockaddr *local, sockaddr *peer);
73 
74 		status_t SetConnection(TCPEndpoint *endpoint, const sockaddr *local,
75 			const sockaddr *peer, const sockaddr *interfaceLocal);
76 		status_t SetPassive(TCPEndpoint *endpoint);
77 
78 		status_t Bind(TCPEndpoint *endpoint, const sockaddr *address);
79 		status_t BindChild(TCPEndpoint *endpoint);
80 		status_t Unbind(TCPEndpoint *endpoint);
81 
82 		status_t ReplyWithReset(tcp_segment_header &segment,
83 			net_buffer *buffer);
84 
85 		void DumpEndpoints() const;
86 
87 		net_domain *Domain() const { return fDomain; }
88 		net_address_module_info *AddressModule() const
89 			{ return Domain()->address_module; }
90 
91 	private:
92 		TCPEndpoint *_LookupConnection(const sockaddr *local,
93 			const sockaddr *peer);
94 		status_t _Bind(TCPEndpoint *endpoint, const sockaddr *address);
95 		status_t _BindToAddress(TCPEndpoint *endpoint, const sockaddr *address);
96 		status_t _BindToEphemeral(TCPEndpoint *endpoint,
97 			const sockaddr *address);
98 
99 		net_domain *fDomain;
100 
101 		typedef OpenHashTable<ConnectionHashDefinition> ConnectionTable;
102 		typedef MultiHashTable<EndpointHashDefinition> EndpointTable;
103 
104 		ConnectionTable fConnectionHash;
105 		EndpointTable fEndpointHash;
106 		benaphore fLock;
107 };
108 
109 #endif	// ENDPOINT_MANAGER_H
110