(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                    
 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 kumpf         1.37.2.2     return is.eof();
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 w.otsuka      1.37.2.1   char buff[4096];
189 tony          1.15       if (fullpath == NULL)
190                            {
191                              dirname[0] = '\0';
192                              return dirname;
193                            }
194 w.otsuka      1.37.2.1   strncpy(buff, fullpath, sizeof(buff)-1);
195                          buff[sizeof(buff)-1] =  '\0';
196 tony          1.15       for(p = buff + strlen(buff); p >= buff; p--)
197                            {
198                              if (*p == '\\' || *p == '/')
199                                {
200                                  strncpy(dirname, buff, p+1 - buff);
201                                  dirname[p+1 - buff] = '\0';
202                                  return dirname;
203                                }
204                            }
205                          strcpy(dirname, fullpath);
206                          return dirname;
207                        }
208                        
209 marek         1.24     String System::getHostIP(const String &hostName)
210                        {
211 kumpf         1.36         struct hostent* hostEntry;
212                            struct in_addr inaddr;
213                            String ipAddress;
214                            CString hostNameCString = hostName.getCString();
215                            const char* hostNamePtr = hostNameCString;
216                        
217                        #if defined(PEGASUS_OS_LINUX)
218                            char hostEntryBuffer[8192];
219                            struct hostent hostEntryStruct;
220                            int hostEntryErrno;
221                        
222                            gethostbyname_r(
223                                hostNamePtr,
224                                &hostEntryStruct,
225                                hostEntryBuffer,
226                                sizeof(hostEntryBuffer),
227                                &hostEntry,
228                                &hostEntryErrno);
229                        #elif defined(PEGASUS_OS_SOLARIS)
230                            char hostEntryBuffer[8192];
231                            struct hostent hostEntryStruct;
232 kumpf         1.36         int hostEntryErrno;
233                        
234                            hostEntry = gethostbyname_r(
235                                (char *)hostNamePtr,
236                                &hostEntryStruct,
237                                hostEntryBuffer,
238                                sizeof(hostEntryBuffer),
239                                &hostEntryErrno);
240                        #elif defined(PEGASUS_OS_OS400)
241 chuck         1.28         char ebcdicHost[PEGASUS_MAXHOSTNAMELEN];
242 kumpf         1.36         if (strlen(hostNamePtr) < PEGASUS_MAXHOSTNAMELEN)
243                                strcpy(ebcdicHost, hostNamePtr);
244 chuck         1.28         else
245                                return ipAddress;
246                            AtoE(ebcdicHost);
247 kumpf         1.36         hostEntry = gethostbyname(ebcdicHost);
248                        #else
249                            hostEntry = gethostbyname(hostNamePtr);
250 humberto      1.27     #endif
251 kumpf         1.36     
252                            if (hostEntry)
253 marek         1.24         {
254 kumpf         1.36             ::memcpy( &inaddr, hostEntry->h_addr,4);
255 marek         1.34     #if defined(PEGASUS_OS_OS400)
256 david         1.33             char * gottenIPAdress = NULL;
257                                gottenIPAdress = ::inet_ntoa( inaddr );
258                                
259                                if (gottenIPAdress != NULL)
260                                {
261                        	    EtoA(gottenIPAdress);
262                                    ipAddress.assign(gottenIPAdress);
263                                }
264 marek         1.24     #else
265                                ipAddress = ::inet_ntoa( inaddr );
266                        #endif
267                            }
268                            return ipAddress;
269                        }
270 r.kieninger   1.21     
271                        // ------------------------------------------------------------------------
272                        // Convert a hostname into a a single host unique integer representation
273                        // ------------------------------------------------------------------------
274                        Uint32 System::_acquireIP(const char* hostname)
275                        {
276                        	Uint32 ip = 0xFFFFFFFF;
277                        	if (!hostname) return 0xFFFFFFFF;
278                        
279                        #ifdef PEGASUS_OS_OS400
280                        	char ebcdicHost[PEGASUS_MAXHOSTNAMELEN];
281                        	if (strlen(hostname) < PEGASUS_MAXHOSTNAMELEN)
282                        		strcpy(ebcdicHost, hostname);
283                        	else
284                        		return 0xFFFFFFFF;
285                        	AtoE(ebcdicHost);
286                        #endif
287                        
288 dave.sudlik   1.22     ////////////////////////////////////////////////////////////////////////////////
289                        // This code used to check if the first character of "hostname" was alphabetic
290                        // to indicate hostname instead of IP address. But RFC 1123, section 2.1, relaxed
291                        // this requirement to alphabetic character *or* digit. So bug 1462 changed the
292                        // flow here to call inet_addr first to check for a valid IP address in dotted
293                        // decimal notation. If it's not a valid IP address, then try to validate
294                        // it as a hostname.
295                        // RFC 1123 states: The host SHOULD check the string syntactically for a 
296                        // dotted-decimal number before looking it up in the Domain Name System. 
297                        // Hence the call to inet_addr() first.
298                        ////////////////////////////////////////////////////////////////////////////////
299                        
300                        #ifdef PEGASUS_OS_OS400
301                        	Uint32 tmp_addr = inet_addr(ebcdicHost);
302                        #else
303                        	Uint32 tmp_addr = inet_addr((char *) hostname);
304                        #endif
305                        
306 kumpf         1.36         struct hostent* hostEntry;
307 r.kieninger   1.21     
308 dave.sudlik   1.22     // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255).
309                        //       A better solution would be to use inet_aton() or equivalent, as
310                        //       inet_addr() is now considered "obsolete".
311                        
312                            if (tmp_addr == 0xFFFFFFFF)  // if hostname is not an IP address
313 kumpf         1.36         {
314                        #if defined(PEGASUS_OS_LINUX)
315                                char hostEntryBuffer[8192];
316                                struct hostent hostEntryStruct;
317                                int hostEntryErrno;
318                        
319                                gethostbyname_r(
320                                    hostname,
321                                    &hostEntryStruct,
322                                    hostEntryBuffer,
323                                    sizeof(hostEntryBuffer),
324                                    &hostEntry,
325                                    &hostEntryErrno);
326                        #elif defined(PEGASUS_OS_SOLARIS)
327                                char hostEntryBuffer[8192];
328                                struct hostent hostEntryStruct;
329                                int hostEntryErrno;
330                        
331                                hostEntry = gethostbyname_r(
332                                    (char *)hostname,
333                                    &hostEntryStruct,
334 kumpf         1.36                 hostEntryBuffer,
335                                    sizeof(hostEntryBuffer),
336                                    &hostEntryErrno);
337 r.kieninger   1.21     #elif defined(PEGASUS_OS_OS400)
338 kumpf         1.36             hostEntry = gethostbyname(ebcdicHost);
339 r.kieninger   1.21     #elif defined(PEGASUS_OS_ZOS)
340 kumpf         1.36             char hostName[PEGASUS_MAXHOSTNAMELEN + 1];
341                                if (String::equalNoCase("localhost",String(hostname)))
342                                {
343                                    gethostname( hostName, PEGASUS_MAXHOSTNAMELEN );
344                                    hostName[sizeof(hostName)-1] = 0;
345                                    hostEntry = gethostbyname(hostName);
346                                } else
347                                {
348                                    hostEntry = gethostbyname((char *)hostname);
349                                }
350 r.kieninger   1.21     #else
351 kumpf         1.36             hostEntry = gethostbyname((char *)hostname);
352 r.kieninger   1.21     #endif
353 kumpf         1.36             if (!hostEntry)
354 r.kieninger   1.21     		{
355                        			return 0xFFFFFFFF;
356                        		}
357                        		unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
358                        
359 kumpf         1.36     		ip_part1 = hostEntry->h_addr[0];
360                        		ip_part2 = hostEntry->h_addr[1];
361                        		ip_part3 = hostEntry->h_addr[2];
362                        		ip_part4 = hostEntry->h_addr[3];
363 r.kieninger   1.21     		ip = ip_part1;
364                        		ip = (ip << 8) + ip_part2;
365                        		ip = (ip << 8) + ip_part3;
366                        		ip = (ip << 8) + ip_part4;
367 dave.sudlik   1.22     	}
368                            else    // else hostname *is* a dotted-decimal IP address
369 r.kieninger   1.21     	{
370                        		// resolve hostaddr to a real host entry
371                        		// casting to (const char *) as (char *) will work as (void *) too, those it fits all platforms
372 humberto      1.27     #ifndef PEGASUS_OS_OS400
373 kumpf         1.36             hostEntry = gethostbyaddr((const char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
374 humberto      1.27     #else
375 kumpf         1.36     		hostEntry = gethostbyaddr((char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
376 humberto      1.27     #endif
377 kumpf         1.36     		if (hostEntry == 0)
378 r.kieninger   1.21     		{
379                        			// error, couldn't resolve the ip
380                        			return 0xFFFFFFFF;
381                        		} else
382                        		{
383                        
384                        			unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
385                        
386 kumpf         1.36     			ip_part1 = hostEntry->h_addr[0];
387                        			ip_part2 = hostEntry->h_addr[1];
388                        			ip_part3 = hostEntry->h_addr[2];
389                        			ip_part4 = hostEntry->h_addr[3];
390 r.kieninger   1.21     			ip = ip_part1;
391                        			ip = (ip << 8) + ip_part2;
392                        			ip = (ip << 8) + ip_part3;
393                        			ip = (ip << 8) + ip_part4;
394                        		}
395                        	}
396                        
397                        	return ip;
398                        }
399                        
400 carolann.graves 1.29     Boolean System::sameHost (const String & hostName)
401                          {
402                              //
403                              //  If a port is included, return false
404                              //
405                              if (hostName.find (":") != PEG_NOT_FOUND)
406                              {
407                                  return false;
408                              }
409                          
410                              //
411                              //  Retrieve IP addresses for both hostnames
412                              //
413                              Uint32 hostNameIP, systemHostIP = 0xFFFFFFFF;
414                              hostNameIP = System::_acquireIP ((const char *) hostName.getCString ());
415                              if (hostNameIP == 0x7F000001)
416                              {
417                                  //
418                                  //  localhost or IP address of 127.0.0.1
419                                  //  real IP address needed for compare
420                                  //
421 carolann.graves 1.29             hostNameIP = System::_acquireIP
422                                      ((const char *) System::getHostName ().getCString ());
423                              }
424                              if (hostNameIP == 0xFFFFFFFF)
425                              {
426                                  //
427                                  //  Malformed IP address or not resolveable
428                                  //
429                                  return false;
430                              }
431                          
432                              systemHostIP = System::_acquireIP
433                                  ((const char *) System::getFullyQualifiedHostName ().getCString ());
434                          
435                              if (systemHostIP == 0x7F000001)
436                              {
437                                  //
438                                  //  localhost or IP address of 127.0.0.1
439                                  //  real IP address needed for compare
440                                  //
441                                  systemHostIP = System::_acquireIP
442 carolann.graves 1.29                 ((const char *) System::getHostName ().getCString ());
443                              }
444                              if (systemHostIP == 0xFFFFFFFF)
445                              {
446                                  //
447                                  //  Malformed IP address or not resolveable
448                                  //
449                                  return false;
450                              }
451                          
452                              if (hostNameIP != systemHostIP)
453                              {
454                                  return false;
455                              }
456                          
457                              return true;
458                          }
459                          
460 tony            1.17     // System ID constants for Logger::put and Logger::trace
461                          const String System::CIMLISTENER = "cimlistener"; // Listener systme ID
462                          
463 mike            1.8      PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2