Merge commit 'origin/tomato-RT' into tomato-shibby
[tomato.git] / release / src / router / rc / heartbeat.c
blobcf8cb66d61cb4b65dc152a140e281f54c44db537
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>
18 int start_heartbeat(int mode)
20 #ifdef TCONFIG_HEARTBEAT
21 FILE *fp;
22 int ret;
23 char authserver[80];
24 char authdomain[80];
25 int n;
27 if (nvram_invmatch("wan_proto", "heartbeat")) return 0;
29 _dprintf("%s: hb_server_ip=%s wan_get_domain=%s\n", __FUNCTION__,
30 nvram_safe_get("hb_server_ip"), nvram_safe_get("wan_get_domain"));
32 strlcpy(authdomain, nvram_safe_get("wan_get_domain"), sizeof(authdomain));
34 if ((nvram_invmatch("hb_server_ip", "")) && (nvram_invmatch("hb_server_ip", "0.0.0.0"))) {
35 strlcpy(authserver, nvram_safe_get("hb_server_ip"), sizeof(authserver));
36 _dprintf("trying %s\n", authserver);
37 if (gethostbyname(authserver) == NULL) {
38 n = strlen(authserver);
39 snprintf(authserver + n, sizeof(authserver) - n, ".%s", authdomain);
40 _dprintf("trying %s\n", authserver);
41 if (gethostbyname(authserver) == NULL) {
42 authserver[n] = 0;
43 _dprintf("reverting to %s\n", authserver);
47 else {
48 /* We must find out HB auth server from domain that get by dhcp if user don't input HB sever. */
49 snprintf(authserver, sizeof(authserver), "sm-server.%s", nvram_safe_get("wan_get_domain"));
53 _dprintf("%s: authserver=%s authdomain=%s\n", __FUNCTION__, authserver, authdomain);
55 // snprintf(buf, sizeof(buf), "%s%c%s", authserver, !strcmp(authdomain, "") ? '\0' : '.', authdomain);
56 // nvram_set("hb_server_name", buf);
57 // _dprintf("heartbeat: Connect to server %s\n", buf);
59 if ((fp = fopen("/tmp/bpalogin.conf", "w")) == NULL) {
60 perror("/tmp/bpalogin.conf");
61 return errno;
63 fprintf(fp, "username %s\n", nvram_safe_get("ppp_username"));
64 fprintf(fp, "password %s\n", nvram_safe_get("ppp_passwd"));
65 fprintf(fp, "authserver %s\n", authserver);
66 fprintf(fp, "authdomain %s\n", authdomain);
67 fprintf(fp, "localport 5050\n");
68 fprintf(fp, "logging stdout\n");
69 fprintf(fp, "debuglevel 4\n");
70 fprintf(fp, "minheartbeatinterval 60\n");
71 fprintf(fp, "maxheartbeatinterval 840\n");
72 fprintf(fp, "connectedprog hb_connect\n");
73 fprintf(fp, "disconnectedprog hb_disconnect\n");
74 fclose(fp);
76 mkdir("/tmp/ppp", 0777);
77 if ((fp = fopen("/tmp/hb_connect_success", "r"))) { // ???
78 ret = eval("bpalogin", "-c", "/tmp/bpalogin.conf", "-t");
79 fclose(fp);
81 else ret = eval("bpalogin", "-c", "/tmp/bpalogin.conf");
83 if (nvram_invmatch("ppp_demand", "1")) {
84 if (mode != REDIAL) start_redial();
87 return ret;
88 #else
89 return 0;
90 #endif
93 int stop_heartbeat(void)
95 #ifdef TCONFIG_HEARTBEAT
96 unlink("/tmp/ppp/link");
97 killall("bpalogin", SIGTERM);
98 killall("bpalogin", SIGKILL);
99 #endif
100 return 0;
103 // <listenport> <pid>
104 int hb_connect_main(int argc, char **argv)
106 #ifdef TCONFIG_HEARTBEAT
107 FILE *fp;
108 char buf[254];
110 _dprintf("hb_connect_main: init\n");
112 mkdir("/tmp/ppp", 0777);
114 if ((fp = fopen("/tmp/ppp/link", "a")) == NULL) {
115 perror("/tmp/ppp/link");
116 return errno;
118 fprintf(fp, "%s", argv[2]);
119 fclose(fp);
121 start_wan_done(nvram_safe_get("wan_ifname"));
123 snprintf(buf, sizeof(buf), "iptables -I INPUT -d %s -i %s -p udp --dport %d -j %s",
124 nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_ifname"), 5050, "ACCEPT");
125 system(buf);
126 #endif
127 return 0;
130 int hb_disconnect_main(int argc, char **argv)
132 #ifdef TCONFIG_HEARTBEAT
133 _dprintf("hb_disconnect_main\n");
135 if (check_wanup()) {
136 stop_heartbeat();
138 #endif
139 return 0;
142 int hb_idle_main(int argc, char **argv)
144 #ifdef TCONFIG_HEARTBEAT
145 struct sysinfo si;
146 long target;
147 FILE *f;
148 char s[64];
149 unsigned long long now;
150 unsigned long long last;
151 long alive;
152 long maxidle;
154 if (fork() != 0) return 0;
156 maxidle = atoi(nvram_safe_get("ppp_idletime")) * 60;
157 if (maxidle < 60) maxidle = 60;
159 last = 0;
161 sysinfo(&si);
162 alive = si.uptime;
164 while (1) {
165 target = si.uptime + 60;
166 do {
167 // _dprintf("uptime = %ld, target = %ld\n", si.uptime, target);
168 sleep(target - si.uptime);
169 sysinfo(&si);
170 } while (si.uptime < target);
172 if (check_action() != ACT_IDLE) continue;
175 // this sucks... -- zzz
177 if ((f = popen("iptables -xnvL FORWARD | grep wanout", "r")) == NULL) {
178 _dprintf("hbidle: error obtaining data\n");
179 continue;
181 fgets(s, sizeof(s), f);
182 pclose(f);
184 if ((now = strtoull(s, NULL, 10)) == last) {
185 if ((si.uptime - alive) > maxidle) {
186 _dprintf("hbidle: idled for %d, stopping.\n", si.uptime - alive);
187 stop_heartbeat();
188 stop_ntpc();
189 xstart("listen", nvram_safe_get("lan_ifname"));
190 return 0;
193 else {
194 _dprintf("hbidle: now = %llu, last = %llu\n", now, last);
196 last = now;
197 alive = si.uptime;
200 _dprintf("hbidle: time = %ld\n", (si.uptime - alive) / 60);
202 #else
203 return 0;
204 #endif
207 void start_hbidle(void)
209 #ifdef TCONFIG_HEARTBEAT
210 if ((nvram_match("wan_proto", "heartbeat")) && (nvram_match("ppp_demand", "1")) && (check_wanup())) {
211 xstart("hb_idle");
213 #endif
216 void stop_hbidle(void)
218 #ifdef TCONFIG_HEARTBEAT
219 killall("hb_idle", SIGTERM);
220 #endif
223 #endif // hbobs