(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 mateus.baur 1.50 #include <Pegasus/Common/FileSystem.h>
 46 dave.sudlik 1.51.6.1 #include <Pegasus/Common/HostAddress.h>
 47                      #include <Pegasus/Common/Array.h>
 48 kumpf       1.10     
 49 mike        1.6      #if defined(PEGASUS_OS_TYPE_WINDOWS)
 50                      # include "SystemWindows.cpp"
 51 kumpf       1.41     #elif defined(PEGASUS_OS_TYPE_UNIX) || defined(PEGASUS_OS_VMS)
 52 mike        1.40     # include "SystemPOSIX.cpp"
 53 mike        1.6      #else
 54                      # error "Unsupported platform"
 55                      #endif
 56 mike        1.8      
 57                      PEGASUS_USING_STD;
 58                      
 59                      PEGASUS_NAMESPACE_BEGIN
 60                      
 61 kumpf       1.20     Boolean System::bindVerbose = false;
 62                      
 63 mike        1.8      Boolean System::copyFile(const char* fromPath, const char* toPath)
 64                      {
 65 ramnath     1.9          ifstream is(fromPath PEGASUS_IOS_BINARY);
 66 mateus.baur 1.50         fstream os(toPath, ios::out  PEGASUS_OR_IOS_BINARY);
 67 mike        1.8      
 68                          char c;
 69                      
 70                          while (is.get(c))
 71                          {
 72 kumpf       1.44             if (!os.put(c))
 73                                  return false;
 74 mike        1.8          }
 75                      
 76 mateus.baur 1.50         FileSystem::syncWithDirectoryUpdates(os);
 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 dave.sudlik 1.51.6.1 Boolean System::getHostIP(const String &hostName, int *af, String &hostIP)
187 marek       1.24     {
188 dave.sudlik 1.51.6.1 #ifdef PEGASUS_ENABLE_IPV6
189                          struct addrinfo *info, hints;
190                          memset (&hints, 0, sizeof(struct addrinfo));
191                      
192                          // Check for valid IPV4 address, if found return ipv4 address
193                          *af = AF_INET;
194                          hints.ai_family = *af;
195                          hints.ai_protocol = IPPROTO_TCP;   
196                          hints.ai_socktype = SOCK_STREAM;
197                          if (!getaddrinfo(hostName.getCString(), 0, &hints, &info))
198                          {
199                              char ipAddress[PEGASUS_INET_ADDRSTR_LEN];
200                              HostAddress::convertBinaryToText(info->ai_family, 
201                                  &((struct sockaddr_in*)(info->ai_addr))->sin_addr, ipAddress,
202                                  PEGASUS_INET_ADDRSTR_LEN);
203                              hostIP = ipAddress;
204                              freeaddrinfo(info);
205                              return true;
206                          }
207                      
208                          // Check for valid IPV6 Address. 
209 dave.sudlik 1.51.6.1     *af = AF_INET6;
210                          hints.ai_family = *af;
211                          hints.ai_protocol = IPPROTO_TCP;
212                          hints.ai_socktype = SOCK_STREAM;
213                          if (!getaddrinfo(hostName.getCString(), 0, &hints, &info))
214                          {
215                              char ipAddress[PEGASUS_INET6_ADDRSTR_LEN];
216                              HostAddress::convertBinaryToText(info->ai_family,
217                                  &((struct sockaddr_in6*)(info->ai_addr))->sin6_addr, ipAddress,
218                                  PEGASUS_INET6_ADDRSTR_LEN);
219                              hostIP = ipAddress;
220                              freeaddrinfo(info);
221                              return true;
222                          }
223                      
224                          return false;
225                      #else
226                          *af = AF_INET;
227 kumpf       1.36         struct hostent* hostEntry;
228                          struct in_addr inaddr;
229                          String ipAddress;
230                          CString hostNameCString = hostName.getCString();
231                          const char* hostNamePtr = hostNameCString;
232                      
233                      #if defined(PEGASUS_OS_LINUX)
234                          char hostEntryBuffer[8192];
235                          struct hostent hostEntryStruct;
236                          int hostEntryErrno;
237                      
238                          gethostbyname_r(
239                              hostNamePtr,
240                              &hostEntryStruct,
241                              hostEntryBuffer,
242                              sizeof(hostEntryBuffer),
243                              &hostEntry,
244                              &hostEntryErrno);
245                      #elif defined(PEGASUS_OS_SOLARIS)
246                          char hostEntryBuffer[8192];
247                          struct hostent hostEntryStruct;
248 kumpf       1.36         int hostEntryErrno;
249                      
250                          hostEntry = gethostbyname_r(
251                              (char *)hostNamePtr,
252                              &hostEntryStruct,
253                              hostEntryBuffer,
254                              sizeof(hostEntryBuffer),
255                              &hostEntryErrno);
256                      #else
257                          hostEntry = gethostbyname(hostNamePtr);
258 humberto    1.27     #endif
259 kumpf       1.36     
260                          if (hostEntry)
261 marek       1.24         {
262 kumpf       1.36             ::memcpy( &inaddr, hostEntry->h_addr,4);
263 marek       1.24             ipAddress = ::inet_ntoa( inaddr );
264                          }
265 dave.sudlik 1.51.6.1     hostIP = ipAddress;
266                          return true;
267                      #endif
268 marek       1.24     }
269 r.kieninger 1.21     
270                      // ------------------------------------------------------------------------
271                      // Convert a hostname into a a single host unique integer representation
272                      // ------------------------------------------------------------------------
273 dave.sudlik 1.51.6.1 Boolean System::_acquireIP(const char* hostname, int *af, void *dst)
274                      {
275                      #ifdef PEGASUS_ENABLE_IPV6
276                          String ipAddress;
277                          if(getHostIP(hostname, af, ipAddress))
278 r.kieninger 1.21     {
279 dave.sudlik 1.51.6.1         HostAddress::convertTextToBinary(*af, ipAddress.getCString(), dst);
280                              return true;
281                          }
282                          return false;
283                      #else
284                          *af = AF_INET;
285 kumpf       1.44         Uint32 ip = 0xFFFFFFFF;
286                          if (!hostname) return 0xFFFFFFFF;
287 r.kieninger 1.21     
288 dave.sudlik 1.22     ////////////////////////////////////////////////////////////////////////////////
289                      // This code used to check if the first character of "hostname" was alphabetic
290 kumpf       1.44     // to indicate hostname instead of IP address. But RFC 1123, section 2.1,
291                      // relaxed this requirement to alphabetic character *or* digit. So bug 1462
292                      // changed the flow here to call inet_addr first to check for a valid IP
293                      // address in dotted decimal notation. If it's not a valid IP address, then
294                      // try to validate 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 dave.sudlik 1.22     // Hence the call to inet_addr() first.
298                      ////////////////////////////////////////////////////////////////////////////////
299                      
300 kumpf       1.44         Uint32 tmp_addr = inet_addr((char *) hostname);
301 dave.sudlik 1.22     
302 kumpf       1.36         struct hostent* hostEntry;
303 r.kieninger 1.21     
304 dave.sudlik 1.22     // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255).
305                      //       A better solution would be to use inet_aton() or equivalent, as
306                      //       inet_addr() is now considered "obsolete".
307                      
308                          if (tmp_addr == 0xFFFFFFFF)  // if hostname is not an IP address
309 kumpf       1.36         {
310                      #if defined(PEGASUS_OS_LINUX)
311                              char hostEntryBuffer[8192];
312                              struct hostent hostEntryStruct;
313                              int hostEntryErrno;
314                      
315                              gethostbyname_r(
316                                  hostname,
317                                  &hostEntryStruct,
318                                  hostEntryBuffer,
319                                  sizeof(hostEntryBuffer),
320                                  &hostEntry,
321                                  &hostEntryErrno);
322                      #elif defined(PEGASUS_OS_SOLARIS)
323                              char hostEntryBuffer[8192];
324                              struct hostent hostEntryStruct;
325                              int hostEntryErrno;
326                      
327                              hostEntry = gethostbyname_r(
328                                  (char *)hostname,
329                                  &hostEntryStruct,
330 kumpf       1.36                 hostEntryBuffer,
331                                  sizeof(hostEntryBuffer),
332                                  &hostEntryErrno);
333 r.kieninger 1.21     #elif defined(PEGASUS_OS_ZOS)
334 kumpf       1.36             char hostName[PEGASUS_MAXHOSTNAMELEN + 1];
335                              if (String::equalNoCase("localhost",String(hostname)))
336                              {
337                                  gethostname( hostName, PEGASUS_MAXHOSTNAMELEN );
338                                  hostName[sizeof(hostName)-1] = 0;
339                                  hostEntry = gethostbyname(hostName);
340 kumpf       1.44             }
341                              else
342 kumpf       1.36             {
343                                  hostEntry = gethostbyname((char *)hostname);
344                              }
345 r.kieninger 1.21     #else
346 kumpf       1.36             hostEntry = gethostbyname((char *)hostname);
347 r.kieninger 1.21     #endif
348 kumpf       1.36             if (!hostEntry)
349 kumpf       1.44             {
350 dave.sudlik 1.51.6.1             // error, couldn't resolve the ip
351                                  memcpy(dst, &ip, sizeof (Uint32));
352                                  return false;
353 kumpf       1.44             }
354                              unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
355                      
356                              ip_part1 = hostEntry->h_addr[0];
357                              ip_part2 = hostEntry->h_addr[1];
358                              ip_part3 = hostEntry->h_addr[2];
359                              ip_part4 = hostEntry->h_addr[3];
360                              ip = ip_part1;
361                              ip = (ip << 8) + ip_part2;
362                              ip = (ip << 8) + ip_part3;
363                              ip = (ip << 8) + ip_part4;
364                          }
365 dave.sudlik 1.22         else    // else hostname *is* a dotted-decimal IP address
366 kumpf       1.44         {
367                              // resolve hostaddr to a real host entry
368                              // casting to (const char *) as (char *) will work as (void *) too,
369                              // those it fits all platforms
370 ms.aruran   1.45     #if defined(PEGASUS_OS_LINUX)
371                              char hostEntryBuffer[8192];
372                              struct hostent hostEntryStruct;
373                              int hostEntryErrno;
374                      
375                              gethostbyaddr_r(
376                                  (const char*) &tmp_addr,
377                                  sizeof(tmp_addr),
378                                  AF_INET,
379                                  &hostEntryStruct,
380                                  hostEntryBuffer,
381                                  sizeof(hostEntryBuffer),
382                                  &hostEntry,
383                                  &hostEntryErrno);
384                      #elif defined(PEGASUS_OS_SOLARIS)
385                              char hostEntryBuffer[8192];
386                              struct hostent hostEntryStruct;
387                              int hostEntryErrno;
388                      
389                              hostEntry = gethostbyaddr_r(
390                                  (const char *) &tmp_addr,
391 ms.aruran   1.45                 sizeof(tmp_addr),
392                                  AF_INET,
393                                  &hostEntryStruct,
394                                  hostEntryBuffer,
395                                  sizeof(hostEntryBuffer),
396                                  &hostEntryErrno);
397 humberto    1.27     #else
398 kumpf       1.44             hostEntry =
399 ms.aruran   1.45                 gethostbyaddr((const char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
400 humberto    1.27     #endif
401 kumpf       1.44             if (hostEntry == 0)
402                              {
403                                  // error, couldn't resolve the ip
404 dave.sudlik 1.51.6.1             memcpy(dst, &ip, sizeof (Uint32));
405                                  return false;
406 kumpf       1.44             }
407                              else
408                              {
409                                  unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
410 r.kieninger 1.21     
411 kumpf       1.44                 ip_part1 = hostEntry->h_addr[0];
412                                  ip_part2 = hostEntry->h_addr[1];
413                                  ip_part3 = hostEntry->h_addr[2];
414                                  ip_part4 = hostEntry->h_addr[3];
415                                  ip = ip_part1;
416                                  ip = (ip << 8) + ip_part2;
417                                  ip = (ip << 8) + ip_part3;
418                                  ip = (ip << 8) + ip_part4;
419                              }
420                          }
421 dave.sudlik 1.51.6.1     memcpy(dst, &ip, sizeof (Uint32));
422 carolann.graves 1.29     
423                              return true;
424 dave.sudlik     1.51.6.1 #endif
425 carolann.graves 1.29     }
426                          
427 kumpf           1.44     Boolean System::resolveHostNameAtDNS(
428                              const char* hostname,
429                              Uint32* resolvedNameIP)
430 marek           1.43     {
431                              // ask the DNS for hostname resolution to IP address
432                              // this can mean a time delay for as long as the DNS
433 kumpf           1.44         // takes to answer
434 marek           1.43         struct hostent* hostEntry;
435                          
436                          #if defined(PEGASUS_OS_LINUX)
437 kumpf           1.44         char hostEntryBuffer[8192];
438                              struct hostent hostEntryStruct;
439                              int hostEntryErrno;
440 marek           1.43     
441 kumpf           1.44         gethostbyname_r(
442                                  hostname,
443                                  &hostEntryStruct,
444                                  hostEntryBuffer,
445                                  sizeof(hostEntryBuffer),
446                                  &hostEntry,
447                                  &hostEntryErrno);
448 marek           1.43     #elif defined(PEGASUS_OS_SOLARIS)
449 kumpf           1.44         char hostEntryBuffer[8192];
450                              struct hostent hostEntryStruct;
451                              int hostEntryErrno;
452 marek           1.43     
453 kumpf           1.44         hostEntry = gethostbyname_r(
454                                  (char *)hostname,
455                                  &hostEntryStruct,
456                                  hostEntryBuffer,
457                                  sizeof(hostEntryBuffer),
458                                  &hostEntryErrno);
459 marek           1.43     #else
460 kumpf           1.44         hostEntry = gethostbyname((char *)hostname);
461 marek           1.43     #endif
462                              if (hostEntry == 0)
463                              {
464                                  // error, couldn't resolve the hostname to an ip address
465                                  return false;
466 kumpf           1.44         }
467                              else
468 marek           1.43         {
469                                  unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
470                                  ip_part1 = hostEntry->h_addr[0];
471                                  ip_part2 = hostEntry->h_addr[1];
472                                  ip_part3 = hostEntry->h_addr[2];
473                                  ip_part4 = hostEntry->h_addr[3];
474                                  *resolvedNameIP = ip_part1;
475                                  *resolvedNameIP = (*resolvedNameIP << 8) + ip_part2;
476                                  *resolvedNameIP = (*resolvedNameIP << 8) + ip_part3;
477                                  *resolvedNameIP = (*resolvedNameIP << 8) + ip_part4;
478                              }
479                              return true;
480                          }
481                          
482                          Boolean System::resolveIPAtDNS(Uint32 ip_addr, Uint32 * resolvedIP)
483                          {
484 kumpf           1.44         struct hostent *entry;
485 marek           1.43     
486 kumpf           1.44         entry = gethostbyaddr((const char *) &ip_addr, sizeof(ip_addr), AF_INET);
487 ouyang.jian     1.51         
488 kumpf           1.44         if (entry == 0)
489                              {
490                                  // error, couldn't resolve the ip
491                                  return false;
492                              }
493                              else
494                              {
495                                  unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
496                                  ip_part1 = entry->h_addr[0];
497                                  ip_part2 = entry->h_addr[1];
498                                  ip_part3 = entry->h_addr[2];
499                                  ip_part4 = entry->h_addr[3];
500                                  *resolvedIP = ip_part1;
501                                  *resolvedIP = (*resolvedIP << 8) + ip_part2;
502                                  *resolvedIP = (*resolvedIP << 8) + ip_part3;
503                                  *resolvedIP = (*resolvedIP << 8) + ip_part4;
504                              }
505                              return true;
506 marek           1.43     }
507                          
508                          
509 dave.sudlik     1.51.6.1 Boolean System::isLoopBack(int af, void *binIPAddress)
510                          {
511                          #ifdef PEGASUS_ENABLE_IPV6
512                              struct in6_addr ip6 = PEGASUS_IPV6_LOOPBACK_INIT;
513                          #endif
514                              Uint32 ip4 = PEGASUS_IPV4_LOOPBACK_INIT;
515                              switch (af)
516                              {
517                          #ifdef PEGASUS_ENABLE_IPV6
518                                  case AF_INET6:
519                                      return !memcmp(&ip6, binIPAddress, sizeof (ip6));
520                          #endif
521                                  case AF_INET:
522                                      Uint32 n = ntohl( *(Uint32*)binIPAddress);
523                                      return !memcmp(&ip4, &n, sizeof (ip4));       
524                              }
525                          
526                              return false;
527                          }
528                          
529 marek           1.43     Boolean System::isLocalHost(const String &hostName)
530                          {
531 dave.sudlik     1.51.6.1 // Get all ip addresses on the node and compare them with the given hostname.
532                          #ifdef PEGASUS_ENABLE_IPV6
533                              CString csName = hostName.getCString();
534                              struct addrinfo hints, *res1, *res2;
535                              char localHostName[PEGASUS_MAXHOSTNAMELEN];
536                              gethostname(localHostName, PEGASUS_MAXHOSTNAMELEN);
537                              Boolean isLocal = false;
538                          
539                              memset(&hints, 0, sizeof(hints));
540                              hints.ai_family = AF_INET;
541                              hints.ai_socktype = SOCK_STREAM;
542                              hints.ai_protocol = IPPROTO_TCP;
543                              res1 = res2 = 0;
544                              getaddrinfo(csName, 0, &hints, &res1);
545                              getaddrinfo(localHostName, 0, &hints, &res2);
546                          
547                              while (res1 && !isLocal)
548                              {
549                                  if (isLoopBack(AF_INET,
550                                      &((struct sockaddr_in*)res1->ai_addr)->sin_addr))
551                                  {
552 dave.sudlik     1.51.6.1             isLocal = true;
553                                      break;
554                                  }
555                          
556                                  struct addrinfo *tmp = res2;
557                                  while (tmp) 
558                                  {
559                                      if (!memcmp(&((struct sockaddr_in*)res1->ai_addr)->sin_addr,
560                                          &((struct sockaddr_in*)res2->ai_addr)->sin_addr,
561                                          sizeof (struct in_addr)))
562                                      {
563                                          isLocal = true;
564                                          break;
565                                      }
566                                      tmp = tmp->ai_next;
567                                  }
568                                  res1 = res1->ai_next;
569                              }   
570                              freeaddrinfo(res1);
571                              freeaddrinfo(res2);
572                              if (isLocal)
573 dave.sudlik     1.51.6.1     {
574                                  return true;
575                              } 
576                          
577                              hints.ai_family = AF_INET6;
578                              res1 = res2 = 0;
579                              getaddrinfo(csName, 0, &hints, &res1);
580                              getaddrinfo(localHostName, 0, &hints, &res2);
581                              while (res1 && !isLocal)
582                              {
583                                  if (isLoopBack(AF_INET6,
584                                      &((struct sockaddr_in6*)res1->ai_addr)->sin6_addr))
585                                  {
586                                      isLocal = true;
587                                      break;
588                                  }
589                          
590                                  struct addrinfo *tmp = res2;
591                                  while (tmp)
592                                  {
593                                      if (!memcmp(&((struct sockaddr_in6*)res1->ai_addr)->sin6_addr,
594 dave.sudlik     1.51.6.1                 &((struct sockaddr_in6*)res2->ai_addr)->sin6_addr,
595                                          sizeof (struct in6_addr)))
596                                      {
597                                          isLocal = true;
598                                          break;
599                                      }
600                                      tmp = tmp->ai_next;
601                                  }
602                                  res1 = res1->ai_next;
603                              }
604                              freeaddrinfo(res1);
605                              freeaddrinfo(res2);
606                          
607                              return isLocal;
608                          #else
609                          
610 marek           1.43         // differentiate between a dotted IP address given
611                              // and a real hostname given
612                              CString csName = hostName.getCString();
613 marek           1.46         char cc_hostname[PEGASUS_MAXHOSTNAMELEN];
614                              strcpy(cc_hostname, (const char*) csName);
615 marek           1.43         Uint32 tmp_addr = 0xFFFFFFFF;
616                              Boolean hostNameIsIPNotation;
617                          
618                              // Note: Platforms already supporting the inet_aton()
619                              //       should define their platform here,
620                              //        as this is the superior way to work
621                          #if defined(PEGASUS_OS_LINUX) || defined(PEGASUS_OS_AIX)
622 kumpf           1.44     
623 marek           1.43         struct in_addr inaddr;
624                              // if inet_aton failed(return=0),
625                              // we do not have a valip IP address (x.x.x.x)
626                              int atonSuccess = inet_aton(cc_hostname, &inaddr);
627                              if (atonSuccess == 0) hostNameIsIPNotation = false;
628                              else
629                              {
630                                  hostNameIsIPNotation = true;
631                                  tmp_addr = inaddr.s_addr;
632                              }
633                          #else
634                              // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255).
635                              //       A better solution would be to use inet_aton() or equivalent, as
636                              //       inet_addr() is now considered "obsolete".
637                              // Note: inet_aton() not yet supported on all Pegasus platforms
638 kumpf           1.44         tmp_addr = inet_addr((char *) cc_hostname);
639 marek           1.43         if (tmp_addr == 0xFFFFFFFF) hostNameIsIPNotation = false;
640                              else hostNameIsIPNotation = true;
641                          #endif
642 kumpf           1.44     
643 marek           1.43         if (!hostNameIsIPNotation)  // if hostname is not an IP address
644 kumpf           1.44         {
645 marek           1.43             // localhost ?
646                                  if (String::equalNoCase(hostName,String("localhost"))) return true;
647 marek           1.46             char localHostName[PEGASUS_MAXHOSTNAMELEN];
648 marek           1.47             CString cstringLocalHostName = System::getHostName().getCString();
649 marek           1.46             strcpy(localHostName, (const char*) cstringLocalHostName); 
650 marek           1.43             // given hostname equals what system returns as local hostname ?
651                                  if (String::equalNoCase(hostName,localHostName)) return true;
652                                  Uint32 hostIP;
653                                  // bail out if hostname unresolveable
654                                  if (!System::resolveHostNameAtDNS(cc_hostname, &hostIP)) return false;
655                                  // lets see if the IP is defined on one of the network interfaces
656                                  // this can help us avoid another call to DNS
657                                  if (System::isIpOnNetworkInterface(hostIP)) return true;
658                                  // need to check if the local hosts name is possibly
659                                  // registered at the DNS with the IP address equal resolvedNameIP
660                                  Uint32 localHostIP;
661 kumpf           1.44             if (!System::resolveHostNameAtDNS(localHostName, &localHostIP))
662                                      return false;
663 marek           1.43             if (localHostIP == hostIP) return true;
664 kumpf           1.44         }
665                              else
666 marek           1.43         {   // hostname is an IP address
667                                  // 127.0.0.1 is always the loopback
668                                  // inet_addr returns network byte order
669                                  if (tmp_addr == htonl(0x7F000001)) return true;
670                                  // IP defined on a local AF_INET network interface
671                                  if (System::isIpOnNetworkInterface(tmp_addr)) return true;
672                                  // out of luck so far, lets ask the DNS what our IP is
673                                  // and check against what we got
674                                  Uint32 localHostIP;
675 kumpf           1.44             if (!System::resolveHostNameAtDNS(
676                                          (const char*) System::getHostName().getCString(), &localHostIP))
677                                      return false;
678 marek           1.43             if (localHostIP == tmp_addr) return true;
679                                  // not yet, sometimes resolving the IP address we got against the DNS
680                                  // can solve the problem
681 kumpf           1.44             // casting to (const char *) as (char *) will work as (void *) too,
682                                  // those it fits all platforms
683 marek           1.43             Uint32 hostIP;
684                                  if (!System::resolveIPAtDNS(tmp_addr, &hostIP)) return false;
685                                  if (hostIP == localHostIP) return true;
686                              }
687                              return false;
688 dave.sudlik     1.51.6.1 #endif
689 marek           1.43     }
690                          
691 tony            1.17     // System ID constants for Logger::put and Logger::trace
692                          const String System::CIMLISTENER = "cimlistener"; // Listener systme ID
693                          
694 mike            1.8      PEGASUS_NAMESPACE_END
695 ms.aruran       1.45     

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2