Remove trailing whitespace
[dockapps.git] / yawmppp / src / ycommon.c
blob1d60becd37c24ec235123454ed5996550be67856
1 /*
3 YAWMPPP - PPP dock app/helper for WindowMaker
4 Copyright (C) 2000,2001:
6 Author: Felipe Bergo (bergo@seul.org)
8 based on the wmppp application by
10 Martijn Pieterse (pieterse@xs4all.nl)
11 Antoine Nulle (warp@xs4all.nl)
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 $Id: ycommon.c,v 1.1.1.1 2001/02/22 07:15:59 bergo Exp $
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <time.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <ctype.h>
37 #include <signal.h>
39 #include <sys/wait.h>
40 #include <sys/stat.h>
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
45 #include <sys/socket.h>
47 #ifdef LINUX
48 #define _LINUX_SOCKET_H
49 #endif
51 /* guessworks for critters beyond the realm of Linux and FreeBSD */
53 #ifdef OPENBSD
54 #define FREEBSD
55 #endif
57 #ifdef NETBSD
58 #define FREEBSD
59 #endif
61 #ifdef BSDISH
62 #define FREEBSD
63 #endif
65 #ifdef LINUX
66 #include <asm/types.h>
67 #include <linux/if.h>
68 #include <linux/ppp_defs.h>
69 #include <linux/if_ppp.h>
70 #endif
72 #ifdef FREEBSD
73 #include <net/if.h>
74 #include <net/ppp_defs.h>
75 #include <net/if_ppp.h>
76 #endif
78 #include "ycommon.h"
79 #include "isprc.h"
82 extern struct LogStruct logconn;
84 extern int ppp_h;
85 extern int ppp_open;
87 extern struct YAWMPPP_ISP_INFO IspData[MAX_ISPS];
88 extern int num_isps;
89 extern int current_isp;
91 extern char *active_interface;
93 /* LOGS = ~/.yawmppp2/log */
95 void
96 clean_guards(void)
98 char *p;
99 char temp[128],aux[128];
100 FILE *f;
102 p = getenv ("HOME");
103 strcpy (temp, p);
104 strcat (temp, "/.yawmppp2/.floatlog");
105 f=fopen(temp,"r");
106 if (!f)
107 return; /* fine: no crash file */
108 if (!fgets(aux,128,f)) return;
109 if ((p=strtok(aux,"\n"))==NULL) return;
110 logconn.start=atoi(p);
111 if (!fgets(aux,128,f)) return;
112 if ((p=strtok(aux,"\n"))==NULL) return;
113 logconn.end=atoi(p);
114 if (!fgets(aux,128,f)) return;
115 if ((p=strtok(aux,"\n"))==NULL) return;
116 logconn.status=atoi(p);
117 if (!fgets(aux,128,f)) return;
118 if ((p=strtok(aux,"\n"))==NULL) return;
119 strcpy(logconn.longname,p);
120 if (!fgets(aux,128,f)) return;
121 if ((p=strtok(aux,"\n"))==NULL) return;
122 strcpy(logconn.shortname,p);
123 if (!fgets(aux,128,f)) return;
124 if ((p=strtok(aux,"\n"))==NULL) return;
125 strcpy(logconn.phone,p);
126 if (!fgets(aux,128,f)) return;
127 if ((p=strtok(aux,"\n"))==NULL) return;
128 strcpy(logconn.user,p);
129 fclose(f);
130 logconn.status=2; /* CRASH */
131 write_log();
134 void
135 make_guards(void)
137 char *p;
138 char temp[128];
139 FILE *f;
141 p = getenv ("HOME");
142 strcpy (temp, p);
143 strcat (temp, "/.yawmppp2/.floatlog");
144 f=fopen(temp,"w");
145 if (!f) return;
147 logconn.end=time(NULL);
148 fprintf(f,"%lu\n%lu\n%d\n",logconn.start,logconn.end,logconn.status);
149 fprintf(f,"%s\n%s\n%s\n",logconn.longname,logconn.shortname,logconn.phone);
150 fprintf(f,"%s\n",logconn.user);
151 fclose(f);
154 void
155 write_log(void)
157 char *p;
158 char temp[128];
159 FILE *f;
160 int i,s;
162 p = getenv ("HOME");
163 strcpy (temp, p);
164 strcat (temp, "/.yawmppp2/logfile");
165 f=fopen(temp,"a");
166 if (!f) return;
168 s=strlen(logconn.phone);
169 for(i=0;i<s;i++)
170 if (logconn.phone[i]=='\t')
171 logconn.phone[i]=' ';
172 s=strlen(logconn.shortname);
173 for(i=0;i<s;i++)
174 if (logconn.shortname[i]=='\t')
175 logconn.shortname[i]=' ';
176 s=strlen(logconn.longname);
177 for(i=0;i<s;i++)
178 if (logconn.longname[i]=='\t')
179 logconn.longname[i]=' ';
180 s=strlen(logconn.user);
181 for(i=0;i<s;i++)
182 if (logconn.user[i]=='\t')
183 logconn.user[i]=' ';
185 fprintf(f,"%lu\t%lu\t%d\t%s\t%s\t%s\t%s\n",
186 logconn.start,
187 logconn.end,
188 logconn.status,
189 logconn.longname,
190 logconn.shortname,
191 logconn.phone,
192 logconn.user);
193 fclose(f);
195 chmod(temp,0600);
197 p = getenv ("HOME");
198 strcpy (temp, p);
199 strcat (temp, "/.yawmppp2/.floatlog");
200 unlink(temp);
204 void
205 warn_pref(void)
207 char *p;
208 char temp[128];
209 FILE *f;
211 p=getenv("HOME");
212 strcpy (temp, p);
213 strcat (temp, "/.yawmppp2/.delayedupdate");
215 f=fopen(temp,"w");
216 if (!f) return;
217 fprintf(f,"This file shouldn't last more than a few millisecs...\n");
218 fclose(f);
221 /* PPP */
223 void
224 open_ppp(void)
226 /* Open the ppp device. */
228 if (ppp_open)
229 return;
231 if ((ppp_h = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
232 ppp_open = 1;
235 void
236 close_ppp(void)
238 if (!ppp_open)
239 return;
240 close(ppp_h);
241 ppp_open=0;
244 /* other stuff */
246 void
247 write_pid_file(void)
249 char *p;
250 char temp[128];
251 FILE *f;
253 p = getenv ("HOME");
254 strcpy (temp, p);
255 strcat (temp, "/.yawmppp2/yawmppp.pid");
257 f=fopen(temp,"w");
258 if (f) {
259 fprintf(f,"%d",getpid());
260 fclose(f);
264 void
265 remove_pid_file(void)
267 char *p;
268 char temp[128];
270 p = getenv ("HOME");
271 strcpy (temp, p);
272 strcat (temp, "/.yawmppp2/yawmppp.pid");
274 unlink(temp);
277 void
278 make_config_dir(void)
280 struct stat ss;
281 char tmp[256];
283 sprintf(tmp,"%s/.yawmppp2",getenv("HOME"));
285 if (stat(tmp,&ss)<0)
286 mkdir(tmp,0700);
290 void
291 grab_isp_info(int rof)
293 char *p;
294 char temp[128];
296 p = getenv ("HOME");
297 strcpy (temp, p);
298 strcat (temp, "/.yawmppp2/yawmppprc");
300 num_isps=GetISPInfo(temp,&IspData[0],MAX_ISPS);
302 if ((!num_isps)&&(rof)) {
303 run_pref_app();
307 void
308 run_pref_app(void)
310 if (!fork()) {
311 execlp("yawmppp.pref","yawmppp.pref",NULL);
312 execlp("/usr/local/bin/yawmppp.pref","yawmppp.pref",NULL);
313 execlp("/usr/bin/yawmppp.pref","yawmppp.pref",NULL);
314 exit(2);
318 void
319 run_log_app(void)
321 if (!fork()) {
322 execlp("yawmppp.log","yawmppp.log",NULL);
323 execlp("/usr/local/bin/yawmppp.log","yawmppp.log",NULL);
324 execlp("/usr/bin/yawmppp.log","yawmppp.log",NULL);
325 exit(2);
329 /* essential */
332 /* get_statistics */
335 get_statistics (char *devname, long *ip, long *op, long *is, long *os)
338 struct ifpppstatsreq req;
339 struct ppp_stats ppp_cur;
341 memset (&ppp_cur, 0, sizeof (ppp_cur));
343 open_ppp();
344 if (!ppp_open)
345 return(-1);
347 memset(&req,0,sizeof(req));
349 #ifdef LINUX
350 req.stats_ptr=(caddr_t) & req.stats;
351 strcpy(req.ifr__name, active_interface);
352 #endif
354 #ifdef FREEBSD
355 strcpy(req.ifr_name, active_interface);
356 #endif
358 if (ioctl(ppp_h,SIOCGPPPSTATS,&req) >= 0)
359 ppp_cur=req.stats;
361 *op = ppp_cur.p.ppp_opackets;
362 *ip = ppp_cur.p.ppp_ipackets;
363 *is = ppp_cur.p.ppp_ibytes;
364 *os = ppp_cur.p.ppp_obytes;
366 return 0;
369 /* stillonline */
372 stillonline (char *ifs)
375 #ifdef LINUX
376 FILE *fp;
377 char temp[128];
378 #endif
379 #ifdef FREEBSD
380 struct ifreq req;
381 #endif
382 int i=0;
384 #ifdef LINUX
385 fp = fopen ("/proc/net/route", "r");
386 if (fp)
388 while (fgets (temp, 128, fp))
390 if (strstr (temp, ifs))
392 i = 1; /* Line is alive */
395 fclose (fp);
397 #endif
399 #ifdef FREEBSD
400 strcpy(req.ifr_name,active_interface);
401 open_ppp();
402 if (!ppp_open)
403 return 0;
404 if (ioctl(ppp_h,SIOCGIFFLAGS,&req)>=0) {
405 if (req.ifr_flags&IFF_UP)
406 i=1;
408 #endif
409 return i;