blob: 4ccd8ec349e4a0be94c77d2d0eeb43361602ecc2 [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>
Mike Frysinger7031f622006-05-08 03:20:50 +000012
13#include "common.h"
14#include "dhcpd.h"
15#include "dhcpc.h"
16#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000017
Mike Frysinger7031f622006-05-08 03:20:50 +000018
19static int state;
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 */
Mike Frysinger7031f622006-05-08 03:20:50 +000025static unsigned long requested_ip; /* = 0 */
26static unsigned long server_addr;
27static unsigned long timeout;
28static int packet_num; /* = 0 */
29static int fd = -1;
30
31#define LISTEN_NONE 0
32#define LISTEN_KERNEL 1
33#define LISTEN_RAW 2
34static int listen_mode;
35
36struct client_config_t client_config = {
37 /* Default options. */
38 .abort_if_no_lease = 0,
39 .foreground = 0,
40 .quit_after_lease = 0,
Denis Vlasenkoe175ff22006-09-26 17:41:00 +000041 .release_on_quit = 0,
Mike Frysinger7031f622006-05-08 03:20:50 +000042 .background_if_no_lease = 0,
43 .interface = "eth0",
44 .pidfile = NULL,
45 .script = DEFAULT_SCRIPT,
46 .clientid = NULL,
47 .vendorclass = NULL,
48 .hostname = NULL,
49 .fqdn = NULL,
50 .ifindex = 0,
51 .retries = 3,
52 .timeout = 3,
53 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
54};
55
Mike Frysinger7031f622006-05-08 03:20:50 +000056/* just a little helper */
57static void change_mode(int new_mode)
58{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000059 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000060 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
61 if (fd >= 0) close(fd);
62 fd = -1;
63 listen_mode = new_mode;
64}
65
66
67/* perform a renew */
68static void perform_renew(void)
69{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000070 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000071 switch (state) {
72 case BOUND:
73 change_mode(LISTEN_KERNEL);
74 case RENEWING:
75 case REBINDING:
76 state = RENEW_REQUESTED;
77 break;
78 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000079 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000080 case REQUESTING:
81 case RELEASED:
82 change_mode(LISTEN_RAW);
83 state = INIT_SELECTING;
84 break;
85 case INIT_SELECTING:
86 break;
87 }
88
89 /* start things over */
90 packet_num = 0;
91
92 /* Kill any timeouts because the user wants this to hurry along */
93 timeout = 0;
94}
95
96
97/* perform a release */
98static void perform_release(void)
99{
100 char buffer[16];
101 struct in_addr temp_addr;
102
103 /* send release packet */
104 if (state == BOUND || state == RENEWING || state == REBINDING) {
105 temp_addr.s_addr = server_addr;
106 sprintf(buffer, "%s", inet_ntoa(temp_addr));
107 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000108 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +0000109 inet_ntoa(temp_addr), buffer);
110 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +0000111 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000112 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000113 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000114
115 change_mode(LISTEN_NONE);
116 state = RELEASED;
117 timeout = 0x7fffffff;
118}
119
120
121static void client_background(void)
122{
Rob Landley3f785612006-05-28 01:06:36 +0000123 udhcp_background(client_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +0000124 client_config.foreground = 1; /* Do not fork again. */
125 client_config.background_if_no_lease = 0;
126}
127
128
Mike Frysinger7031f622006-05-08 03:20:50 +0000129int udhcpc_main(int argc, char *argv[])
Mike Frysinger7031f622006-05-08 03:20:50 +0000130{
131 uint8_t *temp, *message;
132 unsigned long t1 = 0, t2 = 0, xid = 0;
Rob Landley49ea4662006-09-11 01:34:21 +0000133 unsigned long start = 0, lease = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000134 fd_set rfds;
135 int retval;
136 struct timeval tv;
137 int c, len;
138 struct dhcpMessage packet;
139 struct in_addr temp_addr;
140 long now;
141 int max_fd;
142 int sig;
143 int no_clientid = 0;
144
145 static const struct option arg_options[] = {
146 {"clientid", required_argument, 0, 'c'},
147 {"clientid-none", no_argument, 0, 'C'},
148 {"vendorclass", required_argument, 0, 'V'},
149 {"foreground", no_argument, 0, 'f'},
150 {"background", no_argument, 0, 'b'},
151 {"hostname", required_argument, 0, 'H'},
152 {"hostname", required_argument, 0, 'h'},
153 {"fqdn", required_argument, 0, 'F'},
154 {"interface", required_argument, 0, 'i'},
155 {"now", no_argument, 0, 'n'},
156 {"pidfile", required_argument, 0, 'p'},
157 {"quit", no_argument, 0, 'q'},
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000158 {"release", no_argument, 0, 'R'},
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 {"request", required_argument, 0, 'r'},
160 {"script", required_argument, 0, 's'},
161 {"timeout", required_argument, 0, 'T'},
162 {"version", no_argument, 0, 'v'},
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000163 {"retries", required_argument, 0, 't'},
Mike Frysinger7031f622006-05-08 03:20:50 +0000164 {0, 0, 0, 0}
165 };
166
167 /* get options */
168 while (1) {
169 int option_index = 0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000170 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v", arg_options, &option_index);
Mike Frysinger7031f622006-05-08 03:20:50 +0000171 if (c == -1) break;
172
173 switch (c) {
174 case 'c':
Rob Landley11c7a7b2006-06-25 22:39:24 +0000175 if (no_clientid) bb_show_usage();
Mike Frysinger7031f622006-05-08 03:20:50 +0000176 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
177 free(client_config.clientid);
178 client_config.clientid = xmalloc(len + 2);
179 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
180 client_config.clientid[OPT_LEN] = len;
181 client_config.clientid[OPT_DATA] = '\0';
182 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
183 break;
184 case 'C':
Rob Landley11c7a7b2006-06-25 22:39:24 +0000185 if (client_config.clientid) bb_show_usage();
Mike Frysinger7031f622006-05-08 03:20:50 +0000186 no_clientid = 1;
187 break;
188 case 'V':
189 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
190 free(client_config.vendorclass);
191 client_config.vendorclass = xmalloc(len + 2);
192 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
193 client_config.vendorclass[OPT_LEN] = len;
194 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
195 break;
196 case 'f':
197 client_config.foreground = 1;
198 break;
199 case 'b':
200 client_config.background_if_no_lease = 1;
201 break;
202 case 'h':
203 case 'H':
204 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
205 free(client_config.hostname);
206 client_config.hostname = xmalloc(len + 2);
207 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
208 client_config.hostname[OPT_LEN] = len;
209 strncpy((char*)client_config.hostname + 2, optarg, len);
210 break;
211 case 'F':
212 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
213 free(client_config.fqdn);
214 client_config.fqdn = xmalloc(len + 5);
215 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
216 client_config.fqdn[OPT_LEN] = len + 3;
217 /* Flags: 0000NEOS
218 S: 1 => Client requests Server to update A RR in DNS as well as PTR
219 O: 1 => Server indicates to client that DNS has been updated regardless
220 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
221 N: 1 => Client requests Server to not update DNS
222 */
223 client_config.fqdn[OPT_LEN + 1] = 0x1;
224 client_config.fqdn[OPT_LEN + 2] = 0;
225 client_config.fqdn[OPT_LEN + 3] = 0;
226 strncpy((char*)client_config.fqdn + 5, optarg, len);
227 break;
228 case 'i':
229 client_config.interface = optarg;
230 break;
231 case 'n':
232 client_config.abort_if_no_lease = 1;
233 break;
234 case 'p':
235 client_config.pidfile = optarg;
236 break;
237 case 'q':
238 client_config.quit_after_lease = 1;
239 break;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000240 case 'R':
241 client_config.release_on_quit = 1;
242 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000243 case 'r':
244 requested_ip = inet_addr(optarg);
245 break;
246 case 's':
247 client_config.script = optarg;
248 break;
249 case 'T':
Denis Vlasenko13858992006-10-08 12:49:22 +0000250 client_config.timeout = xatoi_u(optarg);
Mike Frysinger7031f622006-05-08 03:20:50 +0000251 break;
252 case 't':
Denis Vlasenko13858992006-10-08 12:49:22 +0000253 client_config.retries = xatoi_u(optarg);
Mike Frysinger7031f622006-05-08 03:20:50 +0000254 break;
255 case 'v':
Rob Landley3f785612006-05-28 01:06:36 +0000256 printf("version %s\n\n", BB_VER);
Mike Frysinger7031f622006-05-08 03:20:50 +0000257 return 0;
258 break;
259 default:
Rob Landley11c7a7b2006-06-25 22:39:24 +0000260 bb_show_usage();
Mike Frysinger7031f622006-05-08 03:20:50 +0000261 }
262 }
263
264 /* Start the log, sanitize fd's, and write a pid file */
Denis Vlasenko239369b2006-09-07 17:05:44 +0000265 udhcp_start_log_and_pid(client_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +0000266
267 if (read_interface(client_config.interface, &client_config.ifindex,
268 NULL, client_config.arp) < 0)
269 return 1;
270
271 /* if not set, and not suppressed, setup the default client ID */
272 if (!client_config.clientid && !no_clientid) {
273 client_config.clientid = xmalloc(6 + 3);
274 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
275 client_config.clientid[OPT_LEN] = 7;
276 client_config.clientid[OPT_DATA] = 1;
277 memcpy(client_config.clientid + 3, client_config.arp, 6);
278 }
279
280 if (!client_config.vendorclass) {
Rob Landley3f785612006-05-28 01:06:36 +0000281 client_config.vendorclass = xmalloc(sizeof("udhcp "BB_VER) + 2);
Mike Frysinger7031f622006-05-08 03:20:50 +0000282 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
Rob Landley3f785612006-05-28 01:06:36 +0000283 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "BB_VER) - 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000284 client_config.vendorclass[OPT_DATA] = 1;
285 memcpy(&client_config.vendorclass[OPT_DATA],
Rob Landley3f785612006-05-28 01:06:36 +0000286 "udhcp "BB_VER, sizeof("udhcp "BB_VER) - 1);
Mike Frysinger7031f622006-05-08 03:20:50 +0000287 }
288
289
290 /* setup the signal pipe */
291 udhcp_sp_setup();
292
293 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000294 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000295 change_mode(LISTEN_RAW);
296
297 for (;;) {
298
299 tv.tv_sec = timeout - uptime();
300 tv.tv_usec = 0;
301
302 if (listen_mode != LISTEN_NONE && fd < 0) {
303 if (listen_mode == LISTEN_KERNEL)
304 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
305 else
306 fd = raw_socket(client_config.ifindex);
307 if (fd < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000308 bb_perror_msg("FATAL: cannot listen on socket");
Mike Frysinger7031f622006-05-08 03:20:50 +0000309 return 0;
310 }
311 }
312 max_fd = udhcp_sp_fd_set(&rfds, fd);
313
314 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000315 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000316 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
317 } else retval = 0; /* If we already timed out, fall through */
318
319 now = uptime();
320 if (retval == 0) {
321 /* timeout dropped to zero */
322 switch (state) {
323 case INIT_SELECTING:
324 if (packet_num < client_config.retries) {
325 if (packet_num == 0)
326 xid = random_xid();
327
328 /* send discover packet */
329 send_discover(xid, requested_ip); /* broadcast */
330
331 timeout = now + client_config.timeout;
332 packet_num++;
333 } else {
Rob Landley3f785612006-05-28 01:06:36 +0000334 udhcp_run_script(NULL, "leasefail");
Mike Frysinger7031f622006-05-08 03:20:50 +0000335 if (client_config.background_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000336 bb_info_msg("No lease, forking to background");
Mike Frysinger7031f622006-05-08 03:20:50 +0000337 client_background();
338 } else if (client_config.abort_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000339 bb_info_msg("No lease, failing");
Mike Frysinger7031f622006-05-08 03:20:50 +0000340 return 1;
341 }
342 /* wait to try again */
343 packet_num = 0;
344 timeout = now + 60;
345 }
346 break;
347 case RENEW_REQUESTED:
348 case REQUESTING:
349 if (packet_num < client_config.retries) {
350 /* send request packet */
351 if (state == RENEW_REQUESTED)
352 send_renew(xid, server_addr, requested_ip); /* unicast */
353 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
354
355 timeout = now + ((packet_num == 2) ? 10 : 2);
356 packet_num++;
357 } else {
358 /* timed out, go back to init state */
Rob Landley3f785612006-05-28 01:06:36 +0000359 if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000360 state = INIT_SELECTING;
361 timeout = now;
362 packet_num = 0;
363 change_mode(LISTEN_RAW);
364 }
365 break;
366 case BOUND:
367 /* Lease is starting to run out, time to enter renewing state */
368 state = RENEWING;
369 change_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000370 DEBUG("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000371 /* fall right through */
372 case RENEWING:
373 /* Either set a new T1, or enter REBINDING state */
374 if ((t2 - t1) <= (lease / 14400 + 1)) {
375 /* timed out, enter rebinding state */
376 state = REBINDING;
377 timeout = now + (t2 - t1);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000378 DEBUG("Entering rebinding state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000379 } else {
380 /* send a request packet */
381 send_renew(xid, server_addr, requested_ip); /* unicast */
382
383 t1 = (t2 - t1) / 2 + t1;
384 timeout = t1 + start;
385 }
386 break;
387 case REBINDING:
388 /* Either set a new T2, or enter INIT state */
389 if ((lease - t2) <= (lease / 14400 + 1)) {
390 /* timed out, enter init state */
391 state = INIT_SELECTING;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000392 bb_info_msg("Lease lost, entering init state");
Rob Landley3f785612006-05-28 01:06:36 +0000393 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000394 timeout = now;
395 packet_num = 0;
396 change_mode(LISTEN_RAW);
397 } else {
398 /* send a request packet */
399 send_renew(xid, 0, requested_ip); /* broadcast */
400
401 t2 = (lease - t2) / 2 + t2;
402 timeout = t2 + start;
403 }
404 break;
405 case RELEASED:
406 /* yah, I know, *you* say it would never happen */
407 timeout = 0x7fffffff;
408 break;
409 }
410 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
411 /* a packet is ready, read it */
412
413 if (listen_mode == LISTEN_KERNEL)
Rob Landley3f785612006-05-28 01:06:36 +0000414 len = udhcp_get_packet(&packet, fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000415 else len = get_raw_packet(&packet, fd);
416
417 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000418 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000419 change_mode(listen_mode); /* just close and reopen */
420 }
421 if (len < 0) continue;
422
423 if (packet.xid != xid) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000424 DEBUG("Ignoring XID %lx (our xid is %lx)",
Mike Frysinger7031f622006-05-08 03:20:50 +0000425 (unsigned long) packet.xid, xid);
426 continue;
427 }
428
429 /* Ignore packets that aren't for us */
430 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000431 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000432 continue;
433 }
434
435 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000436 bb_error_msg("cannot get option from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000437 continue;
438 }
439
440 switch (state) {
441 case INIT_SELECTING:
442 /* Must be a DHCPOFFER to one of our xid's */
443 if (*message == DHCPOFFER) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000444 temp = get_option(&packet, DHCP_SERVER_ID);
445 if (temp) {
446 server_addr = *(uint32_t*)temp;
Mike Frysinger7031f622006-05-08 03:20:50 +0000447 xid = packet.xid;
448 requested_ip = packet.yiaddr;
449
450 /* enter requesting state */
451 state = REQUESTING;
452 timeout = now;
453 packet_num = 0;
454 } else {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000455 bb_error_msg("no server ID in message");
Mike Frysinger7031f622006-05-08 03:20:50 +0000456 }
457 }
458 break;
459 case RENEW_REQUESTED:
460 case REQUESTING:
461 case RENEWING:
462 case REBINDING:
463 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000464 temp = get_option(&packet, DHCP_LEASE_TIME);
465 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000466 bb_error_msg("no lease time with ACK, using 1 hour lease");
Mike Frysinger7031f622006-05-08 03:20:50 +0000467 lease = 60 * 60;
468 } else {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000469 lease = ntohl(*(uint32_t*)temp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000470 }
471
472 /* enter bound state */
473 t1 = lease / 2;
474
475 /* little fixed point for n * .875 */
476 t2 = (lease * 0x7) >> 3;
477 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000478 bb_info_msg("Lease of %s obtained, lease time %ld",
Mike Frysinger7031f622006-05-08 03:20:50 +0000479 inet_ntoa(temp_addr), lease);
480 start = now;
481 timeout = t1 + start;
482 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000483 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000484 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
485
486 state = BOUND;
487 change_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000488 if (client_config.quit_after_lease) {
489 if (client_config.release_on_quit)
490 perform_release();
Mike Frysinger7031f622006-05-08 03:20:50 +0000491 return 0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000492 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000493 if (!client_config.foreground)
494 client_background();
495
496 } else if (*message == DHCPNAK) {
497 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000498 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000499 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000500 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000501 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000502 state = INIT_SELECTING;
503 timeout = now;
504 requested_ip = 0;
505 packet_num = 0;
506 change_mode(LISTEN_RAW);
507 sleep(3); /* avoid excessive network traffic */
508 }
509 break;
510 /* case BOUND, RELEASED: - ignore all packets */
511 }
512 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
513 switch (sig) {
514 case SIGUSR1:
515 perform_renew();
516 break;
517 case SIGUSR2:
518 perform_release();
519 break;
520 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000521 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000522 if (client_config.release_on_quit)
523 perform_release();
Mike Frysinger7031f622006-05-08 03:20:50 +0000524 return 0;
525 }
526 } else if (retval == -1 && errno == EINTR) {
527 /* a signal was caught */
528 } else {
529 /* An error occured */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000530 bb_perror_msg("select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000531 }
532
533 }
534 return 0;
535}