Example #1
0
void refresh_ntpc(void)
{
	if (nvram_match("wan_route_x", "IP_Routed") && (!has_wan_ip() || !found_default_route()))
		return;

	setenv("TZ", nvram_safe_get("time_zone_x"), 1);
	
	if (pids("ntpclient"))
		system("killall ntpclient");

	if (!pids("ntp"))
	{
		stop_ntpc();
		start_ntpc();
	}
	else
		kill_pidfile_s("/var/run/ntp.pid", SIGTSTP);
}
Example #2
0
int ipdown_main(int argc, char **argv)
{
	int proto;
	
	TRACE_PT("begin\n");

	if (!wait_action_idle(10)) return -1;

	stop_ddns();	// avoid to trigger DOD
	stop_ntpc();

	unlink("/tmp/ppp/link");

	proto = get_wan_proto();
	if (proto == WP_L2TP || proto == WP_PPTP) {
		/* clear dns from the resolv.conf */
		nvram_set("wan_get_dns","");
		dns_to_resolv();

		if (proto == WP_L2TP) {
			route_del(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"),
				nvram_safe_get("wan_gateway"), "255.255.255.255"); // fixed routing problem in Israel by kanki
		}

		// Restore the default gateway for WAN interface
		nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));

		// Set default route to gateway if specified
		route_del(nvram_safe_get("wan_ifname"), 0, "0.0.0.0", nvram_safe_get("wan_gateway"), "0.0.0.0");
		route_add(nvram_safe_get("wan_ifname"), 0, "0.0.0.0", nvram_safe_get("wan_gateway"), "0.0.0.0");
	}

	if (nvram_get_int("ppp_demand")) {
		killall("listen", SIGKILL);
		eval("listen", nvram_safe_get("lan_ifname"));
	}

	TRACE_PT("end\n");
	return 1;
}
Example #3
0
int ntp_main(int argc, char *argv[])
{
	FILE *fp;
	int ret;
	pid_t pid;
	char *args[] = {"ntpclient", "-h", servers, "-i", "3", "-l", "-s", NULL};

	strcpy(servers, nvram_safe_get("ntp_server0"));

	fp = fopen("/var/run/ntp.pid", "w");
	if (fp == NULL)
		exit(0);
	fprintf(fp, "%d", getpid());
	fclose(fp);

	dbg("starting ntp...\n");

	signal(SIGTSTP, catch_sig);
	signal(SIGTERM, catch_sig);
	signal(SIGCHLD, chld_reap);

	nvram_set("ntp_ready", "0");
	nvram_set("svc_ready", "0");

	while (1)
	{
		if (sig_cur != -1)
		{
			pause();
		}
		else if (nvram_get_int("sw_mode") == SW_MODE_ROUTER &&
			!nvram_match("link_internet", "1"))
		{
			sleep(NTP_RETRY_INTERVAL);
		}
		else if (strlen(servers))
		{
			stop_ntpc();

			nvram_set("ntp_server_tried", servers);
			ret = _eval(args, NULL, 0, &pid);
			sleep(SECONDS_TO_WAIT);

			if (strlen(nvram_safe_get("ntp_server0")))
			{
				if (server_idx)
					strcpy(servers, nvram_safe_get("ntp_server1"));
				else
					strcpy(servers, nvram_safe_get("ntp_server0"));
				server_idx = (server_idx + 1) % 2;
			}
				else strcpy(servers, "");
			args[2] = servers;

			if(nvram_get_int("ntp_ready"))
			{
				/* ntp sync every hour when time_zone set as "DST" */
				if(strstr(nvram_safe_get("time_zone_x"), "DST")) {
					struct tm local;
					time_t now;
					int diff_sec;

					time(&now);
					localtime_r(&now, &local);
//					dbg("%s: %d-%d-%d, %d:%d:%d dst:%d\n", __FUNCTION__, local.tm_year+1900, local.tm_mon+1, local.tm_mday, local.tm_hour, local.tm_min, local.tm_sec, local.tm_isdst);

					/* every hour */
					if((local.tm_min != 0) || (local.tm_sec != 0)) {
						/* compensate for the sleep(SECONDS_TO_WAIT) */
						diff_sec = (3600 - SECONDS_TO_WAIT) - (local.tm_min * 60 + local.tm_sec);
						if(diff_sec == 0) diff_sec = 3600 - SECONDS_TO_WAIT;
						else if(diff_sec < 0) diff_sec = -diff_sec;
//						dbg("diff_sec: %d \n", diff_sec);
						sleep(diff_sec);
					}
					else sleep(3600 - SECONDS_TO_WAIT);
				}
				else	/* every 12 hours */
				{
					sleep(3600 * 12 - SECONDS_TO_WAIT);
				}
			}
			else
			{
				sleep(NTP_RETRY_INTERVAL - SECONDS_TO_WAIT);
			}
		}
		else
		{
			pause();
		}
	}
}
Example #4
0
/*
 * Called when link goes down
 */
int ipdown_main(int argc, char **argv)
{
	if (check_action() != ACT_IDLE)
		return -1;
	runStartup("/etc/config", ".ipdown");
#ifdef HAVE_REGISTER
	if (isregistered_real())
#endif
	{
#ifdef HAVE_RB500
		runStartup("/usr/local/etc/config", ".ipdown");
#else
		runStartup("/jffs/etc/config", ".ipdown");
		runStartup("/mmc/etc/config", ".ipdown");
		runStartup("/tmp/etc/config", ".ipdown");
#endif
	}
	stop_ddns();
	stop_ntpc();

	unlink("/tmp/ppp/link");

	if (nvram_match("wan_proto", "l2tp")) {
		/*
		 * clear dns from the resolv.conf 
		 */
		nvram_set("wan_get_dns", "");
		dns_to_resolv();

		// todo
		route_del(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway_buf"), "255.255.255.255");
		/*
		 * Restore the default gateway for WAN interface 
		 */
		nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf"));

		/*
		 * Set default route to gateway if specified 
		 */
		route_add(nvram_safe_get("wan_ifname"), 0, "0.0.0.0", nvram_safe_get("wan_gateway"), "0.0.0.0");
	}
	if (nvram_match("wan_proto", "pptp")) {
		eval("route", "del", "default");
		nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf"));
		eval("route", "add", "default", "gw", nvram_safe_get("wan_gateway"));
		sysprintf("iptables -t nat -A POSTROUTING -o %s -j MASQUERADE\n", nvram_safe_get("pptp_ifname"));
	}
#ifdef HAVE_3G
#if defined(HAVE_TMK) || defined(HAVE_BKM)
	else if (nvram_match("wan_proto", "3g")) {
		char *gpio3g = nvram_get("gpio3g");
		if (gpio3g != NULL)
			set_gpio(atoi(gpio3g), 0);
	}
#endif
#endif

	nvram_set("pppoe_ifname", "");
	nvram_set("pppd_pppifname", "");

	// write PPP traffic statistics to nvram if wanted
	if (nvram_match("ppp_traffic", "1")) {
		char buffer[64];
		long long old_in, old_out;
		long long in, out;
		char *pin;
		char *pout;
		time_t stamp;

		old_in = atol(nvram_safe_get("ppp_byte_in"));
		old_out = atol(nvram_safe_get("ppp_byte_out"));

		if ((pin = getenv("BYTES_RCVD")))
			in = atol(pin);
		else
			in = 0;

		if ((pout = getenv("BYTES_SENT")))
			out = atol(pout);
		else
			out = 0;

		in += old_in;
		out += old_out;
		snprintf(buffer, 63, "%lld", in);
		nvram_set("ppp_byte_in", buffer);
		snprintf(buffer, 63, "%lld", out);
		nvram_set("ppp_byte_out", buffer);
		if ((stamp = time(NULL)) < 1087818160)	// clock is not set
			// properly
			stamp = 0;
		snprintf(buffer, 63, "%ld", stamp);
		nvram_set("ppp_byte_stamp", buffer);
		nvram_commit();
	}

	if (nvram_match("ppp_demand", "1")
	    && (nvram_match("wan_proto", "pptp")
		|| nvram_match("wan_proto", "l2tp")
		|| nvram_match("wan_proto", "pppoe"))) {
		stop_process("listen", "activity listener");
		eval("listen", nvram_safe_get("lan_ifname"));
	}

	return 1;
}