]>
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 | #include <sys/socket.h> | |
39 | #include <netdb.h> | |
40 | #include <netinet/in.h> | |
41 | #include <arpa/inet.h> | |
42 | #include <errno.h> | |
43 | ||
44 | wxDEFINE_EVENT( wxEVT_DIALUP_CONNECTED, wxDialUpEvent ); | |
45 | wxDEFINE_EVENT( wxEVT_DIALUP_DISCONNECTED, wxDialUpEvent ); | |
46 | ||
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 | ||
70 | class WXDLLEXPORT wxDialUpManagerImpl : public wxDialUpManager | |
71 | { | |
72 | public: | |
73 | wxDialUpManagerImpl(); | |
74 | virtual ~wxDialUpManagerImpl(); | |
75 | ||
76 | /** Could the dialup manager be initialized correctly? If this function | |
77 | returns false, no other functions will work neither, so it's a good idea | |
78 | to call this function and check its result before calling any other | |
79 | wxDialUpManager methods. | |
80 | */ | |
81 | virtual bool IsOk() const | |
82 | { return true; } | |
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 | |
86 | true on success or false on failure and logs the appropriate error | |
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), | |
94 | const wxString& WXUNUSED(password), | |
95 | bool async); | |
96 | ||
97 | // Hang up the currently active dial up connection. | |
98 | virtual bool HangUp(); | |
99 | ||
100 | // returns true if the computer is connected to the network: under Windows, | |
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 | { | |
105 | CheckStatus(); | |
106 | return m_IsOnline == Net_Connected; | |
107 | } | |
108 | ||
109 | // do we have a constant net connection? | |
110 | virtual bool IsAlwaysOnline() const; | |
111 | ||
112 | // returns true if (async) dialing is in progress | |
113 | virtual bool IsDialing() const | |
114 | { return m_DialProcess != NULL; } | |
115 | ||
116 | // cancel dialing the number initiated with Dial(async = true) | |
117 | // NB: this won't result in DISCONNECTED event being sent | |
118 | virtual bool CancelDialing(); | |
119 | ||
120 | size_t GetISPNames(class wxArrayString &) const | |
121 | { return 0; } | |
122 | ||
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. | |
127 | virtual void SetOnlineStatus(bool isOnline = true) | |
128 | { m_IsOnline = isOnline ? Net_Connected : Net_No; } | |
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 | // | |
139 | // Returns false if couldn't set up automatic check for online status. | |
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 | ||
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 | ||
177 | private: | |
178 | // the current status | |
179 | NetConnection m_IsOnline; | |
180 | ||
181 | // the connection we have with the network card | |
182 | NetConnection m_connCard; | |
183 | ||
184 | // Can we use ifconfig to list active devices? | |
185 | int m_CanUseIfconfig; | |
186 | ||
187 | // The path to ifconfig | |
188 | wxString m_IfconfigPath; | |
189 | ||
190 | // Can we use ping to find hosts? | |
191 | int m_CanUsePing; | |
192 | // The path to ping program | |
193 | wxString m_PingPath; | |
194 | ||
195 | // beacon host: | |
196 | wxString m_BeaconHost; | |
197 | // beacon host portnumber for connect: | |
198 | int m_BeaconPort; | |
199 | ||
200 | // command to connect to network | |
201 | wxString m_ConnectCommand; | |
202 | // command to hang up | |
203 | wxString m_HangUpCommand; | |
204 | // name of ISP | |
205 | wxString m_ISPname; | |
206 | // a timer for regular testing | |
207 | class AutoCheckTimer *m_timer; | |
208 | friend class AutoCheckTimer; | |
209 | ||
210 | // a wxProcess for dialling in background | |
211 | class wxDialProcess *m_DialProcess; | |
212 | // pid of dial process | |
213 | int m_DialPId; | |
214 | friend class wxDialProcess; | |
215 | ||
216 | // determine status | |
217 | void CheckStatus(bool fromAsync = false) const; | |
218 | ||
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(); | |
232 | ||
233 | // pings a host | |
234 | NetConnection CheckPing(); | |
235 | ||
236 | // check by connecting to host on given port. | |
237 | NetConnection CheckConnect(); | |
238 | }; | |
239 | ||
240 | ||
241 | class AutoCheckTimer : public wxTimer | |
242 | { | |
243 | public: | |
244 | AutoCheckTimer(wxDialUpManagerImpl *dupman) | |
245 | { | |
246 | m_dupman = dupman; | |
247 | } | |
248 | ||
249 | virtual void Notify() | |
250 | { | |
251 | wxLogTrace(wxT("dialup"), wxT("Checking dial up network status.")); | |
252 | ||
253 | m_dupman->CheckStatus(); | |
254 | } | |
255 | ||
256 | public: | |
257 | wxDialUpManagerImpl *m_dupman; | |
258 | }; | |
259 | ||
260 | class wxDialProcess : public wxProcess | |
261 | { | |
262 | public: | |
263 | wxDialProcess(wxDialUpManagerImpl *dupman) | |
264 | { | |
265 | m_DupMan = dupman; | |
266 | } | |
267 | void Disconnect() { m_DupMan = NULL; } | |
268 | virtual void OnTerminate(int WXUNUSED(pid), int WXUNUSED(status)) | |
269 | { | |
270 | if(m_DupMan) | |
271 | { | |
272 | m_DupMan->m_DialProcess = NULL; | |
273 | m_DupMan->CheckStatus(true); | |
274 | } | |
275 | } | |
276 | private: | |
277 | wxDialUpManagerImpl *m_DupMan; | |
278 | }; | |
279 | ||
280 | ||
281 | wxDialUpManagerImpl::wxDialUpManagerImpl() | |
282 | { | |
283 | m_IsOnline = | |
284 | m_connCard = Net_Unknown; | |
285 | m_DialProcess = NULL; | |
286 | m_timer = NULL; | |
287 | m_CanUseIfconfig = -1; // unknown | |
288 | m_CanUsePing = -1; // unknown | |
289 | m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST; | |
290 | m_BeaconPort = 80; | |
291 | ||
292 | #ifdef __SGI__ | |
293 | m_ConnectCommand = wxT("/usr/etc/ppp"); | |
294 | #elif defined(__LINUX__) | |
295 | // default values for Debian/GNU linux | |
296 | m_ConnectCommand = wxT("pon"); | |
297 | m_HangUpCommand = wxT("poff"); | |
298 | #endif | |
299 | ||
300 | wxChar * dial = wxGetenv(wxT("WXDIALUP_DIALCMD")); | |
301 | wxChar * hup = wxGetenv(wxT("WXDIALUP_HUPCMD")); | |
302 | SetConnectCommand(dial ? wxString(dial) : m_ConnectCommand, | |
303 | hup ? wxString(hup) : m_HangUpCommand); | |
304 | } | |
305 | ||
306 | wxDialUpManagerImpl::~wxDialUpManagerImpl() | |
307 | { | |
308 | if(m_timer) delete m_timer; | |
309 | if(m_DialProcess) | |
310 | { | |
311 | m_DialProcess->Disconnect(); | |
312 | m_DialProcess->Detach(); | |
313 | } | |
314 | } | |
315 | ||
316 | bool | |
317 | wxDialUpManagerImpl::Dial(const wxString &isp, | |
318 | const wxString & WXUNUSED(username), | |
319 | const wxString & WXUNUSED(password), | |
320 | bool async) | |
321 | { | |
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; | |
330 | ||
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 | { | |
337 | wxDELETE(m_DialProcess); | |
338 | return false; | |
339 | } | |
340 | else | |
341 | return true; | |
342 | } | |
343 | else | |
344 | return wxExecute(cmd, /* sync */ true) == 0; | |
345 | } | |
346 | ||
347 | bool wxDialUpManagerImpl::HangUp() | |
348 | { | |
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; | |
362 | } | |
363 | ||
364 | ||
365 | bool wxDialUpManagerImpl::CancelDialing() | |
366 | { | |
367 | if(! IsDialing()) | |
368 | return false; | |
369 | return kill(m_DialPId, SIGTERM) > 0; | |
370 | } | |
371 | ||
372 | bool wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds) | |
373 | { | |
374 | DisableAutoCheckOnlineStatus(); | |
375 | m_timer = new AutoCheckTimer(this); | |
376 | bool rc = m_timer->Start(nSeconds*1000); | |
377 | if(! rc) | |
378 | { | |
379 | wxDELETE(m_timer); | |
380 | } | |
381 | return rc; | |
382 | } | |
383 | ||
384 | void wxDialUpManagerImpl::DisableAutoCheckOnlineStatus() | |
385 | { | |
386 | if(m_timer != NULL) | |
387 | { | |
388 | m_timer->Stop(); | |
389 | wxDELETE(m_timer); | |
390 | } | |
391 | } | |
392 | ||
393 | ||
394 | void wxDialUpManagerImpl::SetWellKnownHost(const wxString& hostname, int portno) | |
395 | { | |
396 | if( hostname.empty() ) | |
397 | { | |
398 | m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST; | |
399 | m_BeaconPort = 80; | |
400 | return; | |
401 | } | |
402 | ||
403 | // does hostname contain a port number? | |
404 | wxString port = hostname.After(wxT(':')); | |
405 | if( !port.empty() ) | |
406 | { | |
407 | m_BeaconHost = hostname.Before(wxT(':')); | |
408 | m_BeaconPort = wxAtoi(port); | |
409 | } | |
410 | else | |
411 | { | |
412 | m_BeaconHost = hostname; | |
413 | m_BeaconPort = portno; | |
414 | } | |
415 | } | |
416 | ||
417 | ||
418 | void wxDialUpManagerImpl::CheckStatus(bool fromAsync) const | |
419 | { | |
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 | } | |
435 | } | |
436 | ||
437 | /* | |
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. | |
452 | ||
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 | |
456 | ||
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. | |
461 | */ | |
462 | ||
463 | void wxDialUpManagerImpl::CheckStatusInternal() | |
464 | { | |
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: | |
507 | wxFAIL_MSG(wxT("Unexpected netDeviceType")); | |
508 | } | |
509 | } | |
510 | ||
511 | bool 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 | ||
535 | wxDialUpManagerImpl::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 | ||
550 | wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckConnect() | |
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) | |
558 | return Net_No; // no DNS no net | |
559 | ||
560 | serv_addr.sin_family = hp->h_addrtype; | |
561 | memcpy(&serv_addr.sin_addr,hp->h_addr, hp->h_length); | |
562 | serv_addr.sin_port = htons(m_BeaconPort); | |
563 | ||
564 | int sockfd; | |
565 | if( ( sockfd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) | |
566 | { | |
567 | return Net_Unknown; // no info | |
568 | } | |
569 | ||
570 | if( connect(sockfd, (struct sockaddr *) &serv_addr, | |
571 | sizeof(serv_addr)) >= 0) | |
572 | { | |
573 | close(sockfd); | |
574 | return Net_Connected; // we can connect, so we have a network! | |
575 | } | |
576 | else // failed to connect | |
577 | { | |
578 | #ifdef ENETUNREACH | |
579 | if(errno == ENETUNREACH) | |
580 | return Net_No; // network is unreachable | |
581 | else | |
582 | #endif | |
583 | return Net_Unknown; // connect failed, but don't know why | |
584 | } | |
585 | } | |
586 | ||
587 | ||
588 | int | |
589 | wxDialUpManagerImpl::CheckProcNet() | |
590 | { | |
591 | // assume that the test doesn't work | |
592 | int netDevice = NetDevice_Unknown; | |
593 | ||
594 | #ifdef __LINUX__ | |
595 | if (wxFileExists(wxT("/proc/net/route"))) | |
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 | { | |
609 | // Test for the known network interface names | |
610 | if ( strstr(output, "eth") | |
611 | || strstr(output, "wlan") | |
612 | || strstr(output, "ath") ) | |
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; | |
630 | } | |
631 | ||
632 | ||
633 | int | |
634 | wxDialUpManagerImpl::CheckIfconfig() | |
635 | { | |
636 | #ifdef __VMS | |
637 | m_CanUseIfconfig = 0; | |
638 | return -1; | |
639 | #else | |
640 | // assume that the test doesn't work | |
641 | int netDevice = NetDevice_Unknown; | |
642 | ||
643 | // first time check for ifconfig location | |
644 | if ( m_CanUseIfconfig == -1 ) // unknown | |
645 | { | |
646 | static const wxChar *const ifconfigLocations[] = | |
647 | { | |
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 | |
652 | }; | |
653 | ||
654 | for ( size_t n = 0; n < WXSIZEOF(ifconfigLocations); n++ ) | |
655 | { | |
656 | wxString path(ifconfigLocations[n]); | |
657 | path << wxT("/ifconfig"); | |
658 | ||
659 | if ( wxFileExists(path) ) | |
660 | { | |
661 | m_IfconfigPath = path; | |
662 | break; | |
663 | } | |
664 | } | |
665 | } | |
666 | ||
667 | if ( m_CanUseIfconfig != 0 ) // unknown or yes | |
668 | { | |
669 | wxLogNull ln; // suppress all error messages | |
670 | ||
671 | wxASSERT_MSG( !m_IfconfigPath.empty(), | |
672 | wxT("can't use ifconfig if it wasn't found") ); | |
673 | ||
674 | wxString tmpfile = wxFileName::CreateTempFileName( wxT("_wxdialuptest") ); | |
675 | wxString cmd = wxT("/bin/sh -c \'"); | |
676 | cmd << m_IfconfigPath; | |
677 | #if defined(__AIX__) || \ | |
678 | defined(__NETBSD__) || \ | |
679 | defined(__OSF__) || \ | |
680 | defined(__SOLARIS__) || defined (__SUNOS__) | |
681 | // need to add -a flag | |
682 | cmd << wxT(" -a"); | |
683 | #elif defined(__LINUX__) || defined(__SGI__) || defined(__OPENBSD__) | |
684 | // nothing to be added to ifconfig | |
685 | #elif defined(__FREEBSD__) || defined(__DARWIN__) || defined(__QNX__) | |
686 | // add -l flag | |
687 | cmd << wxT(" -l"); | |
688 | #elif defined(__HPUX__) | |
689 | // VZ: a wild guess (but without it, ifconfig fails completely) | |
690 | cmd << wxT(" ppp0"); | |
691 | #else | |
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; | |
699 | return -1; | |
700 | #endif | |
701 | cmd << wxT(" >") << tmpfile << wxT('\''); | |
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 | |
704 | this does not work. That should be faster, as it doesn't call | |
705 | the shell first. I have no idea why. :-( (KB) */ | |
706 | if ( wxExecute(cmd,true /* sync */) == 0 ) | |
707 | { | |
708 | m_CanUseIfconfig = 1; | |
709 | wxFFile file; | |
710 | if( file.Open(tmpfile) ) | |
711 | { | |
712 | wxString output; | |
713 | if ( file.ReadAll(&output) ) | |
714 | { | |
715 | // FIXME shouldn't we grep for "^ppp"? (VZ) | |
716 | ||
717 | bool hasModem = false, | |
718 | hasLAN = false; | |
719 | ||
720 | #if defined(__SOLARIS__) || defined (__SUNOS__) | |
721 | // dialup device under SunOS/Solaris | |
722 | hasModem = strstr(output.fn_str(),"ipdptp") != NULL; | |
723 | hasLAN = strstr(output.fn_str(), "hme") != NULL; | |
724 | #elif defined(__LINUX__) || defined (__FREEBSD__) || defined (__QNX__) || \ | |
725 | defined(__OPENBSD__) | |
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; | |
730 | #elif defined(__SGI__) // IRIX | |
731 | hasModem = strstr(output.fn_str(), "ppp") != NULL; // PPP | |
732 | #elif defined(__HPUX__) | |
733 | // if could run ifconfig on interface, then it exists | |
734 | hasModem = true; | |
735 | #endif | |
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 | |
744 | } | |
745 | //else: error opening the file | |
746 | } | |
747 | else // could not run ifconfig correctly | |
748 | { | |
749 | m_CanUseIfconfig = 0; // don't try again | |
750 | } | |
751 | ||
752 | (void) wxRemoveFile(tmpfile); | |
753 | } | |
754 | ||
755 | return netDevice; | |
756 | #endif | |
757 | } | |
758 | ||
759 | wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckPing() | |
760 | { | |
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 | { | |
765 | #ifdef __VMS | |
766 | if (wxFileExists( wxT("SYS$SYSTEM:TCPIP$PING.EXE") )) | |
767 | m_PingPath = wxT("$SYS$SYSTEM:TCPIP$PING"); | |
768 | #elif defined(__AIX__) | |
769 | m_PingPath = wxT("/etc/ping"); | |
770 | #elif defined(__SGI__) | |
771 | m_PingPath = wxT("/usr/etc/ping"); | |
772 | #else | |
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"); | |
777 | #endif | |
778 | if (!m_PingPath) | |
779 | { | |
780 | m_CanUsePing = 0; | |
781 | } | |
782 | } | |
783 | ||
784 | if(! m_CanUsePing) | |
785 | { | |
786 | // we didn't find ping | |
787 | return Net_Unknown; | |
788 | } | |
789 | ||
790 | wxLogNull ln; // suppress all error messages | |
791 | wxASSERT(m_PingPath.length()); | |
792 | wxString cmd; | |
793 | cmd << m_PingPath << wxT(' '); | |
794 | #if defined(__SOLARIS__) || defined (__SUNOS__) | |
795 | // nothing to add to ping command | |
796 | #elif defined(__AIX__) || \ | |
797 | defined (__BSD__) || \ | |
798 | defined(__LINUX__) || \ | |
799 | defined(__OSF__) || \ | |
800 | defined(__SGI__) || \ | |
801 | defined(__VMS) || \ | |
802 | defined(__QNX__) | |
803 | cmd << wxT("-c 1 "); // only ping once | |
804 | #elif defined(__HPUX__) | |
805 | cmd << wxT("64 1 "); // only ping once (need also specify the packet size) | |
806 | #else | |
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; | |
815 | #endif | |
816 | cmd << m_BeaconHost; | |
817 | if(wxExecute(cmd, true /* sync */) == 0) | |
818 | return Net_Connected; | |
819 | else | |
820 | return Net_No; | |
821 | } | |
822 | ||
823 | /* static */ | |
824 | wxDialUpManager *wxDialUpManager::Create() | |
825 | { | |
826 | return new wxDialUpManagerImpl; | |
827 | } | |
828 | ||
829 | #endif // wxUSE_DIALUP_MANAGER |