Return to System.cpp CVS log | Up to [Pegasus] / pegasus / src / Pegasus / Common |
1 karl 1.37 //%2006//////////////////////////////////////////////////////////////////////// | ||
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 karl 1.37 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.; 12 // EMC Corporation; Symantec Corporation; The Open Group. | ||
13 mike 1.6 // 14 // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
15 kumpf 1.11 // 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 mike 1.6 // 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 karl 1.23 // | ||
21 kumpf 1.11 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN | ||
22 mike 1.6 // 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 kumpf 1.11 // 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 mike 1.6 // 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 // Author: Mike Brasher (mbrasher@bmc.com) 33 // | ||
34 mike 1.7 // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01 | ||
35 david.dillard 1.35 // added nsk platform support 36 // Ramnath Ravindran (Ramnath.Ravindran@compaq.com) 03/21/2002 37 // replaced instances of "| ios::binary" with 38 // PEGASUS_OR_IOS_BINARY | ||
39 r.kieninger 1.21 // Robert Kieninger, IBM (kieningr@de.ibm.com) for Bug#667 | ||
40 dave.sudlik 1.22 // Dave Sudlik, IBM (dsudlik@us.ibm.com) for Bug#1462 | ||
41 gs.keenan 1.26 // Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com) | ||
42 david.dillard 1.35 // David Dillard, Symantec Corp. (david_dillard@symantec.com) | ||
43 mike 1.6 // 44 //%///////////////////////////////////////////////////////////////////////////// | ||
45 mike 1.8 | ||
46 sage 1.12 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) | ||
47 r.kieninger 1.21 #include <Pegasus/Common/Config.h> 48 #endif | ||
49 sage 1.12 50 | ||
51 mike 1.8 #include <fstream> | ||
52 kumpf 1.13 #include <cctype> // for tolower() | ||
53 kumpf 1.14 #include <cstring> | ||
54 mike 1.6 #include "System.h" | ||
55 r.kieninger 1.21 #include "Socket.h" 56 | ||
57 david.dillard 1.35 #ifdef PEGASUS_OS_TYPE_WINDOWS | ||
58 r.kieninger 1.21 # include <windows.h> 59 #else 60 # include <arpa/inet.h> 61 #endif | ||
62 mike 1.6 | ||
63 kumpf 1.10 #include <Pegasus/Common/PegasusVersion.h> 64 | ||
65 mike 1.6 #if defined(PEGASUS_OS_TYPE_WINDOWS) 66 # include "SystemWindows.cpp" 67 #elif defined(PEGASUS_OS_TYPE_UNIX) 68 # include "SystemUnix.cpp" | ||
69 mike 1.7 #elif defined(PEGASUS_OS_TYPE_NSK) 70 # include "SystemNsk.cpp" | ||
71 gs.keenan 1.26 #elif defined(PEGASUS_OS_VMS) 72 # include "SystemVms.cpp" | ||
73 mike 1.6 #else 74 # error "Unsupported platform" 75 #endif | ||
76 mike 1.8 | ||
77 david 1.16 #if defined(PEGASUS_OS_OS400) 78 # include "OS400ConvertChar.h" 79 #endif 80 | ||
81 mike 1.8 PEGASUS_USING_STD; 82 83 PEGASUS_NAMESPACE_BEGIN 84 | ||
85 kumpf 1.20 Boolean System::bindVerbose = false; 86 | ||
87 mike 1.8 Boolean System::copyFile(const char* fromPath, const char* toPath) 88 { | ||
89 ramnath 1.9 ifstream is(fromPath PEGASUS_IOS_BINARY); 90 ofstream os(toPath PEGASUS_IOS_BINARY); | ||
91 mike 1.8 92 char c; 93 94 while (is.get(c)) 95 { 96 if (!os.put(c)) 97 return false; 98 } 99 100 return true; | ||
101 kumpf 1.13 } 102 | ||
103 mike 1.32 static const Uint8 _toLowerTable[256] = 104 { 105 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 106 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F, 107 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 108 0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, 109 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27, 110 0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F, 111 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37, 112 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F, 113 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67, 114 0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, 115 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77, 116 0x78,0x79,0x7A,0x5B,0x5C,0x5D,0x5E,0x5F, 117 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67, 118 0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, 119 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77, 120 0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F, 121 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, 122 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, 123 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, 124 mike 1.32 0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, 125 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7, 126 0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, 127 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7, 128 0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, 129 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7, 130 0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, 131 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7, 132 0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, 133 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7, 134 0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, 135 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7, 136 0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF, 137 }; 138 | ||
139 kumpf 1.13 Sint32 System::strcasecmp(const char* s1, const char* s2) 140 { | ||
141 mike 1.32 // Note: this is faster than glibc strcasecmp(). 142 143 Uint8* p = (Uint8*)s1; 144 Uint8* q = (Uint8*)s2; 145 int r; 146 147 for (;;) | ||
148 kumpf 1.13 { | ||
149 mike 1.32 if ((r = _toLowerTable[p[0]] - _toLowerTable[q[0]]) || !p[0] || 150 (r = _toLowerTable[p[1]] - _toLowerTable[q[1]]) || !p[1] || 151 (r = _toLowerTable[p[2]] - _toLowerTable[q[2]]) || !p[2] || 152 (r = _toLowerTable[p[3]] - _toLowerTable[q[3]]) || !p[3]) 153 break; | ||
154 kumpf 1.13 | ||
155 mike 1.32 p += 4; 156 q += 4; | ||
157 kumpf 1.13 } 158 | ||
159 mike 1.32 return r; | ||
160 mike 1.8 } 161 | ||
162 tony 1.15 // Return the just the file name from the path into basename 163 char *System::extract_file_name(const char *fullpath, char *basename) 164 { 165 if (fullpath == NULL) | ||
166 kumpf 1.31 { | ||
167 tony 1.15 basename[0] = '\0'; 168 return basename; | ||
169 kumpf 1.31 } 170 171 for (const char* p = fullpath + strlen(fullpath) - 1; p >= fullpath; p--) 172 { | ||
173 tony 1.15 if (*p == '\\' || *p == '/') | ||
174 kumpf 1.31 { | ||
175 tony 1.15 strcpy(basename, p+1); 176 return basename; | ||
177 kumpf 1.31 } 178 } 179 | ||
180 tony 1.15 strcpy(basename, fullpath); 181 return basename; 182 } 183 184 // Return the just the path to the file name into dirname 185 char *System::extract_file_path(const char *fullpath, char *dirname) 186 { 187 char *p; 188 char buff[2048]; 189 if (fullpath == NULL) 190 { 191 dirname[0] = '\0'; 192 return dirname; 193 } 194 strcpy(buff, fullpath); 195 for(p = buff + strlen(buff); p >= buff; p--) 196 { 197 if (*p == '\\' || *p == '/') 198 { 199 strncpy(dirname, buff, p+1 - buff); 200 dirname[p+1 - buff] = '\0'; 201 tony 1.15 return dirname; 202 } 203 } 204 strcpy(dirname, fullpath); 205 return dirname; 206 } 207 | ||
208 marek 1.24 String System::getHostIP(const String &hostName) 209 { | ||
210 kumpf 1.36 struct hostent* hostEntry; 211 struct in_addr inaddr; 212 String ipAddress; 213 CString hostNameCString = hostName.getCString(); 214 const char* hostNamePtr = hostNameCString; 215 216 #if defined(PEGASUS_OS_LINUX) 217 char hostEntryBuffer[8192]; 218 struct hostent hostEntryStruct; 219 int hostEntryErrno; 220 221 gethostbyname_r( 222 hostNamePtr, 223 &hostEntryStruct, 224 hostEntryBuffer, 225 sizeof(hostEntryBuffer), 226 &hostEntry, 227 &hostEntryErrno); 228 #elif defined(PEGASUS_OS_SOLARIS) 229 char hostEntryBuffer[8192]; 230 struct hostent hostEntryStruct; 231 kumpf 1.36 int hostEntryErrno; 232 233 hostEntry = gethostbyname_r( 234 (char *)hostNamePtr, 235 &hostEntryStruct, 236 hostEntryBuffer, 237 sizeof(hostEntryBuffer), 238 &hostEntryErrno); 239 #elif defined(PEGASUS_OS_OS400) | ||
240 chuck 1.28 char ebcdicHost[PEGASUS_MAXHOSTNAMELEN]; | ||
241 kumpf 1.36 if (strlen(hostNamePtr) < PEGASUS_MAXHOSTNAMELEN) 242 strcpy(ebcdicHost, hostNamePtr); | ||
243 chuck 1.28 else 244 return ipAddress; 245 AtoE(ebcdicHost); | ||
246 kumpf 1.36 hostEntry = gethostbyname(ebcdicHost); 247 #else 248 hostEntry = gethostbyname(hostNamePtr); | ||
249 humberto 1.27 #endif | ||
250 kumpf 1.36 251 if (hostEntry) | ||
252 marek 1.24 { | ||
253 kumpf 1.36 ::memcpy( &inaddr, hostEntry->h_addr,4); | ||
254 marek 1.34 #if defined(PEGASUS_OS_OS400) | ||
255 david 1.33 char * gottenIPAdress = NULL; 256 gottenIPAdress = ::inet_ntoa( inaddr ); 257 258 if (gottenIPAdress != NULL) 259 { 260 EtoA(gottenIPAdress); 261 ipAddress.assign(gottenIPAdress); 262 } | ||
263 marek 1.24 #else 264 ipAddress = ::inet_ntoa( inaddr ); 265 #endif 266 } 267 return ipAddress; 268 } | ||
269 r.kieninger 1.21 270 // ------------------------------------------------------------------------ 271 // Convert a hostname into a a single host unique integer representation 272 // ------------------------------------------------------------------------ 273 Uint32 System::_acquireIP(const char* hostname) 274 { 275 Uint32 ip = 0xFFFFFFFF; 276 if (!hostname) return 0xFFFFFFFF; 277 278 #ifdef PEGASUS_OS_OS400 279 char ebcdicHost[PEGASUS_MAXHOSTNAMELEN]; 280 if (strlen(hostname) < PEGASUS_MAXHOSTNAMELEN) 281 strcpy(ebcdicHost, hostname); 282 else 283 return 0xFFFFFFFF; 284 AtoE(ebcdicHost); 285 #endif 286 | ||
287 dave.sudlik 1.22 //////////////////////////////////////////////////////////////////////////////// 288 // This code used to check if the first character of "hostname" was alphabetic 289 // to indicate hostname instead of IP address. But RFC 1123, section 2.1, relaxed 290 // this requirement to alphabetic character *or* digit. So bug 1462 changed the 291 // flow here to call inet_addr first to check for a valid IP address in dotted 292 // decimal notation. If it's not a valid IP address, then try to validate 293 // it as a hostname. 294 // RFC 1123 states: The host SHOULD check the string syntactically for a 295 // dotted-decimal number before looking it up in the Domain Name System. 296 // Hence the call to inet_addr() first. 297 //////////////////////////////////////////////////////////////////////////////// 298 299 #ifdef PEGASUS_OS_OS400 300 Uint32 tmp_addr = inet_addr(ebcdicHost); 301 #else 302 Uint32 tmp_addr = inet_addr((char *) hostname); 303 #endif 304 | ||
305 kumpf 1.36 struct hostent* hostEntry; | ||
306 r.kieninger 1.21 | ||
307 dave.sudlik 1.22 // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255). 308 // A better solution would be to use inet_aton() or equivalent, as 309 // inet_addr() is now considered "obsolete". 310 311 if (tmp_addr == 0xFFFFFFFF) // if hostname is not an IP address | ||
312 kumpf 1.36 { 313 #if defined(PEGASUS_OS_LINUX) 314 char hostEntryBuffer[8192]; 315 struct hostent hostEntryStruct; 316 int hostEntryErrno; 317 318 gethostbyname_r( 319 hostname, 320 &hostEntryStruct, 321 hostEntryBuffer, 322 sizeof(hostEntryBuffer), 323 &hostEntry, 324 &hostEntryErrno); 325 #elif defined(PEGASUS_OS_SOLARIS) 326 char hostEntryBuffer[8192]; 327 struct hostent hostEntryStruct; 328 int hostEntryErrno; 329 330 hostEntry = gethostbyname_r( 331 (char *)hostname, 332 &hostEntryStruct, 333 kumpf 1.36 hostEntryBuffer, 334 sizeof(hostEntryBuffer), 335 &hostEntryErrno); | ||
336 r.kieninger 1.21 #elif defined(PEGASUS_OS_OS400) | ||
337 kumpf 1.36 hostEntry = gethostbyname(ebcdicHost); | ||
338 r.kieninger 1.21 #elif defined(PEGASUS_OS_ZOS) | ||
339 kumpf 1.36 char hostName[PEGASUS_MAXHOSTNAMELEN + 1]; 340 if (String::equalNoCase("localhost",String(hostname))) 341 { 342 gethostname( hostName, PEGASUS_MAXHOSTNAMELEN ); 343 hostName[sizeof(hostName)-1] = 0; 344 hostEntry = gethostbyname(hostName); 345 } else 346 { 347 hostEntry = gethostbyname((char *)hostname); 348 } | ||
349 r.kieninger 1.21 #else | ||
350 kumpf 1.36 hostEntry = gethostbyname((char *)hostname); | ||
351 r.kieninger 1.21 #endif | ||
352 kumpf 1.36 if (!hostEntry) | ||
353 r.kieninger 1.21 { 354 return 0xFFFFFFFF; 355 } 356 unsigned char ip_part1,ip_part2,ip_part3,ip_part4; 357 | ||
358 kumpf 1.36 ip_part1 = hostEntry->h_addr[0]; 359 ip_part2 = hostEntry->h_addr[1]; 360 ip_part3 = hostEntry->h_addr[2]; 361 ip_part4 = hostEntry->h_addr[3]; | ||
362 r.kieninger 1.21 ip = ip_part1; 363 ip = (ip << 8) + ip_part2; 364 ip = (ip << 8) + ip_part3; 365 ip = (ip << 8) + ip_part4; | ||
366 dave.sudlik 1.22 } 367 else // else hostname *is* a dotted-decimal IP address | ||
368 r.kieninger 1.21 { 369 // resolve hostaddr to a real host entry 370 // casting to (const char *) as (char *) will work as (void *) too, those it fits all platforms | ||
371 humberto 1.27 #ifndef PEGASUS_OS_OS400 | ||
372 kumpf 1.36 hostEntry = gethostbyaddr((const char *) &tmp_addr, sizeof(tmp_addr), AF_INET); | ||
373 humberto 1.27 #else | ||
374 kumpf 1.36 hostEntry = gethostbyaddr((char *) &tmp_addr, sizeof(tmp_addr), AF_INET); | ||
375 humberto 1.27 #endif | ||
376 kumpf 1.36 if (hostEntry == 0) | ||
377 r.kieninger 1.21 { 378 // error, couldn't resolve the ip 379 return 0xFFFFFFFF; 380 } else 381 { 382 383 unsigned char ip_part1,ip_part2,ip_part3,ip_part4; 384 | ||
385 kumpf 1.36 ip_part1 = hostEntry->h_addr[0]; 386 ip_part2 = hostEntry->h_addr[1]; 387 ip_part3 = hostEntry->h_addr[2]; 388 ip_part4 = hostEntry->h_addr[3]; | ||
389 r.kieninger 1.21 ip = ip_part1; 390 ip = (ip << 8) + ip_part2; 391 ip = (ip << 8) + ip_part3; 392 ip = (ip << 8) + ip_part4; 393 } 394 } 395 396 return ip; 397 } 398 | ||
399 carolann.graves 1.29 Boolean System::sameHost (const String & hostName) 400 { 401 // 402 // If a port is included, return false 403 // 404 if (hostName.find (":") != PEG_NOT_FOUND) 405 { 406 return false; 407 } 408 409 // 410 // Retrieve IP addresses for both hostnames 411 // 412 Uint32 hostNameIP, systemHostIP = 0xFFFFFFFF; 413 hostNameIP = System::_acquireIP ((const char *) hostName.getCString ()); 414 if (hostNameIP == 0x7F000001) 415 { 416 // 417 // localhost or IP address of 127.0.0.1 418 // real IP address needed for compare 419 // 420 carolann.graves 1.29 hostNameIP = System::_acquireIP 421 ((const char *) System::getHostName ().getCString ()); 422 } 423 if (hostNameIP == 0xFFFFFFFF) 424 { 425 // 426 // Malformed IP address or not resolveable 427 // 428 return false; 429 } 430 431 systemHostIP = System::_acquireIP 432 ((const char *) System::getFullyQualifiedHostName ().getCString ()); 433 434 if (systemHostIP == 0x7F000001) 435 { 436 // 437 // localhost or IP address of 127.0.0.1 438 // real IP address needed for compare 439 // 440 systemHostIP = System::_acquireIP 441 carolann.graves 1.29 ((const char *) System::getHostName ().getCString ()); 442 } 443 if (systemHostIP == 0xFFFFFFFF) 444 { 445 // 446 // Malformed IP address or not resolveable 447 // 448 return false; 449 } 450 451 if (hostNameIP != systemHostIP) 452 { 453 return false; 454 } 455 456 return true; 457 } 458 | ||
459 tony 1.17 // System ID constants for Logger::put and Logger::trace 460 const String System::CIMLISTENER = "cimlistener"; // Listener systme ID 461 | ||
462 mike 1.8 PEGASUS_NAMESPACE_END |
No CVS admin address has been configured |
Powered by ViewCVS 0.9.2 |