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