]>
Commit | Line | Data |
---|---|---|
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 | // Copyright: (c) Karsten Ballüder | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // for compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_DIALUP_MANAGER | |
15 | ||
16 | #include "wx/dialup.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/string.h" | |
20 | #include "wx/intl.h" | |
21 | #include "wx/log.h" | |
22 | #include "wx/event.h" | |
23 | #include "wx/app.h" | |
24 | #include "wx/utils.h" | |
25 | #include "wx/timer.h" | |
26 | #endif // !PCH | |
27 | ||
28 | #include "wx/filename.h" | |
29 | #include "wx/ffile.h" | |
30 | #include "wx/process.h" | |
31 | #include "wx/crt.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 | wxDEFINE_EVENT( wxEVT_DIALUP_CONNECTED, wxDialUpEvent ); | |
46 | wxDEFINE_EVENT( wxEVT_DIALUP_DISCONNECTED, wxDialUpEvent ); | |
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 | virtual ~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(wxT("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 = wxT("/usr/etc/ppp"); | |
295 | #elif defined(__LINUX__) | |
296 | // default values for Debian/GNU linux | |
297 | m_ConnectCommand = wxT("pon"); | |
298 | m_HangUpCommand = wxT("poff"); | |
299 | #endif | |
300 | ||
301 | wxChar * dial = wxGetenv(wxT("WXDIALUP_DIALCMD")); | |
302 | wxChar * hup = wxGetenv(wxT("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 | wxDELETE(m_DialProcess); | |
339 | return false; | |
340 | } | |
341 | else | |
342 | return true; | |
343 | } | |
344 | else | |
345 | return wxExecute(cmd, /* sync */ true) == 0; | |
346 | } | |
347 | ||
348 | bool wxDialUpManagerImpl::HangUp() | |
349 | { | |
350 | if(m_IsOnline == Net_No) | |
351 | return false; | |
352 | if(IsDialing()) | |
353 | { | |
354 | wxLogError(_("Already dialling ISP.")); | |
355 | return false; | |
356 | } | |
357 | wxString cmd; | |
358 | if(m_HangUpCommand.Find(wxT("%s"))) | |
359 | cmd.Printf(m_HangUpCommand,m_ISPname.c_str(), m_DialProcess); | |
360 | else | |
361 | cmd = m_HangUpCommand; | |
362 | return wxExecute(cmd, /* sync */ true) == 0; | |
363 | } | |
364 | ||
365 | ||
366 | bool wxDialUpManagerImpl::CancelDialing() | |
367 | { | |
368 | if(! IsDialing()) | |
369 | return false; | |
370 | return kill(m_DialPId, SIGTERM) > 0; | |
371 | } | |
372 | ||
373 | bool wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds) | |
374 | { | |
375 | DisableAutoCheckOnlineStatus(); | |
376 | m_timer = new AutoCheckTimer(this); | |
377 | bool rc = m_timer->Start(nSeconds*1000); | |
378 | if(! rc) | |
379 | { | |
380 | wxDELETE(m_timer); | |
381 | } | |
382 | return rc; | |
383 | } | |
384 | ||
385 | void wxDialUpManagerImpl::DisableAutoCheckOnlineStatus() | |
386 | { | |
387 | if(m_timer != NULL) | |
388 | { | |
389 | m_timer->Stop(); | |
390 | wxDELETE(m_timer); | |
391 | } | |
392 | } | |
393 | ||
394 | ||
395 | void wxDialUpManagerImpl::SetWellKnownHost(const wxString& hostname, int portno) | |
396 | { | |
397 | if( hostname.empty() ) | |
398 | { | |
399 | m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST; | |
400 | m_BeaconPort = 80; | |
401 | return; | |
402 | } | |
403 | ||
404 | // does hostname contain a port number? | |
405 | wxString port = hostname.After(wxT(':')); | |
406 | if( !port.empty() ) | |
407 | { | |
408 | m_BeaconHost = hostname.Before(wxT(':')); | |
409 | m_BeaconPort = wxAtoi(port); | |
410 | } | |
411 | else | |
412 | { | |
413 | m_BeaconHost = hostname; | |
414 | m_BeaconPort = portno; | |
415 | } | |
416 | } | |
417 | ||
418 | ||
419 | void wxDialUpManagerImpl::CheckStatus(bool fromAsync) const | |
420 | { | |
421 | // This function calls the CheckStatusInternal() helper function | |
422 | // which is OS - specific and then sends the events. | |
423 | ||
424 | NetConnection oldIsOnline = m_IsOnline; | |
425 | ( /* non-const */ (wxDialUpManagerImpl *)this)->CheckStatusInternal(); | |
426 | ||
427 | // now send the events as appropriate: i.e. if the status changed and | |
428 | // if we're in defined state | |
429 | if(m_IsOnline != oldIsOnline | |
430 | && m_IsOnline != Net_Unknown | |
431 | && oldIsOnline != Net_Unknown ) | |
432 | { | |
433 | wxDialUpEvent event(m_IsOnline == Net_Connected, ! fromAsync); | |
434 | (void)wxTheApp->ProcessEvent(event); | |
435 | } | |
436 | } | |
437 | ||
438 | /* | |
439 | We first try to find out if ppp interface is active. If it is, we assume | |
440 | that we're online but don't have a permanent connection (this is false if a | |
441 | networked machine uses modem to connect to somewhere else, but we can't do | |
442 | anything in this case anyhow). | |
443 | ||
444 | If no ppp interface is detected, we check for eth interface. If it is | |
445 | found, we check that we can, indeed, connect to an Internet host. The logic | |
446 | here is that connection check should be fast enough in this case and we | |
447 | don't want to give false positives in a (common) case of a machine on a LAN | |
448 | which is not connected to the outside. | |
449 | ||
450 | If we didn't find either ppp or eth interfaces, we stop here and decide | |
451 | that we're connected. However, if couldn't check for this, we try to ping a | |
452 | remote host just in case. | |
453 | ||
454 | NB1: Checking for the interface presence can be done in 2 ways | |
455 | a) reading /proc/net/dev under Linux | |
456 | b) spawning ifconfig under any OS | |
457 | ||
458 | The first method is faster but only works under Linux. | |
459 | ||
460 | NB2: pinging, actually, means that we first try to connect "manually" to | |
461 | a port on remove machine and if it fails, we run ping. | |
462 | */ | |
463 | ||
464 | void wxDialUpManagerImpl::CheckStatusInternal() | |
465 | { | |
466 | m_IsOnline = Net_Unknown; | |
467 | ||
468 | // first do quick checks to determine what kind of network devices do we | |
469 | // have | |
470 | int netDeviceType = CheckProcNet(); | |
471 | if ( netDeviceType == NetDevice_Unknown ) | |
472 | { | |
473 | // nothing found, try ifconfig too | |
474 | netDeviceType = CheckIfconfig(); | |
475 | } | |
476 | ||
477 | switch ( netDeviceType ) | |
478 | { | |
479 | case NetDevice_None: | |
480 | // no network devices, no connection | |
481 | m_IsOnline = Net_No; | |
482 | break; | |
483 | ||
484 | case NetDevice_LAN: | |
485 | // we still do ping to confirm that we're connected but we only do | |
486 | // it once and hope that the purpose of the network card (i.e. | |
487 | // whether it used for connecting to the Internet or just to a | |
488 | // LAN) won't change during the program lifetime | |
489 | if ( m_connCard == Net_Unknown ) | |
490 | { | |
491 | m_connCard = CheckConnectAndPing(); | |
492 | } | |
493 | m_IsOnline = m_connCard; | |
494 | break; | |
495 | ||
496 | case NetDevice_Unknown: | |
497 | // try to ping just in case | |
498 | m_IsOnline = CheckConnectAndPing(); | |
499 | break; | |
500 | ||
501 | case NetDevice_LAN + NetDevice_Modem: | |
502 | case NetDevice_Modem: | |
503 | // assume we're connected | |
504 | m_IsOnline = Net_Connected; | |
505 | break; | |
506 | ||
507 | default: | |
508 | wxFAIL_MSG(wxT("Unexpected netDeviceType")); | |
509 | } | |
510 | } | |
511 | ||
512 | bool wxDialUpManagerImpl::IsAlwaysOnline() const | |
513 | { | |
514 | wxDialUpManagerImpl *self = wxConstCast(this, wxDialUpManagerImpl); | |
515 | ||
516 | int netDeviceType = self->CheckProcNet(); | |
517 | if ( netDeviceType == NetDevice_Unknown ) | |
518 | { | |
519 | // nothing found, try ifconfig too | |
520 | netDeviceType = self->CheckIfconfig(); | |
521 | } | |
522 | ||
523 | if ( netDeviceType == NetDevice_Unknown ) | |
524 | { | |
525 | // this is the only thing we can do unfortunately... | |
526 | self->HangUp(); | |
527 | return IsOnline(); | |
528 | } | |
529 | else | |
530 | { | |
531 | // we are only permanently online if we have a network card | |
532 | return (netDeviceType & NetDevice_LAN) != 0; | |
533 | } | |
534 | } | |
535 | ||
536 | wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckConnectAndPing() | |
537 | { | |
538 | NetConnection conn; | |
539 | ||
540 | // first try connecting - faster | |
541 | conn = CheckConnect(); | |
542 | if ( conn == Net_Unknown ) | |
543 | { | |
544 | // try pinging too | |
545 | conn = CheckPing(); | |
546 | } | |
547 | ||
548 | return conn; | |
549 | } | |
550 | ||
551 | wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckConnect() | |
552 | { | |
553 | // second method: try to connect to a well known host: | |
554 | // This can be used under Win 9x, too! | |
555 | struct hostent *hp; | |
556 | struct sockaddr_in serv_addr; | |
557 | ||
558 | if((hp = gethostbyname(m_BeaconHost.mb_str())) == NULL) | |
559 | return Net_No; // no DNS no net | |
560 | ||
561 | serv_addr.sin_family = hp->h_addrtype; | |
562 | memcpy(&serv_addr.sin_addr,hp->h_addr, hp->h_length); | |
563 | serv_addr.sin_port = htons(m_BeaconPort); | |
564 | ||
565 | int sockfd; | |
566 | if( ( sockfd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) | |
567 | { | |
568 | return Net_Unknown; // no info | |
569 | } | |
570 | ||
571 | if( connect(sockfd, (struct sockaddr *) &serv_addr, | |
572 | sizeof(serv_addr)) >= 0) | |
573 | { | |
574 | close(sockfd); | |
575 | return Net_Connected; // we can connect, so we have a network! | |
576 | } | |
577 | else // failed to connect | |
578 | { | |
579 | #ifdef ENETUNREACH | |
580 | if(errno == ENETUNREACH) | |
581 | return Net_No; // network is unreachable | |
582 | else | |
583 | #endif | |
584 | return Net_Unknown; // connect failed, but don't know why | |
585 | } | |
586 | } | |
587 | ||
588 | ||
589 | int | |
590 | wxDialUpManagerImpl::CheckProcNet() | |
591 | { | |
592 | // assume that the test doesn't work | |
593 | int netDevice = NetDevice_Unknown; | |
594 | ||
595 | #ifdef __LINUX__ | |
596 | if (wxFileExists(wxT("/proc/net/route"))) | |
597 | { | |
598 | // cannot use wxFile::Length because file doesn't support seeking, so | |
599 | // use stdio directly | |
600 | FILE *f = fopen("/proc/net/route", "rt"); | |
601 | if (f != NULL) | |
602 | { | |
603 | // now we know that we will find all devices we may have | |
604 | netDevice = NetDevice_None; | |
605 | ||
606 | char output[256]; | |
607 | ||
608 | while (fgets(output, 256, f) != NULL) | |
609 | { | |
610 | // Test for the known network interface names | |
611 | if ( strstr(output, "eth") | |
612 | || strstr(output, "wlan") | |
613 | || strstr(output, "ath") ) | |
614 | { | |
615 | netDevice |= NetDevice_LAN; | |
616 | } | |
617 | else if (strstr(output,"ppp") // ppp | |
618 | || strstr(output,"sl") // slip | |
619 | || strstr(output,"pl")) // plip | |
620 | { | |
621 | netDevice |= NetDevice_Modem; | |
622 | } | |
623 | } | |
624 | ||
625 | fclose(f); | |
626 | } | |
627 | } | |
628 | #endif // __LINUX__ | |
629 | ||
630 | return netDevice; | |
631 | } | |
632 | ||
633 | ||
634 | int | |
635 | wxDialUpManagerImpl::CheckIfconfig() | |
636 | { | |
637 | #ifdef __VMS | |
638 | m_CanUseIfconfig = 0; | |
639 | return -1; | |
640 | #else | |
641 | // assume that the test doesn't work | |
642 | int netDevice = NetDevice_Unknown; | |
643 | ||
644 | // first time check for ifconfig location | |
645 | if ( m_CanUseIfconfig == -1 ) // unknown | |
646 | { | |
647 | static const wxChar *const ifconfigLocations[] = | |
648 | { | |
649 | wxT("/sbin"), // Linux, FreeBSD, Darwin | |
650 | wxT("/usr/sbin"), // SunOS, Solaris, AIX, HP-UX | |
651 | wxT("/usr/etc"), // IRIX | |
652 | wxT("/etc"), // AIX 5 | |
653 | }; | |
654 | ||
655 | for ( size_t n = 0; n < WXSIZEOF(ifconfigLocations); n++ ) | |
656 | { | |
657 | wxString path(ifconfigLocations[n]); | |
658 | path << wxT("/ifconfig"); | |
659 | ||
660 | if ( wxFileExists(path) ) | |
661 | { | |
662 | m_IfconfigPath = path; | |
663 | break; | |
664 | } | |
665 | } | |
666 | } | |
667 | ||
668 | if ( m_CanUseIfconfig != 0 ) // unknown or yes | |
669 | { | |
670 | wxLogNull ln; // suppress all error messages | |
671 | ||
672 | wxASSERT_MSG( !m_IfconfigPath.empty(), | |
673 | wxT("can't use ifconfig if it wasn't found") ); | |
674 | ||
675 | wxString tmpfile = wxFileName::CreateTempFileName( wxT("_wxdialuptest") ); | |
676 | wxString cmd = wxT("/bin/sh -c \'"); | |
677 | cmd << m_IfconfigPath; | |
678 | #if defined(__AIX__) || \ | |
679 | defined(__NETBSD__) || \ | |
680 | defined(__OSF__) || \ | |
681 | defined(__SOLARIS__) || defined (__SUNOS__) | |
682 | // need to add -a flag | |
683 | cmd << wxT(" -a"); | |
684 | #elif defined(__LINUX__) || defined(__SGI__) || defined(__OPENBSD__) | |
685 | // nothing to be added to ifconfig | |
686 | #elif defined(__FREEBSD__) || defined(__DARWIN__) || defined(__QNX__) | |
687 | // add -l flag | |
688 | cmd << wxT(" -l"); | |
689 | #elif defined(__HPUX__) | |
690 | // VZ: a wild guess (but without it, ifconfig fails completely) | |
691 | cmd << wxT(" ppp0"); | |
692 | #else | |
693 | #if defined(__GNUG__) | |
694 | #warning "No ifconfig information for this OS." | |
695 | #else | |
696 | #pragma warning "No ifconfig information for this OS." | |
697 | #endif | |
698 | ||
699 | m_CanUseIfconfig = 0; | |
700 | return -1; | |
701 | #endif | |
702 | cmd << wxT(" >") << tmpfile << wxT('\''); | |
703 | /* I tried to add an option to wxExecute() to not close stdout, | |
704 | so we could let ifconfig write directly to the tmpfile, but | |
705 | this does not work. That should be faster, as it doesn't call | |
706 | the shell first. I have no idea why. :-( (KB) */ | |
707 | if ( wxExecute(cmd,true /* sync */) == 0 ) | |
708 | { | |
709 | m_CanUseIfconfig = 1; | |
710 | wxFFile file; | |
711 | if( file.Open(tmpfile) ) | |
712 | { | |
713 | wxString output; | |
714 | if ( file.ReadAll(&output) ) | |
715 | { | |
716 | // FIXME shouldn't we grep for "^ppp"? (VZ) | |
717 | ||
718 | bool hasModem = false, | |
719 | hasLAN = false; | |
720 | ||
721 | #if defined(__SOLARIS__) || defined (__SUNOS__) | |
722 | // dialup device under SunOS/Solaris | |
723 | hasModem = strstr(output.fn_str(),"ipdptp") != NULL; | |
724 | hasLAN = strstr(output.fn_str(), "hme") != NULL; | |
725 | #elif defined(__LINUX__) || defined (__FREEBSD__) || defined (__QNX__) || \ | |
726 | defined(__OPENBSD__) | |
727 | hasModem = strstr(output.fn_str(),"ppp") // ppp | |
728 | || strstr(output.fn_str(),"sl") // slip | |
729 | || strstr(output.fn_str(),"pl"); // plip | |
730 | hasLAN = strstr(output.fn_str(), "eth") != NULL; | |
731 | #elif defined(__SGI__) // IRIX | |
732 | hasModem = strstr(output.fn_str(), "ppp") != NULL; // PPP | |
733 | #elif defined(__HPUX__) | |
734 | // if could run ifconfig on interface, then it exists | |
735 | hasModem = true; | |
736 | #endif | |
737 | ||
738 | netDevice = NetDevice_None; | |
739 | if ( hasModem ) | |
740 | netDevice |= NetDevice_Modem; | |
741 | if ( hasLAN ) | |
742 | netDevice |= NetDevice_LAN; | |
743 | } | |
744 | //else: error reading the file | |
745 | } | |
746 | //else: error opening the file | |
747 | } | |
748 | else // could not run ifconfig correctly | |
749 | { | |
750 | m_CanUseIfconfig = 0; // don't try again | |
751 | } | |
752 | ||
753 | (void) wxRemoveFile(tmpfile); | |
754 | } | |
755 | ||
756 | return netDevice; | |
757 | #endif | |
758 | } | |
759 | ||
760 | wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckPing() | |
761 | { | |
762 | // First time check for ping location. We only use the variant | |
763 | // which does not take arguments, a la GNU. | |
764 | if(m_CanUsePing == -1) // unknown | |
765 | { | |
766 | #ifdef __VMS | |
767 | if (wxFileExists( wxT("SYS$SYSTEM:TCPIP$PING.EXE") )) | |
768 | m_PingPath = wxT("$SYS$SYSTEM:TCPIP$PING"); | |
769 | #elif defined(__AIX__) | |
770 | m_PingPath = wxT("/etc/ping"); | |
771 | #elif defined(__SGI__) | |
772 | m_PingPath = wxT("/usr/etc/ping"); | |
773 | #else | |
774 | if (wxFileExists( wxT("/bin/ping") )) | |
775 | m_PingPath = wxT("/bin/ping"); | |
776 | else if (wxFileExists( wxT("/usr/sbin/ping") )) | |
777 | m_PingPath = wxT("/usr/sbin/ping"); | |
778 | #endif | |
779 | if (!m_PingPath) | |
780 | { | |
781 | m_CanUsePing = 0; | |
782 | } | |
783 | } | |
784 | ||
785 | if(! m_CanUsePing) | |
786 | { | |
787 | // we didn't find ping | |
788 | return Net_Unknown; | |
789 | } | |
790 | ||
791 | wxLogNull ln; // suppress all error messages | |
792 | wxASSERT(m_PingPath.length()); | |
793 | wxString cmd; | |
794 | cmd << m_PingPath << wxT(' '); | |
795 | #if defined(__SOLARIS__) || defined (__SUNOS__) | |
796 | // nothing to add to ping command | |
797 | #elif defined(__AIX__) || \ | |
798 | defined (__BSD__) || \ | |
799 | defined(__LINUX__) || \ | |
800 | defined(__OSF__) || \ | |
801 | defined(__SGI__) || \ | |
802 | defined(__VMS) || \ | |
803 | defined(__QNX__) | |
804 | cmd << wxT("-c 1 "); // only ping once | |
805 | #elif defined(__HPUX__) | |
806 | cmd << wxT("64 1 "); // only ping once (need also specify the packet size) | |
807 | #else | |
808 | #if defined(__GNUG__) | |
809 | #warning "No Ping information for this OS." | |
810 | #else | |
811 | #pragma warning "No Ping information for this OS." | |
812 | #endif | |
813 | ||
814 | m_CanUsePing = 0; | |
815 | return Net_Unknown; | |
816 | #endif | |
817 | cmd << m_BeaconHost; | |
818 | if(wxExecute(cmd, true /* sync */) == 0) | |
819 | return Net_Connected; | |
820 | else | |
821 | return Net_No; | |
822 | } | |
823 | ||
824 | /* static */ | |
825 | wxDialUpManager *wxDialUpManager::Create() | |
826 | { | |
827 | return new wxDialUpManagerImpl; | |
828 | } | |
829 | ||
830 | #endif // wxUSE_DIALUP_MANAGER |