(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            //%/////////////////////////////////////////////////////////////////////////////
 33 mike  1.8  
 34 sage  1.12 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 35 r.kieninger 1.21 #include <Pegasus/Common/Config.h>
 36                  #endif
 37 sage        1.12 
 38 mike        1.8  #include <fstream>
 39 kumpf       1.13 #include <cctype>  // for tolower()
 40 kumpf       1.14 #include <cstring>
 41 mike        1.6  #include "System.h"
 42 r.kieninger 1.21 #include "Socket.h"
 43 mike        1.39 #include "Network.h"
 44 kumpf       1.10 #include <Pegasus/Common/PegasusVersion.h>
 45                  
 46 mike        1.6  #if defined(PEGASUS_OS_TYPE_WINDOWS)
 47                  # include "SystemWindows.cpp"
 48 kumpf       1.41 #elif defined(PEGASUS_OS_TYPE_UNIX) || defined(PEGASUS_OS_VMS)
 49 mike        1.40 # include "SystemPOSIX.cpp"
 50 mike        1.6  #else
 51                  # error "Unsupported platform"
 52                  #endif
 53 mike        1.8  
 54 david       1.16 #if defined(PEGASUS_OS_OS400)
 55 mike        1.40 # include "EBCDIC_OS400.h"
 56 david       1.16 #endif
 57                  
 58 mike        1.8  PEGASUS_USING_STD;
 59                  
 60                  PEGASUS_NAMESPACE_BEGIN
 61                  
 62 kumpf       1.20 Boolean System::bindVerbose = false;
 63                  
 64 mike        1.8  Boolean System::copyFile(const char* fromPath, const char* toPath)
 65                  {
 66 ramnath     1.9      ifstream is(fromPath PEGASUS_IOS_BINARY);
 67                      ofstream os(toPath PEGASUS_IOS_BINARY);
 68 mike        1.8  
 69                      char c;
 70                  
 71                      while (is.get(c))
 72                      {
 73 kumpf       1.44         if (!os.put(c))
 74                              return false;
 75 mike        1.8      }
 76                  
 77 kumpf       1.42     return is.eof();
 78 kumpf       1.13 }
 79                  
 80 kumpf       1.44 static const Uint8 _toLowerTable[256] =
 81 mike        1.32 {
 82                      0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
 83                      0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
 84                      0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
 85                      0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
 86                      0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
 87                      0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
 88                      0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
 89                      0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
 90                      0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
 91                      0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
 92                      0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
 93                      0x78,0x79,0x7A,0x5B,0x5C,0x5D,0x5E,0x5F,
 94                      0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
 95                      0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
 96                      0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
 97                      0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
 98                      0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
 99                      0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
100                      0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
101                      0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
102 mike        1.32     0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
103                      0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
104                      0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,
105                      0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
106                      0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,
107                      0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
108                      0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,
109                      0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
110                      0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,
111                      0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
112                      0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
113                      0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,
114                  };
115                  
116 kumpf       1.13 Sint32 System::strcasecmp(const char* s1, const char* s2)
117                  {
118 mike        1.32     // Note: this is faster than glibc strcasecmp().
119                  
120                      Uint8* p = (Uint8*)s1;
121                      Uint8* q = (Uint8*)s2;
122                      int r;
123                  
124                      for (;;)
125 kumpf       1.13     {
126 kumpf       1.44         if ((r = _toLowerTable[p[0]] - _toLowerTable[q[0]]) || !p[0] ||
127                              (r = _toLowerTable[p[1]] - _toLowerTable[q[1]]) || !p[1] ||
128                              (r = _toLowerTable[p[2]] - _toLowerTable[q[2]]) || !p[2] ||
129                              (r = _toLowerTable[p[3]] - _toLowerTable[q[3]]) || !p[3])
130                              break;
131 kumpf       1.13 
132 kumpf       1.44         p += 4;
133                          q += 4;
134 kumpf       1.13     }
135                  
136 mike        1.32     return r;
137 mike        1.8  }
138                  
139 tony        1.15 // Return the just the file name from the path into basename
140                  char *System::extract_file_name(const char *fullpath, char *basename)
141                  {
142 kumpf       1.44     if (fullpath == NULL)
143                      {
144                          basename[0] = '\0';
145                          return basename;
146                      }
147 kumpf       1.31 
148 kumpf       1.44     for (const char* p = fullpath + strlen(fullpath) - 1; p >= fullpath; p--)
149                      {
150                          if (*p == '\\' || *p == '/')
151                          {
152                              strcpy(basename, p+1);
153                              return basename;
154                          }
155                      }
156                  
157                      strcpy(basename, fullpath);
158                      return basename;
159 tony        1.15 }
160                  
161                  // Return the just the path to the file name into dirname
162                  char *System::extract_file_path(const char *fullpath, char *dirname)
163                  {
164 kumpf       1.44     char *p;
165                      char buff[4096];
166                      if (fullpath == NULL)
167                      {
168                          dirname[0] = '\0';
169                          return dirname;
170                      }
171                      strncpy(buff, fullpath, sizeof(buff)-1);
172                      buff[sizeof(buff)-1] =  '\0';
173                      for (p = buff + strlen(buff); p >= buff; p--)
174 tony        1.15     {
175 kumpf       1.44         if (*p == '\\' || *p == '/')
176 tony        1.15         {
177                            strncpy(dirname, buff, p+1 - buff);
178                            dirname[p+1 - buff] = '\0';
179                            return dirname;
180                          }
181                      }
182 kumpf       1.44     strcpy(dirname, fullpath);
183                      return dirname;
184 tony        1.15 }
185                  
186 marek       1.24 String System::getHostIP(const String &hostName)
187                  {
188 kumpf       1.36     struct hostent* hostEntry;
189                      struct in_addr inaddr;
190                      String ipAddress;
191                      CString hostNameCString = hostName.getCString();
192                      const char* hostNamePtr = hostNameCString;
193                  
194                  #if defined(PEGASUS_OS_LINUX)
195                      char hostEntryBuffer[8192];
196                      struct hostent hostEntryStruct;
197                      int hostEntryErrno;
198                  
199                      gethostbyname_r(
200                          hostNamePtr,
201                          &hostEntryStruct,
202                          hostEntryBuffer,
203                          sizeof(hostEntryBuffer),
204                          &hostEntry,
205                          &hostEntryErrno);
206                  #elif defined(PEGASUS_OS_SOLARIS)
207                      char hostEntryBuffer[8192];
208                      struct hostent hostEntryStruct;
209 kumpf       1.36     int hostEntryErrno;
210                  
211                      hostEntry = gethostbyname_r(
212                          (char *)hostNamePtr,
213                          &hostEntryStruct,
214                          hostEntryBuffer,
215                          sizeof(hostEntryBuffer),
216                          &hostEntryErrno);
217                  #elif defined(PEGASUS_OS_OS400)
218 chuck       1.28     char ebcdicHost[PEGASUS_MAXHOSTNAMELEN];
219 kumpf       1.36     if (strlen(hostNamePtr) < PEGASUS_MAXHOSTNAMELEN)
220                          strcpy(ebcdicHost, hostNamePtr);
221 chuck       1.28     else
222                          return ipAddress;
223                      AtoE(ebcdicHost);
224 kumpf       1.36     hostEntry = gethostbyname(ebcdicHost);
225                  #else
226                      hostEntry = gethostbyname(hostNamePtr);
227 humberto    1.27 #endif
228 kumpf       1.36 
229                      if (hostEntry)
230 marek       1.24     {
231 kumpf       1.36         ::memcpy( &inaddr, hostEntry->h_addr,4);
232 marek       1.34 #if defined(PEGASUS_OS_OS400)
233 david       1.33         char * gottenIPAdress = NULL;
234                          gottenIPAdress = ::inet_ntoa( inaddr );
235 kumpf       1.44 
236 david       1.33         if (gottenIPAdress != NULL)
237                          {
238 kumpf       1.44             EtoA(gottenIPAdress);
239 david       1.33             ipAddress.assign(gottenIPAdress);
240                          }
241 marek       1.24 #else
242                          ipAddress = ::inet_ntoa( inaddr );
243                  #endif
244                      }
245                      return ipAddress;
246                  }
247 r.kieninger 1.21 
248                  // ------------------------------------------------------------------------
249                  // Convert a hostname into a a single host unique integer representation
250                  // ------------------------------------------------------------------------
251                  Uint32 System::_acquireIP(const char* hostname)
252                  {
253 kumpf       1.44     Uint32 ip = 0xFFFFFFFF;
254                      if (!hostname) return 0xFFFFFFFF;
255 r.kieninger 1.21 
256                  #ifdef PEGASUS_OS_OS400
257 kumpf       1.44     char ebcdicHost[PEGASUS_MAXHOSTNAMELEN];
258                      if (strlen(hostname) < PEGASUS_MAXHOSTNAMELEN)
259                          strcpy(ebcdicHost, hostname);
260                      else
261                          return 0xFFFFFFFF;
262                      AtoE(ebcdicHost);
263 r.kieninger 1.21 #endif
264                  
265 dave.sudlik 1.22 ////////////////////////////////////////////////////////////////////////////////
266                  // This code used to check if the first character of "hostname" was alphabetic
267 kumpf       1.44 // to indicate hostname instead of IP address. But RFC 1123, section 2.1,
268                  // relaxed this requirement to alphabetic character *or* digit. So bug 1462
269                  // changed the flow here to call inet_addr first to check for a valid IP
270                  // address in dotted decimal notation. If it's not a valid IP address, then
271                  // try to validate it as a hostname.
272                  // RFC 1123 states: The host SHOULD check the string syntactically for a
273                  // dotted-decimal number before looking it up in the Domain Name System.
274 dave.sudlik 1.22 // Hence the call to inet_addr() first.
275                  ////////////////////////////////////////////////////////////////////////////////
276                  
277                  #ifdef PEGASUS_OS_OS400
278 kumpf       1.44     Uint32 tmp_addr = inet_addr(ebcdicHost);
279 dave.sudlik 1.22 #else
280 kumpf       1.44     Uint32 tmp_addr = inet_addr((char *) hostname);
281 dave.sudlik 1.22 #endif
282                  
283 kumpf       1.36     struct hostent* hostEntry;
284 r.kieninger 1.21 
285 dave.sudlik 1.22 // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255).
286                  //       A better solution would be to use inet_aton() or equivalent, as
287                  //       inet_addr() is now considered "obsolete".
288                  
289                      if (tmp_addr == 0xFFFFFFFF)  // if hostname is not an IP address
290 kumpf       1.36     {
291                  #if defined(PEGASUS_OS_LINUX)
292                          char hostEntryBuffer[8192];
293                          struct hostent hostEntryStruct;
294                          int hostEntryErrno;
295                  
296                          gethostbyname_r(
297                              hostname,
298                              &hostEntryStruct,
299                              hostEntryBuffer,
300                              sizeof(hostEntryBuffer),
301                              &hostEntry,
302                              &hostEntryErrno);
303                  #elif defined(PEGASUS_OS_SOLARIS)
304                          char hostEntryBuffer[8192];
305                          struct hostent hostEntryStruct;
306                          int hostEntryErrno;
307                  
308                          hostEntry = gethostbyname_r(
309                              (char *)hostname,
310                              &hostEntryStruct,
311 kumpf       1.36             hostEntryBuffer,
312                              sizeof(hostEntryBuffer),
313                              &hostEntryErrno);
314 r.kieninger 1.21 #elif defined(PEGASUS_OS_OS400)
315 kumpf       1.36         hostEntry = gethostbyname(ebcdicHost);
316 r.kieninger 1.21 #elif defined(PEGASUS_OS_ZOS)
317 kumpf       1.36         char hostName[PEGASUS_MAXHOSTNAMELEN + 1];
318                          if (String::equalNoCase("localhost",String(hostname)))
319                          {
320                              gethostname( hostName, PEGASUS_MAXHOSTNAMELEN );
321                              hostName[sizeof(hostName)-1] = 0;
322                              hostEntry = gethostbyname(hostName);
323 kumpf       1.44         }
324                          else
325 kumpf       1.36         {
326                              hostEntry = gethostbyname((char *)hostname);
327                          }
328 r.kieninger 1.21 #else
329 kumpf       1.36         hostEntry = gethostbyname((char *)hostname);
330 r.kieninger 1.21 #endif
331 kumpf       1.36         if (!hostEntry)
332 kumpf       1.44         {
333                              return 0xFFFFFFFF;
334                          }
335                          unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
336                  
337                          ip_part1 = hostEntry->h_addr[0];
338                          ip_part2 = hostEntry->h_addr[1];
339                          ip_part3 = hostEntry->h_addr[2];
340                          ip_part4 = hostEntry->h_addr[3];
341                          ip = ip_part1;
342                          ip = (ip << 8) + ip_part2;
343                          ip = (ip << 8) + ip_part3;
344                          ip = (ip << 8) + ip_part4;
345                      }
346 dave.sudlik 1.22     else    // else hostname *is* a dotted-decimal IP address
347 kumpf       1.44     {
348                          // resolve hostaddr to a real host entry
349                          // casting to (const char *) as (char *) will work as (void *) too,
350                          // those it fits all platforms
351 ms.aruran   1.45 #if defined(PEGASUS_OS_LINUX)
352                          char hostEntryBuffer[8192];
353                          struct hostent hostEntryStruct;
354                          int hostEntryErrno;
355                  
356                          gethostbyaddr_r(
357                              (const char*) &tmp_addr,
358                              sizeof(tmp_addr),
359                              AF_INET,
360                              &hostEntryStruct,
361                              hostEntryBuffer,
362                              sizeof(hostEntryBuffer),
363                              &hostEntry,
364                              &hostEntryErrno);
365                  #elif defined(PEGASUS_OS_SOLARIS)
366                          char hostEntryBuffer[8192];
367                          struct hostent hostEntryStruct;
368                          int hostEntryErrno;
369                  
370                          hostEntry = gethostbyaddr_r(
371                              (const char *) &tmp_addr,
372 ms.aruran   1.45             sizeof(tmp_addr),
373                              AF_INET,
374                              &hostEntryStruct,
375                              hostEntryBuffer,
376                              sizeof(hostEntryBuffer),
377                              &hostEntryErrno);
378                  #elif defined(PEGASUS_OS_OS400)
379 kumpf       1.44         hostEntry =
380 ms.aruran   1.45             gethostbyaddr((char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
381 humberto    1.27 #else
382 kumpf       1.44         hostEntry =
383 ms.aruran   1.45             gethostbyaddr((const char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
384 humberto    1.27 #endif
385 kumpf       1.44         if (hostEntry == 0)
386                          {
387                              // error, couldn't resolve the ip
388                              return 0xFFFFFFFF;
389                          }
390                          else
391                          {
392                              unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
393 r.kieninger 1.21 
394 kumpf       1.44             ip_part1 = hostEntry->h_addr[0];
395                              ip_part2 = hostEntry->h_addr[1];
396                              ip_part3 = hostEntry->h_addr[2];
397                              ip_part4 = hostEntry->h_addr[3];
398                              ip = ip_part1;
399                              ip = (ip << 8) + ip_part2;
400                              ip = (ip << 8) + ip_part3;
401                              ip = (ip << 8) + ip_part4;
402                          }
403                      }
404                  
405                      return ip;
406 r.kieninger 1.21 }
407                  
408 carolann.graves 1.29 Boolean System::sameHost (const String & hostName)
409                      {
410                          //
411                          //  If a port is included, return false
412                          //
413                          if (hostName.find (":") != PEG_NOT_FOUND)
414                          {
415                              return false;
416                          }
417                      
418                          //
419                          //  Retrieve IP addresses for both hostnames
420                          //
421                          Uint32 hostNameIP, systemHostIP = 0xFFFFFFFF;
422                          hostNameIP = System::_acquireIP ((const char *) hostName.getCString ());
423                          if (hostNameIP == 0x7F000001)
424                          {
425                              //
426                              //  localhost or IP address of 127.0.0.1
427                              //  real IP address needed for compare
428                              //
429 carolann.graves 1.29         hostNameIP = System::_acquireIP
430                                  ((const char *) System::getHostName ().getCString ());
431                          }
432                          if (hostNameIP == 0xFFFFFFFF)
433                          {
434                              //
435                              //  Malformed IP address or not resolveable
436                              //
437                              return false;
438                          }
439                      
440                          systemHostIP = System::_acquireIP
441                              ((const char *) System::getFullyQualifiedHostName ().getCString ());
442                      
443                          if (systemHostIP == 0x7F000001)
444                          {
445                              //
446                              //  localhost or IP address of 127.0.0.1
447                              //  real IP address needed for compare
448                              //
449                              systemHostIP = System::_acquireIP
450 carolann.graves 1.29             ((const char *) System::getHostName ().getCString ());
451                          }
452                          if (systemHostIP == 0xFFFFFFFF)
453                          {
454                              //
455                              //  Malformed IP address or not resolveable
456                              //
457                              return false;
458                          }
459                      
460                          if (hostNameIP != systemHostIP)
461                          {
462                              return false;
463                          }
464                      
465                          return true;
466                      }
467                      
468 kumpf           1.44 Boolean System::resolveHostNameAtDNS(
469                          const char* hostname,
470                          Uint32* resolvedNameIP)
471 marek           1.43 {
472                          // ask the DNS for hostname resolution to IP address
473                          // this can mean a time delay for as long as the DNS
474 kumpf           1.44     // takes to answer
475 marek           1.43     struct hostent* hostEntry;
476                      
477                      #if defined(PEGASUS_OS_LINUX)
478 kumpf           1.44     char hostEntryBuffer[8192];
479                          struct hostent hostEntryStruct;
480                          int hostEntryErrno;
481 marek           1.43 
482 kumpf           1.44     gethostbyname_r(
483                              hostname,
484                              &hostEntryStruct,
485                              hostEntryBuffer,
486                              sizeof(hostEntryBuffer),
487                              &hostEntry,
488                              &hostEntryErrno);
489 marek           1.43 #elif defined(PEGASUS_OS_SOLARIS)
490 kumpf           1.44     char hostEntryBuffer[8192];
491                          struct hostent hostEntryStruct;
492                          int hostEntryErrno;
493 marek           1.43 
494 kumpf           1.44     hostEntry = gethostbyname_r(
495                              (char *)hostname,
496                              &hostEntryStruct,
497                              hostEntryBuffer,
498                              sizeof(hostEntryBuffer),
499                              &hostEntryErrno);
500 marek           1.43 #else
501 kumpf           1.44     hostEntry = gethostbyname((char *)hostname);
502 marek           1.43 #endif
503                          if (hostEntry == 0)
504                          {
505                              // error, couldn't resolve the hostname to an ip address
506                              return false;
507 kumpf           1.44     }
508                          else
509 marek           1.43     {
510                              unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
511                              ip_part1 = hostEntry->h_addr[0];
512                              ip_part2 = hostEntry->h_addr[1];
513                              ip_part3 = hostEntry->h_addr[2];
514                              ip_part4 = hostEntry->h_addr[3];
515                              *resolvedNameIP = ip_part1;
516                              *resolvedNameIP = (*resolvedNameIP << 8) + ip_part2;
517                              *resolvedNameIP = (*resolvedNameIP << 8) + ip_part3;
518                              *resolvedNameIP = (*resolvedNameIP << 8) + ip_part4;
519                          }
520                          return true;
521                      }
522                      
523                      Boolean System::resolveIPAtDNS(Uint32 ip_addr, Uint32 * resolvedIP)
524                      {
525 kumpf           1.44     struct hostent *entry;
526 marek           1.43 
527                      #ifndef PEGASUS_OS_OS400
528 kumpf           1.44     entry = gethostbyaddr((const char *) &ip_addr, sizeof(ip_addr), AF_INET);
529 marek           1.43 #else
530 kumpf           1.44     entry = gethostbyaddr((char *) &ip_addr, sizeof(ip_addr), AF_INET);
531 marek           1.43 #endif
532 kumpf           1.44     if (entry == 0)
533                          {
534                              // error, couldn't resolve the ip
535                              return false;
536                          }
537                          else
538                          {
539                              unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
540                              ip_part1 = entry->h_addr[0];
541                              ip_part2 = entry->h_addr[1];
542                              ip_part3 = entry->h_addr[2];
543                              ip_part4 = entry->h_addr[3];
544                              *resolvedIP = ip_part1;
545                              *resolvedIP = (*resolvedIP << 8) + ip_part2;
546                              *resolvedIP = (*resolvedIP << 8) + ip_part3;
547                              *resolvedIP = (*resolvedIP << 8) + ip_part4;
548                          }
549                          return true;
550 marek           1.43 }
551                      
552                      
553                      Boolean System::isLocalHost(const String &hostName)
554                      {
555                          // differentiate between a dotted IP address given
556                          // and a real hostname given
557                          CString csName = hostName.getCString();
558 marek           1.46     char cc_hostname[PEGASUS_MAXHOSTNAMELEN];
559                          strcpy(cc_hostname, (const char*) csName);
560 marek           1.43     Uint32 tmp_addr = 0xFFFFFFFF;
561                          Boolean hostNameIsIPNotation;
562                      
563                      #ifdef PEGASUS_OS_OS400
564                          AtoE(cc_hostname);
565                      #endif
566                      
567                          // Note: Platforms already supporting the inet_aton()
568                          //       should define their platform here,
569                          //        as this is the superior way to work
570                      #if defined(PEGASUS_OS_LINUX) || defined(PEGASUS_OS_AIX)
571 kumpf           1.44 
572 marek           1.43     struct in_addr inaddr;
573                          // if inet_aton failed(return=0),
574                          // we do not have a valip IP address (x.x.x.x)
575                          int atonSuccess = inet_aton(cc_hostname, &inaddr);
576                          if (atonSuccess == 0) hostNameIsIPNotation = false;
577                          else
578                          {
579                              hostNameIsIPNotation = true;
580                              tmp_addr = inaddr.s_addr;
581                          }
582                      #else
583                          // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255).
584                          //       A better solution would be to use inet_aton() or equivalent, as
585                          //       inet_addr() is now considered "obsolete".
586                          // Note: inet_aton() not yet supported on all Pegasus platforms
587 kumpf           1.44     tmp_addr = inet_addr((char *) cc_hostname);
588 marek           1.43     if (tmp_addr == 0xFFFFFFFF) hostNameIsIPNotation = false;
589                          else hostNameIsIPNotation = true;
590                      #endif
591 kumpf           1.44 
592 marek           1.43     if (!hostNameIsIPNotation)  // if hostname is not an IP address
593 kumpf           1.44     {
594 marek           1.43         // localhost ?
595                              if (String::equalNoCase(hostName,String("localhost"))) return true;
596 marek           1.46         char localHostName[PEGASUS_MAXHOSTNAMELEN];
597 marek           1.47         CString cstringLocalHostName = System::getHostName().getCString();
598 marek           1.46         strcpy(localHostName, (const char*) cstringLocalHostName); 
599 marek           1.43         // given hostname equals what system returns as local hostname ?
600                              if (String::equalNoCase(hostName,localHostName)) return true;
601                              Uint32 hostIP;
602                              // bail out if hostname unresolveable
603                              if (!System::resolveHostNameAtDNS(cc_hostname, &hostIP)) return false;
604                              // lets see if the IP is defined on one of the network interfaces
605                              // this can help us avoid another call to DNS
606                              if (System::isIpOnNetworkInterface(hostIP)) return true;
607                              // need to check if the local hosts name is possibly
608                              // registered at the DNS with the IP address equal resolvedNameIP
609                              Uint32 localHostIP;
610 kumpf           1.44         if (!System::resolveHostNameAtDNS(localHostName, &localHostIP))
611                                  return false;
612 marek           1.43         if (localHostIP == hostIP) return true;
613 kumpf           1.44     }
614                          else
615 marek           1.43     {   // hostname is an IP address
616                              // 127.0.0.1 is always the loopback
617                              // inet_addr returns network byte order
618                              if (tmp_addr == htonl(0x7F000001)) return true;
619                              // IP defined on a local AF_INET network interface
620                              if (System::isIpOnNetworkInterface(tmp_addr)) return true;
621                              // out of luck so far, lets ask the DNS what our IP is
622                              // and check against what we got
623                              Uint32 localHostIP;
624 kumpf           1.44         if (!System::resolveHostNameAtDNS(
625                                      (const char*) System::getHostName().getCString(), &localHostIP))
626                                  return false;
627 marek           1.43         if (localHostIP == tmp_addr) return true;
628                              // not yet, sometimes resolving the IP address we got against the DNS
629                              // can solve the problem
630 kumpf           1.44         // casting to (const char *) as (char *) will work as (void *) too,
631                              // those it fits all platforms
632 marek           1.43         Uint32 hostIP;
633                              if (!System::resolveIPAtDNS(tmp_addr, &hostIP)) return false;
634                              if (hostIP == localHostIP) return true;
635                          }
636                          return false;
637                      }
638                      
639 tony            1.17 // System ID constants for Logger::put and Logger::trace
640                      const String System::CIMLISTENER = "cimlistener"; // Listener systme ID
641                      
642 mike            1.8  PEGASUS_NAMESPACE_END
643 ms.aruran       1.45 

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2