RCFProto

- Main Page
- Classes
- Files

- File List

All Classes Functions Typedefs
IpAddress.hpp
1
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, 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: 2.0
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/util/Platform/OS/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 boost::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
131 class RCF_EXPORT IpAddressV4 : public IpAddress
132 {
133 public:
134
135 IpAddressV4() : IpAddress(V4)
136 {}
137
138 IpAddressV4(const std::string & ip) : IpAddress(ip, 0, V4)
139 {}
140
141 IpAddressV4(const std::string & ip, int port) : IpAddress(ip, port, V4)
142 {}
143
144 IpAddressV4(const sockaddr_in & addr) : IpAddress(addr)
145 {}
146 };
147
148 class RCF_EXPORT IpAddressV6 : public IpAddress
149 {
150 public:
151
152 IpAddressV6() : IpAddress(V6)
153 {}
154
155 IpAddressV6(const std::string & ip) : IpAddress(ip, 0, V6)
156 {}
157
158 IpAddressV6(const std::string & ip, int port) : IpAddress(ip, port, V6)
159 {}
160
161 IpAddressV6(const SockAddrIn6 & addr) : IpAddress(addr)
162 {}
163 };
164
165 } // namespace RCF
166
167 #endif // ! INCLUDE_RCF_IPADDRESS_HPP