b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
7fd59977 |
15 | #ifdef HAVE_CONFIG_H |
16 | # include <config.h> |
17 | #endif |
18 | |
19 | #ifndef WNT |
20 | |
21 | #include <Standard_NullObject.hxx> |
22 | #include <Standard_ConstructionError.hxx> |
23 | #include <OSD_Host.ixx> |
24 | #include <OSD_WhoAmI.hxx> |
25 | |
26 | const OSD_WhoAmI Iam = OSD_WHost; |
27 | |
28 | #include <errno.h> |
29 | |
30 | #ifdef HAVE_SYS_UTSNAME_H |
31 | # include <sys/utsname.h> // For 'uname' |
32 | #endif |
33 | |
34 | #ifdef HAVE_NETDB_H |
35 | # include <netdb.h> // This is for 'gethostbyname' |
36 | #endif |
37 | |
38 | #include <stdio.h> |
39 | |
40 | #ifdef HAVE_SYS_IOCTL_H |
41 | # include <sys/ioctl.h> |
42 | #endif |
43 | |
44 | #if defined(__osf__) || defined(DECOSF1) |
45 | #include <sys/types.h> |
46 | #include <sys/sysinfo.h> // For 'getsysinfo' |
47 | #include <sys/socket.h> // To get ethernet address |
48 | #include <sys/ioctl.h> |
49 | #include <net/if.h> |
50 | extern "C" { |
51 | int gethostname(char* address, int len); |
52 | } |
53 | #endif |
54 | |
55 | #ifdef HAVE_SYSENT_H |
56 | # include <sysent.h> // for 'gethostname' |
57 | #endif |
58 | |
59 | #ifdef HAVE_SYS_SOCKET_H |
60 | # include <sys/socket.h> |
61 | #endif |
62 | |
63 | #ifdef HAVE_SYS_UNISTD_H |
64 | # include <sys/unistd.h> |
65 | #endif |
66 | |
67 | #ifdef HAVE_UNISTD_H |
68 | # include <unistd.h> |
69 | #endif |
70 | |
71 | #ifdef HAVE_SYS_SYSTEMINFO_H |
72 | # include <sys/systeminfo.h> |
73 | #endif |
74 | |
75 | extern "C" {int sysinfo(int, char *, long);} |
76 | |
77 | |
78 | // ========================================================================= |
79 | |
80 | OSD_Host::OSD_Host(){} |
81 | |
82 | // ========================================================================= |
83 | |
84 | TCollection_AsciiString OSD_Host::SystemVersion(){ |
85 | struct utsname info; |
86 | TCollection_AsciiString result; |
87 | #ifdef HAVE_SYS_SYSTEMINFO_H |
88 | char buf[100]; |
89 | #endif |
90 | |
91 | uname (&info); |
92 | result = info.sysname; |
93 | result += " "; |
94 | result += info.release; |
95 | #ifdef HAVE_SYS_SYSTEMINFO_H |
96 | result += " "; |
97 | sysinfo(SI_ARCHITECTURE,buf,99); |
98 | result += buf; |
99 | result += " "; |
100 | sysinfo(SI_HW_PROVIDER,buf,99); |
101 | result += buf; |
102 | #endif |
103 | return(result); |
104 | } |
105 | |
106 | // ========================================================================= |
107 | |
108 | OSD_SysType OSD_Host::SystemId()const{ |
109 | struct utsname info; |
110 | |
111 | uname (&info); |
112 | |
113 | if (!strcmp(info.sysname,"SunOS")) return (OSD_UnixBSD); |
114 | if (!strcmp(info.sysname,"ULTRIX")) return (OSD_UnixBSD); |
115 | if (!strcmp(info.sysname,"FreeBSD")) return (OSD_UnixBSD); |
116 | if (!strncmp(info.sysname,"Linux",5)) return (OSD_LinuxREDHAT); |
117 | if (!strncmp(info.sysname,"IRIX", 4)) return (OSD_UnixSystemV); |
118 | if (!strncmp(info.sysname,"OSF", 3)) return (OSD_OSF); |
119 | if (!strcmp(info.sysname,"AIX")) return (OSD_Aix); |
120 | if (!strcmp(info.sysname,"UNIX_System_V")) return (OSD_UnixSystemV); |
121 | if (!strcmp(info.sysname,"VMS_POSIX")) return (OSD_VMS); |
122 | if (!strcmp(info.sysname,"Darwin")) return (OSD_MacOs); |
123 | return (OSD_Unknown); |
124 | } |
125 | |
126 | // ========================================================================= |
127 | |
128 | TCollection_AsciiString OSD_Host::HostName(){ |
129 | TCollection_AsciiString result; |
130 | char value[65]; |
131 | int status; |
132 | |
133 | status = gethostname(value, 64); |
134 | if (status == -1) myError.SetValue(errno, Iam, "Host Name"); |
135 | |
136 | result = value; |
137 | return(result); |
138 | } |
139 | |
140 | |
141 | // ========================================================================= |
142 | |
143 | |
144 | Standard_Integer OSD_Host::AvailableMemory(){ |
145 | Standard_Integer result; |
146 | |
147 | #if defined(__osf__) || defined(DECOSF1) |
148 | char buffer[16]; |
149 | //// result = getsysinfo(GSI_PHYSMEM,buffer, 16,0,NULL); |
150 | if (result != -1) |
151 | result *= 1024; |
152 | #else |
153 | result = 0; |
154 | //@@ A faire |
155 | #endif |
156 | return (result); |
157 | } |
158 | |
159 | // ========================================================================= |
160 | |
161 | TCollection_AsciiString OSD_Host::InternetAddress(){ |
162 | struct hostent internet_address; |
163 | int a,b,c,d; |
164 | char buffer[16]; |
165 | TCollection_AsciiString result,host; |
166 | |
167 | host = HostName(); |
168 | memcpy(&internet_address, |
169 | gethostbyname(host.ToCString()), |
170 | sizeof(struct hostent)); |
171 | |
172 | // Gets each bytes into integers |
173 | a = (unsigned char)internet_address.h_addr_list[0][0]; |
174 | b = (unsigned char)internet_address.h_addr_list[0][1]; |
175 | c = (unsigned char)internet_address.h_addr_list[0][2]; |
176 | d = (unsigned char)internet_address.h_addr_list[0][3]; |
177 | sprintf(buffer,"%d.%d.%d.%d",a,b,c,d); |
178 | result = buffer; |
179 | return(result); |
180 | } |
181 | |
7fd59977 |
182 | // ========================================================================= |
183 | OSD_OEMType OSD_Host::MachineType(){ |
184 | struct utsname info; |
185 | |
186 | uname (&info); |
187 | |
188 | if (!strcmp(info.sysname,"SunOS")) return (OSD_SUN); |
189 | if (!strcmp(info.sysname,"ULTRIX")) return (OSD_DEC); |
190 | if (!strncmp(info.sysname,"IRIX",4)) return (OSD_SGI); |
191 | if (!strcmp(info.sysname,"HP-UX")) return (OSD_HP); |
192 | if (!strcmp(info.sysname,"UNIX_System_V")) return (OSD_NEC); |
193 | if (!strcmp(info.sysname,"VMS_POSIX")) return (OSD_VAX); |
194 | if (!strncmp(info.sysname,"OSF",3)) return (OSD_DEC); |
195 | if (!strncmp(info.sysname,"Linux",5)) return (OSD_LIN); |
196 | if (!strcmp(info.sysname,"FreeBSD")) return (OSD_LIN); |
197 | if (!strncmp(info.sysname,"AIX",3)) return (OSD_AIX); |
198 | if (!strcmp(info.sysname,"Darwin")) return (OSD_MAC); |
199 | return (OSD_Unavailable); |
200 | |
201 | } |
202 | |
203 | void OSD_Host::Reset(){ |
204 | myError.Reset(); |
205 | } |
206 | |
207 | Standard_Boolean OSD_Host::Failed()const{ |
208 | return( myError.Failed()); |
209 | } |
210 | |
211 | void OSD_Host::Perror() { |
212 | myError.Perror(); |
213 | } |
214 | |
215 | |
216 | Standard_Integer OSD_Host::Error()const{ |
217 | return( myError.Error()); |
218 | } |
219 | |
220 | #else |
221 | |
222 | //------------------------------------------------------------------------ |
223 | //------------------- WNT Sources of OSD_Host --------------------------- |
224 | //------------------------------------------------------------------------ |
225 | |
226 | #define STRICT |
227 | #include <windows.h> |
228 | |
229 | #include <OSD_Host.hxx> |
230 | |
231 | #pragma comment( lib, "WSOCK32.LIB" ) |
232 | |
233 | void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... ); |
234 | |
235 | static BOOL fInit = FALSE; |
236 | static TCollection_AsciiString hostName; |
237 | static TCollection_AsciiString version; |
238 | static TCollection_AsciiString interAddr; |
7fd59977 |
239 | static Standard_Integer memSize; |
240 | |
241 | OSD_Host :: OSD_Host () { |
242 | |
243 | DWORD nSize; |
244 | Standard_Character szHostName[ MAX_COMPUTERNAME_LENGTH + 1 ]; |
302f96fb |
245 | char* hostAddr = 0; |
7fd59977 |
246 | MEMORYSTATUS ms; |
247 | WSADATA wd; |
248 | PHOSTENT phe; |
249 | IN_ADDR inAddr; |
250 | OSVERSIONINFO osVerInfo; |
251 | |
252 | if ( !fInit ) { |
253 | |
254 | nSize = MAX_COMPUTERNAME_LENGTH + 1; |
255 | osVerInfo.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO ); |
256 | |
302f96fb |
257 | ZeroMemory (&ms, sizeof(ms)); |
8ec3edb0 |
258 | ZeroMemory ( szHostName, sizeof ( Standard_Character ) * (MAX_COMPUTERNAME_LENGTH + 1) ); |
7fd59977 |
259 | |
260 | if ( !GetVersionEx ( &osVerInfo ) ) { |
261 | |
262 | _osd_wnt_set_error ( myError, OSD_WHost ); |
263 | |
264 | } else if ( !GetComputerName ( szHostName, &nSize ) ) { |
265 | |
266 | _osd_wnt_set_error ( myError, OSD_WHost ); |
267 | |
268 | } else { |
269 | |
270 | ms.dwLength = sizeof ( MEMORYSTATUS ); |
271 | GlobalMemoryStatus ( &ms ); |
272 | |
273 | } // end else |
274 | |
275 | if ( !Failed () ) { |
276 | |
60be1f9b |
277 | memSize = (Standard_Integer) ms.dwAvailPageFile; |
7fd59977 |
278 | |
279 | if ( WSAStartup ( MAKEWORD( 1, 1 ), &wd ) ) { |
280 | |
281 | _osd_wnt_set_error ( myError, OSD_WHost ); |
282 | |
283 | } else if ( ( phe = gethostbyname ( szHostName ) ) == NULL ) { |
284 | |
285 | _osd_wnt_set_error ( myError, OSD_WHost ); |
286 | |
287 | } else { |
288 | |
289 | CopyMemory ( &inAddr, *phe -> h_addr_list, sizeof ( IN_ADDR ) ); |
290 | hostAddr = inet_ntoa ( inAddr ); |
291 | |
292 | } // end else |
293 | |
294 | } // end if |
295 | |
296 | if ( !Failed () ) { |
297 | |
298 | hostName = szHostName; |
299 | interAddr = Standard_CString ( hostAddr ); |
300 | wsprintf ( |
301 | osVerInfo.szCSDVersion, TEXT( "Windows NT Version %d.%d" ), |
302 | osVerInfo.dwMajorVersion, osVerInfo.dwMinorVersion |
303 | ); |
304 | version = osVerInfo.szCSDVersion; |
305 | |
306 | fInit = TRUE; |
307 | |
308 | } // end if |
309 | |
310 | } // end if |
311 | |
312 | if ( fInit ) |
313 | |
314 | myName = hostName; |
315 | |
316 | } // end constructor |
317 | |
318 | TCollection_AsciiString OSD_Host :: SystemVersion () { |
319 | |
320 | return version; |
321 | |
322 | } // end OSD_Host :: SystemVersion |
323 | |
324 | OSD_SysType OSD_Host :: SystemId () const { |
325 | |
326 | return OSD_WindowsNT; |
327 | |
328 | } // end OSD_Host :: SystemId |
329 | |
330 | TCollection_AsciiString OSD_Host :: HostName () { |
331 | |
332 | return hostName; |
333 | |
334 | } // end OSD_Host :: HostName |
335 | |
336 | Standard_Integer OSD_Host :: AvailableMemory () { |
337 | |
338 | return memSize; |
339 | |
340 | } // end OSD_Host :: AvailableMemory |
341 | |
342 | TCollection_AsciiString OSD_Host :: InternetAddress () { |
343 | |
344 | return interAddr; |
345 | |
346 | } // end OSD_Host :: InternetAddress |
347 | |
7fd59977 |
348 | OSD_OEMType OSD_Host :: MachineType () { |
349 | |
350 | return OSD_PC; |
351 | |
352 | } // end OSD_Host :: MachineTYpe |
353 | |
354 | Standard_Boolean OSD_Host :: Failed () const { |
355 | |
356 | return myError.Failed (); |
357 | |
358 | } // end OSD_Host :: Failed |
359 | |
360 | void OSD_Host :: Reset () { |
361 | |
362 | myError.Reset (); |
363 | |
364 | } // end OSD_Host :: Reset |
365 | |
366 | void OSD_Host :: Perror () { |
367 | |
368 | myError.Perror (); |
369 | |
370 | } // end OSD_Host :: Perror |
371 | |
372 | Standard_Integer OSD_Host :: Error () const { |
373 | |
374 | return myError.Error (); |
375 | |
376 | } //end OSD_Host :: Error |
377 | |
378 | #endif |
379 | |
380 | |
381 | |
382 | |
383 | |