wx/wxprec.h already includes wx/defs.h (with other minor cleaning).
[wxWidgets.git] / src / unix / dialup.cpp
1 // -*- c++ -*- ////////////////////////////////////////////////////////////////
2 // Name: src/unix/dialup.cpp
3 // Purpose: Network related wxWidgets classes and functions
4 // Author: Karsten Ballüder
5 // Modified by:
6 // Created: 03.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_DIALUP_MANAGER
16
17 #ifndef WX_PRECOMP
18 #include "wx/string.h"
19 #endif // !PCH
20
21 #include "wx/event.h"
22 #include "wx/dialup.h"
23 #include "wx/timer.h"
24 #include "wx/filefn.h"
25 #include "wx/utils.h"
26 #include "wx/log.h"
27 #include "wx/ffile.h"
28 #include "wx/process.h"
29 #include "wx/intl.h"
30 #include "wx/app.h"
31 #include "wx/wxchar.h"
32
33 #include <stdlib.h>
34
35 #include <signal.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #define __STRICT_ANSI__
39 #include <sys/socket.h>
40 #include <netdb.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <errno.h>
44
45 DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED)
46 DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED)
47
48 // ----------------------------------------------------------------------------
49 // A class which groups functions dealing with connecting to the network from a
50 // workstation using dial-up access to the net. There is at most one instance
51 // of this class in the program accessed via GetDialUpManager().
52 // ----------------------------------------------------------------------------
53
54 /* TODO
55 *
56 * 1. more configurability for Unix: i.e. how to initiate the connection, how
57 * to check for online status, &c.
58 * 2. add a "long Dial(long connectionId = -1)" function which asks the user
59 * about which connection to dial (this may be done using native dialogs
60 * under NT, need generic dialogs for all others) and returns the identifier
61 * of the selected connection (it's opaque to the application) - it may be
62 * reused later to dial the same connection later (or use strings instead of
63 * longs may be?)
64 * 3. add an async version of dialing functions which notify the caller about
65 * the progress (or may be even start another thread to monitor it)
66 * 4. the static creation/accessor functions are not MT-safe - but is this
67 * really crucial? I think we may suppose they're always called from the
68 * main thread?
69 */
70
71 class WXDLLEXPORT wxDialUpManagerImpl : public wxDialUpManager
72 {
73 public:
74 wxDialUpManagerImpl();
75 ~wxDialUpManagerImpl();
76
77 /** Could the dialup manager be initialized correctly? If this function
78 returns false, no other functions will work neither, so it's a good idea
79 to call this function and check its result before calling any other
80 wxDialUpManager methods.
81 */
82 virtual bool IsOk() const
83 { return true; }
84
85 /** The simplest way to initiate a dial up: this function dials the given
86 ISP (exact meaning of the parameter depends on the platform), returns
87 true on success or false on failure and logs the appropriate error
88 message in the latter case.
89 @param nameOfISP optional paramater for dial program
90 @param username unused
91 @param password unused
92 */
93 virtual bool Dial(const wxString& nameOfISP,
94 const wxString& WXUNUSED(username),
95 const wxString& WXUNUSED(password),
96 bool async);
97
98 // Hang up the currently active dial up connection.
99 virtual bool HangUp();
100
101 // returns true if the computer is connected to the network: under Windows,
102 // this just means that a RAS connection exists, under Unix we check that
103 // the "well-known host" (as specified by SetWellKnownHost) is reachable
104 virtual bool IsOnline() const
105 {
106 CheckStatus();
107 return m_IsOnline == Net_Connected;
108 }
109
110 // do we have a constant net connection?
111 virtual bool IsAlwaysOnline() const;
112
113 // returns true if (async) dialing is in progress
114 virtual bool IsDialing() const
115 { return m_DialProcess != NULL; }
116
117 // cancel dialing the number initiated with Dial(async = true)
118 // NB: this won't result in DISCONNECTED event being sent
119 virtual bool CancelDialing();
120
121 size_t GetISPNames(class wxArrayString &) const
122 { return 0; }
123
124 // sometimes the built-in logic for determining the online status may fail,
125 // so, in general, the user should be allowed to override it. This function
126 // allows to forcefully set the online status - whatever our internal
127 // algorithm may think about it.
128 virtual void SetOnlineStatus(bool isOnline = true)
129 { m_IsOnline = isOnline ? Net_Connected : Net_No; }
130
131 // set misc wxDialUpManager options
132 // --------------------------------
133
134 // enable automatical checks for the connection status and sending of
135 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
136 // parameter is only for Unix where we do the check manually: under
137 // Windows, the notification about the change of connection status is
138 // instantenous.
139 //
140 // Returns false if couldn't set up automatic check for online status.
141 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds);
142
143 // disable automatic check for connection status change - notice that the
144 // wxEVT_DIALUP_XXX events won't be sent any more neither.
145 virtual void DisableAutoCheckOnlineStatus();
146
147 // under Unix, the value of well-known host is used to check whether we're
148 // connected to the internet. It's unused under Windows, but this function
149 // is always safe to call. The default value is www.yahoo.com.
150 virtual void SetWellKnownHost(const wxString& hostname,
151 int portno = 80);
152 /** Sets the commands to start up the network and to hang up
153 again. Used by the Unix implementations only.
154 */
155 virtual void SetConnectCommand(const wxString &command, const wxString &hupcmd)
156 { m_ConnectCommand = command; m_HangUpCommand = hupcmd; }
157
158 //private: -- Sun CC 4.2 objects to using NetConnection enum as the return
159 // type if it is declared private
160
161 // the possible results of testing for Online() status
162 enum NetConnection
163 {
164 Net_Unknown = -1, // we couldn't learn anything
165 Net_No, // no network connection [currently]
166 Net_Connected // currently connected
167 };
168
169 // the possible net connection types
170 enum NetDeviceType
171 {
172 NetDevice_None = 0x0000, // no network devices (authoritative)
173 NetDevice_Unknown = 0x0001, // test doesn't work on this OS
174 NetDevice_Modem = 0x0002, // we have a modem
175 NetDevice_LAN = 0x0004 // a network card
176 };
177
178 private:
179 // the current status
180 NetConnection m_IsOnline;
181
182 // the connection we have with the network card
183 NetConnection m_connCard;
184
185 // Can we use ifconfig to list active devices?
186 int m_CanUseIfconfig;
187
188 // The path to ifconfig
189 wxString m_IfconfigPath;
190
191 // Can we use ping to find hosts?
192 int m_CanUsePing;
193 // The path to ping program
194 wxString m_PingPath;
195
196 // beacon host:
197 wxString m_BeaconHost;
198 // beacon host portnumber for connect:
199 int m_BeaconPort;
200
201 // command to connect to network
202 wxString m_ConnectCommand;
203 // command to hang up
204 wxString m_HangUpCommand;
205 // name of ISP
206 wxString m_ISPname;
207 // a timer for regular testing
208 class AutoCheckTimer *m_timer;
209 friend class AutoCheckTimer;
210
211 // a wxProcess for dialling in background
212 class wxDialProcess *m_DialProcess;
213 // pid of dial process
214 int m_DialPId;
215 friend class wxDialProcess;
216
217 // determine status
218 void CheckStatus(bool fromAsync = false) const;
219
220 // real status check
221 void CheckStatusInternal();
222
223 // check /proc/net (Linux only) for ppp/eth interfaces, returns the bit
224 // mask of NetDeviceType constants
225 int CheckProcNet();
226
227 // check output of ifconfig command for PPP/SLIP/PLIP devices, returns the
228 // bit mask of NetDeviceType constants
229 int CheckIfconfig();
230
231 // combines the 2 possible checks for determining the connection status
232 NetConnection CheckConnectAndPing();
233
234 // pings a host
235 NetConnection CheckPing();
236
237 // check by connecting to host on given port.
238 NetConnection CheckConnect();
239 };
240
241
242 class AutoCheckTimer : public wxTimer
243 {
244 public:
245 AutoCheckTimer(wxDialUpManagerImpl *dupman)
246 {
247 m_dupman = dupman;
248 }
249
250 virtual void Notify()
251 {
252 wxLogTrace(_T("dialup"), wxT("Checking dial up network status."));
253
254 m_dupman->CheckStatus();
255 }
256
257 public:
258 wxDialUpManagerImpl *m_dupman;
259 };
260
261 class wxDialProcess : public wxProcess
262 {
263 public:
264 wxDialProcess(wxDialUpManagerImpl *dupman)
265 {
266 m_DupMan = dupman;
267 }
268 void Disconnect() { m_DupMan = NULL; }
269 virtual void OnTerminate(int WXUNUSED(pid), int WXUNUSED(status))
270 {
271 if(m_DupMan)
272 {
273 m_DupMan->m_DialProcess = NULL;
274 m_DupMan->CheckStatus(true);
275 }
276 }
277 private:
278 wxDialUpManagerImpl *m_DupMan;
279 };
280
281
282 wxDialUpManagerImpl::wxDialUpManagerImpl()
283 {
284 m_IsOnline =
285 m_connCard = Net_Unknown;
286 m_DialProcess = NULL;
287 m_timer = NULL;
288 m_CanUseIfconfig = -1; // unknown
289 m_CanUsePing = -1; // unknown
290 m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST;
291 m_BeaconPort = 80;
292
293 #ifdef __SGI__
294 m_ConnectCommand = _T("/usr/etc/ppp");
295 #elif defined(__LINUX__)
296 // default values for Debian/GNU linux
297 m_ConnectCommand = _T("pon");
298 m_HangUpCommand = _T("poff");
299 #endif
300
301 wxChar * dial = wxGetenv(_T("WXDIALUP_DIALCMD"));
302 wxChar * hup = wxGetenv(_T("WXDIALUP_HUPCMD"));
303 SetConnectCommand(dial ? wxString(dial) : m_ConnectCommand,
304 hup ? wxString(hup) : m_HangUpCommand);
305 }
306
307 wxDialUpManagerImpl::~wxDialUpManagerImpl()
308 {
309 if(m_timer) delete m_timer;
310 if(m_DialProcess)
311 {
312 m_DialProcess->Disconnect();
313 m_DialProcess->Detach();
314 }
315 }
316
317 bool
318 wxDialUpManagerImpl::Dial(const wxString &isp,
319 const wxString & WXUNUSED(username),
320 const wxString & WXUNUSED(password),
321 bool async)
322 {
323 if(m_IsOnline == Net_Connected)
324 return false;
325 m_ISPname = isp;
326 wxString cmd;
327 if(m_ConnectCommand.Find(wxT("%s")))
328 cmd.Printf(m_ConnectCommand,m_ISPname.c_str());
329 else
330 cmd = m_ConnectCommand;
331
332 if ( async )
333 {
334 m_DialProcess = new wxDialProcess(this);
335 m_DialPId = (int)wxExecute(cmd, false, m_DialProcess);
336 if(m_DialPId == 0)
337 {
338 delete m_DialProcess;
339 m_DialProcess = NULL;
340 return false;
341 }
342 else
343 return true;
344 }
345 else
346 return wxExecute(cmd, /* sync */ true) == 0;
347 }
348
349 bool wxDialUpManagerImpl::HangUp()
350 {
351 if(m_IsOnline == Net_No)
352 return false;
353 if(IsDialing())
354 {
355 wxLogError(_("Already dialling ISP."));
356 return false;
357 }
358 wxString cmd;
359 if(m_HangUpCommand.Find(wxT("%s")))
360 cmd.Printf(m_HangUpCommand,m_ISPname.c_str(), m_DialProcess);
361 else
362 cmd = m_HangUpCommand;
363 return wxExecute(cmd, /* sync */ true) == 0;
364 }
365
366
367 bool wxDialUpManagerImpl::CancelDialing()
368 {
369 if(! IsDialing())
370 return false;
371 return kill(m_DialPId, SIGTERM) > 0;
372 }
373
374 bool wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds)
375 {
376 DisableAutoCheckOnlineStatus();
377 m_timer = new AutoCheckTimer(this);
378 bool rc = m_timer->Start(nSeconds*1000);
379 if(! rc)
380 {
381 delete m_timer;
382 m_timer = NULL;
383 }
384 return rc;
385 }
386
387 void wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
388 {
389 if(m_timer != NULL)
390 {
391 m_timer->Stop();
392 delete m_timer;
393 m_timer = NULL;
394 }
395 }
396
397
398 void wxDialUpManagerImpl::SetWellKnownHost(const wxString& hostname, int portno)
399 {
400 if(hostname.length() == 0)
401 {
402 m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST;
403 m_BeaconPort = 80;
404 return;
405 }
406
407 // does hostname contain a port number?
408 wxString port = hostname.After(wxT(':'));
409 if(port.length())
410 {
411 m_BeaconHost = hostname.Before(wxT(':'));
412 m_BeaconPort = wxAtoi(port);
413 }
414 else
415 {
416 m_BeaconHost = hostname;
417 m_BeaconPort = portno;
418 }
419 }
420
421
422 void wxDialUpManagerImpl::CheckStatus(bool fromAsync) const
423 {
424 // This function calls the CheckStatusInternal() helper function
425 // which is OS - specific and then sends the events.
426
427 NetConnection oldIsOnline = m_IsOnline;
428 ( /* non-const */ (wxDialUpManagerImpl *)this)->CheckStatusInternal();
429
430 // now send the events as appropriate: i.e. if the status changed and
431 // if we're in defined state
432 if(m_IsOnline != oldIsOnline
433 && m_IsOnline != Net_Unknown
434 && oldIsOnline != Net_Unknown )
435 {
436 wxDialUpEvent event(m_IsOnline == Net_Connected, ! fromAsync);
437 (void)wxTheApp->ProcessEvent(event);
438 }
439 }
440
441 /*
442 We first try to find out if ppp interface is active. If it is, we assume
443 that we're online but don't have a permanent connection (this is false if a
444 networked machine uses modem to connect to somewhere else, but we can't do
445 anything in this case anyhow).
446
447 If no ppp interface is detected, we check for eth interface. If it is
448 found, we check that we can, indeed, connect to an Internet host. The logic
449 here is that connection check should be fast enough in this case and we
450 don't want to give false positives in a (common) case of a machine on a LAN
451 which is not connected to the outside.
452
453 If we didn't find either ppp or eth interfaces, we stop here and decide
454 that we're connected. However, if couldn't check for this, we try to ping a
455 remote host just in case.
456
457 NB1: Checking for the interface presence can be done in 2 ways
458 a) reading /proc/net/dev under Linux
459 b) spawning ifconfig under any OS
460
461 The first method is faster but only works under Linux.
462
463 NB2: pinging, actually, means that we first try to connect "manually" to
464 a port on remove machine and if it fails, we run ping.
465 */
466
467 void wxDialUpManagerImpl::CheckStatusInternal()
468 {
469 m_IsOnline = Net_Unknown;
470
471 // first do quick checks to determine what kind of network devices do we
472 // have
473 int netDeviceType = CheckProcNet();
474 if ( netDeviceType == NetDevice_Unknown )
475 {
476 // nothing found, try ifconfig too
477 netDeviceType = CheckIfconfig();
478 }
479
480 switch ( netDeviceType )
481 {
482 case NetDevice_None:
483 // no network devices, no connection
484 m_IsOnline = Net_No;
485 break;
486
487 case NetDevice_LAN:
488 // we still do ping to confirm that we're connected but we only do
489 // it once and hope that the purpose of the network card (i.e.
490 // whether it used for connecting to the Internet or just to a
491 // LAN) won't change during the program lifetime
492 if ( m_connCard == Net_Unknown )
493 {
494 m_connCard = CheckConnectAndPing();
495 }
496 m_IsOnline = m_connCard;
497 break;
498
499 case NetDevice_Unknown:
500 // try to ping just in case
501 m_IsOnline = CheckConnectAndPing();
502 break;
503
504 case NetDevice_LAN + NetDevice_Modem:
505 case NetDevice_Modem:
506 // assume we're connected
507 m_IsOnline = Net_Connected;
508 break;
509
510 default:
511 wxFAIL_MSG(_T("Unexpected netDeviceType"));
512 }
513 }
514
515 bool wxDialUpManagerImpl::IsAlwaysOnline() const
516 {
517 wxDialUpManagerImpl *self = wxConstCast(this, wxDialUpManagerImpl);
518
519 int netDeviceType = self->CheckProcNet();
520 if ( netDeviceType == NetDevice_Unknown )
521 {
522 // nothing found, try ifconfig too
523 netDeviceType = self->CheckIfconfig();
524 }
525
526 if ( netDeviceType == NetDevice_Unknown )
527 {
528 // this is the only thing we can do unfortunately...
529 self->HangUp();
530 return IsOnline();
531 }
532 else
533 {
534 // we are only permanently online if we have a network card
535 return (netDeviceType & NetDevice_LAN) != 0;
536 }
537 }
538
539 wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckConnectAndPing()
540 {
541 NetConnection conn;
542
543 // first try connecting - faster
544 conn = CheckConnect();
545 if ( conn == Net_Unknown )
546 {
547 // try pinging too
548 conn = CheckPing();
549 }
550
551 return conn;
552 }
553
554 wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckConnect()
555 {
556 // second method: try to connect to a well known host:
557 // This can be used under Win 9x, too!
558 struct hostent *hp;
559 struct sockaddr_in serv_addr;
560
561 if((hp = gethostbyname(m_BeaconHost.mb_str())) == NULL)
562 return Net_No; // no DNS no net
563
564 serv_addr.sin_family = hp->h_addrtype;
565 memcpy(&serv_addr.sin_addr,hp->h_addr, hp->h_length);
566 serv_addr.sin_port = htons(m_BeaconPort);
567
568 int sockfd;
569 if( ( sockfd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
570 {
571 return Net_Unknown; // no info
572 }
573
574 if( connect(sockfd, (struct sockaddr *) &serv_addr,
575 sizeof(serv_addr)) >= 0)
576 {
577 close(sockfd);
578 return Net_Connected; // we can connect, so we have a network!
579 }
580 else // failed to connect
581 {
582 if(errno == ENETUNREACH)
583 return Net_No; // network is unreachable
584 else
585 return Net_Unknown; // connect failed, but don't know why
586 }
587 }
588
589
590 int
591 wxDialUpManagerImpl::CheckProcNet()
592 {
593 // assume that the test doesn't work
594 int netDevice = NetDevice_Unknown;
595
596 #ifdef __LINUX__
597 if (wxFileExists(_T("/proc/net/route")))
598 {
599 // cannot use wxFile::Length because file doesn't support seeking, so
600 // use stdio directly
601 FILE *f = fopen("/proc/net/route", "rt");
602 if (f != NULL)
603 {
604 // now we know that we will find all devices we may have
605 netDevice = NetDevice_None;
606
607 char output[256];
608
609 while (fgets(output, 256, f) != NULL)
610 {
611 if ( strstr(output, "eth") ) // network card
612 {
613 netDevice |= NetDevice_LAN;
614 }
615 else if (strstr(output,"ppp") // ppp
616 || strstr(output,"sl") // slip
617 || strstr(output,"pl")) // plip
618 {
619 netDevice |= NetDevice_Modem;
620 }
621 }
622
623 fclose(f);
624 }
625 }
626 #endif // __LINUX__
627
628 return netDevice;
629 }
630
631
632 int
633 wxDialUpManagerImpl::CheckIfconfig()
634 {
635 #ifdef __VMS
636 m_CanUseIfconfig = 0;
637 return -1;
638 #else
639 // assume that the test doesn't work
640 int netDevice = NetDevice_Unknown;
641
642 // first time check for ifconfig location
643 if ( m_CanUseIfconfig == -1 ) // unknown
644 {
645 static const wxChar *ifconfigLocations[] =
646 {
647 _T("/sbin"), // Linux, FreeBSD, Darwin
648 _T("/usr/sbin"), // SunOS, Solaris, AIX, HP-UX
649 _T("/usr/etc"), // IRIX
650 _T("/etc"), // AIX 5
651 };
652
653 for ( size_t n = 0; n < WXSIZEOF(ifconfigLocations); n++ )
654 {
655 wxString path(ifconfigLocations[n]);
656 path << _T("/ifconfig");
657
658 if ( wxFileExists(path) )
659 {
660 m_IfconfigPath = path;
661 break;
662 }
663 }
664 }
665
666 if ( m_CanUseIfconfig != 0 ) // unknown or yes
667 {
668 wxLogNull ln; // suppress all error messages
669
670 wxASSERT_MSG( m_IfconfigPath.length(),
671 _T("can't use ifconfig if it wasn't found") );
672
673 wxString tmpfile = wxGetTempFileName( wxT("_wxdialuptest") );
674 wxString cmd = wxT("/bin/sh -c \'");
675 cmd << m_IfconfigPath;
676 #if defined(__AIX__) || \
677 defined(__OSF__) || \
678 defined(__SOLARIS__) || defined (__SUNOS__)
679 // need to add -a flag
680 cmd << wxT(" -a");
681 #elif defined(__LINUX__) || defined(__SGI__)
682 // nothing to be added to ifconfig
683 #elif defined(__FREEBSD__) || defined(__DARWIN__)
684 // add -l flag
685 cmd << wxT(" -l");
686 #elif defined(__HPUX__)
687 // VZ: a wild guess (but without it, ifconfig fails completely)
688 cmd << wxT(" ppp0");
689 #else
690 #if defined(__GNUG__)
691 #warning "No ifconfig information for this OS."
692 #else
693 #pragma warning "No ifconfig information for this OS."
694 #endif
695
696 m_CanUseIfconfig = 0;
697 return -1;
698 #endif
699 cmd << wxT(" >") << tmpfile << wxT('\'');
700 /* I tried to add an option to wxExecute() to not close stdout,
701 so we could let ifconfig write directly to the tmpfile, but
702 this does not work. That should be faster, as it doesn´t call
703 the shell first. I have no idea why. :-( (KB) */
704 if ( wxExecute(cmd,true /* sync */) == 0 )
705 {
706 m_CanUseIfconfig = 1;
707 wxFFile file;
708 if( file.Open(tmpfile) )
709 {
710 wxString output;
711 if ( file.ReadAll(&output) )
712 {
713 // FIXME shouldn't we grep for "^ppp"? (VZ)
714
715 bool hasModem = false,
716 hasLAN = false;
717
718 #if defined(__SOLARIS__) || defined (__SUNOS__)
719 // dialup device under SunOS/Solaris
720 hasModem = strstr(output.fn_str(),"ipdptp") != (char *)NULL;
721 hasLAN = strstr(output.fn_str(), "hme") != (char *)NULL;
722 #elif defined(__LINUX__) || defined (__FREEBSD__)
723 hasModem = strstr(output.fn_str(),"ppp") // ppp
724 || strstr(output.fn_str(),"sl") // slip
725 || strstr(output.fn_str(),"pl"); // plip
726 hasLAN = strstr(output.fn_str(), "eth") != NULL;
727 #elif defined(__SGI__) // IRIX
728 hasModem = strstr(output.fn_str(), "ppp") != NULL; // PPP
729 #elif defined(__HPUX__)
730 // if could run ifconfig on interface, then it exists
731 hasModem = true;
732 #endif
733
734 netDevice = NetDevice_None;
735 if ( hasModem )
736 netDevice |= NetDevice_Modem;
737 if ( hasLAN )
738 netDevice |= NetDevice_LAN;
739 }
740 //else: error reading the file
741 }
742 //else: error opening the file
743 }
744 else // could not run ifconfig correctly
745 {
746 m_CanUseIfconfig = 0; // don´t try again
747 }
748
749 (void) wxRemoveFile(tmpfile);
750 }
751
752 return netDevice;
753 #endif
754 }
755
756 wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckPing()
757 {
758 // First time check for ping location. We only use the variant
759 // which does not take arguments, a la GNU.
760 if(m_CanUsePing == -1) // unknown
761 {
762 #ifdef __VMS
763 if (wxFileExists( wxT("SYS$SYSTEM:TCPIP$PING.EXE") ))
764 m_PingPath = wxT("$SYS$SYSTEM:TCPIP$PING");
765 #elif defined(__AIX__)
766 m_PingPath = _T("/etc/ping");
767 #elif defined(__SGI__)
768 m_PingPath = _T("/usr/etc/ping");
769 #else
770 if (wxFileExists( wxT("/bin/ping") ))
771 m_PingPath = wxT("/bin/ping");
772 else if (wxFileExists( wxT("/usr/sbin/ping") ))
773 m_PingPath = wxT("/usr/sbin/ping");
774 #endif
775 if (!m_PingPath)
776 {
777 m_CanUsePing = 0;
778 }
779 }
780
781 if(! m_CanUsePing)
782 {
783 // we didn't find ping
784 return Net_Unknown;
785 }
786
787 wxLogNull ln; // suppress all error messages
788 wxASSERT(m_PingPath.length());
789 wxString cmd;
790 cmd << m_PingPath << wxT(' ');
791 #if defined(__SOLARIS__) || defined (__SUNOS__)
792 // nothing to add to ping command
793 #elif defined(__AIX__) || \
794 defined (__BSD__) || \
795 defined(__LINUX__) || \
796 defined(__OSF__) || \
797 defined(__SGI__) || \
798 defined(__VMS)
799 cmd << wxT("-c 1 "); // only ping once
800 #elif defined(__HPUX__)
801 cmd << wxT("64 1 "); // only ping once (need also specify the packet size)
802 #else
803 #if defined(__GNUG__)
804 #warning "No Ping information for this OS."
805 #else
806 #pragma warning "No Ping information for this OS."
807 #endif
808
809 m_CanUsePing = 0;
810 return Net_Unknown;
811 #endif
812 cmd << m_BeaconHost;
813 if(wxExecute(cmd, true /* sync */) == 0)
814 return Net_Connected;
815 else
816 return Net_No;
817 }
818
819 /* static */
820 wxDialUpManager *wxDialUpManager::Create()
821 {
822 return new wxDialUpManagerImpl;
823 }
824
825 #endif // wxUSE_DIALUP_MANAGER