Tomato 1.28
[tomato.git] / release / src / router / rc / heartbeat.c
blobff85d94f209bd2a7aa688186e34c223ff0af5e5a
1 #if 0 // hbobs
2 /*
4 Modified for Tomato Firmware
5 Portions, Copyright (C) 2006-2009 Jonathan Zarate
7 */
9 // checkme: can remove bpalogin soon? -- zzz
11 #include "rc.h"
13 #include <netdb.h>
14 #include <errno.h>
15 #include <sys/sysinfo.h>
17 #undef _dprintf
18 #define _dprintf(args...) cprintf(args)
19 // #define _dprintf(args...) do { } while(0)
22 int start_heartbeat(int mode)
24 #ifdef TCONFIG_HEARTBEAT
25 FILE *fp;
26 int ret;
27 char authserver[80];
28 char authdomain[80];
29 int n;
31 if (nvram_invmatch("wan_proto", "heartbeat")) return 0;
33 _dprintf("%s: hb_server_ip=%s wan_get_domain=%s\n", __FUNCTION__,
34 nvram_safe_get("hb_server_ip"), nvram_safe_get("wan_get_domain"));
36 strlcpy(authdomain, nvram_safe_get("wan_get_domain"), sizeof(authdomain));
38 if ((nvram_invmatch("hb_server_ip", "")) && (nvram_invmatch("hb_server_ip", "0.0.0.0"))) {
39 strlcpy(authserver, nvram_safe_get("hb_server_ip"), sizeof(authserver));
40 _dprintf("trying %s\n", authserver);
41 if (gethostbyname(authserver) == NULL) {
42 n = strlen(authserver);
43 snprintf(authserver + n, sizeof(authserver) - n, ".%s", authdomain);
44 _dprintf("trying %s\n", authserver);
45 if (gethostbyname(authserver) == NULL) {
46 authserver[n] = 0;
47 _dprintf("reverting to %s\n", authserver);
51 else {
52 /* We must find out HB auth server from domain that get by dhcp if user don't input HB sever. */
53 snprintf(authserver, sizeof(authserver), "sm-server.%s", nvram_safe_get("wan_get_domain"));
57 _dprintf("%s: authserver=%s authdomain=%s\n", __FUNCTION__, authserver, authdomain);
59 // snprintf(buf, sizeof(buf), "%s%c%s", authserver, !strcmp(authdomain, "") ? '\0' : '.', authdomain);
60 // nvram_set("hb_server_name", buf);
61 // _dprintf("heartbeat: Connect to server %s\n", buf);
63 if ((fp = fopen("/tmp/bpalogin.conf", "w")) == NULL) {
64 perror("/tmp/bpalogin.conf");
65 return errno;
67 fprintf(fp, "username %s\n", nvram_safe_get("ppp_username"));
68 fprintf(fp, "password %s\n", nvram_safe_get("ppp_passwd"));
69 fprintf(fp, "authserver %s\n", authserver);
70 fprintf(fp, "authdomain %s\n", authdomain);
71 fprintf(fp, "localport 5050\n");
72 fprintf(fp, "logging stdout\n");
73 fprintf(fp, "debuglevel 4\n");
74 fprintf(fp, "minheartbeatinterval 60\n");
75 fprintf(fp, "maxheartbeatinterval 840\n");
76 fprintf(fp, "connectedprog hb_connect\n");
77 fprintf(fp, "disconnectedprog hb_disconnect\n");
78 fclose(fp);
80 mkdir("/tmp/ppp", 0777);
81 if ((fp = fopen("/tmp/hb_connect_success", "r"))) { // ???
82 ret = eval("bpalogin", "-c", "/tmp/bpalogin.conf", "-t");
83 fclose(fp);
85 else ret = eval("bpalogin", "-c", "/tmp/bpalogin.conf");
87 if (nvram_invmatch("ppp_demand", "1")) {
88 if (mode != REDIAL) start_redial();
91 return ret;
92 #else
93 return 0;
94 #endif
97 int stop_heartbeat(void)
99 #ifdef TCONFIG_HEARTBEAT
100 unlink("/tmp/ppp/link");
101 killall("bpalogin", SIGTERM);
102 killall("bpalogin", SIGKILL);
103 #endif
104 return 0;
107 // <listenport> <pid>
108 int hb_connect_main(int argc, char **argv)
110 #ifdef TCONFIG_HEARTBEAT
111 FILE *fp;
112 char buf[254];
114 _dprintf("hb_connect_main: init\n");
116 mkdir("/tmp/ppp", 0777);
118 if ((fp = fopen("/tmp/ppp/link", "a")) == NULL) {
119 perror("/tmp/ppp/link");
120 return errno;
122 fprintf(fp, "%s", argv[2]);
123 fclose(fp);
125 start_wan_done(nvram_safe_get("wan_ifname"));
127 snprintf(buf, sizeof(buf), "iptables -I INPUT -d %s -i %s -p udp --dport %d -j %s",
128 nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_ifname"), 5050, "ACCEPT");
129 system(buf);
130 #endif
131 return 0;
134 int hb_disconnect_main(int argc, char **argv)
136 #ifdef TCONFIG_HEARTBEAT
137 _dprintf("hb_disconnect_main\n");
139 if (check_wanup()) {
140 stop_heartbeat();
142 #endif
143 return 0;
146 int hb_idle_main(int argc, char **argv)
148 #ifdef TCONFIG_HEARTBEAT
149 struct sysinfo si;
150 long target;
151 FILE *f;
152 char s[64];
153 unsigned long long now;
154 unsigned long long last;
155 long alive;
156 long maxidle;
158 if (fork() != 0) return 0;
160 maxidle = atoi(nvram_safe_get("ppp_idletime")) * 60;
161 if (maxidle < 60) maxidle = 60;
163 last = 0;
165 sysinfo(&si);
166 alive = si.uptime;
168 while (1) {
169 target = si.uptime + 60;
170 do {
171 // _dprintf("uptime = %ld, target = %ld\n", si.uptime, target);
172 sleep(target - si.uptime);
173 sysinfo(&si);
174 } while (si.uptime < target);
176 if (check_action() != ACT_IDLE) continue;
179 // this sucks... -- zzz
181 if ((f = popen("iptables -xnvL FORWARD | grep wanout", "r")) == NULL) {
182 _dprintf("hbidle: error obtaining data\n");
183 continue;
185 fgets(s, sizeof(s), f);
186 pclose(f);
188 if ((now = strtoull(s, NULL, 10)) == last) {
189 if ((si.uptime - alive) > maxidle) {
190 _dprintf("hbidle: idled for %d, stopping.\n", si.uptime - alive);
191 stop_heartbeat();
192 stop_ntpc();
193 xstart("listen", nvram_safe_get("lan_ifname"));
194 return 0;
197 else {
198 _dprintf("hbidle: now = %llu, last = %llu\n", now, last);
200 last = now;
201 alive = si.uptime;
204 _dprintf("hbidle: time = %ld\n", (si.uptime - alive) / 60);
206 #else
207 return 0;
208 #endif
211 void start_hbidle(void)
213 #ifdef TCONFIG_HEARTBEAT
214 if ((nvram_match("wan_proto", "heartbeat")) && (nvram_match("ppp_demand", "1")) && (check_wanup())) {
215 xstart("hb_idle");
217 #endif
220 void stop_hbidle(void)
222 #ifdef TCONFIG_HEARTBEAT
223 killall("hb_idle", SIGTERM);
224 #endif
227 #endif // hbobs