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 #define _dprintf(args...) cprintf(args)
19 // #define _dprintf(args...) do { } while(0)
22 int start_heartbeat(int mode
)
24 #ifdef TCONFIG_HEARTBEAT
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
) {
47 _dprintf("reverting to %s\n", authserver
);
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");
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");
80 mkdir("/tmp/ppp", 0777);
81 if ((fp
= fopen("/tmp/hb_connect_success", "r"))) { // ???
82 ret
= eval("bpalogin", "-c", "/tmp/bpalogin.conf", "-t");
85 else ret
= eval("bpalogin", "-c", "/tmp/bpalogin.conf");
87 if (nvram_invmatch("ppp_demand", "1")) {
88 if (mode
!= REDIAL
) start_redial();
97 int stop_heartbeat(void)
99 #ifdef TCONFIG_HEARTBEAT
100 unlink("/tmp/ppp/link");
101 killall("bpalogin", SIGTERM
);
102 killall("bpalogin", SIGKILL
);
107 // <listenport> <pid>
108 int hb_connect_main(int argc
, char **argv
)
110 #ifdef TCONFIG_HEARTBEAT
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");
122 fprintf(fp
, "%s", argv
[2]);
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");
134 int hb_disconnect_main(int argc
, char **argv
)
136 #ifdef TCONFIG_HEARTBEAT
137 _dprintf("hb_disconnect_main\n");
146 int hb_idle_main(int argc
, char **argv
)
148 #ifdef TCONFIG_HEARTBEAT
153 unsigned long long now
;
154 unsigned long long last
;
158 if (fork() != 0) return 0;
160 maxidle
= atoi(nvram_safe_get("ppp_idletime")) * 60;
161 if (maxidle
< 60) maxidle
= 60;
169 target
= si
.uptime
+ 60;
171 // _dprintf("uptime = %ld, target = %ld\n", si.uptime, target);
172 sleep(target
- si
.uptime
);
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");
185 fgets(s
, sizeof(s
), 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
);
193 xstart("listen", nvram_safe_get("lan_ifname"));
198 _dprintf("hbidle: now = %llu, last = %llu\n", now
, last
);
204 _dprintf("hbidle: time = %ld\n", (si
.uptime
- alive
) / 60);
211 void start_hbidle(void)
213 #ifdef TCONFIG_HEARTBEAT
214 if ((nvram_match("wan_proto", "heartbeat")) && (nvram_match("ppp_demand", "1")) && (check_wanup())) {
220 void stop_hbidle(void)
222 #ifdef TCONFIG_HEARTBEAT
223 killall("hb_idle", SIGTERM
);