Unix implementation of wxDialUpManager.
[wxWidgets.git] / src / unix / net.cpp
1 // -*- c++ -*- ///////////////////////////////////////////////////////////////
2 // Name: unix/net.cpp
3 // Purpose: Network related wxWindows classes and functions
4 // Author: Karsten Ballüder
5 // Modified by:
6 // Created: 03.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/setup.h"
13
14 #if wxUSE_DIALUP_MANAGER
15
16 #ifndef WX_PRECOMP
17 # include "wx/defs.h"
18 #endif // !PCH
19
20 #include "wx/string.h"
21 #include "wx/event.h"
22 #include "wx/net.h"
23 #include "wx/timer.h"
24 #include "wx/filefn.h"
25 #include "wx/utils.h"
26 #include "wx/log.h"
27
28 #include <stdlib.h>
29
30 #include <signal.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #define __STRICT_ANSI__
34 #include <sys/socket.h>
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38
39 // ----------------------------------------------------------------------------
40 // A class which groups functions dealing with connecting to the network from a
41 // workstation using dial-up access to the net. There is at most one instance
42 // of this class in the program accessed via GetDialUpManager().
43 // ----------------------------------------------------------------------------
44
45 /* TODO
46 *
47 * 1. more configurability for Unix: i.e. how to initiate the connection, how
48 * to check for online status, &c.
49 * 2. add a "long Dial(long connectionId = -1)" function which asks the user
50 * about which connection to dial (this may be done using native dialogs
51 * under NT, need generic dialogs for all others) and returns the identifier
52 * of the selected connection (it's opaque to the application) - it may be
53 * reused later to dial the same connection later (or use strings instead of
54 * longs may be?)
55 * 3. add an async version of dialing functions which notify the caller about
56 * the progress (or may be even start another thread to monitor it)
57 * 4. the static creation/accessor functions are not MT-safe - but is this
58 * really crucial? I think we may suppose they're always called from the
59 * main thread?
60 */
61
62 class WXDLLEXPORT wxDialUpManagerImpl : public wxDialUpManager
63 {
64 public:
65 wxDialUpManagerImpl()
66 {
67 m_IsOnline = -1; // unknown
68 m_timer = NULL;
69 m_CanUseIfconfig = -1; // unknown
70 m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST;
71 m_BeaconPort = 80;
72 }
73
74 /** Could the dialup manager be initialized correctly? If this function
75 returns FALSE, no other functions will work neither, so it's a good idea
76 to call this function and check its result before calling any other
77 wxDialUpManager methods.
78 */
79 virtual bool IsOk() const
80 { return TRUE; }
81
82 /** The simplest way to initiate a dial up: this function dials the given
83 ISP (exact meaning of the parameter depends on the platform), returns
84 TRUE on success or FALSE on failure and logs the appropriate error
85 message in the latter case.
86 @param nameOfISP optional paramater for dial program
87 @param username unused
88 @param password unused
89 */
90 virtual bool Dial(const wxString& nameOfISP,
91 const wxString& WXUNUSED(username),
92 const wxString& WXUNUSED(password));
93
94 /// Hang up the currently active dial up connection.
95 virtual bool HangUp();
96
97 // returns TRUE if the computer is connected to the network: under Windows,
98 // this just means that a RAS connection exists, under Unix we check that
99 // the "well-known host" (as specified by SetWellKnownHost) is reachable
100 virtual bool IsOnline() const
101 {
102 if( (! m_timer) // we are not polling, so test now:
103 || m_IsOnline == -1
104 )
105 CheckStatus();
106 return m_IsOnline != 0;
107 }
108
109 // sometimes the built-in logic for determining the online status may fail,
110 // so, in general, the user should be allowed to override it. This function
111 // allows to forcefully set the online status - whatever our internal
112 // algorithm may think about it.
113 virtual void SetOnlineStatus(bool isOnline = TRUE)
114 { m_IsOnline = isOnline; }
115
116 // set misc wxDialUpManager options
117 // --------------------------------
118
119 // enable automatical checks for the connection status and sending of
120 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
121 // parameter is only for Unix where we do the check manually: under
122 // Windows, the notification about the change of connection status is
123 // instantenous.
124 //
125 // Returns FALSE if couldn't set up automatic check for online status.
126 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds);
127
128 // disable automatic check for connection status change - notice that the
129 // wxEVT_DIALUP_XXX events won't be sent any more neither.
130 virtual void DisableAutoCheckOnlineStatus();
131
132 // under Unix, the value of well-known host is used to check whether we're
133 // connected to the internet. It's unused under Windows, but this function
134 // is always safe to call. The default value is www.yahoo.com.
135 virtual void SetWellKnownHost(const wxString& hostname,
136 int portno = 80);
137 /** Sets the commands to start up the network and to hang up
138 again. Used by the Unix implementations only.
139 */
140 virtual void SetConnectCommand(const wxString &command, const wxString &hupcmd)
141 { m_ConnectCommand = command; m_HangUpCommand = hupcmd; }
142
143 private:
144 /// -1: don´t know, 0 = no, 1 = yes
145 int m_IsOnline;
146
147 /// Can we use ifconfig to list active devices?
148 int m_CanUseIfconfig;
149 /// The path to ifconfig
150 wxString m_IfconfigPath;
151
152 /// beacon host:
153 wxString m_BeaconHost;
154 /// beacon host portnumber for connect:
155 int m_BeaconPort;
156
157 /// command to connect to network
158 wxString m_ConnectCommand;
159 /// command to hang up
160 wxString m_HangUpCommand;
161 /// name of ISP
162 wxString m_ISPname;
163 /// a timer for regular testing
164 class AutoCheckTimer *m_timer;
165
166 friend class AutoCheckTimer;
167 /// determine status
168 void CheckStatus(void) const;
169
170 /// real status check
171 void CheckStatusInternal(void);
172 };
173
174
175 class AutoCheckTimer : public wxTimer
176 {
177 public:
178 AutoCheckTimer(wxDialUpManagerImpl *dupman)
179 {
180 m_dupman = dupman;
181 m_started = FALSE;
182 }
183
184 virtual bool Start( int millisecs = -1 )
185 { m_started = TRUE; return wxTimer::Start(millisecs, FALSE); }
186
187 virtual void Notify()
188 { wxLogTrace("Checking dial up network status."); m_dupman->CheckStatus(); }
189
190 virtual void Stop()
191 { if ( m_started ) wxTimer::Stop(); }
192 public:
193 bool m_started;
194 wxDialUpManagerImpl *m_dupman;
195 };
196
197 bool
198 wxDialUpManagerImpl::Dial(const wxString &isp,
199 const wxString & WXUNUSED(username),
200 const wxString & WXUNUSED(password))
201 {
202 if(m_IsOnline == 1)
203 return FALSE;
204 m_IsOnline = -1;
205 m_ISPname = isp;
206 wxString cmd;
207 if(m_ConnectCommand.Find("%s"))
208 cmd.Printf(m_ConnectCommand,m_ISPname.c_str());
209 else
210 cmd = m_ConnectCommand;
211 return wxExecute(cmd, /* sync */ TRUE) == 0;
212 }
213
214 bool
215 wxDialUpManagerImpl::HangUp(void)
216 {
217 if(m_IsOnline == 0)
218 return FALSE;
219 m_IsOnline = -1;
220 wxString cmd;
221 if(m_HangUpCommand.Find("%s"))
222 cmd.Printf(m_HangUpCommand,m_ISPname.c_str());
223 else
224 cmd = m_HangUpCommand;
225 return wxExecute(cmd, /* sync */ TRUE) == 0;
226 }
227
228
229 bool
230 wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds)
231 {
232 wxASSERT(m_timer == NULL);
233 m_timer = new AutoCheckTimer(this);
234 bool rc = m_timer->Start(nSeconds*1000);
235 if(! rc)
236 {
237 delete m_timer;
238 m_timer = NULL;
239 }
240 return rc;
241 }
242
243 void
244 wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
245 {
246 wxASSERT(m_timer != NULL);
247 m_timer->Stop();
248 delete m_timer;
249 m_timer = NULL;
250 }
251
252
253 void
254 wxDialUpManagerImpl::SetWellKnownHost(const wxString& hostname, int portno)
255 {
256 /// does hostname contain a port number?
257 wxString port = hostname.After(':');
258 if(port.Length())
259 {
260 m_BeaconHost = hostname.Before(':');
261 m_BeaconPort = atoi(port);
262 }
263 else
264 {
265 m_BeaconHost = hostname;
266 m_BeaconPort = portno;
267 }
268 }
269
270
271 void
272 wxDialUpManagerImpl::CheckStatus(void) const
273 {
274 // This function calls the CheckStatusInternal() helper function
275 // which is OS - specific and then sends the events.
276
277 int oldIsOnline = m_IsOnline;
278 ( /* non-const */ (wxDialUpManagerImpl *)this)->CheckStatusInternal();
279
280 // now send the events as appropriate:
281 if(m_IsOnline != oldIsOnline)
282 {
283 if(m_IsOnline)
284 ; // send ev
285 else
286 ; // send ev
287 }
288 }
289
290 /*
291 We have three methods that we can use:
292
293 1. test via /sbin/ifconfig and grep for "sl", "ppp", "pl"
294 --> should be fast enough for regular polling
295 2. test if we can reach the well known beacon host
296 --> too slow for polling
297 3. check /proc/net/dev on linux??
298 This method should be preferred, if possible. Need to do more
299 testing.
300
301 */
302
303 void
304 wxDialUpManagerImpl::CheckStatusInternal(void)
305 {
306 m_IsOnline = -1;
307
308 // First time check for ifconfig location. We only use the variant
309 // which does not take arguments, a la GNU.
310 if(m_CanUseIfconfig == -1) // unknown
311 {
312 if(wxFileExists("/sbin/ifconfig"))
313 m_IfconfigPath = "/sbin/ifconfig";
314 else if(wxFileExists("/usr/sbin/ifconfig"))
315 m_IfconfigPath = "/usr/sbin/ifconfig";
316 }
317
318 // Let´s try the ifconfig method first, should be fastest:
319 if(m_CanUseIfconfig != 0) // unknown or yes
320 {
321 wxASSERT(m_IfconfigPath.length());
322
323 wxString tmpfile = wxGetTempFileName("_wxdialuptest");
324 wxString cmd = m_IfconfigPath;
325 cmd << " >" << tmpfile;
326 if(wxExecute(m_IfconfigPath,TRUE /* sync */) == 0)
327 {
328 m_CanUseIfconfig = 1;
329 wxString cmd1 = "grep ppp <"+tmpfile; // PPP
330 wxString cmd2 = "grep sl <"+tmpfile; // SLIP
331 wxString cmd3 = "grep pl <"+tmpfile; // PLIP
332 if(wxExecute(cmd1,TRUE) == 0
333 || wxExecute(cmd2,TRUE) == 0
334 || wxExecute(cmd3,TRUE) == 0
335 )
336 m_IsOnline = 1;
337 }
338 else // could not run ifconfig correctly
339 m_CanUseIfconfig = 0; // don´t try again
340 wxRemoveFile(tmpfile);
341 if(m_IsOnline != -1) // we are done
342 return;
343 }
344
345 // second method: try to connect to well known host:
346 struct hostent *hp;
347 struct sockaddr_in serv_addr;
348 int sockfd;
349
350 m_IsOnline = 0; // assume false
351 if((hp = gethostbyname(m_BeaconHost)) == NULL)
352 return; // no DNS no net
353
354 serv_addr.sin_family = hp->h_addrtype;
355 memcpy(&serv_addr.sin_addr,hp->h_addr, hp->h_length);
356 serv_addr.sin_port = htons(m_BeaconPort);
357 if( ( sockfd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
358 {
359 // sys_error("cannot create socket for gw");
360 return;
361 }
362 if( connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
363 {
364 //sys_error("cannot connect to server");
365 return;
366 }
367 //connected!
368 close(sockfd);
369 }
370
371
372 /* static */
373 wxDialUpManager *
374 wxDialUpManager::wxDialUpManager::Create(void)
375 {
376 return new wxDialUpManagerImpl;
377 }
378
379 #endif // wxUSE_DIALUP_MANAGER