xref: /haiku/headers/os/net/NetworkAddressResolver.h (revision b140a1c3400f7d6341a401461b38a4566aea24cd)
1836c43f2SAxel Dörfler /*
2*b140a1c3SAdrien Destugues  * Copyright 2010-2017, Haiku, Inc. All Rights Reserved.
3836c43f2SAxel Dörfler  * Distributed under the terms of the MIT License.
4836c43f2SAxel Dörfler  */
5836c43f2SAxel Dörfler #ifndef _NETWORK_ADDRESS_RESOLVER_H
6836c43f2SAxel Dörfler #define _NETWORK_ADDRESS_RESOLVER_H
7836c43f2SAxel Dörfler 
8836c43f2SAxel Dörfler 
9*b140a1c3SAdrien Destugues #include <Locker.h>
10f972422cSAdrien Destugues #include <ObjectList.h>
11f972422cSAdrien Destugues #include <Referenceable.h>
12f972422cSAdrien Destugues #include <String.h>
13836c43f2SAxel Dörfler #include <SupportDefs.h>
14836c43f2SAxel Dörfler 
15836c43f2SAxel Dörfler 
16836c43f2SAxel Dörfler class BNetworkAddress;
17836c43f2SAxel Dörfler struct addrinfo;
18836c43f2SAxel Dörfler 
19836c43f2SAxel Dörfler 
20836c43f2SAxel Dörfler // flags for name resolution
21836c43f2SAxel Dörfler enum {
22836c43f2SAxel Dörfler 	B_NO_ADDRESS_RESOLUTION			= 0x0001,
23836c43f2SAxel Dörfler 	B_UNCONFIGURED_ADDRESS_FAMILIES	= 0x0002,
24836c43f2SAxel Dörfler };
25836c43f2SAxel Dörfler 
26836c43f2SAxel Dörfler 
27f972422cSAdrien Destugues class BNetworkAddressResolver: public BReferenceable {
28836c43f2SAxel Dörfler public:
29836c43f2SAxel Dörfler 								BNetworkAddressResolver();
30836c43f2SAxel Dörfler 								BNetworkAddressResolver(const char* address,
31836c43f2SAxel Dörfler 									uint16 port = 0, uint32 flags = 0);
32836c43f2SAxel Dörfler 								BNetworkAddressResolver(const char* address,
33836c43f2SAxel Dörfler 									const char* service, uint32 flags = 0);
34836c43f2SAxel Dörfler 								BNetworkAddressResolver(int family,
35836c43f2SAxel Dörfler 									const char* address, uint16 port = 0,
36836c43f2SAxel Dörfler 									uint32 flags = 0);
37836c43f2SAxel Dörfler 								BNetworkAddressResolver(int family,
38836c43f2SAxel Dörfler 									const char* address, const char* service,
39836c43f2SAxel Dörfler 									uint32 flags = 0);
40836c43f2SAxel Dörfler 								~BNetworkAddressResolver();
41836c43f2SAxel Dörfler 
42836c43f2SAxel Dörfler 			status_t			InitCheck() const;
43836c43f2SAxel Dörfler 
44836c43f2SAxel Dörfler 			void				Unset();
45836c43f2SAxel Dörfler 
46836c43f2SAxel Dörfler 			status_t			SetTo(const char* address, uint16 port = 0,
47836c43f2SAxel Dörfler 									uint32 flags = 0);
48836c43f2SAxel Dörfler 			status_t			SetTo(const char* address, const char* service,
49836c43f2SAxel Dörfler 									uint32 flags = 0);
50836c43f2SAxel Dörfler 			status_t			SetTo(int family, const char* address,
51836c43f2SAxel Dörfler 									uint16 port = 0, uint32 flags = 0);
52836c43f2SAxel Dörfler 			status_t			SetTo(int family, const char* address,
53836c43f2SAxel Dörfler 									const char* service, uint32 flags = 0);
54836c43f2SAxel Dörfler 
55836c43f2SAxel Dörfler 			status_t			GetNextAddress(uint32* cookie,
56836c43f2SAxel Dörfler 									BNetworkAddress& address) const;
57836c43f2SAxel Dörfler 			status_t			GetNextAddress(int family, uint32* cookie,
58836c43f2SAxel Dörfler 									BNetworkAddress& address) const;
59836c43f2SAxel Dörfler 
60f972422cSAdrien Destugues 	// TODO all the ::Resolve variants are needed. Maybe the SetTo and
61f972422cSAdrien Destugues 	// constructors could be removed as ::Resolve is the right way to get a
62f972422cSAdrien Destugues 	// resolver (using the cache).
63f972422cSAdrien Destugues 	static	BReference<const BNetworkAddressResolver> Resolve(
64f972422cSAdrien Destugues 									const char* address, const char* service,
65f972422cSAdrien Destugues 									uint32 flags = 0);
66f972422cSAdrien Destugues 	static	BReference<const BNetworkAddressResolver> Resolve(
67f972422cSAdrien Destugues 									const char* address, uint16 port = 0,
68f972422cSAdrien Destugues 									uint32 flags = 0);
69f972422cSAdrien Destugues 	static	BReference<const BNetworkAddressResolver> Resolve(int family,
70f972422cSAdrien Destugues 									const char* address, const char* service,
71f972422cSAdrien Destugues 									uint32 flags = 0);
72f972422cSAdrien Destugues 	static	BReference<const BNetworkAddressResolver> Resolve(int family,
73f972422cSAdrien Destugues 									const char* address, uint16 port = 0,
74f972422cSAdrien Destugues 									uint32 flags = 0);
75f972422cSAdrien Destugues 
76836c43f2SAxel Dörfler private:
77836c43f2SAxel Dörfler 			addrinfo*			fInfo;
78836c43f2SAxel Dörfler 			status_t			fStatus;
79f972422cSAdrien Destugues 
80f972422cSAdrien Destugues 
81f972422cSAdrien Destugues 	struct CacheEntry {
CacheEntryCacheEntry82f972422cSAdrien Destugues 		CacheEntry(int family, const char* address, const char* service,
83f972422cSAdrien Destugues 			uint32 flags, BNetworkAddressResolver* resolver)
84f972422cSAdrien Destugues 			:
85f972422cSAdrien Destugues 			fFamily(family),
86f972422cSAdrien Destugues 			fAddress(address),
87f972422cSAdrien Destugues 			fService(service),
88f972422cSAdrien Destugues 			fFlags(flags),
89f972422cSAdrien Destugues 			fResolver(resolver, false)
90f972422cSAdrien Destugues 		{
91f972422cSAdrien Destugues 		}
92f972422cSAdrien Destugues 
MatchesCacheEntry93f972422cSAdrien Destugues 		bool Matches(int family, BString address, BString service, uint32 flags)
94f972422cSAdrien Destugues 		{
95f972422cSAdrien Destugues 			return family == fFamily && flags == fFlags && address == fAddress
96f972422cSAdrien Destugues 				&& service == fService;
97f972422cSAdrien Destugues 		}
98f972422cSAdrien Destugues 
99f972422cSAdrien Destugues 		int fFamily;
100f972422cSAdrien Destugues 		BString fAddress;
101f972422cSAdrien Destugues 		BString fService;
102f972422cSAdrien Destugues 		uint32 fFlags;
103f972422cSAdrien Destugues 
104f972422cSAdrien Destugues 		BReference<const BNetworkAddressResolver> fResolver;
105f972422cSAdrien Destugues 	};
106f972422cSAdrien Destugues 
107*b140a1c3SAdrien Destugues 	static	BLocker					sCacheLock;
108f972422cSAdrien Destugues 	static	BObjectList<CacheEntry> sCacheMap;
109836c43f2SAxel Dörfler };
110836c43f2SAxel Dörfler 
111836c43f2SAxel Dörfler 
112836c43f2SAxel Dörfler #endif	// _NETWORK_ADDRESS_RESOLVER_H
113