1 dave.sudlik 1.2 //%2007////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 dave.sudlik 1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #ifndef Pegasus_HostAddress_h
35 #define Pegasus_HostAddress_h
36
37 #include <Pegasus/Common/Config.h>
38 #include <Pegasus/Common/Linkage.h>
39 #include <Pegasus/Common/String.h>
40 #include <Pegasus/Common/Socket.h>
41
42 #ifdef PEGASUS_OS_VMS
43 dave.sudlik 1.2 #include <netinet/in6.h>
44 #endif
45
46 PEGASUS_NAMESPACE_BEGIN
47
48 #ifdef INET_ADDRSTRLEN
49 #define PEGASUS_INET_ADDRSTR_LEN INET_ADDRSTRLEN
50 #else
51 #define PEGASUS_INET_ADDRSTR_LEN 16
52 #endif
53
54 #ifdef INET6_ADDRSTRLEN
55 #define PEGASUS_INET6_ADDRSTR_LEN INET6_ADDRSTRLEN
56 #else
57 #define PEGASUS_INET6_ADDRSTR_LEN 46
58 #endif
59
60 #ifdef IN6ADDR_LOOPBACK_INIT
61 #define PEGASUS_IPV6_LOOPBACK_INIT IN6ADDR_LOOPBACK_INIT
62 #else
63 #define PEGASUS_IPV6_LOOPBACK_INIT {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}
64 dave.sudlik 1.2 #endif
65
66
67 #ifdef INADDR_LOOPBACK
68 #define PEGASUS_IPV4_LOOPBACK_INIT INADDR_LOOPBACK
69 #else
70 #define PEGASUS_IPV4_LOOPBACK_INIT 0x7F000001
71 #endif
72
73 #ifdef PEGASUS_ENABLE_IPV6
74 #define PEGASUS_IN6_ADDR_SIZE (sizeof (struct in6_addr))
75 #else
76 #define PEGASUS_IN6_ADDR_SIZE 0x10
77 #endif
78
79 /**
80 This class is used to store the host address. HostAddress can be Hostname or
81 IPv4 address or IPv6 address.
82 */
83
84 class PEGASUS_COMMON_LINKAGE HostAddress
85 dave.sudlik 1.2 {
86 public:
87
88 /**
89 Address types that HostAddress can have.
90 */
91 enum
92 {
93 AT_INVALID,
94 AT_IPV4 = AF_INET,
95 #ifdef PEGASUS_ENABLE_IPV6
96 AT_IPV6 = AF_INET6,
97 #else
98 AT_IPV6 = 0x17,
99 #endif
100 AT_HOSTNAME
101 };
102
103
104 HostAddress();
105 ~HostAddress();
106 dave.sudlik 1.2
107 /**
108 Constructor. addrStr can be HostName or IPv4Address or
109 IPv6Address (without brackets).
110 */
111 HostAddress(const String &addrStr);
112 HostAddress(const HostAddress &rhs);
113 HostAddress& operator =(const HostAddress &rhs);
114
115 void setHostAddress(const String &addrStr);
116
117 /**
118 Returns true if the constructed HostAddress is valid.
119 If valid it can be HostName or IPv4Address or IPv6Address.
120 Check if HostAddress is valid by using isValid() method
121 before making any calls on HostAddress object.
122 */
123 Boolean isValid();
124
125 /**
126 Verifies given IPv4Address and returns true if it is valid.
127 dave.sudlik 1.2 */
128 static Boolean isValidIPV4Address(const String &ipv4Address);
129
130 /**
131 Verifies given IPv6Address (without brackets) and returns
132 true if it is valid.
133 */
134 static Boolean isValidIPV6Address(const String &ipv6Address);
135
136 /**
137 Verifies given hostName and returns true if it is valid.
138 */
139 static Boolean isValidHostName(const String &hostName);
140
141 /**
142 The covertBinaryToText (inet_ntop()) function shall convert a numeric
143 address into a text string suitable for presentation. The af argument
144 shall specify the family of the address. This can be AF_INET or
145 AF_INET6. The src argument points to a buffer holding an IPv4 address
146 if the af argument is AF_INET, or an IPv6 address if the af argument is
147 AF_INET6. The address must be in network byte order. The dst argument
148 dave.sudlik 1.2 points to a buffer where the function stores the resulting text
149 string. It shall not be NULL. The size argument specifies the size of
150 this buffer, which shall be large enough to hold the text string
151 (PEGASUS_INET_ADDRSTR_LEN characters for IPv4,
152 PEGASUS_INET6_ADDRSTR_LEN characters for IPv6).
153 */
154 static const char *convertBinaryToText(int af, const void *src,
155 char *dst, Uint32 size);
156
157 /*
158 The convertTextToBinray (inet_pton()) function shall convert an
159 address in its standard text presentation form into its numeric
160 binary form. The af argument shall specify the family
161 of the address. The AF_INET and AF_INET6 address families
162 shall be supported. The src argument points to the
163 string being passed in. The dst argument points to a buffer into which
164 the function stores the numeric address. This shall be large enough to
165 hold the numeric address (32 bits for AF_INET, 128 bits for
166 AF_INET6).
167 */
168 static int convertTextToBinary(int af, const char *src, void *dst);
169 dave.sudlik 1.2
170 /**
171 Checks whether the two addresses in binary form are equal based on
172 address family. af can be AF_INET or AF_INET6.
173 */
174 static Boolean equal(int af, void *p1, void *p2);
175
176 /**
177 Returns HostName or IPv4Address or IPv6Address. This returns an empty
178 String if HostAddress is not valid.
179 Check if HostAddress is valid by using isValid() method
180 before making any calls on HostAddress object.
181 */
182 String getHost();
183
184 /**
185 Returns address type. It can be AT_IPV4, AT_IPV6 or AT_HOSTNAME.
186 */
187 Uint32 getAddressType();
188
189 private:
190 dave.sudlik 1.2 void _init();
191 void _parseAddress();
192 String _hostAddrStr;
193 Uint16 _addrType;
194 Boolean _isValid;
195 };
196
197 PEGASUS_NAMESPACE_END
198
199 #endif //Pegasus_HostAddress_h
|