libBsdSockets
C++ Wrapper classes to the BSD Socket API
InetAddress.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2013, Komodo Does Inc
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 
13 */
14 
15 #ifndef INET_ADDRESS_H
16 
17 #ifndef ADDRESS_H
18 #include "Address.h"
19 #endif // ADDRESS_H
20 
21 namespace BsdSockets {
22 
23  /*
24  * Forward Declarations
25  */
26  class LowLevelAddress;
27  class InetAddressPimpl;
28 
29  /** \class InetAddress
30  *
31  * \brief Internet Address Class supporting IPv4 and IPv6 and creatable from
32  * hostnames and service names.
33  *
34  * Most users will create addresses with a hostname and either a service name
35  * or port number. This supports that with ease. Additionally, create() can
36  * accept a std::vector<InetAddress> to put all matching Addresses into. For
37  * example, some hostnames are served by multiple IP addresses to load
38  * balance. The caller can use create() to either return the first match or
39  * populate a std::vector<InetAddress> with all matches.
40  */
41  class InetAddress : public Address {
42  public:
43  typedef std::shared_ptr<InetAddress> Ptr;
44 
45  /** Virtual destructor to support derived classes */
46  virtual ~InetAddress();
47 
48  /** Create an address with the given parameters putting all matches into
49  * created.
50  *
51  * \note Either of address or serviceName may be empty, but not both.
52  *
53  * @param socketType the SocketType of the Address
54  * @param serviceName name of standard service or port number desired, may be empty if address is not
55  * @param address desired, may be empty if serviceName is not empty
56  * @param[out] created the container to add created Addresses to
57  * @param max the limit on the count of Addresses to match, 0 means unlimited matches
58  *
59  * @return count of Addresses added to created
60  */
61  static int create(SocketType socketType, const std::string& serviceName, const std::string& address,
62  std::vector<InetAddress::Ptr>& created, unsigned int max = 0);
63 
64  /** Create one address with the given parameters.
65  *
66  * This is a convenience method equivalent to:
67  * \code
68  * std::vector<InetAddress> found;
69  * InetAddress::create(type, address, serviceName, found, 1);
70  * return created.at(0);
71  * \endcode
72  *
73  * \note Either of address or serviceName may be empty, but not both.
74  *
75  * @param socketType the SocketType of the Address
76  * @param serviceName name of standard service or port number desired, may be empty if address is not
77  * @param address desired, may be empty if serviceName is not empty
78  *
79  * @return InetAddress created from first match of address and serviceName
80  */
81  static InetAddress::Ptr create(SocketType socketType, const std::string& serviceName, const std::string& address = "");
82 
83  private:
84  /** Internal method to instantiate with all const values from static method/
85  *
86  * @param theSocketDomain domain of the socket from the Address
87  * @param theSocketType type of the socket from the Address
88  * @param theProtocol protocol of the socket from the address
89  * @param theServiceName requested by the user
90  * @param theRequestedAddress used to create the address
91  * @param pimpl private implementation of the class
92  */
93  InetAddress(SocketDomain theSocketDomain, SocketType theSocketType, int theProtocol,
94  const std::string& theServiceName, const std::string& theRequestedAddress,
95  std::shared_ptr<InetAddressPimpl> pimpl);
96 
97  private:
98  InetAddress() = delete;
99  InetAddress(const InetAddress& rhs) = delete;
100  InetAddress(InetAddress&& rhs) = delete;
101  InetAddress& operator=(const InetAddress& rhs) = delete;
102  InetAddress& operator=(InetAddress&& rhs) = delete;
103 
104  public:
105  /** @return the string address used to create this */
106  const std::string& getRequestedAddress() const;
107 
108  /** @return the computed string address that was actually created.
109  *
110  * \note This may be an IP if a hostname was requested, etc.
111  */
112  const std::string& getActualAddress() const;
113 
114  /** @return the service name or port number requested for this address may be blank */
115  const std::string& getServiceName() const;
116 
117  /** @return the actual port associated with this address */
118  const unsigned int getPort() const;
119 
120  public:
121  virtual std::shared_ptr<LowLevelAddress> makeTempLowLevelAddress() const;
122  virtual Address::Ptr create(std::shared_ptr<LowLevelAddress> lowLevelAddress) const;
123  virtual LowLevelAddress& getLowLevelAddress() const;
124 
125  private:
126  /** The serviceName used to create this Address */
127  const std::string serviceName;
128 
129  /** The string address used to create this */
130  const std::string requestedAddress;
131 
132  /** The private implementation of this InetAddress */
133  std::shared_ptr<InetAddressPimpl> const pimpl;
134  };
135 
136 }
137 // namespace BsdSockets
138 
139 #endif // INET_ADDRESS_H
Base class for all addresses.
Definition: Address.h:36
std::shared_ptr< Address > Ptr
Definition: Address.h:38
Internet Address Class supporting IPv4 and IPv6 and creatable from hostnames and service names.
Definition: InetAddress.h:41
const unsigned int getPort() const
const std::string & getActualAddress() const
const std::string & getServiceName() const
const std::string & getRequestedAddress() const
std::shared_ptr< InetAddress > Ptr
Definition: InetAddress.h:43
virtual std::shared_ptr< LowLevelAddress > makeTempLowLevelAddress() const
static int create(SocketType socketType, const std::string &serviceName, const std::string &address, std::vector< InetAddress::Ptr > &created, unsigned int max=0)
virtual LowLevelAddress & getLowLevelAddress() const
Interface for the low-level address implementation.
Namespace of the BsdSockets library.
Definition: Address.cpp:20