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