4 Modified for Tomato Firmware
5 Portions, Copyright (C) 2006-2009 Jonathan Zarate
9 // checkme: can remove bpalogin soon? -- zzz
15 #include <sys/sysinfo.h>
18 int start_heartbeat(int mode
)
20 #ifdef TCONFIG_HEARTBEAT
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
) {
43 _dprintf("reverting to %s\n", authserver
);
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");
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");
76 mkdir("/tmp/ppp", 0777);
77 if ((fp
= fopen("/tmp/hb_connect_success", "r"))) { // ???
78 ret
= eval("bpalogin", "-c", "/tmp/bpalogin.conf", "-t");
81 else ret
= eval("bpalogin", "-c", "/tmp/bpalogin.conf");
83 if (nvram_invmatch("ppp_demand", "1")) {
84 if (mode
!= REDIAL
) start_redial();
93 int stop_heartbeat(void)
95 #ifdef TCONFIG_HEARTBEAT
96 unlink("/tmp/ppp/link");
97 killall("bpalogin", SIGTERM
);
98 killall("bpalogin", SIGKILL
);
103 // <listenport> <pid>
104 int hb_connect_main(int argc
, char **argv
)
106 #ifdef TCONFIG_HEARTBEAT
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");
118 fprintf(fp
, "%s", argv
[2]);
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");
130 int hb_disconnect_main(int argc
, char **argv
)
132 #ifdef TCONFIG_HEARTBEAT
133 _dprintf("hb_disconnect_main\n");
142 int hb_idle_main(int argc
, char **argv
)
144 #ifdef TCONFIG_HEARTBEAT
149 unsigned long long now
;
150 unsigned long long last
;
154 if (fork() != 0) return 0;
156 maxidle
= atoi(nvram_safe_get("ppp_idletime")) * 60;
157 if (maxidle
< 60) maxidle
= 60;
165 target
= si
.uptime
+ 60;
167 // _dprintf("uptime = %ld, target = %ld\n", si.uptime, target);
168 sleep(target
- si
.uptime
);
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");
181 fgets(s
, sizeof(s
), 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
);
189 xstart("listen", nvram_safe_get("lan_ifname"));
194 _dprintf("hbidle: now = %llu, last = %llu\n", now
, last
);
200 _dprintf("hbidle: time = %ld\n", (si
.uptime
- alive
) / 60);
207 void start_hbidle(void)
209 #ifdef TCONFIG_HEARTBEAT
210 if ((nvram_match("wan_proto", "heartbeat")) && (nvram_match("ppp_demand", "1")) && (check_wanup())) {
216 void stop_hbidle(void)
218 #ifdef TCONFIG_HEARTBEAT
219 killall("hb_idle", SIGTERM
);