xref: /haiku/headers/os/net/NetworkAddressResolver.h (revision 6aff37d1c79e20748c683ae224bd629f88a5b0be)
1 /*
2  * Copyright 2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _NETWORK_ADDRESS_RESOLVER_H
6 #define _NETWORK_ADDRESS_RESOLVER_H
7 
8 
9 #include <ObjectList.h>
10 #include <Referenceable.h>
11 #include <String.h>
12 #include <SupportDefs.h>
13 
14 
15 class BNetworkAddress;
16 struct addrinfo;
17 
18 
19 // flags for name resolution
20 enum {
21 	B_NO_ADDRESS_RESOLUTION			= 0x0001,
22 	B_UNCONFIGURED_ADDRESS_FAMILIES	= 0x0002,
23 };
24 
25 
26 class BNetworkAddressResolver: public BReferenceable {
27 public:
28 								BNetworkAddressResolver();
29 								BNetworkAddressResolver(const char* address,
30 									uint16 port = 0, uint32 flags = 0);
31 								BNetworkAddressResolver(const char* address,
32 									const char* service, uint32 flags = 0);
33 								BNetworkAddressResolver(int family,
34 									const char* address, uint16 port = 0,
35 									uint32 flags = 0);
36 								BNetworkAddressResolver(int family,
37 									const char* address, const char* service,
38 									uint32 flags = 0);
39 								~BNetworkAddressResolver();
40 
41 			status_t			InitCheck() const;
42 
43 			void				Unset();
44 
45 			status_t			SetTo(const char* address, uint16 port = 0,
46 									uint32 flags = 0);
47 			status_t			SetTo(const char* address, const char* service,
48 									uint32 flags = 0);
49 			status_t			SetTo(int family, const char* address,
50 									uint16 port = 0, uint32 flags = 0);
51 			status_t			SetTo(int family, const char* address,
52 									const char* service, uint32 flags = 0);
53 
54 			status_t			GetNextAddress(uint32* cookie,
55 									BNetworkAddress& address) const;
56 			status_t			GetNextAddress(int family, uint32* cookie,
57 									BNetworkAddress& address) const;
58 
59 	// TODO all the ::Resolve variants are needed. Maybe the SetTo and
60 	// constructors could be removed as ::Resolve is the right way to get a
61 	// resolver (using the cache).
62 	static	BReference<const BNetworkAddressResolver> Resolve(
63 									const char* address, const char* service,
64 									uint32 flags = 0);
65 	static	BReference<const BNetworkAddressResolver> Resolve(
66 									const char* address, uint16 port = 0,
67 									uint32 flags = 0);
68 	static	BReference<const BNetworkAddressResolver> Resolve(int family,
69 									const char* address, const char* service,
70 									uint32 flags = 0);
71 	static	BReference<const BNetworkAddressResolver> Resolve(int family,
72 									const char* address, uint16 port = 0,
73 									uint32 flags = 0);
74 
75 private:
76 			addrinfo*			fInfo;
77 			status_t			fStatus;
78 
79 
80 	struct CacheEntry {
81 		CacheEntry(int family, const char* address, const char* service,
82 			uint32 flags, BNetworkAddressResolver* resolver)
83 			:
84 			fFamily(family),
85 			fAddress(address),
86 			fService(service),
87 			fFlags(flags),
88 			fResolver(resolver, false)
89 		{
90 		}
91 
92 		bool Matches(int family, BString address, BString service, uint32 flags)
93 		{
94 			return family == fFamily && flags == fFlags && address == fAddress
95 				&& service == fService;
96 		}
97 
98 		int fFamily;
99 		BString fAddress;
100 		BString fService;
101 		uint32 fFlags;
102 
103 		BReference<const BNetworkAddressResolver> fResolver;
104 	};
105 
106 	static	BObjectList<CacheEntry> sCacheMap;
107 };
108 
109 
110 #endif	// _NETWORK_ADDRESS_RESOLVER_H
111