(file) Return to System.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.25 //%2005////////////////////////////////////////////////////////////////////////
  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 mike  1.6  //
 12            // Permission is hereby granted, free of charge, to any person obtaining a copy
 13 kumpf 1.11 // of this software and associated documentation files (the "Software"), to
 14            // deal in the Software without restriction, including without limitation the
 15            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 16 mike  1.6  // sell copies of the Software, and to permit persons to whom the Software is
 17            // furnished to do so, subject to the following conditions:
 18 karl  1.23 // 
 19 kumpf 1.11 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 20 mike  1.6  // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 21            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 22 kumpf 1.11 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 23            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 24            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 25 mike  1.6  // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27            //
 28            //==============================================================================
 29            //
 30            // Author: Mike Brasher (mbrasher@bmc.com)
 31            //
 32 mike  1.7  // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01
 33 ramnath 1.9  //					added nsk platform support
 34              //				Ramnath Ravindran (Ramnath.Ravindran@compaq.com) 03/21/2002
 35 r.kieninger 1.21 //					replaced instances of "| ios::binary" with
 36 ramnath     1.9  //					PEGASUS_OR_IOS_BINARY
 37 r.kieninger 1.21 //              Robert Kieninger, IBM (kieningr@de.ibm.com) for Bug#667
 38 dave.sudlik 1.22 //              Dave Sudlik, IBM (dsudlik@us.ibm.com) for Bug#1462
 39 gs.keenan   1.26 //              Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
 40 mike        1.6  //
 41                  //%/////////////////////////////////////////////////////////////////////////////
 42 mike        1.8  
 43 sage        1.12 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 44 r.kieninger 1.21 #include <Pegasus/Common/Config.h>
 45                  #endif
 46 sage        1.12 
 47                  
 48 mike        1.8  #include <fstream>
 49 kumpf       1.13 #include <cctype>  // for tolower()
 50 kumpf       1.14 #include <cstring>
 51 mike        1.6  #include "System.h"
 52 r.kieninger 1.21 #include "Socket.h"
 53                  
 54                  #ifdef PEGASUS_PLATFORM_WIN32_IX86_MSVC
 55                  # include <windows.h>
 56                  #else
 57                  # include <arpa/inet.h>
 58                  #endif
 59 mike        1.6  
 60 kumpf       1.10 #include <Pegasus/Common/PegasusVersion.h>
 61                  
 62 mike        1.6  #if defined(PEGASUS_OS_TYPE_WINDOWS)
 63                  # include "SystemWindows.cpp"
 64                  #elif defined(PEGASUS_OS_TYPE_UNIX)
 65                  # include "SystemUnix.cpp"
 66 mike        1.7  #elif defined(PEGASUS_OS_TYPE_NSK)
 67                  # include "SystemNsk.cpp"
 68 gs.keenan   1.26 #elif defined(PEGASUS_OS_VMS)
 69                  # include "SystemVms.cpp"
 70 mike        1.6  #else
 71                  # error "Unsupported platform"
 72                  #endif
 73 mike        1.8  
 74 david       1.16 #if defined(PEGASUS_OS_OS400)
 75                  # include "OS400ConvertChar.h"
 76                  #endif
 77                  
 78 mike        1.8  PEGASUS_USING_STD;
 79                  
 80                  PEGASUS_NAMESPACE_BEGIN
 81                  
 82 kumpf       1.20 Boolean System::bindVerbose = false;
 83                  
 84 mike        1.8  Boolean System::copyFile(const char* fromPath, const char* toPath)
 85                  {
 86 ramnath     1.9      ifstream is(fromPath PEGASUS_IOS_BINARY);
 87                      ofstream os(toPath PEGASUS_IOS_BINARY);
 88 mike        1.8  
 89                      char c;
 90                  
 91                      while (is.get(c))
 92                      {
 93                  	if (!os.put(c))
 94                  	    return false;
 95                      }
 96                  
 97                      return true;
 98 kumpf       1.13 }
 99                  
100                  // ATTN: Move to platform-specific System implementation files and call
101                  // strcasecmp where it is available.
102                  Sint32 System::strcasecmp(const char* s1, const char* s2)
103                  {
104                      while (*s1 && *s2)
105                      {
106                          int r = tolower(*s1++) - tolower(*s2++);
107                  
108                          if (r)
109                              return r;
110                      }
111                  
112                      if (*s2)
113                          return -1;
114                      else if (*s1)
115                          return 1;
116                  
117                      return 0;
118 mike        1.8  }
119                  
120 tony        1.15 // Return the just the file name from the path into basename
121                  char *System::extract_file_name(const char *fullpath, char *basename)
122                  {
123                    char *p;
124                    char buff[2048];
125                    if (fullpath == NULL)
126                      {
127                        basename[0] = '\0';
128                        return basename;
129                      }
130                    strcpy(buff, fullpath);
131                    for(p = buff + strlen(buff); p >= buff; p--)
132                      {
133                        if (*p == '\\' || *p == '/')
134                          {
135                            strcpy(basename, p+1);
136                            return basename;
137                          }
138                      }
139                    strcpy(basename, fullpath);
140                    return basename;
141 tony        1.15 }
142                  
143                  // Return the just the path to the file name into dirname
144                  char *System::extract_file_path(const char *fullpath, char *dirname)
145                  {
146                    char *p;
147                    char buff[2048];
148                    if (fullpath == NULL)
149                      {
150                        dirname[0] = '\0';
151                        return dirname;
152                      }
153                    strcpy(buff, fullpath);
154                    for(p = buff + strlen(buff); p >= buff; p--)
155                      {
156                        if (*p == '\\' || *p == '/')
157                          {
158                            strncpy(dirname, buff, p+1 - buff);
159                            dirname[p+1 - buff] = '\0';
160                            return dirname;
161                          }
162 tony        1.15     }
163                    strcpy(dirname, fullpath);
164                    return dirname;
165                  }
166                  
167 marek       1.24 String System::getHostIP(const String &hostName)
168                  {
169                      struct hostent * phostent;
170                      struct in_addr   inaddr;
171                      String ipAddress = String::EMPTY;
172 humberto    1.27     CString csName = hostName.getCString();
173                      const char * ccName = csName;
174                  #ifndef PEGASUS_OS_OS400
175                      if ((phostent = ::gethostbyname(ccName)) != NULL)
176                  #else
177 chuck       1.28     char ebcdicHost[PEGASUS_MAXHOSTNAMELEN];
178                      if (strlen(ccName) < PEGASUS_MAXHOSTNAMELEN)
179                          strcpy(ebcdicHost, ccName);
180                      else
181                          return ipAddress;
182                      AtoE(ebcdicHost);
183                      if ((phostent = ::gethostbyname(ebcdicHost)) != NULL)
184 humberto    1.27 #endif
185 marek       1.24     {
186                          ::memcpy( &inaddr, phostent->h_addr,4);
187                  #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
188                          char * gottenIPAdress = NULL;
189                          gottenIPAdress = ::inet_ntoa( inaddr );
190                          __etoa(gottenIPAdress);
191                          if (gottenIPAdress != NULL)
192                          {
193                              ipAddress.assign(gottenIPAdress);
194                          }
195                  #else
196                          ipAddress = ::inet_ntoa( inaddr );
197                  #endif
198                      }
199                      return ipAddress;
200                  }
201 r.kieninger 1.21 
202                  // ------------------------------------------------------------------------
203                  // Convert a hostname into a a single host unique integer representation
204                  // ------------------------------------------------------------------------
205                  Uint32 System::_acquireIP(const char* hostname)
206                  {
207                  	Uint32 ip = 0xFFFFFFFF;
208                  	if (!hostname) return 0xFFFFFFFF;
209                  
210                  #ifdef PEGASUS_OS_OS400
211                  	char ebcdicHost[PEGASUS_MAXHOSTNAMELEN];
212                  	if (strlen(hostname) < PEGASUS_MAXHOSTNAMELEN)
213                  		strcpy(ebcdicHost, hostname);
214                  	else
215                  		return 0xFFFFFFFF;
216                  	AtoE(ebcdicHost);
217                  #endif
218                  
219 dave.sudlik 1.22 ////////////////////////////////////////////////////////////////////////////////
220                  // This code used to check if the first character of "hostname" was alphabetic
221                  // to indicate hostname instead of IP address. But RFC 1123, section 2.1, relaxed
222                  // this requirement to alphabetic character *or* digit. So bug 1462 changed the
223                  // flow here to call inet_addr first to check for a valid IP address in dotted
224                  // decimal notation. If it's not a valid IP address, then try to validate
225                  // it as a hostname.
226                  // RFC 1123 states: The host SHOULD check the string syntactically for a 
227                  // dotted-decimal number before looking it up in the Domain Name System. 
228                  // Hence the call to inet_addr() first.
229                  ////////////////////////////////////////////////////////////////////////////////
230                  
231                  #ifdef PEGASUS_OS_OS400
232                  	Uint32 tmp_addr = inet_addr(ebcdicHost);
233                  #elif defined(PEGASUS_OS_ZOS)
234                  	Uint32 tmp_addr = inet_addr_ebcdic((char *)hostname);
235                  #else
236                  	Uint32 tmp_addr = inet_addr((char *) hostname);
237                  #endif
238                  
239 r.kieninger 1.21 	struct hostent *entry;
240                  
241 dave.sudlik 1.22 // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255).
242                  //       A better solution would be to use inet_aton() or equivalent, as
243                  //       inet_addr() is now considered "obsolete".
244                  
245                      if (tmp_addr == 0xFFFFFFFF)  // if hostname is not an IP address
246 r.kieninger 1.21 	{
247                  #ifdef PEGASUS_PLATFORM_SOLARIS_SPARC_CC
248                  #define HOSTENT_BUFF_SIZE        8192
249                  		char      buf[HOSTENT_BUFF_SIZE];
250                  		int       h_errorp;
251                  		struct    hostent hp;
252                  
253                  		entry = gethostbyname_r((char *)hostname, &hp, buf,
254                  								HOSTENT_BUFF_SIZE, &h_errorp);
255                  #elif defined(PEGASUS_OS_OS400)
256                  		entry = gethostbyname(ebcdicHost);
257                  #elif defined(PEGASUS_OS_ZOS)
258                  		char hostName[ PEGASUS_MAXHOSTNAMELEN ];
259                  		if (String::equalNoCase("localhost",String(hostname)))
260                  		{
261                  			gethostname( hostName, PEGASUS_MAXHOSTNAMELEN );
262                  			entry = gethostbyname(hostName);
263                  		} else
264                  		{
265                  			entry = gethostbyname((char *)hostname);
266                  		}
267 r.kieninger 1.21 #else
268                  		entry = gethostbyname((char *)hostname);
269                  #endif
270                  		if (!entry)
271                  		{
272                  			return 0xFFFFFFFF;
273                  		}
274                  		unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
275                  
276                  		ip_part1 = entry->h_addr[0];
277                  		ip_part2 = entry->h_addr[1];
278                  		ip_part3 = entry->h_addr[2];
279                  		ip_part4 = entry->h_addr[3];
280                  		ip = ip_part1;
281                  		ip = (ip << 8) + ip_part2;
282                  		ip = (ip << 8) + ip_part3;
283                  		ip = (ip << 8) + ip_part4;
284 dave.sudlik 1.22 	}
285                      else    // else hostname *is* a dotted-decimal IP address
286 r.kieninger 1.21 	{
287                  		// resolve hostaddr to a real host entry
288                  		// casting to (const char *) as (char *) will work as (void *) too, those it fits all platforms
289 humberto    1.27 #ifndef PEGASUS_OS_OS400
290                          entry = gethostbyaddr((const char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
291                  #else
292 chuck       1.28 		entry = gethostbyaddr((char *) &tmp_addr, sizeof(tmp_addr), AF_INET);
293 humberto    1.27 #endif
294 r.kieninger 1.21 		if (entry == 0)
295                  		{
296                  			// error, couldn't resolve the ip
297                  			return 0xFFFFFFFF;
298                  		} else
299                  		{
300                  
301                  			unsigned char ip_part1,ip_part2,ip_part3,ip_part4;
302                  
303                  			ip_part1 = entry->h_addr[0];
304                  			ip_part2 = entry->h_addr[1];
305                  			ip_part3 = entry->h_addr[2];
306                  			ip_part4 = entry->h_addr[3];
307                  			ip = ip_part1;
308                  			ip = (ip << 8) + ip_part2;
309                  			ip = (ip << 8) + ip_part3;
310                  			ip = (ip << 8) + ip_part4;
311                  		}
312                  	}
313                  
314                  	return ip;
315 r.kieninger 1.21 }
316                  
317 tony        1.17 // System ID constants for Logger::put and Logger::trace
318                  const String System::CIMLISTENER = "cimlistener"; // Listener systme ID
319                  
320 mike        1.8  PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2