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