summaryrefslogtreecommitdiff
path: root/src/osdsh/connectionwatch.c
blob: 804eaa182f056fc032194bc2d471b6efb77af962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "osdsh.h"
#include "../config.h"
#define IAM 3
#define MYNAME "Net"

settings_t pppset;
void *connection_watch(void *arg);

char pppdevice[PATH_MAX+1];
char connecting1[BUFSIZ+1];
char connecting2[BUFSIZ+1];
char connected1[BUFSIZ+1];
char connected2[BUFSIZ+1];
char disconnected[BUFSIZ+1];
char timeconnected[255];

/* ==========================================================================
   ====================== REQUIERED STUFF FOR PLUGINS =======================
   ==========================================================================

***** ID of the plugin, contact me before you asign this to your plugin!! *****
*/

int whoami(void) {
	return IAM;
}

/* Return a pointer to the name of the plugin */
char *mynameis() {
	char *a = MYNAME;
	return a;
}

/* This function sets the default (initual) values for our osd */
void initialize_myosd() {
	set_defaults(&pppset);
}

/* This function takes a command and arguments, if the command is ours, execute
   it and return 1, if not, return 0*/

int isitmine( char command[BUFSIZ], char arg_first[BUFSIZ],
			char arg_secound[BUFSIZ])
{
	int i=1;

	if(strcmp(command, "stop")==0) {
		pppset.displaying = 0;
	}
	else if(strcmp(command, "start")==0) {
		if(!pppset.displaying) {
			pppset.displaying = 1;
			pthread_create(&pppset.mythread, NULL, connection_watch, NULL);
		}
	}
	else if (strcmp(command, "pppw")==0) {
		if (atoi(arg_first)) {
			isitmine("start", NULL, NULL);
		}
		else {
			pppset.displaying = 0;
		}
	}
	else if (strcmp(command, "sppp")==0) {
		control_options(&pppset, arg_first, arg_secound);
	}
	else if (strcmp(command, "pdev")==0) {
		sprintf(pppdevice, "%s", arg_first);
	}
	else if (strcmp(command, "connecting")==0) {
		sprintf(connecting1, "%s", arg_first);
		sprintf(connecting2, "%s", arg_secound);
	}
	else if (strcmp(command, "connected")==0) {
		sprintf(connected1, "%s", arg_first);
		sprintf(connected2, "%s", arg_secound);
	}
	else if (strcmp(command, "disconnected")==0) {
		sprintf(disconnected, "%s", arg_first);
	}
	else if (strcmp(command, "showconntime")==0) {
		xosd_display(pppset.myosd, 0, XOSD_string, "Connected for");
		xosd_display(pppset.myosd, 1, XOSD_string, timeconnected);
	}
	else {
		i = 0;
	}
	return i;
}


/*=============================connection_watch()=========================== */
/* some ideas from ppptime */
void *connection_watch(void *arg)
{
    FILE *fp;
    int ppp0run;

    long start;
    long end;
    int total;
    struct stat st;

    char ppp0pidfile[PATH_MAX];

    pppset.myosd = xosd_create(2);
    initialize_osd(&pppset);

    if(strcmp(pppdevice, "")==0)
	sprintf(pppdevice, "%s", PPP_DEVICE);

    sprintf(ppp0pidfile, "%s/%s.pid", PPP_PID_PATH, pppdevice);

    if (strcmp(connecting1, "")==0)
	sprintf(connecting1, "Connecting");
    if (strcmp(connecting2, "")==0)
	sprintf(connecting2, "please wait...");
	
    if (strcmp(connected1, "")==0)
	sprintf(connected1, "Connected");
    if (strcmp(connected2, "")==0)
	sprintf(connected2, "GO!");
	
    if (strcmp(disconnected, "")==0)
	sprintf(disconnected, "Connection terminated");

    while (pppset.displaying) {
	
	    xosd_display(pppset.myosd, 0, XOSD_string, connecting1);
	    xosd_display(pppset.myosd, 1, XOSD_string, connecting2);
	
	    sprintf(ppp0pidfile, "%s/%s.pid", PPP_PID_PATH, pppdevice);
	
	    if((fp=fopen(ppp0pidfile, "r"))==NULL) {
		ppp0run = 0;
	    }
	    else {
		ppp0run = 1;
		fclose(fp);
	    }
	
	    if(ppp0run) {
		xosd_display(pppset.myosd,0, XOSD_string, connected1);
		xosd_display(pppset.myosd,1, XOSD_string, connected2);
	
		stat(ppp0pidfile, &st);
		start = st.st_mtime; 
	
		while((fp=fopen(ppp0pidfile, "r"))!=NULL) {
		    fclose(fp);
		    end = time(0);
		    total = end - start;
		    sprintf(timeconnected, "Connected for %d hr %d min %d sec",
			((total/60)/60)%100, (total/60)%60, total%60);
		    usleep(3);
	   	}
		end = time(0);
		total = end - start;
		sprintf(timeconnected, "Disconnected after %d hr %d min %d sec",
			((total/60)/60)%100, (total/60)%60, total%60);
	
		xosd_display(pppset.myosd,0, XOSD_string, disconnected);
	   	xosd_display(pppset.myosd,1, XOSD_string, timeconnected);
	
		pppset.displaying = 0;
	    }
    }
    sleep(1);
    xosd_destroy(pppset.myosd);
    pthread_exit(0);
}