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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2