Ocean
Resolver.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef FACEBOOK_NETWORK_RESOLVER_H
9 #define FACEBOOK_NETWORK_RESOLVER_H
10 
11 #include "ocean/network/Network.h"
12 #include "ocean/network/Address4.h"
13 
14 #include "ocean/base/Lock.h"
15 #include "ocean/base/Singleton.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Network
21 {
22 
23 /**
24  * This class implements an address resolver.
25  * @ingroup network
26  */
27 class OCEAN_NETWORK_EXPORT Resolver : public Singleton<Resolver>
28 {
29  friend class Singleton<Resolver>;
30 
31  public:
32 
33  /**
34  * Definition of a vector holding addresses.
35  */
36  typedef std::vector<Address4> Addresses4;
37 
38  /**
39  * Definition of an unordered set holding addresses.
40  */
41  typedef std::unordered_set<Address4, Address4> AddressSet4;
42 
43  public:
44 
45  /**
46  * Returns all current local addresses.
47  * @return The local addresses of the system, empty if unknown
48  */
50 
51  /**
52  * Adds an explicit local address.
53  * @param localAddress The explicit local address to be set, must be valid
54  */
55  void addLocalAddress(const Address4& localAddress);
56 
57  /**
58  * Resolves a given host name to it's first found ip4 address.
59  * @param host Name of the host to resolve
60  * @return Resolved address, a zero address is returned if the host could not be resolved
61  */
62  static Address4 resolveFirstIp4(const std::string& host);
63 
64  /**
65  * Resolves a given host name to all possible ip4 addresses.
66  * @param host Name of the host to resolve
67  * @return Resolved addresses
68  */
69  static Addresses4 resolveIp4(const std::string& host);
70 
71  /**
72  * Resolves a given address to it's host name.
73  * If the address could not be resolved an empty string is returned.
74  * @param address Address4 to resolve
75  * @return Resolved host name
76  */
77  static std::string resolve(const Address4& address);
78 
79  protected:
80 
81  /**
82  * Creates a new resolver object.
83  */
85 
86  protected:
87 
88  /// The local addresses explicitly set.
90 
91  /// The resolver's lock.
92  mutable Lock lock_;
93 };
94 
95 }
96 
97 }
98 
99 #endif // FACEBOOK_NETWORK_RESOLVER_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class wraps an address number with 32 bits.
Definition: Address4.h:26
This class implements an address resolver.
Definition: Resolver.h:28
Resolver()
Creates a new resolver object.
static std::string resolve(const Address4 &address)
Resolves a given address to it's host name.
void addLocalAddress(const Address4 &localAddress)
Adds an explicit local address.
static Address4 resolveFirstIp4(const std::string &host)
Resolves a given host name to it's first found ip4 address.
Addresses4 localAddresses() const
Returns all current local addresses.
std::unordered_set< Address4, Address4 > AddressSet4
Definition of an unordered set holding addresses.
Definition: Resolver.h:41
AddressSet4 explicitLocalAddresses_
The local addresses explicitly set.
Definition: Resolver.h:89
Lock lock_
The resolver's lock.
Definition: Resolver.h:92
std::vector< Address4 > Addresses4
Definition of a vector holding addresses.
Definition: Resolver.h:36
static Addresses4 resolveIp4(const std::string &host)
Resolves a given host name to all possible ip4 addresses.
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15