blob: 35ae757e7baa3da8270872662b738dc13b8a2108 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001/* dhcpc.c
2 *
3 * udhcp DHCP client
4 *
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000021
Russ Dill61fb4892002-10-14 21:41:28 +000022#include <sys/time.h>
Russ Dill61fb4892002-10-14 21:41:28 +000023#include <sys/file.h>
24#include <unistd.h>
25#include <getopt.h>
26#include <stdlib.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <signal.h>
31#include <time.h>
32#include <string.h>
33#include <sys/ioctl.h>
34#include <net/if.h>
35#include <errno.h>
36
37#include "dhcpd.h"
38#include "dhcpc.h"
39#include "options.h"
40#include "clientpacket.h"
Russ Dill4e864a32003-12-18 22:25:38 +000041#include "clientsocket.h"
Russ Dill61fb4892002-10-14 21:41:28 +000042#include "script.h"
43#include "socket.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000044#include "common.h"
Russ Dill4e864a32003-12-18 22:25:38 +000045#include "signalpipe.h"
Russ Dill61fb4892002-10-14 21:41:28 +000046
47static int state;
48static unsigned long requested_ip; /* = 0 */
49static unsigned long server_addr;
50static unsigned long timeout;
51static int packet_num; /* = 0 */
Russ Dill6393d692003-01-21 22:39:34 +000052static int fd = -1;
Russ Dill61fb4892002-10-14 21:41:28 +000053
54#define LISTEN_NONE 0
55#define LISTEN_KERNEL 1
56#define LISTEN_RAW 2
57static int listen_mode;
58
Russ Dill61fb4892002-10-14 21:41:28 +000059struct client_config_t client_config = {
60 /* Default options. */
Mike Frysinger3cc01a82005-04-16 04:17:39 +000061 .abort_if_no_lease = 0,
62 .foreground = 0,
63 .quit_after_lease = 0,
64 .background_if_no_lease = 0,
65 .interface = "eth0",
66 .pidfile = NULL,
67 .script = DEFAULT_SCRIPT,
68 .clientid = NULL,
69 .hostname = NULL,
70 .fqdn = NULL,
71 .ifindex = 0,
72 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
Russ Dill61fb4892002-10-14 21:41:28 +000073};
74
Russ Dill4b77aca2003-12-16 02:28:20 +000075#ifndef IN_BUSYBOX
76static void __attribute__ ((noreturn)) show_usage(void)
77{
Russ Dill4e864a32003-12-18 22:25:38 +000078 printf(
Russ Dill4b77aca2003-12-16 02:28:20 +000079"Usage: udhcpc [OPTIONS]\n\n"
Paul Foxa39bba32005-08-01 14:31:13 +000080" -c, --clientid=CLIENTID Set client identifier\n"
81" -C, --clientid-none Suppress default client identifier\n"
Russ Dill4b77aca2003-12-16 02:28:20 +000082" -H, --hostname=HOSTNAME Client hostname\n"
83" -h Alias for -H\n"
Mike Frysingerd8248532004-12-06 14:59:45 +000084" -F, --fqdn=FQDN Client fully qualified domain name\n"
Russ Dill4b77aca2003-12-16 02:28:20 +000085" -f, --foreground Do not fork after getting lease\n"
86" -b, --background Fork to background if lease cannot be\n"
87" immediately negotiated.\n"
88" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
89" -n, --now Exit with failure if lease cannot be\n"
90" immediately negotiated.\n"
91" -p, --pidfile=file Store process ID of daemon in file\n"
92" -q, --quit Quit after obtaining lease\n"
93" -r, --request=IP IP address to request (default: none)\n"
94" -s, --script=file Run file at dhcp events (default:\n"
95" " DEFAULT_SCRIPT ")\n"
96" -v, --version Display version\n"
Russ Dill4e864a32003-12-18 22:25:38 +000097 );
98 exit(0);
Russ Dill4b77aca2003-12-16 02:28:20 +000099}
100#else
101#define show_usage bb_show_usage
102extern void show_usage(void) __attribute__ ((noreturn));
103#endif
104
105
Russ Dill61fb4892002-10-14 21:41:28 +0000106/* just a little helper */
107static void change_mode(int new_mode)
108{
109 DEBUG(LOG_INFO, "entering %s listen mode",
110 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Russ Dill6393d692003-01-21 22:39:34 +0000111 if (fd >= 0) close(fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000112 fd = -1;
113 listen_mode = new_mode;
114}
115
116
117/* perform a renew */
118static void perform_renew(void)
119{
120 LOG(LOG_INFO, "Performing a DHCP renew");
121 switch (state) {
Russ Dill61fb4892002-10-14 21:41:28 +0000122 case BOUND:
Russ Dill61fb4892002-10-14 21:41:28 +0000123 change_mode(LISTEN_KERNEL);
Russ Dillf5ecd432002-10-31 19:21:27 +0000124 case RENEWING:
125 case REBINDING:
Russ Dill61fb4892002-10-14 21:41:28 +0000126 state = RENEW_REQUESTED;
127 break;
Russ Dillf5ecd432002-10-31 19:21:27 +0000128 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
129 run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000130 case REQUESTING:
131 case RELEASED:
132 change_mode(LISTEN_RAW);
133 state = INIT_SELECTING;
134 break;
135 case INIT_SELECTING:
Eric Andersen1b2e7c32003-06-20 09:20:28 +0000136 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000137 }
138
139 /* start things over */
140 packet_num = 0;
141
142 /* Kill any timeouts because the user wants this to hurry along */
143 timeout = 0;
144}
145
146
147/* perform a release */
148static void perform_release(void)
149{
150 char buffer[16];
151 struct in_addr temp_addr;
152
153 /* send release packet */
154 if (state == BOUND || state == RENEWING || state == REBINDING) {
155 temp_addr.s_addr = server_addr;
156 sprintf(buffer, "%s", inet_ntoa(temp_addr));
157 temp_addr.s_addr = requested_ip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000158 LOG(LOG_INFO, "Unicasting a release of %s to %s",
Russ Dill61fb4892002-10-14 21:41:28 +0000159 inet_ntoa(temp_addr), buffer);
160 send_release(server_addr, requested_ip); /* unicast */
161 run_script(NULL, "deconfig");
162 }
163 LOG(LOG_INFO, "Entering released state");
164
165 change_mode(LISTEN_NONE);
166 state = RELEASED;
167 timeout = 0x7fffffff;
168}
169
170
Glenn L McGrath24833432003-06-10 17:22:49 +0000171static void client_background(void)
Russ Dill61fb4892002-10-14 21:41:28 +0000172{
Glenn L McGrath24833432003-06-10 17:22:49 +0000173 background(client_config.pidfile);
Russ Dill61fb4892002-10-14 21:41:28 +0000174 client_config.foreground = 1; /* Do not fork again. */
Russ Dill6393d692003-01-21 22:39:34 +0000175 client_config.background_if_no_lease = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000176}
177
178
Russ Dill4e864a32003-12-18 22:25:38 +0000179#ifdef COMBINED_BINARY
Russ Dill61fb4892002-10-14 21:41:28 +0000180int udhcpc_main(int argc, char *argv[])
Russ Dill4e864a32003-12-18 22:25:38 +0000181#else
182int main(int argc, char *argv[])
183#endif
Russ Dill61fb4892002-10-14 21:41:28 +0000184{
Eric Andersenad953732004-01-30 23:45:53 +0000185 uint8_t *temp, *message;
Russ Dill61fb4892002-10-14 21:41:28 +0000186 unsigned long t1 = 0, t2 = 0, xid = 0;
187 unsigned long start = 0, lease;
188 fd_set rfds;
189 int retval;
190 struct timeval tv;
191 int c, len;
192 struct dhcpMessage packet;
193 struct in_addr temp_addr;
Russ Dill309c7b72004-05-19 08:29:05 +0000194 long now;
Russ Dill61fb4892002-10-14 21:41:28 +0000195 int max_fd;
196 int sig;
Paul Foxa39bba32005-08-01 14:31:13 +0000197 int no_clientid = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000198
Glenn L McGrath24833432003-06-10 17:22:49 +0000199 static const struct option arg_options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +0000200 {"clientid", required_argument, 0, 'c'},
Paul Foxa39bba32005-08-01 14:31:13 +0000201 {"clientid-none", no_argument, 0, 'C'},
Russ Dill61fb4892002-10-14 21:41:28 +0000202 {"foreground", no_argument, 0, 'f'},
203 {"background", no_argument, 0, 'b'},
204 {"hostname", required_argument, 0, 'H'},
Mike Frysingerd8248532004-12-06 14:59:45 +0000205 {"hostname", required_argument, 0, 'h'},
206 {"fqdn", required_argument, 0, 'F'},
Russ Dill61fb4892002-10-14 21:41:28 +0000207 {"interface", required_argument, 0, 'i'},
208 {"now", no_argument, 0, 'n'},
209 {"pidfile", required_argument, 0, 'p'},
210 {"quit", no_argument, 0, 'q'},
211 {"request", required_argument, 0, 'r'},
212 {"script", required_argument, 0, 's'},
213 {"version", no_argument, 0, 'v'},
Russ Dill61fb4892002-10-14 21:41:28 +0000214 {0, 0, 0, 0}
215 };
216
217 /* get options */
218 while (1) {
219 int option_index = 0;
Paul Foxa39bba32005-08-01 14:31:13 +0000220 c = getopt_long(argc, argv, "c:CfbH:h:F:i:np:qr:s:v", arg_options, &option_index);
Russ Dill61fb4892002-10-14 21:41:28 +0000221 if (c == -1) break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000222
Russ Dill61fb4892002-10-14 21:41:28 +0000223 switch (c) {
224 case 'c':
Paul Foxa39bba32005-08-01 14:31:13 +0000225 if (no_clientid) show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +0000226 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Russ Dill1eb7a172002-12-11 21:12:45 +0000227 if (client_config.clientid) free(client_config.clientid);
Russ Dill61fb4892002-10-14 21:41:28 +0000228 client_config.clientid = xmalloc(len + 2);
229 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
230 client_config.clientid[OPT_LEN] = len;
231 client_config.clientid[OPT_DATA] = '\0';
232 strncpy(client_config.clientid + OPT_DATA, optarg, len);
233 break;
Paul Foxa39bba32005-08-01 14:31:13 +0000234 case 'C':
235 if (client_config.clientid) show_usage();
236 no_clientid = 1;
237 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000238 case 'f':
239 client_config.foreground = 1;
240 break;
241 case 'b':
242 client_config.background_if_no_lease = 1;
243 break;
244 case 'h':
245 case 'H':
246 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Russ Dill1eb7a172002-12-11 21:12:45 +0000247 if (client_config.hostname) free(client_config.hostname);
Russ Dill61fb4892002-10-14 21:41:28 +0000248 client_config.hostname = xmalloc(len + 2);
249 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
250 client_config.hostname[OPT_LEN] = len;
251 strncpy(client_config.hostname + 2, optarg, len);
252 break;
Mike Frysingerd8248532004-12-06 14:59:45 +0000253 case 'F':
254 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
255 if (client_config.fqdn) free(client_config.fqdn);
256 client_config.fqdn = xmalloc(len + 5);
257 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
258 client_config.fqdn[OPT_LEN] = len + 3;
259 /* Flags: 0000NEOS
260 S: 1 => Client requests Server to update A RR in DNS as well as PTR
261 O: 1 => Server indicates to client that DNS has been updated regardless
262 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
263 N: 1 => Client requests Server to not update DNS
264 */
265 client_config.fqdn[OPT_LEN + 1] = 0x1;
266 client_config.fqdn[OPT_LEN + 2] = 0;
267 client_config.fqdn[OPT_LEN + 3] = 0;
268 strncpy(client_config.fqdn + 5, optarg, len);
269 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000270 case 'i':
271 client_config.interface = optarg;
272 break;
273 case 'n':
274 client_config.abort_if_no_lease = 1;
275 break;
276 case 'p':
277 client_config.pidfile = optarg;
278 break;
279 case 'q':
280 client_config.quit_after_lease = 1;
281 break;
282 case 'r':
283 requested_ip = inet_addr(optarg);
284 break;
285 case 's':
286 client_config.script = optarg;
287 break;
288 case 'v':
Russ Dill4e864a32003-12-18 22:25:38 +0000289 printf("udhcpcd, version %s\n\n", VERSION);
290 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000291 break;
292 default:
Russ Dill4b77aca2003-12-16 02:28:20 +0000293 show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +0000294 }
295 }
296
Russ Dill4e864a32003-12-18 22:25:38 +0000297 /* Start the log, sanitize fd's, and write a pid file */
298 start_log_and_pid("udhcpc", client_config.pidfile);
299
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000300 if (read_interface(client_config.interface, &client_config.ifindex,
Russ Dill61fb4892002-10-14 21:41:28 +0000301 NULL, client_config.arp) < 0)
Russ Dill4e864a32003-12-18 22:25:38 +0000302 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000303
Paul Foxa39bba32005-08-01 14:31:13 +0000304 /* if not set, and not suppressed, setup the default client ID */
305 if (!client_config.clientid && !no_clientid) {
Russ Dill61fb4892002-10-14 21:41:28 +0000306 client_config.clientid = xmalloc(6 + 3);
307 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
308 client_config.clientid[OPT_LEN] = 7;
309 client_config.clientid[OPT_DATA] = 1;
310 memcpy(client_config.clientid + 3, client_config.arp, 6);
311 }
312
Russ Dill4e864a32003-12-18 22:25:38 +0000313 /* setup the signal pipe */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000314 udhcp_sp_setup();
315
Russ Dill61fb4892002-10-14 21:41:28 +0000316 state = INIT_SELECTING;
317 run_script(NULL, "deconfig");
318 change_mode(LISTEN_RAW);
319
320 for (;;) {
321
Rob Landley918f2ab2005-05-04 02:15:23 +0000322 tv.tv_sec = timeout - uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000323 tv.tv_usec = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000324
325 if (listen_mode != LISTEN_NONE && fd < 0) {
326 if (listen_mode == LISTEN_KERNEL)
327 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
328 else
329 fd = raw_socket(client_config.ifindex);
330 if (fd < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000331 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
Russ Dill4e864a32003-12-18 22:25:38 +0000332 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000333 }
334 }
Russ Dill4e864a32003-12-18 22:25:38 +0000335 max_fd = udhcp_sp_fd_set(&rfds, fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000336
337 if (tv.tv_sec > 0) {
Glenn L McGrath8ce8f9b2003-08-29 15:19:44 +0000338 DEBUG(LOG_INFO, "Waiting on select...");
Russ Dill61fb4892002-10-14 21:41:28 +0000339 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
340 } else retval = 0; /* If we already timed out, fall through */
341
Rob Landley918f2ab2005-05-04 02:15:23 +0000342 now = uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000343 if (retval == 0) {
344 /* timeout dropped to zero */
345 switch (state) {
346 case INIT_SELECTING:
347 if (packet_num < 3) {
348 if (packet_num == 0)
349 xid = random_xid();
350
351 /* send discover packet */
352 send_discover(xid, requested_ip); /* broadcast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000353
Russ Dill61fb4892002-10-14 21:41:28 +0000354 timeout = now + ((packet_num == 2) ? 4 : 2);
355 packet_num++;
356 } else {
Glenn L McGrathd9461f82003-09-01 04:08:36 +0000357 run_script(NULL, "leasefail");
Russ Dill61fb4892002-10-14 21:41:28 +0000358 if (client_config.background_if_no_lease) {
359 LOG(LOG_INFO, "No lease, forking to background.");
Glenn L McGrath24833432003-06-10 17:22:49 +0000360 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000361 } else if (client_config.abort_if_no_lease) {
362 LOG(LOG_INFO, "No lease, failing.");
Russ Dill4e864a32003-12-18 22:25:38 +0000363 return 1;
Russ Dill61fb4892002-10-14 21:41:28 +0000364 }
365 /* wait to try again */
366 packet_num = 0;
367 timeout = now + 60;
368 }
369 break;
370 case RENEW_REQUESTED:
371 case REQUESTING:
372 if (packet_num < 3) {
373 /* send request packet */
374 if (state == RENEW_REQUESTED)
375 send_renew(xid, server_addr, requested_ip); /* unicast */
376 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000377
Russ Dill61fb4892002-10-14 21:41:28 +0000378 timeout = now + ((packet_num == 2) ? 10 : 2);
379 packet_num++;
380 } else {
381 /* timed out, go back to init state */
Russ Dillf5ecd432002-10-31 19:21:27 +0000382 if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000383 state = INIT_SELECTING;
384 timeout = now;
385 packet_num = 0;
386 change_mode(LISTEN_RAW);
387 }
388 break;
389 case BOUND:
390 /* Lease is starting to run out, time to enter renewing state */
391 state = RENEWING;
392 change_mode(LISTEN_KERNEL);
393 DEBUG(LOG_INFO, "Entering renew state");
394 /* fall right through */
395 case RENEWING:
396 /* Either set a new T1, or enter REBINDING state */
397 if ((t2 - t1) <= (lease / 14400 + 1)) {
398 /* timed out, enter rebinding state */
399 state = REBINDING;
400 timeout = now + (t2 - t1);
401 DEBUG(LOG_INFO, "Entering rebinding state");
402 } else {
403 /* send a request packet */
404 send_renew(xid, server_addr, requested_ip); /* unicast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000405
Russ Dill61fb4892002-10-14 21:41:28 +0000406 t1 = (t2 - t1) / 2 + t1;
407 timeout = t1 + start;
408 }
409 break;
410 case REBINDING:
411 /* Either set a new T2, or enter INIT state */
412 if ((lease - t2) <= (lease / 14400 + 1)) {
413 /* timed out, enter init state */
414 state = INIT_SELECTING;
415 LOG(LOG_INFO, "Lease lost, entering init state");
416 run_script(NULL, "deconfig");
417 timeout = now;
418 packet_num = 0;
419 change_mode(LISTEN_RAW);
420 } else {
421 /* send a request packet */
422 send_renew(xid, 0, requested_ip); /* broadcast */
423
424 t2 = (lease - t2) / 2 + t2;
425 timeout = t2 + start;
426 }
427 break;
428 case RELEASED:
429 /* yah, I know, *you* say it would never happen */
430 timeout = 0x7fffffff;
431 break;
432 }
433 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
434 /* a packet is ready, read it */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000435
Russ Dill61fb4892002-10-14 21:41:28 +0000436 if (listen_mode == LISTEN_KERNEL)
437 len = get_packet(&packet, fd);
438 else len = get_raw_packet(&packet, fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000439
Russ Dill61fb4892002-10-14 21:41:28 +0000440 if (len == -1 && errno != EINTR) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000441 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
Russ Dill61fb4892002-10-14 21:41:28 +0000442 change_mode(listen_mode); /* just close and reopen */
443 }
444 if (len < 0) continue;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000445
Russ Dill61fb4892002-10-14 21:41:28 +0000446 if (packet.xid != xid) {
447 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
448 (unsigned long) packet.xid, xid);
449 continue;
450 }
Rob Landley0b1ff5a2005-05-26 05:25:12 +0000451 /* Ignore packets that aren't for us */
Paul Fox01f67982005-07-20 19:13:21 +0000452 if (memcmp(packet.chaddr, client_config.arp, 6)) {
453 DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
454 continue;
455 }
456
Russ Dill61fb4892002-10-14 21:41:28 +0000457 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
458 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
459 continue;
460 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000461
Russ Dill61fb4892002-10-14 21:41:28 +0000462 switch (state) {
463 case INIT_SELECTING:
464 /* Must be a DHCPOFFER to one of our xid's */
465 if (*message == DHCPOFFER) {
466 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
467 memcpy(&server_addr, temp, 4);
468 xid = packet.xid;
469 requested_ip = packet.yiaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000470
Russ Dill61fb4892002-10-14 21:41:28 +0000471 /* enter requesting state */
472 state = REQUESTING;
473 timeout = now;
474 packet_num = 0;
475 } else {
476 DEBUG(LOG_ERR, "No server ID in message");
477 }
478 }
479 break;
480 case RENEW_REQUESTED:
481 case REQUESTING:
482 case RENEWING:
483 case REBINDING:
484 if (*message == DHCPACK) {
485 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
486 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
487 lease = 60 * 60;
488 } else {
489 memcpy(&lease, temp, 4);
490 lease = ntohl(lease);
491 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000492
Russ Dill61fb4892002-10-14 21:41:28 +0000493 /* enter bound state */
494 t1 = lease / 2;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000495
Russ Dill61fb4892002-10-14 21:41:28 +0000496 /* little fixed point for n * .875 */
497 t2 = (lease * 0x7) >> 3;
498 temp_addr.s_addr = packet.yiaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000499 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
Russ Dill61fb4892002-10-14 21:41:28 +0000500 inet_ntoa(temp_addr), lease);
501 start = now;
502 timeout = t1 + start;
503 requested_ip = packet.yiaddr;
504 run_script(&packet,
505 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
506
507 state = BOUND;
508 change_mode(LISTEN_NONE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000509 if (client_config.quit_after_lease)
Russ Dill4e864a32003-12-18 22:25:38 +0000510 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000511 if (!client_config.foreground)
Glenn L McGrath24833432003-06-10 17:22:49 +0000512 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000513
514 } else if (*message == DHCPNAK) {
515 /* return to init state */
516 LOG(LOG_INFO, "Received DHCP NAK");
517 run_script(&packet, "nak");
518 if (state != REQUESTING)
519 run_script(NULL, "deconfig");
520 state = INIT_SELECTING;
521 timeout = now;
522 requested_ip = 0;
523 packet_num = 0;
524 change_mode(LISTEN_RAW);
525 sleep(3); /* avoid excessive network traffic */
526 }
527 break;
528 /* case BOUND, RELEASED: - ignore all packets */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000529 }
Russ Dill4e864a32003-12-18 22:25:38 +0000530 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
Russ Dill61fb4892002-10-14 21:41:28 +0000531 switch (sig) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000532 case SIGUSR1:
Russ Dill61fb4892002-10-14 21:41:28 +0000533 perform_renew();
534 break;
535 case SIGUSR2:
536 perform_release();
537 break;
538 case SIGTERM:
539 LOG(LOG_INFO, "Received SIGTERM");
Russ Dill4e864a32003-12-18 22:25:38 +0000540 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000541 }
542 } else if (retval == -1 && errno == EINTR) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000543 /* a signal was caught */
Russ Dill61fb4892002-10-14 21:41:28 +0000544 } else {
545 /* An error occured */
546 DEBUG(LOG_ERR, "Error on select");
547 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000548
Russ Dill61fb4892002-10-14 21:41:28 +0000549 }
550 return 0;
551}