Remote Call Framework 3.2
IpAddress.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2020, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 3.2
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_IPADDRESS_HPP
20 #define INCLUDE_RCF_IPADDRESS_HPP
21 
22 #include <string>
23 
24 #include <RCF/Export.hpp>
25 #include <RCF/Config.hpp>
26 #include <RCF/ServerTransport.hpp>
27 
28 #include <RCF/BsdSockets.hpp>
29 
30 namespace RCF {
31 
32 #if RCF_FEATURE_IPV6==1
33 
34  typedef in6_addr In6Addr;
35  typedef sockaddr_in6 SockAddrIn6;
36  typedef sockaddr_storage SockAddrStorage;
37 
38 #else
39 
40  // Dummy IPv6 structures.
41 
42  struct In6Addr {
43  union {
44  u_char Byte[16];
45  } u;
46  };
47 
48  struct SockAddrIn6 {
49  short sin6_family;
50  u_short sin6_port;
51  u_long sin6_flowinfo;
52  struct In6Addr sin6_addr;
53  u_long sin6_scope_id;
54  };
55 
56  struct SockAddrStorage
57  {
58  sockaddr_in mSockAddrIn;
59  };
60 
61 #endif
62 
63  class Exception;
64  typedef std::shared_ptr<Exception> ExceptionPtr;
65 
67  class RCF_EXPORT IpAddress : public RemoteAddress
68  {
69  public:
70 
71  // *** SWIG BEGIN ***
72 
74  explicit IpAddress(const std::string & ip);
75 
77  explicit IpAddress(const std::string & ip, int port);
78 
79  // *** SWIG END ***
80 
81  enum Type { V4_or_V6, V4, V6 };
82 
83  static void setPreferredResolveProtocol(Type type);
84  static Type getPreferredResolveProtocol();
85 
86  IpAddress();
87  explicit IpAddress(Type restrictTo);
88  explicit IpAddress(const std::string & ip, int port, Type restrictTo);
89  explicit IpAddress(const sockaddr_in &addr);
90  explicit IpAddress(const SockAddrIn6 &addr);
91  explicit IpAddress(const sockaddr &addr, std::size_t addrLen, Type type);
92  explicit IpAddress(int fd, Type type);
93 
94  void init(const sockaddr &addr, std::size_t addrLen, Type type);
95 
96  int createSocket(int socketType = SOCK_STREAM, int protocol = IPPROTO_TCP) const;
97  void getSockAddr(sockaddr *&, Platform::OS::BsdSockets::socklen_t &) const;
98  std::string getIp() const;
99  int getPort() const;
100  Type getType();
101 
102  void resolve() const;
103  void resolve(ExceptionPtr & e) const;
104  std::string string() const;
105  bool empty() const;
106  bool isResolved() const;
107  bool isBroadcast() const;
108  bool isMulticast() const;
109  bool isLoopback() const;
110  bool matches(const IpAddress & rhs, std::size_t bits = std::size_t(-1)) const;
111 
112  void setPort(int port);
113 
114  bool operator==(const IpAddress & rhs) const;
115  bool operator!=(const IpAddress & rhs) const;
116  bool operator<(const IpAddress &rhs) const;
117 
118  private:
119 
120  void extractIpAndPort();
121 
122  mutable Type mType;
123  mutable bool mResolved;
124  mutable sockaddr_in mAddrV4;
125  mutable SockAddrIn6 mAddrV6;
126 
127  std::string mIp;
128  int mPort;
129  };
130 
132  class RCF_EXPORT IpAddressV4 : public IpAddress
133  {
134  public:
135  IpAddressV4();
136  IpAddressV4(const std::string & ip);
137  IpAddressV4(const std::string & ip, int port);
138  IpAddressV4(const sockaddr_in & addr);
139  };
140 
142  class RCF_EXPORT IpAddressV6 : public IpAddress
143  {
144  public:
145  IpAddressV6();
146  IpAddressV6(const std::string & ip);
147  IpAddressV6(const std::string & ip, int port);
148  IpAddressV6(const SockAddrIn6 & addr);
149  };
150 
151 } // namespace RCF
152 
153 #endif // ! INCLUDE_RCF_IPADDRESS_HPP
Describes the network address of a remote peer.
Definition: ServerTransport.hpp:37
Represents an IPv6 IP address.
Definition: IpAddress.hpp:142
Represents an IPv4 IP address.
Definition: IpAddress.hpp:132
Definition: AmiIoHandler.hpp:24
RCF_EXPORT bool init(RcfConfigT *=nullptr)
Reference-counted initialization of RCF library. May be called multiple times (see deinit())...
Represents an IP address (IPv4 or IPv6).
Definition: IpAddress.hpp:67