socket_name.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 
30 #pragma once
31 
32 #include <memory>
33 
34 struct sockaddr;
35 
36 namespace uicore
37 {
39  class SocketName
40  {
41  public:
46  SocketName() : _port("0") { }
47  SocketName(const std::string &port) : _port(port) { }
48  SocketName(const std::string &address, const std::string &port) : _address(address), _port(port) { }
49 
51  const std::string &address() const { return _address; }
52 
54  const std::string &port() const { return _port; }
55 
60  void set_name(const std::string &hostname, const std::string &port) { _address = hostname; _port = port; }
61 
64  void set_address(const std::string &address) { _address = address; }
65 
67  void set_port(const std::string &port) { _port = port; }
68 
70  std::string lookup_ipv4() const;
71 
74 
77 
80 
82  void to_sockaddr(int domain, sockaddr *addr, int len) const;
83 
85  void from_sockaddr(int domain, sockaddr *addr, int len);
86 
87  bool operator == (const SocketName &other_instance) const;
88  bool operator < (const SocketName &other_instance) const;
89 
90  private:
91  std::string _address;
92  std::string _port;
93  };
94 }
void set_name(const std::string &hostname, const std::string &port)
Set the socket name using a hostname and port.
Definition: socket_name.h:60
const std::string & port() const
Returns the port part of the socket name.
Definition: socket_name.h:54
bool operator<(const SocketName &other_instance) const
const std::string & address() const
Returns the address part of the socket name.
Definition: socket_name.h:51
SocketName()
Constructs a new socket name.
Definition: socket_name.h:46
SocketName to_ipv4()
Create socket name that uses the IP v4 address as its address.
SocketName to_hostname()
Create socket name that uses the hostname as its address.
std::string lookup_hostname() const
Perform a DNS lookup, if needed, for the hostname.
void from_sockaddr(int domain, sockaddr *addr, int len)
Get the socket name from a C sockets sockaddr structure.
void set_port(const std::string &port)
Set the IP port.
Definition: socket_name.h:67
Socket name; container class for an IP address and port.
Definition: socket_name.h:39
void to_sockaddr(int domain, sockaddr *addr, int len) const
Fill the socket name into a C sockets sockaddr structure.
bool operator==(const SocketName &other_instance) const
std::string lookup_ipv4() const
Perform a DNS lookup, if needed, for the IP v4 address.
SocketName(const std::string &port)
Definition: socket_name.h:47
void set_address(const std::string &address)
Set the IP address.
Definition: socket_name.h:64
SocketName(const std::string &address, const std::string &port)
Definition: socket_name.h:48
Definition: Application/application.h:35