示例#1
0
文件: dhcp.c 项目: twtomato/twtomato
int dhcpc_renew_main(int argc, char **argv)
{
	int pid;

	_dprintf("%s: begin\n", __FUNCTION__);

	if (!using_dhcpc()) return 1;

	if ((pid = pidof("udhcpc")) > 1) {
		kill(pid, SIGUSR1);
		f_write(renewing, NULL, 0, 0, 0);
	}
	else {
		stop_dhcpc();
		start_dhcpc();
	}
	
	_dprintf("%s: end\n", __FUNCTION__);	
	return 0;
}
示例#2
0
void start_pptp(int status)
{
	int ret;
	FILE *fp;
	char *pptp_argv[] = { "pppd",
		NULL
	};
	char username[80], passwd[80];

	stop_dhcpc();
#ifdef HAVE_PPPOE
	stop_pppoe();
#endif
	stop_vpn_modules();

	snprintf(username, sizeof(username), "%s",
		 nvram_safe_get("ppp_username"));
	snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));

	if (status != REDIAL) {
		create_pptp_config(nvram_safe_get("pptp_server_name"),
				   username);
		/*
		 * Generate pap-secrets file 
		 */
		if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) {
			perror("/tmp/ppp/pap-secrets");
			return;
		}
		fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
		fclose(fp);
		chmod("/tmp/ppp/pap-secrets", 0600);

		/*
		 * Generate chap-secrets file 
		 */
		if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) {
			perror("/tmp/ppp/chap-secrets");
			return;
		}
		fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
		fclose(fp);
		chmod("/tmp/ppp/chap-secrets", 0600);

		/*
		 * Enable Forwarding 
		 */
		if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) {
			fputc('1', fp);
			fclose(fp);
		} else
			perror("/proc/sys/net/ipv4/ip_forward");
	}
	char *wan_ifname = nvram_safe_get("wan_ifname");

	if (isClient()) {
		wan_ifname = getSTA();
	}

	nvram_set("pptp_ifname", wan_ifname);
	/*
	 * Bring up WAN interface 
	 */
	if (nvram_match("pptp_use_dhcp", "1")) {
		// pid_t pid;
		// char *wan_ipaddr;
		// char *wan_netmask;
		// char *wan_gateway;

		// char *pptp_server_ip = nvram_safe_get ("pptp_server_ip");
		// char *wan_hostname = nvram_safe_get ("wan_hostname");

		nvram_set("wan_get_dns", "");
		nvram_unset("dhcpc_done");
		//dirty hack
		start_dhcpc(wan_ifname, NULL, NULL, 1);
		int timeout;

		for (timeout = 60; !nvram_match("dhcpc_done", "1") && timeout > 0; --timeout) {	/* wait for info from dhcp server */
			sleep(1);
		}
		stop_dhcpc();	/* we don't need dhcp client anymore */
		create_pptp_config(nvram_safe_get("pptp_server_ip"), username);

	} else {
		ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"),
			 nvram_safe_get("wan_netmask"));
		struct dns_lists *dns_list = NULL;
		dns_to_resolv();
		dns_list = get_dns_list();
		int i = 0;

		if (dns_list) {
			for (i = 0; i < dns_list->num_servers; i++)
				route_add(wan_ifname, 0,
					  dns_list->dns_server[i],
					  nvram_safe_get("pptp_wan_gateway"),
					  "255.255.255.255");
		}
		route_add(wan_ifname, 0, "0.0.0.0",
			  nvram_safe_get("pptp_wan_gateway"), "0.0.0.0");
		char pptpip[64];
		getIPFromName(nvram_safe_get("pptp_server_name"), pptpip);
		route_del(wan_ifname, 0, "0.0.0.0",
			  nvram_safe_get("pptp_wan_gateway"), "0.0.0.0");
		if (dns_list) {
			for (i = 0; i < dns_list->num_servers; i++)
				route_del(wan_ifname, 0,
					  dns_list->dns_server[i],
					  nvram_safe_get("pptp_wan_gateway"),
					  "255.255.255.255");
			free(dns_list);
		}
		
		nvram_set("pptp_server_ip", pptpip);
		if (!nvram_match("pptp_wan_gateway", "0.0.0.0"))
			route_add(wan_ifname, 0,
				  nvram_safe_get("pptp_server_ip"),
				  nvram_safe_get("pptp_wan_gateway"),
				  "255.255.255.255");
	}
	ret = _evalpid(pptp_argv, NULL, 0, NULL);

	if (nvram_match("ppp_demand", "1")) {
		/*
		 * Trigger Connect On Demand if user press Connect button in Status
		 * page 
		 */
		if (nvram_match("action_service", "start_pptp")
		    || nvram_match("action_service", "start_l2tp")) {
			start_force_to_dial();
			// force_to_dial(nvram_safe_get("action_service"));
			nvram_unset("action_service");
		}
		/*
		 * Trigger Connect On Demand if user ping pptp server 
		 */
		else {
			eval("listen", nvram_safe_get("lan_ifname"));
		}
	}
	stop_wland();
	start_wshaper();
	start_wland();
	cprintf("done\n");
	return;
}