blob: e48e1f25e4856cd00c6a489f031df124e87cb76c [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 */
Denis Vlasenko74c9d232007-01-18 15:42:00 +000026static uint32_t server_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000027static 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
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000036struct client_config_t client_config;
37
Mike Frysinger7031f622006-05-08 03:20:50 +000038
Mike Frysinger7031f622006-05-08 03:20:50 +000039/* just a little helper */
40static void change_mode(int new_mode)
41{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000042 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000043 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
44 if (fd >= 0) close(fd);
45 fd = -1;
46 listen_mode = new_mode;
47}
48
49
50/* perform a renew */
51static void perform_renew(void)
52{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000053 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000054 switch (state) {
55 case BOUND:
56 change_mode(LISTEN_KERNEL);
57 case RENEWING:
58 case REBINDING:
59 state = RENEW_REQUESTED;
60 break;
61 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000062 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000063 case REQUESTING:
64 case RELEASED:
65 change_mode(LISTEN_RAW);
66 state = INIT_SELECTING;
67 break;
68 case INIT_SELECTING:
69 break;
70 }
71
72 /* start things over */
73 packet_num = 0;
74
75 /* Kill any timeouts because the user wants this to hurry along */
76 timeout = 0;
77}
78
79
80/* perform a release */
81static void perform_release(void)
82{
83 char buffer[16];
84 struct in_addr temp_addr;
85
86 /* send release packet */
87 if (state == BOUND || state == RENEWING || state == REBINDING) {
88 temp_addr.s_addr = server_addr;
89 sprintf(buffer, "%s", inet_ntoa(temp_addr));
90 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000091 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000092 inet_ntoa(temp_addr), buffer);
93 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +000094 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000095 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000096 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +000097
98 change_mode(LISTEN_NONE);
99 state = RELEASED;
100 timeout = 0x7fffffff;
101}
102
103
104static void client_background(void)
105{
Rob Landley3f785612006-05-28 01:06:36 +0000106 udhcp_background(client_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +0000107 client_config.foreground = 1; /* Do not fork again. */
108 client_config.background_if_no_lease = 0;
109}
110
111
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000112static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
113{
114 uint8_t *storage;
115 int len = strlen(str);
116 if (len > 255) len = 255;
117 storage = xzalloc(len + extra + OPT_DATA);
118 storage[OPT_CODE] = code;
119 storage[OPT_LEN] = len + extra;
120 memcpy(storage + extra + OPT_DATA, str, len);
121 return storage;
122}
123
124
Mike Frysinger7031f622006-05-08 03:20:50 +0000125int udhcpc_main(int argc, char *argv[])
Mike Frysinger7031f622006-05-08 03:20:50 +0000126{
127 uint8_t *temp, *message;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000128 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
Mike Frysinger7031f622006-05-08 03:20:50 +0000129 unsigned long t1 = 0, t2 = 0, xid = 0;
Rob Landley49ea4662006-09-11 01:34:21 +0000130 unsigned long start = 0, lease = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000131 long now;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000132 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000133 int max_fd;
134 int sig;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000135 int retval;
136 int len;
Mike Frysinger7031f622006-05-08 03:20:50 +0000137 int no_clientid = 0;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000138 fd_set rfds;
139 struct timeval tv;
140 struct dhcpMessage packet;
141 struct in_addr temp_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000142
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000143 enum {
144 OPT_c = 1 << 0,
145 OPT_C = 1 << 1,
146 OPT_V = 1 << 2,
147 OPT_f = 1 << 3,
148 OPT_b = 1 << 4,
149 OPT_H = 1 << 5,
150 OPT_h = 1 << 6,
151 OPT_F = 1 << 7,
152 OPT_i = 1 << 8,
153 OPT_n = 1 << 9,
154 OPT_p = 1 << 10,
155 OPT_q = 1 << 11,
156 OPT_R = 1 << 12,
157 OPT_r = 1 << 13,
158 OPT_s = 1 << 14,
159 OPT_T = 1 << 15,
160 OPT_t = 1 << 16,
161 OPT_v = 1 << 17,
162 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000163#if ENABLE_GETOPT_LONG
Mike Frysinger7031f622006-05-08 03:20:50 +0000164 static const struct option arg_options[] = {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000165 { "clientid", required_argument, 0, 'c' },
166 { "clientid-none", no_argument, 0, 'C' },
167 { "vendorclass", required_argument, 0, 'V' },
168 { "foreground", no_argument, 0, 'f' },
169 { "background", no_argument, 0, 'b' },
170 { "hostname", required_argument, 0, 'H' },
171 { "hostname", required_argument, 0, 'h' },
172 { "fqdn", required_argument, 0, 'F' },
173 { "interface", required_argument, 0, 'i' },
174 { "now", no_argument, 0, 'n' },
175 { "pidfile", required_argument, 0, 'p' },
176 { "quit", no_argument, 0, 'q' },
177 { "release", no_argument, 0, 'R' },
178 { "request", required_argument, 0, 'r' },
179 { "script", required_argument, 0, 's' },
180 { "timeout", required_argument, 0, 'T' },
181 { "version", no_argument, 0, 'v' },
182 { "retries", required_argument, 0, 't' },
183 { 0, 0, 0, 0 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000184 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000185#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000186 /* Default options. */
187 client_config.interface = "eth0";
188 client_config.script = DEFAULT_SCRIPT;
189 client_config.retries = 3;
190 client_config.timeout = 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000191
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000192 /* Parse command line */
193 opt_complementary = "?:c--C:C--c" // mutually exclusive
194 ":hH:Hh"; // -h and -H are the same
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000195#if ENABLE_GETOPT_LONG
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000196 applet_long_options = arg_options;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000197#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000198 opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
199 &str_c, &str_V, &str_h, &str_h, &str_F,
200 &client_config.interface, &client_config.pidfile, &str_r,
201 &client_config.script, &str_T, &str_t
202 );
203
204 if (opt & OPT_c)
205 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
206 if (opt & OPT_C)
207 no_clientid = 1;
208 if (opt & OPT_V)
209 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
210 if (opt & OPT_f)
211 client_config.foreground = 1;
212 if (opt & OPT_b)
213 client_config.background_if_no_lease = 1;
214 if (opt & OPT_h)
215 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
216 if (opt & OPT_F) {
217 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
218 /* Flags: 0000NEOS
219 S: 1 => Client requests Server to update A RR in DNS as well as PTR
220 O: 1 => Server indicates to client that DNS has been updated regardless
221 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
222 N: 1 => Client requests Server to not update DNS
223 */
224 client_config.fqdn[OPT_DATA + 0] = 0x1;
225 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
226 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
227 }
228 // if (opt & OPT_i) client_config.interface = ...
229 if (opt & OPT_n)
230 client_config.abort_if_no_lease = 1;
231 // if (opt & OPT_p) client_config.pidfile = ...
232 if (opt & OPT_q)
233 client_config.quit_after_lease = 1;
234 if (opt & OPT_R)
235 client_config.release_on_quit = 1;
236 if (opt & OPT_r)
237 requested_ip = inet_addr(str_r);
238 // if (opt & OPT_s) client_config.script = ...
239 if (opt & OPT_T)
240 client_config.timeout = xatoi_u(str_T);
241 if (opt & OPT_t)
242 client_config.retries = xatoi_u(str_t);
243 if (opt & OPT_v) {
244 printf("version %s\n\n", BB_VER);
245 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000246 }
247
248 /* Start the log, sanitize fd's, and write a pid file */
Denis Vlasenko239369b2006-09-07 17:05:44 +0000249 udhcp_start_log_and_pid(client_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +0000250
251 if (read_interface(client_config.interface, &client_config.ifindex,
252 NULL, client_config.arp) < 0)
253 return 1;
254
255 /* if not set, and not suppressed, setup the default client ID */
256 if (!client_config.clientid && !no_clientid) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000257 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000258 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000259 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000260 }
261
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000262 if (!client_config.vendorclass)
263 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000264
265 /* setup the signal pipe */
266 udhcp_sp_setup();
267
268 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000269 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000270 change_mode(LISTEN_RAW);
271
272 for (;;) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000273 tv.tv_sec = timeout - uptime();
274 tv.tv_usec = 0;
275
276 if (listen_mode != LISTEN_NONE && fd < 0) {
277 if (listen_mode == LISTEN_KERNEL)
278 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
279 else
280 fd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000281 }
282 max_fd = udhcp_sp_fd_set(&rfds, fd);
283
284 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000285 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000286 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
287 } else retval = 0; /* If we already timed out, fall through */
288
289 now = uptime();
290 if (retval == 0) {
291 /* timeout dropped to zero */
292 switch (state) {
293 case INIT_SELECTING:
294 if (packet_num < client_config.retries) {
295 if (packet_num == 0)
296 xid = random_xid();
297
298 /* send discover packet */
299 send_discover(xid, requested_ip); /* broadcast */
300
301 timeout = now + client_config.timeout;
302 packet_num++;
303 } else {
Rob Landley3f785612006-05-28 01:06:36 +0000304 udhcp_run_script(NULL, "leasefail");
Mike Frysinger7031f622006-05-08 03:20:50 +0000305 if (client_config.background_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000306 bb_info_msg("No lease, forking to background");
Mike Frysinger7031f622006-05-08 03:20:50 +0000307 client_background();
308 } else if (client_config.abort_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000309 bb_info_msg("No lease, failing");
Mike Frysinger7031f622006-05-08 03:20:50 +0000310 return 1;
311 }
312 /* wait to try again */
313 packet_num = 0;
314 timeout = now + 60;
315 }
316 break;
317 case RENEW_REQUESTED:
318 case REQUESTING:
319 if (packet_num < client_config.retries) {
320 /* send request packet */
321 if (state == RENEW_REQUESTED)
322 send_renew(xid, server_addr, requested_ip); /* unicast */
323 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
324
325 timeout = now + ((packet_num == 2) ? 10 : 2);
326 packet_num++;
327 } else {
328 /* timed out, go back to init state */
Rob Landley3f785612006-05-28 01:06:36 +0000329 if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000330 state = INIT_SELECTING;
331 timeout = now;
332 packet_num = 0;
333 change_mode(LISTEN_RAW);
334 }
335 break;
336 case BOUND:
337 /* Lease is starting to run out, time to enter renewing state */
338 state = RENEWING;
339 change_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000340 DEBUG("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000341 /* fall right through */
342 case RENEWING:
343 /* Either set a new T1, or enter REBINDING state */
344 if ((t2 - t1) <= (lease / 14400 + 1)) {
345 /* timed out, enter rebinding state */
346 state = REBINDING;
347 timeout = now + (t2 - t1);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000348 DEBUG("Entering rebinding state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000349 } else {
350 /* send a request packet */
351 send_renew(xid, server_addr, requested_ip); /* unicast */
352
353 t1 = (t2 - t1) / 2 + t1;
354 timeout = t1 + start;
355 }
356 break;
357 case REBINDING:
358 /* Either set a new T2, or enter INIT state */
359 if ((lease - t2) <= (lease / 14400 + 1)) {
360 /* timed out, enter init state */
361 state = INIT_SELECTING;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000362 bb_info_msg("Lease lost, entering init state");
Rob Landley3f785612006-05-28 01:06:36 +0000363 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000364 timeout = now;
365 packet_num = 0;
366 change_mode(LISTEN_RAW);
367 } else {
368 /* send a request packet */
369 send_renew(xid, 0, requested_ip); /* broadcast */
370
371 t2 = (lease - t2) / 2 + t2;
372 timeout = t2 + start;
373 }
374 break;
375 case RELEASED:
376 /* yah, I know, *you* say it would never happen */
377 timeout = 0x7fffffff;
378 break;
379 }
380 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
381 /* a packet is ready, read it */
382
383 if (listen_mode == LISTEN_KERNEL)
Rob Landley3f785612006-05-28 01:06:36 +0000384 len = udhcp_get_packet(&packet, fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000385 else len = get_raw_packet(&packet, fd);
386
387 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000388 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000389 change_mode(listen_mode); /* just close and reopen */
390 }
391 if (len < 0) continue;
392
393 if (packet.xid != xid) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000394 DEBUG("Ignoring XID %lx (our xid is %lx)",
Mike Frysinger7031f622006-05-08 03:20:50 +0000395 (unsigned long) packet.xid, xid);
396 continue;
397 }
398
399 /* Ignore packets that aren't for us */
400 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000401 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000402 continue;
403 }
404
405 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000406 bb_error_msg("cannot get option from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000407 continue;
408 }
409
410 switch (state) {
411 case INIT_SELECTING:
412 /* Must be a DHCPOFFER to one of our xid's */
413 if (*message == DHCPOFFER) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000414 temp = get_option(&packet, DHCP_SERVER_ID);
415 if (temp) {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000416 /* can be misaligned, thus memcpy */
417 memcpy(&server_addr, temp, 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000418 xid = packet.xid;
419 requested_ip = packet.yiaddr;
420
421 /* enter requesting state */
422 state = REQUESTING;
423 timeout = now;
424 packet_num = 0;
425 } else {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000426 bb_error_msg("no server ID in message");
Mike Frysinger7031f622006-05-08 03:20:50 +0000427 }
428 }
429 break;
430 case RENEW_REQUESTED:
431 case REQUESTING:
432 case RENEWING:
433 case REBINDING:
434 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000435 temp = get_option(&packet, DHCP_LEASE_TIME);
436 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000437 bb_error_msg("no lease time with ACK, using 1 hour lease");
Mike Frysinger7031f622006-05-08 03:20:50 +0000438 lease = 60 * 60;
439 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000440 /* can be misaligned, thus memcpy */
441 memcpy(&lease, temp, 4);
442 lease = ntohl(lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000443 }
444
445 /* enter bound state */
446 t1 = lease / 2;
447
448 /* little fixed point for n * .875 */
449 t2 = (lease * 0x7) >> 3;
450 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000451 bb_info_msg("Lease of %s obtained, lease time %ld",
Mike Frysinger7031f622006-05-08 03:20:50 +0000452 inet_ntoa(temp_addr), lease);
453 start = now;
454 timeout = t1 + start;
455 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000456 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000457 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
458
459 state = BOUND;
460 change_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000461 if (client_config.quit_after_lease) {
462 if (client_config.release_on_quit)
463 perform_release();
Mike Frysinger7031f622006-05-08 03:20:50 +0000464 return 0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000465 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000466 if (!client_config.foreground)
467 client_background();
468
469 } else if (*message == DHCPNAK) {
470 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000471 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000472 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000473 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000474 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000475 state = INIT_SELECTING;
476 timeout = now;
477 requested_ip = 0;
478 packet_num = 0;
479 change_mode(LISTEN_RAW);
480 sleep(3); /* avoid excessive network traffic */
481 }
482 break;
483 /* case BOUND, RELEASED: - ignore all packets */
484 }
485 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
486 switch (sig) {
487 case SIGUSR1:
488 perform_renew();
489 break;
490 case SIGUSR2:
491 perform_release();
492 break;
493 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000494 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000495 if (client_config.release_on_quit)
496 perform_release();
Mike Frysinger7031f622006-05-08 03:20:50 +0000497 return 0;
498 }
499 } else if (retval == -1 && errno == EINTR) {
500 /* a signal was caught */
501 } else {
502 /* An error occured */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000503 bb_perror_msg("select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000504 }
505
506 }
507 return 0;
508}