blob: 50ac31e617ddbc5b703483150cba064b2b17e251 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/* dhcpc.c
3 *
4 * udhcp DHCP client
5 *
6 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 *
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9 */
10
Mike Frysinger7031f622006-05-08 03:20:50 +000011#include <getopt.h>
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000012#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000013
14#include "common.h"
15#include "dhcpd.h"
16#include "dhcpc.h"
17#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000018
Mike Frysinger7031f622006-05-08 03:20:50 +000019
Denis Vlasenko239369b2006-09-07 17:05:44 +000020/* Something is definitely wrong here. IPv4 addresses
21 * in variables of type long?? BTW, we use inet_ntoa()
22 * in the code. Manpage says that struct in_addr has a member of type long (!)
23 * which holds IPv4 address, and the struct is passed by value (!!)
24 */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000025static unsigned long timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +000026static unsigned long requested_ip; /* = 0 */
Denis Vlasenko74c9d232007-01-18 15:42:00 +000027static uint32_t server_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000028static int packet_num; /* = 0 */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000029static int sockfd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000030
31#define LISTEN_NONE 0
32#define LISTEN_KERNEL 1
33#define LISTEN_RAW 2
Denis Vlasenkofbd29182007-04-07 01:05:47 +000034static smallint listen_mode;
35
36static smallint state;
Mike Frysinger7031f622006-05-08 03:20:50 +000037
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000038struct client_config_t client_config;
39
Mike Frysinger7031f622006-05-08 03:20:50 +000040
Mike Frysinger7031f622006-05-08 03:20:50 +000041/* just a little helper */
42static void change_mode(int new_mode)
43{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000044 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000045 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Denis Vlasenkofbd29182007-04-07 01:05:47 +000046 if (sockfd >= 0) {
47 close(sockfd);
48 sockfd = -1;
49 }
Mike Frysinger7031f622006-05-08 03:20:50 +000050 listen_mode = new_mode;
51}
52
53
54/* perform a renew */
55static void perform_renew(void)
56{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000057 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000058 switch (state) {
59 case BOUND:
60 change_mode(LISTEN_KERNEL);
61 case RENEWING:
62 case REBINDING:
63 state = RENEW_REQUESTED;
64 break;
65 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000066 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000067 case REQUESTING:
68 case RELEASED:
69 change_mode(LISTEN_RAW);
70 state = INIT_SELECTING;
71 break;
72 case INIT_SELECTING:
73 break;
74 }
75
76 /* start things over */
77 packet_num = 0;
78
79 /* Kill any timeouts because the user wants this to hurry along */
80 timeout = 0;
81}
82
83
84/* perform a release */
85static void perform_release(void)
86{
87 char buffer[16];
88 struct in_addr temp_addr;
89
90 /* send release packet */
91 if (state == BOUND || state == RENEWING || state == REBINDING) {
92 temp_addr.s_addr = server_addr;
93 sprintf(buffer, "%s", inet_ntoa(temp_addr));
94 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000095 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000096 inet_ntoa(temp_addr), buffer);
97 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +000098 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000099 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000100 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000101
102 change_mode(LISTEN_NONE);
103 state = RELEASED;
104 timeout = 0x7fffffff;
105}
106
107
108static void client_background(void)
109{
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000110#ifdef __uClinux__
111 bb_error_msg("cannot background in uclinux (yet)");
112/* ... mainly because udhcpc calls client_background()
113 * in _the _middle _of _udhcpc _run_, not at the start!
114 * If that will be properly disabled for NOMMU, client_background()
115 * will work on NOMMU too */
116#else
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000117 bb_daemonize(0);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000118 /* rewrite pidfile, as our pid is different now */
119 if (client_config.pidfile)
120 write_pidfile(client_config.pidfile);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000121 logmode &= ~LOGMODE_STDIO;
122#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000123 client_config.foreground = 1; /* Do not fork again. */
124 client_config.background_if_no_lease = 0;
125}
126
127
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000128static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
129{
130 uint8_t *storage;
131 int len = strlen(str);
132 if (len > 255) len = 255;
133 storage = xzalloc(len + extra + OPT_DATA);
134 storage[OPT_CODE] = code;
135 storage[OPT_LEN] = len + extra;
136 memcpy(storage + extra + OPT_DATA, str, len);
137 return storage;
138}
139
140
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000141int udhcpc_main(int argc, char **argv);
142int udhcpc_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000143{
144 uint8_t *temp, *message;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000145 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
Mike Frysinger7031f622006-05-08 03:20:50 +0000146 unsigned long t1 = 0, t2 = 0, xid = 0;
Rob Landley49ea4662006-09-11 01:34:21 +0000147 unsigned long start = 0, lease = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000148 long now;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000149 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000150 int max_fd;
151 int sig;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000152 int retval;
153 int len;
Mike Frysinger7031f622006-05-08 03:20:50 +0000154 int no_clientid = 0;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000155 fd_set rfds;
156 struct timeval tv;
157 struct dhcpMessage packet;
158 struct in_addr temp_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000159
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000160 enum {
161 OPT_c = 1 << 0,
162 OPT_C = 1 << 1,
163 OPT_V = 1 << 2,
164 OPT_f = 1 << 3,
165 OPT_b = 1 << 4,
166 OPT_H = 1 << 5,
167 OPT_h = 1 << 6,
168 OPT_F = 1 << 7,
169 OPT_i = 1 << 8,
170 OPT_n = 1 << 9,
171 OPT_p = 1 << 10,
172 OPT_q = 1 << 11,
173 OPT_R = 1 << 12,
174 OPT_r = 1 << 13,
175 OPT_s = 1 << 14,
176 OPT_T = 1 << 15,
177 OPT_t = 1 << 16,
178 OPT_v = 1 << 17,
179 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000180#if ENABLE_GETOPT_LONG
Mike Frysinger7031f622006-05-08 03:20:50 +0000181 static const struct option arg_options[] = {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000182 { "clientid", required_argument, 0, 'c' },
183 { "clientid-none", no_argument, 0, 'C' },
184 { "vendorclass", required_argument, 0, 'V' },
185 { "foreground", no_argument, 0, 'f' },
186 { "background", no_argument, 0, 'b' },
187 { "hostname", required_argument, 0, 'H' },
188 { "hostname", required_argument, 0, 'h' },
189 { "fqdn", required_argument, 0, 'F' },
190 { "interface", required_argument, 0, 'i' },
191 { "now", no_argument, 0, 'n' },
192 { "pidfile", required_argument, 0, 'p' },
193 { "quit", no_argument, 0, 'q' },
194 { "release", no_argument, 0, 'R' },
195 { "request", required_argument, 0, 'r' },
196 { "script", required_argument, 0, 's' },
197 { "timeout", required_argument, 0, 'T' },
198 { "version", no_argument, 0, 'v' },
199 { "retries", required_argument, 0, 't' },
200 { 0, 0, 0, 0 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000201 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000202#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000203 /* Default options. */
204 client_config.interface = "eth0";
205 client_config.script = DEFAULT_SCRIPT;
206 client_config.retries = 3;
207 client_config.timeout = 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000208
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000209 /* Parse command line */
210 opt_complementary = "?:c--C:C--c" // mutually exclusive
211 ":hH:Hh"; // -h and -H are the same
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000212#if ENABLE_GETOPT_LONG
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000213 applet_long_options = arg_options;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000214#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000215 opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
216 &str_c, &str_V, &str_h, &str_h, &str_F,
217 &client_config.interface, &client_config.pidfile, &str_r,
218 &client_config.script, &str_T, &str_t
219 );
220
221 if (opt & OPT_c)
222 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
223 if (opt & OPT_C)
224 no_clientid = 1;
225 if (opt & OPT_V)
226 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
227 if (opt & OPT_f)
228 client_config.foreground = 1;
229 if (opt & OPT_b)
230 client_config.background_if_no_lease = 1;
231 if (opt & OPT_h)
232 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
233 if (opt & OPT_F) {
234 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
235 /* Flags: 0000NEOS
236 S: 1 => Client requests Server to update A RR in DNS as well as PTR
237 O: 1 => Server indicates to client that DNS has been updated regardless
238 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
239 N: 1 => Client requests Server to not update DNS
240 */
241 client_config.fqdn[OPT_DATA + 0] = 0x1;
242 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
243 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
244 }
245 // if (opt & OPT_i) client_config.interface = ...
246 if (opt & OPT_n)
247 client_config.abort_if_no_lease = 1;
248 // if (opt & OPT_p) client_config.pidfile = ...
249 if (opt & OPT_q)
250 client_config.quit_after_lease = 1;
251 if (opt & OPT_R)
252 client_config.release_on_quit = 1;
253 if (opt & OPT_r)
254 requested_ip = inet_addr(str_r);
255 // if (opt & OPT_s) client_config.script = ...
256 if (opt & OPT_T)
257 client_config.timeout = xatoi_u(str_T);
258 if (opt & OPT_t)
259 client_config.retries = xatoi_u(str_t);
260 if (opt & OPT_v) {
261 printf("version %s\n\n", BB_VER);
262 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000263 }
264
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000265 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
266 openlog(applet_name, LOG_PID, LOG_LOCAL0);
267 logmode |= LOGMODE_SYSLOG;
268 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000269
270 if (read_interface(client_config.interface, &client_config.ifindex,
271 NULL, client_config.arp) < 0)
272 return 1;
273
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000274 /* Sanitize fd's and write pidfile */
275 udhcp_make_pidfile(client_config.pidfile);
276
Mike Frysinger7031f622006-05-08 03:20:50 +0000277 /* if not set, and not suppressed, setup the default client ID */
278 if (!client_config.clientid && !no_clientid) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000279 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000280 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000281 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000282 }
283
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000284 if (!client_config.vendorclass)
285 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000286
287 /* setup the signal pipe */
288 udhcp_sp_setup();
289
290 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000291 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000292 change_mode(LISTEN_RAW);
293
294 for (;;) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000295 tv.tv_sec = timeout - uptime();
296 tv.tv_usec = 0;
297
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000298 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000299 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000300 sockfd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000301 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000302 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000303 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000304 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000305
306 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000307 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000308 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
309 } else retval = 0; /* If we already timed out, fall through */
310
311 now = uptime();
312 if (retval == 0) {
313 /* timeout dropped to zero */
314 switch (state) {
315 case INIT_SELECTING:
316 if (packet_num < client_config.retries) {
317 if (packet_num == 0)
318 xid = random_xid();
319
320 /* send discover packet */
321 send_discover(xid, requested_ip); /* broadcast */
322
323 timeout = now + client_config.timeout;
324 packet_num++;
325 } else {
Rob Landley3f785612006-05-28 01:06:36 +0000326 udhcp_run_script(NULL, "leasefail");
Mike Frysinger7031f622006-05-08 03:20:50 +0000327 if (client_config.background_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000328 bb_info_msg("No lease, forking to background");
Mike Frysinger7031f622006-05-08 03:20:50 +0000329 client_background();
330 } else if (client_config.abort_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000331 bb_info_msg("No lease, failing");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000332 retval = 1;
333 goto ret;
Mike Frysinger7031f622006-05-08 03:20:50 +0000334 }
335 /* wait to try again */
336 packet_num = 0;
337 timeout = now + 60;
338 }
339 break;
340 case RENEW_REQUESTED:
341 case REQUESTING:
342 if (packet_num < client_config.retries) {
343 /* send request packet */
344 if (state == RENEW_REQUESTED)
345 send_renew(xid, server_addr, requested_ip); /* unicast */
346 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
347
348 timeout = now + ((packet_num == 2) ? 10 : 2);
349 packet_num++;
350 } else {
351 /* timed out, go back to init state */
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000352 if (state == RENEW_REQUESTED)
353 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000354 state = INIT_SELECTING;
355 timeout = now;
356 packet_num = 0;
357 change_mode(LISTEN_RAW);
358 }
359 break;
360 case BOUND:
361 /* Lease is starting to run out, time to enter renewing state */
362 state = RENEWING;
363 change_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000364 DEBUG("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000365 /* fall right through */
366 case RENEWING:
367 /* Either set a new T1, or enter REBINDING state */
368 if ((t2 - t1) <= (lease / 14400 + 1)) {
369 /* timed out, enter rebinding state */
370 state = REBINDING;
371 timeout = now + (t2 - t1);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000372 DEBUG("Entering rebinding state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000373 } else {
374 /* send a request packet */
375 send_renew(xid, server_addr, requested_ip); /* unicast */
376
377 t1 = (t2 - t1) / 2 + t1;
378 timeout = t1 + start;
379 }
380 break;
381 case REBINDING:
382 /* Either set a new T2, or enter INIT state */
383 if ((lease - t2) <= (lease / 14400 + 1)) {
384 /* timed out, enter init state */
385 state = INIT_SELECTING;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000386 bb_info_msg("Lease lost, entering init state");
Rob Landley3f785612006-05-28 01:06:36 +0000387 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000388 timeout = now;
389 packet_num = 0;
390 change_mode(LISTEN_RAW);
391 } else {
392 /* send a request packet */
393 send_renew(xid, 0, requested_ip); /* broadcast */
394
395 t2 = (lease - t2) / 2 + t2;
396 timeout = t2 + start;
397 }
398 break;
399 case RELEASED:
400 /* yah, I know, *you* say it would never happen */
401 timeout = 0x7fffffff;
402 break;
403 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000404 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000405 /* a packet is ready, read it */
406
407 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000408 len = udhcp_get_packet(&packet, sockfd);
409 else len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000410
411 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000412 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000413 change_mode(listen_mode); /* just close and reopen */
414 }
415 if (len < 0) continue;
416
417 if (packet.xid != xid) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000418 DEBUG("Ignoring XID %lx (our xid is %lx)",
Mike Frysinger7031f622006-05-08 03:20:50 +0000419 (unsigned long) packet.xid, xid);
420 continue;
421 }
422
423 /* Ignore packets that aren't for us */
424 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000425 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000426 continue;
427 }
428
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000429 message = get_option(&packet, DHCP_MESSAGE_TYPE);
430 if (message == NULL) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000431 bb_error_msg("cannot get option from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000432 continue;
433 }
434
435 switch (state) {
436 case INIT_SELECTING:
437 /* Must be a DHCPOFFER to one of our xid's */
438 if (*message == DHCPOFFER) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000439 temp = get_option(&packet, DHCP_SERVER_ID);
440 if (temp) {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000441 /* can be misaligned, thus memcpy */
442 memcpy(&server_addr, temp, 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000443 xid = packet.xid;
444 requested_ip = packet.yiaddr;
445
446 /* enter requesting state */
447 state = REQUESTING;
448 timeout = now;
449 packet_num = 0;
450 } else {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000451 bb_error_msg("no server ID in message");
Mike Frysinger7031f622006-05-08 03:20:50 +0000452 }
453 }
454 break;
455 case RENEW_REQUESTED:
456 case REQUESTING:
457 case RENEWING:
458 case REBINDING:
459 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000460 temp = get_option(&packet, DHCP_LEASE_TIME);
461 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000462 bb_error_msg("no lease time with ACK, using 1 hour lease");
Mike Frysinger7031f622006-05-08 03:20:50 +0000463 lease = 60 * 60;
464 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000465 /* can be misaligned, thus memcpy */
466 memcpy(&lease, temp, 4);
467 lease = ntohl(lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000468 }
469
470 /* enter bound state */
471 t1 = lease / 2;
472
473 /* little fixed point for n * .875 */
474 t2 = (lease * 0x7) >> 3;
475 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000476 bb_info_msg("Lease of %s obtained, lease time %ld",
Mike Frysinger7031f622006-05-08 03:20:50 +0000477 inet_ntoa(temp_addr), lease);
478 start = now;
479 timeout = t1 + start;
480 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000481 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000482 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
483
484 state = BOUND;
485 change_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000486 if (client_config.quit_after_lease) {
487 if (client_config.release_on_quit)
488 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000489 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000490 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000491 if (!client_config.foreground)
492 client_background();
493
494 } else if (*message == DHCPNAK) {
495 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000496 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000497 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000498 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000499 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000500 state = INIT_SELECTING;
501 timeout = now;
502 requested_ip = 0;
503 packet_num = 0;
504 change_mode(LISTEN_RAW);
505 sleep(3); /* avoid excessive network traffic */
506 }
507 break;
508 /* case BOUND, RELEASED: - ignore all packets */
509 }
510 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
511 switch (sig) {
512 case SIGUSR1:
513 perform_renew();
514 break;
515 case SIGUSR2:
516 perform_release();
517 break;
518 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000519 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000520 if (client_config.release_on_quit)
521 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000522 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000523 }
524 } else if (retval == -1 && errno == EINTR) {
525 /* a signal was caught */
526 } else {
527 /* An error occured */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000528 bb_perror_msg("select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000529 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000530 } /* for (;;) */
531 ret0:
532 retval = 0;
533 ret:
534 if (client_config.pidfile)
535 remove_pidfile(client_config.pidfile);
536 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000537}