blob: 8009bec041a2785a9f4624b6ceece60578fc9cf0 [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"
80" -c, --clientid=CLIENTID Client identifier\n"
81" -H, --hostname=HOSTNAME Client hostname\n"
82" -h Alias for -H\n"
Mike Frysingerd8248532004-12-06 14:59:45 +000083" -F, --fqdn=FQDN Client fully qualified domain name\n"
Russ Dill4b77aca2003-12-16 02:28:20 +000084" -f, --foreground Do not fork after getting lease\n"
85" -b, --background Fork to background if lease cannot be\n"
86" immediately negotiated.\n"
87" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
88" -n, --now Exit with failure if lease cannot be\n"
89" immediately negotiated.\n"
90" -p, --pidfile=file Store process ID of daemon in file\n"
91" -q, --quit Quit after obtaining lease\n"
92" -r, --request=IP IP address to request (default: none)\n"
93" -s, --script=file Run file at dhcp events (default:\n"
94" " DEFAULT_SCRIPT ")\n"
95" -v, --version Display version\n"
Russ Dill4e864a32003-12-18 22:25:38 +000096 );
97 exit(0);
Russ Dill4b77aca2003-12-16 02:28:20 +000098}
99#else
100#define show_usage bb_show_usage
101extern void show_usage(void) __attribute__ ((noreturn));
102#endif
103
104
Russ Dill61fb4892002-10-14 21:41:28 +0000105/* just a little helper */
106static void change_mode(int new_mode)
107{
108 DEBUG(LOG_INFO, "entering %s listen mode",
109 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Russ Dill6393d692003-01-21 22:39:34 +0000110 if (fd >= 0) close(fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000111 fd = -1;
112 listen_mode = new_mode;
113}
114
115
116/* perform a renew */
117static void perform_renew(void)
118{
119 LOG(LOG_INFO, "Performing a DHCP renew");
120 switch (state) {
Russ Dill61fb4892002-10-14 21:41:28 +0000121 case BOUND:
Russ Dill61fb4892002-10-14 21:41:28 +0000122 change_mode(LISTEN_KERNEL);
Russ Dillf5ecd432002-10-31 19:21:27 +0000123 case RENEWING:
124 case REBINDING:
Russ Dill61fb4892002-10-14 21:41:28 +0000125 state = RENEW_REQUESTED;
126 break;
Russ Dillf5ecd432002-10-31 19:21:27 +0000127 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
128 run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000129 case REQUESTING:
130 case RELEASED:
131 change_mode(LISTEN_RAW);
132 state = INIT_SELECTING;
133 break;
134 case INIT_SELECTING:
Eric Andersen1b2e7c32003-06-20 09:20:28 +0000135 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000136 }
137
138 /* start things over */
139 packet_num = 0;
140
141 /* Kill any timeouts because the user wants this to hurry along */
142 timeout = 0;
143}
144
145
146/* perform a release */
147static void perform_release(void)
148{
149 char buffer[16];
150 struct in_addr temp_addr;
151
152 /* send release packet */
153 if (state == BOUND || state == RENEWING || state == REBINDING) {
154 temp_addr.s_addr = server_addr;
155 sprintf(buffer, "%s", inet_ntoa(temp_addr));
156 temp_addr.s_addr = requested_ip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000157 LOG(LOG_INFO, "Unicasting a release of %s to %s",
Russ Dill61fb4892002-10-14 21:41:28 +0000158 inet_ntoa(temp_addr), buffer);
159 send_release(server_addr, requested_ip); /* unicast */
160 run_script(NULL, "deconfig");
161 }
162 LOG(LOG_INFO, "Entering released state");
163
164 change_mode(LISTEN_NONE);
165 state = RELEASED;
166 timeout = 0x7fffffff;
167}
168
169
Glenn L McGrath24833432003-06-10 17:22:49 +0000170static void client_background(void)
Russ Dill61fb4892002-10-14 21:41:28 +0000171{
Glenn L McGrath24833432003-06-10 17:22:49 +0000172 background(client_config.pidfile);
Russ Dill61fb4892002-10-14 21:41:28 +0000173 client_config.foreground = 1; /* Do not fork again. */
Russ Dill6393d692003-01-21 22:39:34 +0000174 client_config.background_if_no_lease = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000175}
176
177
Russ Dill4e864a32003-12-18 22:25:38 +0000178#ifdef COMBINED_BINARY
Russ Dill61fb4892002-10-14 21:41:28 +0000179int udhcpc_main(int argc, char *argv[])
Russ Dill4e864a32003-12-18 22:25:38 +0000180#else
181int main(int argc, char *argv[])
182#endif
Russ Dill61fb4892002-10-14 21:41:28 +0000183{
Eric Andersenad953732004-01-30 23:45:53 +0000184 uint8_t *temp, *message;
Russ Dill61fb4892002-10-14 21:41:28 +0000185 unsigned long t1 = 0, t2 = 0, xid = 0;
186 unsigned long start = 0, lease;
187 fd_set rfds;
188 int retval;
189 struct timeval tv;
190 int c, len;
191 struct dhcpMessage packet;
192 struct in_addr temp_addr;
Russ Dill309c7b72004-05-19 08:29:05 +0000193 long now;
Russ Dill61fb4892002-10-14 21:41:28 +0000194 int max_fd;
195 int sig;
196
Glenn L McGrath24833432003-06-10 17:22:49 +0000197 static const struct option arg_options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +0000198 {"clientid", required_argument, 0, 'c'},
199 {"foreground", no_argument, 0, 'f'},
200 {"background", no_argument, 0, 'b'},
201 {"hostname", required_argument, 0, 'H'},
Mike Frysingerd8248532004-12-06 14:59:45 +0000202 {"hostname", required_argument, 0, 'h'},
203 {"fqdn", required_argument, 0, 'F'},
Russ Dill61fb4892002-10-14 21:41:28 +0000204 {"interface", required_argument, 0, 'i'},
205 {"now", no_argument, 0, 'n'},
206 {"pidfile", required_argument, 0, 'p'},
207 {"quit", no_argument, 0, 'q'},
208 {"request", required_argument, 0, 'r'},
209 {"script", required_argument, 0, 's'},
210 {"version", no_argument, 0, 'v'},
Russ Dill61fb4892002-10-14 21:41:28 +0000211 {0, 0, 0, 0}
212 };
213
214 /* get options */
215 while (1) {
216 int option_index = 0;
Mike Frysingerd8248532004-12-06 14:59:45 +0000217 c = getopt_long(argc, argv, "c:fbH:h:F:i:np:qr:s:v", arg_options, &option_index);
Russ Dill61fb4892002-10-14 21:41:28 +0000218 if (c == -1) break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000219
Russ Dill61fb4892002-10-14 21:41:28 +0000220 switch (c) {
221 case 'c':
222 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Russ Dill1eb7a172002-12-11 21:12:45 +0000223 if (client_config.clientid) free(client_config.clientid);
Russ Dill61fb4892002-10-14 21:41:28 +0000224 client_config.clientid = xmalloc(len + 2);
225 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
226 client_config.clientid[OPT_LEN] = len;
227 client_config.clientid[OPT_DATA] = '\0';
228 strncpy(client_config.clientid + OPT_DATA, optarg, len);
229 break;
230 case 'f':
231 client_config.foreground = 1;
232 break;
233 case 'b':
234 client_config.background_if_no_lease = 1;
235 break;
236 case 'h':
237 case 'H':
238 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Russ Dill1eb7a172002-12-11 21:12:45 +0000239 if (client_config.hostname) free(client_config.hostname);
Russ Dill61fb4892002-10-14 21:41:28 +0000240 client_config.hostname = xmalloc(len + 2);
241 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
242 client_config.hostname[OPT_LEN] = len;
243 strncpy(client_config.hostname + 2, optarg, len);
244 break;
Mike Frysingerd8248532004-12-06 14:59:45 +0000245 case 'F':
246 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
247 if (client_config.fqdn) free(client_config.fqdn);
248 client_config.fqdn = xmalloc(len + 5);
249 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
250 client_config.fqdn[OPT_LEN] = len + 3;
251 /* Flags: 0000NEOS
252 S: 1 => Client requests Server to update A RR in DNS as well as PTR
253 O: 1 => Server indicates to client that DNS has been updated regardless
254 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
255 N: 1 => Client requests Server to not update DNS
256 */
257 client_config.fqdn[OPT_LEN + 1] = 0x1;
258 client_config.fqdn[OPT_LEN + 2] = 0;
259 client_config.fqdn[OPT_LEN + 3] = 0;
260 strncpy(client_config.fqdn + 5, optarg, len);
261 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000262 case 'i':
263 client_config.interface = optarg;
264 break;
265 case 'n':
266 client_config.abort_if_no_lease = 1;
267 break;
268 case 'p':
269 client_config.pidfile = optarg;
270 break;
271 case 'q':
272 client_config.quit_after_lease = 1;
273 break;
274 case 'r':
275 requested_ip = inet_addr(optarg);
276 break;
277 case 's':
278 client_config.script = optarg;
279 break;
280 case 'v':
Russ Dill4e864a32003-12-18 22:25:38 +0000281 printf("udhcpcd, version %s\n\n", VERSION);
282 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000283 break;
284 default:
Russ Dill4b77aca2003-12-16 02:28:20 +0000285 show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +0000286 }
287 }
288
Russ Dill4e864a32003-12-18 22:25:38 +0000289 /* Start the log, sanitize fd's, and write a pid file */
290 start_log_and_pid("udhcpc", client_config.pidfile);
291
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000292 if (read_interface(client_config.interface, &client_config.ifindex,
Russ Dill61fb4892002-10-14 21:41:28 +0000293 NULL, client_config.arp) < 0)
Russ Dill4e864a32003-12-18 22:25:38 +0000294 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000295
Russ Dill61fb4892002-10-14 21:41:28 +0000296 if (!client_config.clientid) {
297 client_config.clientid = xmalloc(6 + 3);
298 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
299 client_config.clientid[OPT_LEN] = 7;
300 client_config.clientid[OPT_DATA] = 1;
301 memcpy(client_config.clientid + 3, client_config.arp, 6);
302 }
303
Russ Dill4e864a32003-12-18 22:25:38 +0000304 /* setup the signal pipe */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000305 udhcp_sp_setup();
306
Russ Dill61fb4892002-10-14 21:41:28 +0000307 state = INIT_SELECTING;
308 run_script(NULL, "deconfig");
309 change_mode(LISTEN_RAW);
310
311 for (;;) {
312
Rob Landley918f2ab2005-05-04 02:15:23 +0000313 tv.tv_sec = timeout - uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000314 tv.tv_usec = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000315
316 if (listen_mode != LISTEN_NONE && fd < 0) {
317 if (listen_mode == LISTEN_KERNEL)
318 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
319 else
320 fd = raw_socket(client_config.ifindex);
321 if (fd < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000322 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
Russ Dill4e864a32003-12-18 22:25:38 +0000323 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000324 }
325 }
Russ Dill4e864a32003-12-18 22:25:38 +0000326 max_fd = udhcp_sp_fd_set(&rfds, fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000327
328 if (tv.tv_sec > 0) {
Glenn L McGrath8ce8f9b2003-08-29 15:19:44 +0000329 DEBUG(LOG_INFO, "Waiting on select...");
Russ Dill61fb4892002-10-14 21:41:28 +0000330 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
331 } else retval = 0; /* If we already timed out, fall through */
332
Rob Landley918f2ab2005-05-04 02:15:23 +0000333 now = uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000334 if (retval == 0) {
335 /* timeout dropped to zero */
336 switch (state) {
337 case INIT_SELECTING:
338 if (packet_num < 3) {
339 if (packet_num == 0)
340 xid = random_xid();
341
342 /* send discover packet */
343 send_discover(xid, requested_ip); /* broadcast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000344
Russ Dill61fb4892002-10-14 21:41:28 +0000345 timeout = now + ((packet_num == 2) ? 4 : 2);
346 packet_num++;
347 } else {
Glenn L McGrathd9461f82003-09-01 04:08:36 +0000348 run_script(NULL, "leasefail");
Russ Dill61fb4892002-10-14 21:41:28 +0000349 if (client_config.background_if_no_lease) {
350 LOG(LOG_INFO, "No lease, forking to background.");
Glenn L McGrath24833432003-06-10 17:22:49 +0000351 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000352 } else if (client_config.abort_if_no_lease) {
353 LOG(LOG_INFO, "No lease, failing.");
Russ Dill4e864a32003-12-18 22:25:38 +0000354 return 1;
Russ Dill61fb4892002-10-14 21:41:28 +0000355 }
356 /* wait to try again */
357 packet_num = 0;
358 timeout = now + 60;
359 }
360 break;
361 case RENEW_REQUESTED:
362 case REQUESTING:
363 if (packet_num < 3) {
364 /* send request packet */
365 if (state == RENEW_REQUESTED)
366 send_renew(xid, server_addr, requested_ip); /* unicast */
367 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000368
Russ Dill61fb4892002-10-14 21:41:28 +0000369 timeout = now + ((packet_num == 2) ? 10 : 2);
370 packet_num++;
371 } else {
372 /* timed out, go back to init state */
Russ Dillf5ecd432002-10-31 19:21:27 +0000373 if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000374 state = INIT_SELECTING;
375 timeout = now;
376 packet_num = 0;
377 change_mode(LISTEN_RAW);
378 }
379 break;
380 case BOUND:
381 /* Lease is starting to run out, time to enter renewing state */
382 state = RENEWING;
383 change_mode(LISTEN_KERNEL);
384 DEBUG(LOG_INFO, "Entering renew state");
385 /* fall right through */
386 case RENEWING:
387 /* Either set a new T1, or enter REBINDING state */
388 if ((t2 - t1) <= (lease / 14400 + 1)) {
389 /* timed out, enter rebinding state */
390 state = REBINDING;
391 timeout = now + (t2 - t1);
392 DEBUG(LOG_INFO, "Entering rebinding state");
393 } else {
394 /* send a request packet */
395 send_renew(xid, server_addr, requested_ip); /* unicast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000396
Russ Dill61fb4892002-10-14 21:41:28 +0000397 t1 = (t2 - t1) / 2 + t1;
398 timeout = t1 + start;
399 }
400 break;
401 case REBINDING:
402 /* Either set a new T2, or enter INIT state */
403 if ((lease - t2) <= (lease / 14400 + 1)) {
404 /* timed out, enter init state */
405 state = INIT_SELECTING;
406 LOG(LOG_INFO, "Lease lost, entering init state");
407 run_script(NULL, "deconfig");
408 timeout = now;
409 packet_num = 0;
410 change_mode(LISTEN_RAW);
411 } else {
412 /* send a request packet */
413 send_renew(xid, 0, requested_ip); /* broadcast */
414
415 t2 = (lease - t2) / 2 + t2;
416 timeout = t2 + start;
417 }
418 break;
419 case RELEASED:
420 /* yah, I know, *you* say it would never happen */
421 timeout = 0x7fffffff;
422 break;
423 }
424 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
425 /* a packet is ready, read it */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000426
Russ Dill61fb4892002-10-14 21:41:28 +0000427 if (listen_mode == LISTEN_KERNEL)
428 len = get_packet(&packet, fd);
429 else len = get_raw_packet(&packet, fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000430
Russ Dill61fb4892002-10-14 21:41:28 +0000431 if (len == -1 && errno != EINTR) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000432 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
Russ Dill61fb4892002-10-14 21:41:28 +0000433 change_mode(listen_mode); /* just close and reopen */
434 }
435 if (len < 0) continue;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000436
Russ Dill61fb4892002-10-14 21:41:28 +0000437 if (packet.xid != xid) {
438 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
439 (unsigned long) packet.xid, xid);
440 continue;
441 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000442
Russ Dill61fb4892002-10-14 21:41:28 +0000443 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
444 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
445 continue;
446 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000447
Russ Dill61fb4892002-10-14 21:41:28 +0000448 switch (state) {
449 case INIT_SELECTING:
450 /* Must be a DHCPOFFER to one of our xid's */
451 if (*message == DHCPOFFER) {
452 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
453 memcpy(&server_addr, temp, 4);
454 xid = packet.xid;
455 requested_ip = packet.yiaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000456
Russ Dill61fb4892002-10-14 21:41:28 +0000457 /* enter requesting state */
458 state = REQUESTING;
459 timeout = now;
460 packet_num = 0;
461 } else {
462 DEBUG(LOG_ERR, "No server ID in message");
463 }
464 }
465 break;
466 case RENEW_REQUESTED:
467 case REQUESTING:
468 case RENEWING:
469 case REBINDING:
470 if (*message == DHCPACK) {
471 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
472 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
473 lease = 60 * 60;
474 } else {
475 memcpy(&lease, temp, 4);
476 lease = ntohl(lease);
477 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000478
Russ Dill61fb4892002-10-14 21:41:28 +0000479 /* enter bound state */
480 t1 = lease / 2;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000481
Russ Dill61fb4892002-10-14 21:41:28 +0000482 /* little fixed point for n * .875 */
483 t2 = (lease * 0x7) >> 3;
484 temp_addr.s_addr = packet.yiaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000485 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
Russ Dill61fb4892002-10-14 21:41:28 +0000486 inet_ntoa(temp_addr), lease);
487 start = now;
488 timeout = t1 + start;
489 requested_ip = packet.yiaddr;
490 run_script(&packet,
491 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
492
493 state = BOUND;
494 change_mode(LISTEN_NONE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000495 if (client_config.quit_after_lease)
Russ Dill4e864a32003-12-18 22:25:38 +0000496 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000497 if (!client_config.foreground)
Glenn L McGrath24833432003-06-10 17:22:49 +0000498 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000499
500 } else if (*message == DHCPNAK) {
501 /* return to init state */
502 LOG(LOG_INFO, "Received DHCP NAK");
503 run_script(&packet, "nak");
504 if (state != REQUESTING)
505 run_script(NULL, "deconfig");
506 state = INIT_SELECTING;
507 timeout = now;
508 requested_ip = 0;
509 packet_num = 0;
510 change_mode(LISTEN_RAW);
511 sleep(3); /* avoid excessive network traffic */
512 }
513 break;
514 /* case BOUND, RELEASED: - ignore all packets */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000515 }
Russ Dill4e864a32003-12-18 22:25:38 +0000516 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
Russ Dill61fb4892002-10-14 21:41:28 +0000517 switch (sig) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000518 case SIGUSR1:
Russ Dill61fb4892002-10-14 21:41:28 +0000519 perform_renew();
520 break;
521 case SIGUSR2:
522 perform_release();
523 break;
524 case SIGTERM:
525 LOG(LOG_INFO, "Received SIGTERM");
Russ Dill4e864a32003-12-18 22:25:38 +0000526 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000527 }
528 } else if (retval == -1 && errno == EINTR) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000529 /* a signal was caught */
Russ Dill61fb4892002-10-14 21:41:28 +0000530 } else {
531 /* An error occured */
532 DEBUG(LOG_ERR, "Error on select");
533 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000534
Russ Dill61fb4892002-10-14 21:41:28 +0000535 }
536 return 0;
537}