1 karl 1.25 //%2005////////////////////////////////////////////////////////////////////////
|
2 mike 1.6 //
|
3 karl 1.23 // 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 karl 1.19 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.23 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.25 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 mike 1.6 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
13 kumpf 1.11 // of this software and associated documentation files (the "Software"), to
14 // deal in the Software without restriction, including without limitation the
15 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
16 mike 1.6 // sell copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
|
18 karl 1.23 //
|
19 kumpf 1.11 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
20 mike 1.6 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
21 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
22 kumpf 1.11 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
25 mike 1.6 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 //==============================================================================
29 //
30 // Author: Mike Brasher (mbrasher@bmc.com)
31 //
|
32 mike 1.7 // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01
|
33 ramnath 1.9 // added nsk platform support
34 // Ramnath Ravindran (Ramnath.Ravindran@compaq.com) 03/21/2002
|
35 r.kieninger 1.21 // replaced instances of "| ios::binary" with
|
36 ramnath 1.9 // PEGASUS_OR_IOS_BINARY
|
37 r.kieninger 1.21 // Robert Kieninger, IBM (kieningr@de.ibm.com) for Bug#667
|
38 dave.sudlik 1.22 // Dave Sudlik, IBM (dsudlik@us.ibm.com) for Bug#1462
|
39 gs.keenan 1.26 // Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
|
40 mike 1.6 //
41 //%/////////////////////////////////////////////////////////////////////////////
|
42 mike 1.8
|
43 sage 1.12 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
|
44 r.kieninger 1.21 #include <Pegasus/Common/Config.h>
45 #endif
|
46 sage 1.12
47
|
48 mike 1.8 #include <fstream>
|
49 kumpf 1.13 #include <cctype> // for tolower()
|
50 kumpf 1.14 #include <cstring>
|
51 mike 1.6 #include "System.h"
|
52 r.kieninger 1.21 #include "Socket.h"
53
54 #ifdef PEGASUS_PLATFORM_WIN32_IX86_MSVC
55 # include <windows.h>
56 #else
57 # include <arpa/inet.h>
58 #endif
|
59 mike 1.6
|
60 kumpf 1.10 #include <Pegasus/Common/PegasusVersion.h>
61
|
62 mike 1.6 #if defined(PEGASUS_OS_TYPE_WINDOWS)
63 # include "SystemWindows.cpp"
64 #elif defined(PEGASUS_OS_TYPE_UNIX)
65 # include "SystemUnix.cpp"
|
66 mike 1.7 #elif defined(PEGASUS_OS_TYPE_NSK)
67 # include "SystemNsk.cpp"
|
68 gs.keenan 1.26 #elif defined(PEGASUS_OS_VMS)
69 # include "SystemVms.cpp"
|
70 mike 1.6 #else
71 # error "Unsupported platform"
72 #endif
|
73 mike 1.8
|
74 david 1.16 #if defined(PEGASUS_OS_OS400)
75 # include "OS400ConvertChar.h"
76 #endif
77
|
78 mike 1.8 PEGASUS_USING_STD;
79
80 PEGASUS_NAMESPACE_BEGIN
81
|
82 kumpf 1.20 Boolean System::bindVerbose = false;
83
|
84 mike 1.8 Boolean System::copyFile(const char* fromPath, const char* toPath)
85 {
|
86 ramnath 1.9 ifstream is(fromPath PEGASUS_IOS_BINARY);
87 ofstream os(toPath PEGASUS_IOS_BINARY);
|
88 mike 1.8
89 char c;
90
91 while (is.get(c))
92 {
93 if (!os.put(c))
94 return false;
95 }
96
97 return true;
|
98 kumpf 1.13 }
99
100 // ATTN: Move to platform-specific System implementation files and call
101 // strcasecmp where it is available.
102 Sint32 System::strcasecmp(const char* s1, const char* s2)
103 {
104 while (*s1 && *s2)
105 {
106 int r = tolower(*s1++) - tolower(*s2++);
107
108 if (r)
109 return r;
110 }
111
112 if (*s2)
113 return -1;
114 else if (*s1)
115 return 1;
116
117 return 0;
|
118 mike 1.8 }
119
|
120 tony 1.15 // Return the just the file name from the path into basename
121 char *System::extract_file_name(const char *fullpath, char *basename)
122 {
123 if (fullpath == NULL)
|
124 kumpf 1.31 {
|
125 tony 1.15 basename[0] = '\0';
126 return basename;
|
127 kumpf 1.31 }
128
129 for (const char* p = fullpath + strlen(fullpath) - 1; p >= fullpath; p--)
130 {
|
131 tony 1.15 if (*p == '\\' || *p == '/')
|
132 kumpf 1.31 {
|
133 tony 1.15 strcpy(basename, p+1);
134 return basename;
|
135 kumpf 1.31 }
136 }
137
|
138 tony 1.15 strcpy(basename, fullpath);
139 return basename;
140 }
141
142 // Return the just the path to the file name into dirname
143 char *System::extract_file_path(const char *fullpath, char *dirname)
144 {
145 char *p;
146 char buff[2048];
147 if (fullpath == NULL)
148 {
149 dirname[0] = '\0';
150 return dirname;
151 }
152 strcpy(buff, fullpath);
153 for(p = buff + strlen(buff); p >= buff; p--)
154 {
155 if (*p == '\\' || *p == '/')
156 {
157 strncpy(dirname, buff, p+1 - buff);
158 dirname[p+1 - buff] = '\0';
159 tony 1.15 return dirname;
160 }
161 }
162 strcpy(dirname, fullpath);
163 return dirname;
164 }
165
|
166 marek 1.24 String System::getHostIP(const String &hostName)
167 {
168 struct hostent * phostent;
169 struct in_addr inaddr;
170 String ipAddress = String::EMPTY;
|
171 humberto 1.27 CString csName = hostName.getCString();
172 const char * ccName = csName;
173 #ifndef PEGASUS_OS_OS400
174 if ((phostent = ::gethostbyname(ccName)) != NULL)
175 #else
|
176 chuck 1.28 char ebcdicHost[PEGASUS_MAXHOSTNAMELEN];
177 if (strlen(ccName) < PEGASUS_MAXHOSTNAMELEN)
178 strcpy(ebcdicHost, ccName);
179 else
180 return ipAddress;
181 AtoE(ebcdicHost);
182 if ((phostent = ::gethostbyname(ebcdicHost)) != NULL)
|
183 humberto 1.27 #endif
|
184 marek 1.24 {
185 ::memcpy( &inaddr, phostent->h_addr,4);
186 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
187 char * gottenIPAdress = NULL;
188 gottenIPAdress = ::inet_ntoa( inaddr );
189 __etoa(gottenIPAdress);
190 if (gottenIPAdress != NULL)
191 {
192 ipAddress.assign(gottenIPAdress);
193 }
194 #else
195 ipAddress = ::inet_ntoa( inaddr );
196 #endif
197 }
198 return ipAddress;
199 }
|
200 r.kieninger 1.21
201 // ------------------------------------------------------------------------
202 // Convert a hostname into a a single host unique integer representation
203 // ------------------------------------------------------------------------
204 Uint32 System::_acquireIP(const char* hostname)
205 {
206 Uint32 ip = 0xFFFFFFFF;
207 if (!hostname) return 0xFFFFFFFF;
208
209 #ifdef PEGASUS_OS_OS400
210 char ebcdicHost[PEGASUS_MAXHOSTNAMELEN];
211 if (strlen(hostname) < PEGASUS_MAXHOSTNAMELEN)
212 strcpy(ebcdicHost, hostname);
213 else
214 return 0xFFFFFFFF;
215 AtoE(ebcdicHost);
216 #endif
217
|
218 dave.sudlik 1.22 ////////////////////////////////////////////////////////////////////////////////
219 // This code used to check if the first character of "hostname" was alphabetic
220 // to indicate hostname instead of IP address. But RFC 1123, section 2.1, relaxed
221 // this requirement to alphabetic character *or* digit. So bug 1462 changed the
222 // flow here to call inet_addr first to check for a valid IP address in dotted
223 // decimal notation. If it's not a valid IP address, then try to validate
224 // it as a hostname.
225 // RFC 1123 states: The host SHOULD check the string syntactically for a
226 // dotted-decimal number before looking it up in the Domain Name System.
227 // Hence the call to inet_addr() first.
228 ////////////////////////////////////////////////////////////////////////////////
229
230 #ifdef PEGASUS_OS_OS400
231 Uint32 tmp_addr = inet_addr(ebcdicHost);
232 #elif defined(PEGASUS_OS_ZOS)
233 Uint32 tmp_addr = inet_addr_ebcdic((char *)hostname);
234 #else
235 Uint32 tmp_addr = inet_addr((char *) hostname);
236 #endif
237
|
238 r.kieninger 1.21 struct hostent *entry;
239
|
240 dave.sudlik 1.22 // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255).
241 // A better solution would be to use inet_aton() or equivalent, as
242 // inet_addr() is now considered "obsolete".
243
244 if (tmp_addr == 0xFFFFFFFF) // if hostname is not an IP address
|
245 r.kieninger 1.21 {
246 #ifdef PEGASUS_PLATFORM_SOLARIS_SPARC_CC
247 #define HOSTENT_BUFF_SIZE 8192
248 char buf[HOSTENT_BUFF_SIZE];
249 int h_errorp;
250 struct hostent hp;
251
252 entry = gethostbyname_r((char *)hostname, &hp, buf,
253 HOSTENT_BUFF_SIZE, &h_errorp);
254 #elif defined(PEGASUS_OS_OS400)
255 entry = gethostbyname(ebcdicHost);
256 #elif defined(PEGASUS_OS_ZOS)
|
257 kumpf 1.30 char hostName[PEGASUS_MAXHOSTNAMELEN + 1];
|
258 r.kieninger 1.21 if (String::equalNoCase("localhost",String(hostname)))
259 {
260 gethostname( hostName, PEGASUS_MAXHOSTNAMELEN );
|
261 kumpf 1.30 hostName[sizeof(hostName)-1] = 0;
|
262 r.kieninger 1.21 entry = gethostbyname(hostName);
263 } else
264 {
265 entry = gethostbyname((char *)hostname);
266 }
267 #else
268 entry = gethostbyname((char *)hostname);
269 #endif
270 if (!entry)
271 {
272 return 0xFFFFFFFF;
273 }
274 unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
275
276 ip_part1 = entry->h_addr[0];
277 ip_part2 = entry->h_addr[1];
278 ip_part3 = entry->h_addr[2];
279 ip_part4 = entry->h_addr[3];
280 ip = ip_part1;
281 ip = (ip << 8) + ip_part2;
282 ip = (ip << 8) + ip_part3;
283 r.kieninger 1.21 ip = (ip << 8) + ip_part4;
|
284 dave.sudlik 1.22 }
285 else // else hostname *is* a dotted-decimal IP address
|
286 r.kieninger 1.21 {
287 // resolve hostaddr to a real host entry
288 // casting to (const char *) as (char *) will work as (void *) too, those it fits all platforms
|
289 humberto 1.27 #ifndef PEGASUS_OS_OS400
290 entry = gethostbyaddr((const char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
291 #else
|
292 chuck 1.28 entry = gethostbyaddr((char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
|
293 humberto 1.27 #endif
|
294 r.kieninger 1.21 if (entry == 0)
295 {
296 // error, couldn't resolve the ip
297 return 0xFFFFFFFF;
298 } else
299 {
300
301 unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
302
303 ip_part1 = entry->h_addr[0];
304 ip_part2 = entry->h_addr[1];
305 ip_part3 = entry->h_addr[2];
306 ip_part4 = entry->h_addr[3];
307 ip = ip_part1;
308 ip = (ip << 8) + ip_part2;
309 ip = (ip << 8) + ip_part3;
310 ip = (ip << 8) + ip_part4;
311 }
312 }
313
314 return ip;
315 r.kieninger 1.21 }
316
|
317 carolann.graves 1.29 Boolean System::sameHost (const String & hostName)
318 {
319 //
320 // If a port is included, return false
321 //
322 if (hostName.find (":") != PEG_NOT_FOUND)
323 {
324 return false;
325 }
326
327 //
328 // Retrieve IP addresses for both hostnames
329 //
330 Uint32 hostNameIP, systemHostIP = 0xFFFFFFFF;
331 hostNameIP = System::_acquireIP ((const char *) hostName.getCString ());
332 if (hostNameIP == 0x7F000001)
333 {
334 //
335 // localhost or IP address of 127.0.0.1
336 // real IP address needed for compare
337 //
338 carolann.graves 1.29 hostNameIP = System::_acquireIP
339 ((const char *) System::getHostName ().getCString ());
340 }
341 if (hostNameIP == 0xFFFFFFFF)
342 {
343 //
344 // Malformed IP address or not resolveable
345 //
346 return false;
347 }
348
349 systemHostIP = System::_acquireIP
350 ((const char *) System::getFullyQualifiedHostName ().getCString ());
351
352 if (systemHostIP == 0x7F000001)
353 {
354 //
355 // localhost or IP address of 127.0.0.1
356 // real IP address needed for compare
357 //
358 systemHostIP = System::_acquireIP
359 carolann.graves 1.29 ((const char *) System::getHostName ().getCString ());
360 }
361 if (systemHostIP == 0xFFFFFFFF)
362 {
363 //
364 // Malformed IP address or not resolveable
365 //
366 return false;
367 }
368
369 if (hostNameIP != systemHostIP)
370 {
371 return false;
372 }
373
374 return true;
375 }
376
|
377 tony 1.17 // System ID constants for Logger::put and Logger::trace
378 const String System::CIMLISTENER = "cimlistener"; // Listener systme ID
379
|
380 mike 1.8 PEGASUS_NAMESPACE_END
|