blob: 362e701694fbbcdf646ac8c8f764261242990ad8 [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 Vlasenkofbd29182007-04-07 01:05:47 +0000117// chdir(/) is problematic. Imagine that e.g. pidfile name is RELATIVE! what will unlink do then, eh?
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000118 bb_daemonize(DAEMON_CHDIR_ROOT);
119 logmode &= ~LOGMODE_STDIO;
120#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000121 client_config.foreground = 1; /* Do not fork again. */
122 client_config.background_if_no_lease = 0;
123}
124
125
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000126static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
127{
128 uint8_t *storage;
129 int len = strlen(str);
130 if (len > 255) len = 255;
131 storage = xzalloc(len + extra + OPT_DATA);
132 storage[OPT_CODE] = code;
133 storage[OPT_LEN] = len + extra;
134 memcpy(storage + extra + OPT_DATA, str, len);
135 return storage;
136}
137
138
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000139int udhcpc_main(int argc, char **argv);
140int udhcpc_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000141{
142 uint8_t *temp, *message;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000143 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
Mike Frysinger7031f622006-05-08 03:20:50 +0000144 unsigned long t1 = 0, t2 = 0, xid = 0;
Rob Landley49ea4662006-09-11 01:34:21 +0000145 unsigned long start = 0, lease = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000146 long now;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000147 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000148 int max_fd;
149 int sig;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000150 int retval;
151 int len;
Mike Frysinger7031f622006-05-08 03:20:50 +0000152 int no_clientid = 0;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000153 fd_set rfds;
154 struct timeval tv;
155 struct dhcpMessage packet;
156 struct in_addr temp_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000157
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000158 enum {
159 OPT_c = 1 << 0,
160 OPT_C = 1 << 1,
161 OPT_V = 1 << 2,
162 OPT_f = 1 << 3,
163 OPT_b = 1 << 4,
164 OPT_H = 1 << 5,
165 OPT_h = 1 << 6,
166 OPT_F = 1 << 7,
167 OPT_i = 1 << 8,
168 OPT_n = 1 << 9,
169 OPT_p = 1 << 10,
170 OPT_q = 1 << 11,
171 OPT_R = 1 << 12,
172 OPT_r = 1 << 13,
173 OPT_s = 1 << 14,
174 OPT_T = 1 << 15,
175 OPT_t = 1 << 16,
176 OPT_v = 1 << 17,
177 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000178#if ENABLE_GETOPT_LONG
Mike Frysinger7031f622006-05-08 03:20:50 +0000179 static const struct option arg_options[] = {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000180 { "clientid", required_argument, 0, 'c' },
181 { "clientid-none", no_argument, 0, 'C' },
182 { "vendorclass", required_argument, 0, 'V' },
183 { "foreground", no_argument, 0, 'f' },
184 { "background", no_argument, 0, 'b' },
185 { "hostname", required_argument, 0, 'H' },
186 { "hostname", required_argument, 0, 'h' },
187 { "fqdn", required_argument, 0, 'F' },
188 { "interface", required_argument, 0, 'i' },
189 { "now", no_argument, 0, 'n' },
190 { "pidfile", required_argument, 0, 'p' },
191 { "quit", no_argument, 0, 'q' },
192 { "release", no_argument, 0, 'R' },
193 { "request", required_argument, 0, 'r' },
194 { "script", required_argument, 0, 's' },
195 { "timeout", required_argument, 0, 'T' },
196 { "version", no_argument, 0, 'v' },
197 { "retries", required_argument, 0, 't' },
198 { 0, 0, 0, 0 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000199 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000200#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000201 /* Default options. */
202 client_config.interface = "eth0";
203 client_config.script = DEFAULT_SCRIPT;
204 client_config.retries = 3;
205 client_config.timeout = 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000206
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000207 /* Parse command line */
208 opt_complementary = "?:c--C:C--c" // mutually exclusive
209 ":hH:Hh"; // -h and -H are the same
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000210#if ENABLE_GETOPT_LONG
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000211 applet_long_options = arg_options;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000212#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000213 opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
214 &str_c, &str_V, &str_h, &str_h, &str_F,
215 &client_config.interface, &client_config.pidfile, &str_r,
216 &client_config.script, &str_T, &str_t
217 );
218
219 if (opt & OPT_c)
220 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
221 if (opt & OPT_C)
222 no_clientid = 1;
223 if (opt & OPT_V)
224 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
225 if (opt & OPT_f)
226 client_config.foreground = 1;
227 if (opt & OPT_b)
228 client_config.background_if_no_lease = 1;
229 if (opt & OPT_h)
230 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
231 if (opt & OPT_F) {
232 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
233 /* Flags: 0000NEOS
234 S: 1 => Client requests Server to update A RR in DNS as well as PTR
235 O: 1 => Server indicates to client that DNS has been updated regardless
236 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
237 N: 1 => Client requests Server to not update DNS
238 */
239 client_config.fqdn[OPT_DATA + 0] = 0x1;
240 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
241 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
242 }
243 // if (opt & OPT_i) client_config.interface = ...
244 if (opt & OPT_n)
245 client_config.abort_if_no_lease = 1;
246 // if (opt & OPT_p) client_config.pidfile = ...
247 if (opt & OPT_q)
248 client_config.quit_after_lease = 1;
249 if (opt & OPT_R)
250 client_config.release_on_quit = 1;
251 if (opt & OPT_r)
252 requested_ip = inet_addr(str_r);
253 // if (opt & OPT_s) client_config.script = ...
254 if (opt & OPT_T)
255 client_config.timeout = xatoi_u(str_T);
256 if (opt & OPT_t)
257 client_config.retries = xatoi_u(str_t);
258 if (opt & OPT_v) {
259 printf("version %s\n\n", BB_VER);
260 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000261 }
262
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000263 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
264 openlog(applet_name, LOG_PID, LOG_LOCAL0);
265 logmode |= LOGMODE_SYSLOG;
266 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000267
268 if (read_interface(client_config.interface, &client_config.ifindex,
269 NULL, client_config.arp) < 0)
270 return 1;
271
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000272 /* Sanitize fd's and write pidfile */
273 udhcp_make_pidfile(client_config.pidfile);
274
Mike Frysinger7031f622006-05-08 03:20:50 +0000275 /* if not set, and not suppressed, setup the default client ID */
276 if (!client_config.clientid && !no_clientid) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000277 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000278 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000279 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000280 }
281
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000282 if (!client_config.vendorclass)
283 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000284
285 /* setup the signal pipe */
286 udhcp_sp_setup();
287
288 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000289 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000290 change_mode(LISTEN_RAW);
291
292 for (;;) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000293 tv.tv_sec = timeout - uptime();
294 tv.tv_usec = 0;
295
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000296 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000297 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000298 sockfd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000299 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000300 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000301 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000302 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000303
304 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000305 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000306 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
307 } else retval = 0; /* If we already timed out, fall through */
308
309 now = uptime();
310 if (retval == 0) {
311 /* timeout dropped to zero */
312 switch (state) {
313 case INIT_SELECTING:
314 if (packet_num < client_config.retries) {
315 if (packet_num == 0)
316 xid = random_xid();
317
318 /* send discover packet */
319 send_discover(xid, requested_ip); /* broadcast */
320
321 timeout = now + client_config.timeout;
322 packet_num++;
323 } else {
Rob Landley3f785612006-05-28 01:06:36 +0000324 udhcp_run_script(NULL, "leasefail");
Mike Frysinger7031f622006-05-08 03:20:50 +0000325 if (client_config.background_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000326 bb_info_msg("No lease, forking to background");
Mike Frysinger7031f622006-05-08 03:20:50 +0000327 client_background();
328 } else if (client_config.abort_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000329 bb_info_msg("No lease, failing");
Mike Frysinger7031f622006-05-08 03:20:50 +0000330 return 1;
331 }
332 /* wait to try again */
333 packet_num = 0;
334 timeout = now + 60;
335 }
336 break;
337 case RENEW_REQUESTED:
338 case REQUESTING:
339 if (packet_num < client_config.retries) {
340 /* send request packet */
341 if (state == RENEW_REQUESTED)
342 send_renew(xid, server_addr, requested_ip); /* unicast */
343 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
344
345 timeout = now + ((packet_num == 2) ? 10 : 2);
346 packet_num++;
347 } else {
348 /* timed out, go back to init state */
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000349 if (state == RENEW_REQUESTED)
350 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000351 state = INIT_SELECTING;
352 timeout = now;
353 packet_num = 0;
354 change_mode(LISTEN_RAW);
355 }
356 break;
357 case BOUND:
358 /* Lease is starting to run out, time to enter renewing state */
359 state = RENEWING;
360 change_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000361 DEBUG("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000362 /* fall right through */
363 case RENEWING:
364 /* Either set a new T1, or enter REBINDING state */
365 if ((t2 - t1) <= (lease / 14400 + 1)) {
366 /* timed out, enter rebinding state */
367 state = REBINDING;
368 timeout = now + (t2 - t1);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000369 DEBUG("Entering rebinding state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000370 } else {
371 /* send a request packet */
372 send_renew(xid, server_addr, requested_ip); /* unicast */
373
374 t1 = (t2 - t1) / 2 + t1;
375 timeout = t1 + start;
376 }
377 break;
378 case REBINDING:
379 /* Either set a new T2, or enter INIT state */
380 if ((lease - t2) <= (lease / 14400 + 1)) {
381 /* timed out, enter init state */
382 state = INIT_SELECTING;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000383 bb_info_msg("Lease lost, entering init state");
Rob Landley3f785612006-05-28 01:06:36 +0000384 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000385 timeout = now;
386 packet_num = 0;
387 change_mode(LISTEN_RAW);
388 } else {
389 /* send a request packet */
390 send_renew(xid, 0, requested_ip); /* broadcast */
391
392 t2 = (lease - t2) / 2 + t2;
393 timeout = t2 + start;
394 }
395 break;
396 case RELEASED:
397 /* yah, I know, *you* say it would never happen */
398 timeout = 0x7fffffff;
399 break;
400 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000401 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000402 /* a packet is ready, read it */
403
404 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000405 len = udhcp_get_packet(&packet, sockfd);
406 else len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000407
408 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000409 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000410 change_mode(listen_mode); /* just close and reopen */
411 }
412 if (len < 0) continue;
413
414 if (packet.xid != xid) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000415 DEBUG("Ignoring XID %lx (our xid is %lx)",
Mike Frysinger7031f622006-05-08 03:20:50 +0000416 (unsigned long) packet.xid, xid);
417 continue;
418 }
419
420 /* Ignore packets that aren't for us */
421 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000422 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000423 continue;
424 }
425
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000426 message = get_option(&packet, DHCP_MESSAGE_TYPE);
427 if (message == NULL) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000428 bb_error_msg("cannot get option from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000429 continue;
430 }
431
432 switch (state) {
433 case INIT_SELECTING:
434 /* Must be a DHCPOFFER to one of our xid's */
435 if (*message == DHCPOFFER) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000436 temp = get_option(&packet, DHCP_SERVER_ID);
437 if (temp) {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000438 /* can be misaligned, thus memcpy */
439 memcpy(&server_addr, temp, 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000440 xid = packet.xid;
441 requested_ip = packet.yiaddr;
442
443 /* enter requesting state */
444 state = REQUESTING;
445 timeout = now;
446 packet_num = 0;
447 } else {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000448 bb_error_msg("no server ID in message");
Mike Frysinger7031f622006-05-08 03:20:50 +0000449 }
450 }
451 break;
452 case RENEW_REQUESTED:
453 case REQUESTING:
454 case RENEWING:
455 case REBINDING:
456 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000457 temp = get_option(&packet, DHCP_LEASE_TIME);
458 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000459 bb_error_msg("no lease time with ACK, using 1 hour lease");
Mike Frysinger7031f622006-05-08 03:20:50 +0000460 lease = 60 * 60;
461 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000462 /* can be misaligned, thus memcpy */
463 memcpy(&lease, temp, 4);
464 lease = ntohl(lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000465 }
466
467 /* enter bound state */
468 t1 = lease / 2;
469
470 /* little fixed point for n * .875 */
471 t2 = (lease * 0x7) >> 3;
472 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000473 bb_info_msg("Lease of %s obtained, lease time %ld",
Mike Frysinger7031f622006-05-08 03:20:50 +0000474 inet_ntoa(temp_addr), lease);
475 start = now;
476 timeout = t1 + start;
477 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000478 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000479 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
480
481 state = BOUND;
482 change_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000483 if (client_config.quit_after_lease) {
484 if (client_config.release_on_quit)
485 perform_release();
Mike Frysinger7031f622006-05-08 03:20:50 +0000486 return 0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000487 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000488 if (!client_config.foreground)
489 client_background();
490
491 } else if (*message == DHCPNAK) {
492 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000493 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000494 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000495 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000496 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000497 state = INIT_SELECTING;
498 timeout = now;
499 requested_ip = 0;
500 packet_num = 0;
501 change_mode(LISTEN_RAW);
502 sleep(3); /* avoid excessive network traffic */
503 }
504 break;
505 /* case BOUND, RELEASED: - ignore all packets */
506 }
507 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
508 switch (sig) {
509 case SIGUSR1:
510 perform_renew();
511 break;
512 case SIGUSR2:
513 perform_release();
514 break;
515 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000516 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000517 if (client_config.release_on_quit)
518 perform_release();
Mike Frysinger7031f622006-05-08 03:20:50 +0000519 return 0;
520 }
521 } else if (retval == -1 && errno == EINTR) {
522 /* a signal was caught */
523 } else {
524 /* An error occured */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000525 bb_perror_msg("select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000526 }
527
528 }
529 return 0;
530}