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