summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/udhcpc_script.c
diff options
context:
space:
mode:
Diffstat (limited to 'urunlevel/runlevel/udhcpc_script.c')
-rw-r--r--urunlevel/runlevel/udhcpc_script.c191
1 files changed, 191 insertions, 0 deletions
diff --git a/urunlevel/runlevel/udhcpc_script.c b/urunlevel/runlevel/udhcpc_script.c
new file mode 100644
index 0000000..5d5f592
--- /dev/null
+++ b/urunlevel/runlevel/udhcpc_script.c
@@ -0,0 +1,191 @@
1/*
2 * Mini udhcpc_script implementation for busybox.
3 *
4 * Copyright (C) 2004 by David Seikel won_fang@yahoo.com.au
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 *
21 */
22
23#include <errno.h>
24#include <string.h>
25#include <unistd.h>
26
27#include "busybox.h"
28#include "lib_init_d.h"
29
30
31static void printit(char *name, char *description, char *value)
32{
33 if (value != NULL)
34 bb_printf("%s=%s\t\t- %s\n", name, value, description);
35}
36
37int udhcpc_script_main(int argc, char **argv)
38{
39 argv += optind;
40
41#if 0
42 printit("action", "What action the script should perform", *argv);
43 printit("HOME", "The set $HOME env or '/'", getenv("HOME"));
44 printit("PATH", "the set $PATH env or '/bin:/usr/bin:/sbin:/usr/sbin'", getenv("PATH"));
45 printit("interface", "The interface this was obtained on", getenv("interface"));
46 printit("ip", "The obtained IP", getenv("ip"));
47 printit("mask", "The number of bits in the netmask (ie: 24)", getenv("mask"));
48 printit("siaddr", "The bootp next server option", getenv("siaddr"));
49 printit("sname", "The bootp server name option", getenv("sname"));
50 printit("boot_file", "The bootp boot file option", getenv("boot_file"));
51 printit("subnet", "The assigend subnet mask", getenv("subnet"));
52 printit("timezone", "Offset in seconds from UTC", getenv("timezone"));
53 printit("router", "A list of routers", getenv("router"));
54 printit("timesvr", "A list of time servers", getenv("timesvr"));
55 printit("namesvr", "A list of IEN 116 name servers", getenv("namesvr"));
56 printit("dns", "A list of DNS server", getenv("dns"));
57 printit("logsvr", "A list of MIT-LCS UDP log servers", getenv("logsvr"));
58 printit("cookiesvr", "A list of RFC 865 cookie servers", getenv("cookiesvr"));
59 printit("lprsvr", "A list of LPR servers", getenv("lprsvr"));
60 printit("hostname", "The assigned hostname", getenv("hostname"));
61 printit("bootsize", "The length in 512 octect blocks of the bootfile", getenv("bootsize"));
62 printit("domain", "The domain name of the network", getenv("domain"));
63 printit("swapsvr", "The IP address of the client's swap server", getenv("swapsvr"));
64 printit("rootpath", "The path name of the client's root disk", getenv("rootpath"));
65 printit("ipttl", "The TTL to use for this network", getenv("ipttl"));
66 printit("mtu", "The MTU to use for this network", getenv("mtu"));
67 printit("broadcast", "The broadcast address for this network", getenv("broadcast"));
68 printit("ntpsrv", "A list of NTP servers", getenv("ntpsrv"));
69 printit("wins", "A list of WINS servers", getenv("wins"));
70 printit("lease", "The lease time, in seconds", getenv("lease"));
71 printit("dhcptype", "DHCP message type (safely ignored)", getenv("dhcptype"));
72 printit("serverid", "The IP of the server", getenv("serverid"));
73 printit("message", "Reason for a DHCPNAK", getenv("message"));
74 printit("tftp", "The TFTP server name", getenv("tftp"));
75 printit("bootfile", "The bootfile name", getenv("bootfile"));
76#endif
77
78 if (!*argv)
79 bb_printf("Error: should be called from udhcpc\n");
80 else
81 {
82 char *interface = getenv("interface");
83
84 if (strcmp(*argv, "deconfig") == 0)
85 {
86#ifdef CONFIG_FEATURE_IFUPDOWN_IP
87 doit(QUIET, "ip route flush dev %s", interface);
88 doit(QUIET, "ip addr flush dev %s", interface);
89 doit(QUIET, "ip link set %s up", interface);
90#else
91 errno = 0;
92 while (errno == 0)
93 doit(QUIET, "route del default gw 0.0.0.0 dev %s", interface);
94 doit(QUIET, "ifconfig %s 0.0.0.0", interface);
95#endif
96 }
97 else if (strcmp(*argv, "nak") == 0)
98 bb_printf("udhcpc received a NAK: %s\n", getenv("message"));
99 else if ((strcmp(*argv, "bound") == 0) || (strcmp(*argv, "renew") == 0))
100 {
101 const char *RESOLV_CONF = "/etc/resolv.conf";
102 char *broadcast = getenv("broadcast");
103 char *subnet = getenv("subnet");
104 char *mask = getenv("mask");
105 char *ip = getenv("ip");
106 char *hostname = getenv("hostname");
107 char *domain = getenv("domain");
108 char *router = getenv("router");
109 char *dns = getenv("dns");
110 char *siaddr = getenv("siaddr");
111 char *tftp = getenv("tftp");
112 char *BROADCAST = "";
113 char *NETMASK = "";
114 char *MASK = "";
115 char *HOSTNAME = 0;
116 int i, metric = 0;
117 char *token;
118 char *strtok_temp;
119
120 if (broadcast != 0)
121 bb_xasprintf(&BROADCAST, "broadcast %s", broadcast);
122 if (subnet != 0)
123 bb_xasprintf(&NETMASK, "netmask %s", subnet);
124 if (mask != 0)
125 bb_xasprintf(&MASK, "/%s", mask);
126 if (hostname != 0)
127 bb_xasprintf(&HOSTNAME, "/%s", hostname);
128#ifdef CONFIG_FEATURE_IFUPDOWN_IP
129 doit(0, "ip addr add %s%s %s dev %s", ip, MASK, BROADCAST, interface);
130 doit(0, "ip link set %s up", interface);
131#else
132 doit(0, "/sbin/ifconfig %s %s %s %s", interface, ip, BROADCAST, NETMASK);
133#endif
134 if (router != 0)
135 {
136 token = strtok_r(router, " ,\t\r\n", &strtok_temp);
137 for (i = 0; token != NULL; i++)
138 {
139#ifdef CONFIG_FEATURE_IFUPDOWN_IP
140 doit(0, "ip route add to default via %s dev %s", token, interface, metric++);
141#else
142 doit(0, "route add default gw %s dev %s metric %d", token, interface, metric++);
143#endif
144 token = strtok_r(NULL, " ,\t\r\n", &strtok_temp);
145 }
146 }
147
148 doit(0, "echo -n > %s", RESOLV_CONF);
149 if (domain != 0)
150 doit(0, "echo \"search %s\" > %s", domain, RESOLV_CONF);
151
152 token = strtok_r(dns, " \r\n", &strtok_temp);
153 for (i = 0; token != NULL; i++)
154 {
155 doit(0, "echo \"nameserver %s\" >> %s", token, RESOLV_CONF);
156
157 token = strtok_r(NULL, " \r\n", &strtok_temp);
158 }
159
160 if (HOSTNAME == 0)
161 {
162 bb_xasprintf(&HOSTNAME, doit(REDIR, "nslookup %s | grep -v default | grep Name | cut -d\":\" -f2 | tr -d ' ' | tail -n 1", ip));
163 if ((HOSTNAME == 0) && (stat("/var/lib/my_linux/config/hostname", &path_stat) == 0))
164 HOSTNAME = quick_read("/var/lib/my_linux/config/hostname");
165 if (HOSTNAME == 0)
166 HOSTNAME = "my_linux";
167 }
168 doit(0, "hostname %s", HOSTNAME);
169// Should remove old entry
170 doit(0, "echo \"%s %s\" >> /etc/hosts", ip, HOSTNAME);
171
172// Should dump these in a per interface file
173 if (siaddr != NULL)
174 quick_write("/var/lib/network/nfsaddr", siaddr);
175 if (tftp != NULL)
176 quick_write("/var/lib/network/tftpaddr", tftp);
177
178 /* This is what the network init script is waiting for. */
179 quick_write("/var/lib/network/ipaddr", ip);
180 bb_printf("IP %s HOSTNAME %s\n", ip, HOSTNAME);
181
182 free(HOSTNAME);
183 }
184 else
185 {
186 bb_printf("Huh?\n");
187 }
188 }
189
190 return EXIT_SUCCESS;
191}