1#ifndef FLASH2M
2/*
3 * udhcpc scripts
4 *
5 * Copyright 2004, Broadcom Corporation
6 * All Rights Reserved.
7 *
8 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
9 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
10 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
11 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
12 *
13 * $Id: udhcpc_ex.c,v 1.1.1.1 2008/10/15 03:28:48 james26_jang Exp $
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <net/route.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <errno.h>
24#include <time.h>
25#include <unistd.h>
26
27#include <bcmnvram.h>
28#include <netconf.h>
29#include <shutils.h>
30#include <rc.h>
31
32static char udhcpstate[8];
33
34static int
35expires(char *lan_ifname, unsigned int in)
36{
37	time_t now;
38	FILE *fp;
39	char tmp[100];
40
41	time(&now);
42	snprintf(tmp, sizeof(tmp), "/tmp/udhcpc%d.expires", 0);
43	if (!(fp = fopen(tmp, "w"))) {
44		perror(tmp);
45		return errno;
46	}
47	fprintf(fp, "%d", (unsigned int) now + in);
48	fclose(fp);
49	return 0;
50}
51
52/*
53 * deconfig: This argument is used when udhcpc starts, and when a
54 * leases is lost. The script should put the interface in an up, but
55 * deconfigured state.
56*/
57static int
58deconfig(void)
59{
60	char *lan_ifname = safe_getenv("interface");
61
62	ifconfig(lan_ifname, IFUP,
63		 nvram_safe_get("lan_ipaddr"),
64		 nvram_safe_get("lan_netmask"));
65
66	expires(lan_ifname, 0);
67
68	lan_down_ex(lan_ifname);
69
70	logmessage("dhcp client", "%s: lease is lost", udhcpstate);
71	//wanmessage("lost IP from server");
72
73	dprintf("done\n");
74	return 0;
75}
76
77/*
78 * bound: This argument is used when udhcpc moves from an unbound, to
79 * a bound state. All of the paramaters are set in enviromental
80 * variables, The script should configure the interface, and set any
81 * other relavent parameters (default gateway, dns server, etc).
82*/
83static int
84bound(void)
85{
86	char *lan_ifname = safe_getenv("interface");
87	char *value, *value1;
88	char tmp[100], prefix[] = "lanXXXXXXXXXX_";
89	struct in_addr fixed_addr;
90
91	snprintf(prefix, sizeof(prefix), "lan_");
92	if ((value = getenv("ip")))
93	{
94		inet_aton(value, &fixed_addr);
95		value = inet_ntoa(fixed_addr);
96		nvram_set(strcat_r(prefix, "ipaddr_t", tmp), value);
97	}
98	if ((value = getenv("subnet")))
99		nvram_set(strcat_r(prefix, "netmask_t", tmp), value);
100	if ((value = getenv("router")))
101		nvram_set(strcat_r(prefix, "gateway_t", tmp), value);
102	if ((value = getenv("dns")))
103		nvram_set(strcat_r(prefix, "dns_t", tmp), value);
104	if ((value = getenv("wins")))
105		nvram_set(strcat_r(prefix, "wins_t", tmp), value);
106//	if ((value = getenv("hostname")))
107//		sethostname(value, strlen(value) + 1);
108	if ((value = getenv("domain")))
109		nvram_set(strcat_r(prefix, "domain_t", tmp), value);
110	if ((value = getenv("lease"))) {
111		nvram_set(strcat_r(prefix, "lease_t", tmp), value);
112		expires(lan_ifname, atoi(value));
113	}
114
115	ifconfig(lan_ifname, IFUP,
116		 nvram_safe_get(strcat_r(prefix, "ipaddr_t", tmp)),
117		 nvram_safe_get(strcat_r(prefix, "netmask_t", tmp)));
118
119	lan_up_ex(lan_ifname);
120
121	logmessage("dhcp client", "%s IP : %s from %s",
122		udhcpstate,
123		nvram_safe_get(strcat_r(prefix, "ipaddr_t", tmp)),
124		nvram_safe_get(strcat_r(prefix, "gateway_t", tmp)));
125
126	//wanmessage("");
127
128// 2009.09 James. {
129	char *wsc_argv[] = {"/bin/wsccmd", NULL};
130	pid_t pid;
131
132	nvram_set("wan_ready", "1");
133
134	if(!strcmp(nvram_safe_get("wan_route_x"), "IP_Bridged")
135			&& !strcmp(nvram_safe_get("system_ready"), "1")){
136csprintf("+++ Restart WSC from udhcpc. +++\n");
137		eval("killall", "wsccmd");
138		eval("killall", "nas");
139		//sleep(1);
140		eval("ifconfig", "br0", "down");
141		//sleep(1);
142		eval("ifconfig", "br0", "up");
143		//sleep(1);
144		_eval(wsc_argv, NULL, 0, &pid);
145	}
146// 2009.09 James. }
147	dprintf("done\n");
148	return 0;
149}
150
151/*
152 * renew: This argument is used when a DHCP lease is renewed. All of
153 * the paramaters are set in enviromental variables. This argument is
154 * used when the interface is already configured, so the IP address,
155 * will not change, however, the other DHCP paramaters, such as the
156 * default gateway, subnet mask, and dns server may change.
157 */
158static int
159renew(void)
160{
161	bound();
162
163	dprintf("done\n");
164	return 0;
165}
166
167int
168udhcpc_ex_main(int argc, char **argv)
169{
170	if (argv[1]) strcpy(udhcpstate, argv[1]);
171
172	if (!argv[1])
173		return EINVAL;
174	else if (strstr(argv[1], "deconfig"))
175		return deconfig();
176	else if (strstr(argv[1], "bound"))
177		return bound();
178	else if (strstr(argv[1], "renew"))
179		return renew();
180	else if (strstr(argv[1], "leasefail"))
181		return 0;
182	else return deconfig();
183}
184#endif
185