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