xref: /haiku/headers/os/net/NetworkAddressResolver.h (revision f972422c66af92a5c6abf9852c904b92ea9255ea)
1836c43f2SAxel Dörfler /*
2836c43f2SAxel Dörfler  * Copyright 2010, 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*f972422cSAdrien Destugues #include <ObjectList.h>
10*f972422cSAdrien Destugues #include <Referenceable.h>
11*f972422cSAdrien Destugues #include <String.h>
12836c43f2SAxel Dörfler #include <SupportDefs.h>
13836c43f2SAxel Dörfler 
14836c43f2SAxel Dörfler 
15836c43f2SAxel Dörfler class BNetworkAddress;
16836c43f2SAxel Dörfler struct addrinfo;
17836c43f2SAxel Dörfler 
18836c43f2SAxel Dörfler 
19836c43f2SAxel Dörfler // flags for name resolution
20836c43f2SAxel Dörfler enum {
21836c43f2SAxel Dörfler 	B_NO_ADDRESS_RESOLUTION			= 0x0001,
22836c43f2SAxel Dörfler 	B_UNCONFIGURED_ADDRESS_FAMILIES	= 0x0002,
23836c43f2SAxel Dörfler };
24836c43f2SAxel Dörfler 
25836c43f2SAxel Dörfler 
26*f972422cSAdrien Destugues class BNetworkAddressResolver: public BReferenceable {
27836c43f2SAxel Dörfler public:
28836c43f2SAxel Dörfler 								BNetworkAddressResolver();
29836c43f2SAxel Dörfler 								BNetworkAddressResolver(const char* address,
30836c43f2SAxel Dörfler 									uint16 port = 0, uint32 flags = 0);
31836c43f2SAxel Dörfler 								BNetworkAddressResolver(const char* address,
32836c43f2SAxel Dörfler 									const char* service, uint32 flags = 0);
33836c43f2SAxel Dörfler 								BNetworkAddressResolver(int family,
34836c43f2SAxel Dörfler 									const char* address, uint16 port = 0,
35836c43f2SAxel Dörfler 									uint32 flags = 0);
36836c43f2SAxel Dörfler 								BNetworkAddressResolver(int family,
37836c43f2SAxel Dörfler 									const char* address, const char* service,
38836c43f2SAxel Dörfler 									uint32 flags = 0);
39836c43f2SAxel Dörfler 								~BNetworkAddressResolver();
40836c43f2SAxel Dörfler 
41836c43f2SAxel Dörfler 			status_t			InitCheck() const;
42836c43f2SAxel Dörfler 
43836c43f2SAxel Dörfler 			void				Unset();
44836c43f2SAxel Dörfler 
45836c43f2SAxel Dörfler 			status_t			SetTo(const char* address, uint16 port = 0,
46836c43f2SAxel Dörfler 									uint32 flags = 0);
47836c43f2SAxel Dörfler 			status_t			SetTo(const char* address, const char* service,
48836c43f2SAxel Dörfler 									uint32 flags = 0);
49836c43f2SAxel Dörfler 			status_t			SetTo(int family, const char* address,
50836c43f2SAxel Dörfler 									uint16 port = 0, uint32 flags = 0);
51836c43f2SAxel Dörfler 			status_t			SetTo(int family, const char* address,
52836c43f2SAxel Dörfler 									const char* service, uint32 flags = 0);
53836c43f2SAxel Dörfler 
54836c43f2SAxel Dörfler 			status_t			GetNextAddress(uint32* cookie,
55836c43f2SAxel Dörfler 									BNetworkAddress& address) const;
56836c43f2SAxel Dörfler 			status_t			GetNextAddress(int family, uint32* cookie,
57836c43f2SAxel Dörfler 									BNetworkAddress& address) const;
58836c43f2SAxel Dörfler 
59*f972422cSAdrien Destugues 	// TODO all the ::Resolve variants are needed. Maybe the SetTo and
60*f972422cSAdrien Destugues 	// constructors could be removed as ::Resolve is the right way to get a
61*f972422cSAdrien Destugues 	// resolver (using the cache).
62*f972422cSAdrien Destugues 	static	BReference<const BNetworkAddressResolver> Resolve(
63*f972422cSAdrien Destugues 									const char* address, const char* service,
64*f972422cSAdrien Destugues 									uint32 flags = 0);
65*f972422cSAdrien Destugues 	static	BReference<const BNetworkAddressResolver> Resolve(
66*f972422cSAdrien Destugues 									const char* address, uint16 port = 0,
67*f972422cSAdrien Destugues 									uint32 flags = 0);
68*f972422cSAdrien Destugues 	static	BReference<const BNetworkAddressResolver> Resolve(int family,
69*f972422cSAdrien Destugues 									const char* address, const char* service,
70*f972422cSAdrien Destugues 									uint32 flags = 0);
71*f972422cSAdrien Destugues 	static	BReference<const BNetworkAddressResolver> Resolve(int family,
72*f972422cSAdrien Destugues 									const char* address, uint16 port = 0,
73*f972422cSAdrien Destugues 									uint32 flags = 0);
74*f972422cSAdrien Destugues 
75836c43f2SAxel Dörfler private:
76836c43f2SAxel Dörfler 			addrinfo*			fInfo;
77836c43f2SAxel Dörfler 			status_t			fStatus;
78*f972422cSAdrien Destugues 
79*f972422cSAdrien Destugues 
80*f972422cSAdrien Destugues 	struct CacheEntry {
81*f972422cSAdrien Destugues 		CacheEntry(int family, const char* address, const char* service,
82*f972422cSAdrien Destugues 			uint32 flags, BNetworkAddressResolver* resolver)
83*f972422cSAdrien Destugues 			:
84*f972422cSAdrien Destugues 			fFamily(family),
85*f972422cSAdrien Destugues 			fAddress(address),
86*f972422cSAdrien Destugues 			fService(service),
87*f972422cSAdrien Destugues 			fFlags(flags),
88*f972422cSAdrien Destugues 			fResolver(resolver, false)
89*f972422cSAdrien Destugues 		{
90*f972422cSAdrien Destugues 		}
91*f972422cSAdrien Destugues 
92*f972422cSAdrien Destugues 		bool Matches(int family, BString address, BString service, uint32 flags)
93*f972422cSAdrien Destugues 		{
94*f972422cSAdrien Destugues 			return family == fFamily && flags == fFlags && address == fAddress
95*f972422cSAdrien Destugues 				&& service == fService;
96*f972422cSAdrien Destugues 		}
97*f972422cSAdrien Destugues 
98*f972422cSAdrien Destugues 		int fFamily;
99*f972422cSAdrien Destugues 		BString fAddress;
100*f972422cSAdrien Destugues 		BString fService;
101*f972422cSAdrien Destugues 		uint32 fFlags;
102*f972422cSAdrien Destugues 
103*f972422cSAdrien Destugues 		BReference<const BNetworkAddressResolver> fResolver;
104*f972422cSAdrien Destugues 	};
105*f972422cSAdrien Destugues 
106*f972422cSAdrien Destugues 	static	BObjectList<CacheEntry> sCacheMap;
107836c43f2SAxel Dörfler };
108836c43f2SAxel Dörfler 
109836c43f2SAxel Dörfler 
110836c43f2SAxel Dörfler #endif	// _NETWORK_ADDRESS_RESOLVER_H
111