Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | /* 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 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <sys/time.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/file.h> |
| 26 | #include <unistd.h> |
| 27 | #include <getopt.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <sys/socket.h> |
| 30 | #include <netinet/in.h> |
| 31 | #include <arpa/inet.h> |
| 32 | #include <signal.h> |
| 33 | #include <time.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/ioctl.h> |
| 36 | #include <net/if.h> |
| 37 | #include <errno.h> |
| 38 | |
| 39 | #include "dhcpd.h" |
| 40 | #include "dhcpc.h" |
| 41 | #include "options.h" |
| 42 | #include "clientpacket.h" |
| 43 | #include "packet.h" |
| 44 | #include "script.h" |
| 45 | #include "socket.h" |
| 46 | #include "debug.h" |
| 47 | #include "pidfile.h" |
| 48 | |
| 49 | static int state; |
| 50 | static unsigned long requested_ip; /* = 0 */ |
| 51 | static unsigned long server_addr; |
| 52 | static unsigned long timeout; |
| 53 | static int packet_num; /* = 0 */ |
Russ Dill | 6393d69 | 2003-01-21 22:39:34 +0000 | [diff] [blame] | 54 | static int fd = -1; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 55 | static int signal_pipe[2]; |
| 56 | |
| 57 | #define LISTEN_NONE 0 |
| 58 | #define LISTEN_KERNEL 1 |
| 59 | #define LISTEN_RAW 2 |
| 60 | static int listen_mode; |
| 61 | |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame^] | 62 | #ifdef CONFIG_INSTALL_NO_USR |
| 63 | #define DEFAULT_SCRIPT "/share/udhcpc/default.script" |
| 64 | #else |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 65 | #define DEFAULT_SCRIPT "/usr/share/udhcpc/default.script" |
Glenn L McGrath | 4a4c677 | 2003-02-15 11:50:33 +0000 | [diff] [blame^] | 66 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 67 | |
| 68 | struct client_config_t client_config = { |
| 69 | /* Default options. */ |
| 70 | abort_if_no_lease: 0, |
| 71 | foreground: 0, |
| 72 | quit_after_lease: 0, |
| 73 | background_if_no_lease: 0, |
| 74 | interface: "eth0", |
| 75 | pidfile: NULL, |
| 76 | script: DEFAULT_SCRIPT, |
| 77 | clientid: NULL, |
| 78 | hostname: NULL, |
| 79 | ifindex: 0, |
| 80 | arp: "\0\0\0\0\0\0", /* appease gcc-3.0 */ |
| 81 | }; |
| 82 | |
Russ Dill | 54fde9e | 2002-12-11 22:43:37 +0000 | [diff] [blame] | 83 | #ifndef IN_BUSYBOX |
Eric Andersen | d7e1a6c | 2002-12-11 07:46:30 +0000 | [diff] [blame] | 84 | static void __attribute__ ((noreturn)) show_usage(void) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 85 | { |
| 86 | printf( |
| 87 | "Usage: udhcpc [OPTIONS]\n\n" |
| 88 | " -c, --clientid=CLIENTID Client identifier\n" |
| 89 | " -H, --hostname=HOSTNAME Client hostname\n" |
| 90 | " -h Alias for -H\n" |
| 91 | " -f, --foreground Do not fork after getting lease\n" |
| 92 | " -b, --background Fork to background if lease cannot be\n" |
| 93 | " immediately negotiated.\n" |
| 94 | " -i, --interface=INTERFACE Interface to use (default: eth0)\n" |
| 95 | " -n, --now Exit with failure if lease cannot be\n" |
| 96 | " immediately negotiated.\n" |
| 97 | " -p, --pidfile=file Store process ID of daemon in file\n" |
| 98 | " -q, --quit Quit after obtaining lease\n" |
| 99 | " -r, --request=IP IP address to request (default: none)\n" |
| 100 | " -s, --script=file Run file at dhcp events (default:\n" |
| 101 | " " DEFAULT_SCRIPT ")\n" |
| 102 | " -v, --version Display version\n" |
| 103 | ); |
| 104 | exit(0); |
| 105 | } |
Eric Andersen | d7e1a6c | 2002-12-11 07:46:30 +0000 | [diff] [blame] | 106 | #else |
| 107 | extern void show_usage(void) __attribute__ ((noreturn)); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | |
| 111 | /* just a little helper */ |
| 112 | static void change_mode(int new_mode) |
| 113 | { |
| 114 | DEBUG(LOG_INFO, "entering %s listen mode", |
| 115 | new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); |
Russ Dill | 6393d69 | 2003-01-21 22:39:34 +0000 | [diff] [blame] | 116 | if (fd >= 0) close(fd); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 117 | fd = -1; |
| 118 | listen_mode = new_mode; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /* perform a renew */ |
| 123 | static void perform_renew(void) |
| 124 | { |
| 125 | LOG(LOG_INFO, "Performing a DHCP renew"); |
| 126 | switch (state) { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 127 | case BOUND: |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 128 | change_mode(LISTEN_KERNEL); |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 129 | case RENEWING: |
| 130 | case REBINDING: |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 131 | state = RENEW_REQUESTED; |
| 132 | break; |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 133 | case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ |
| 134 | run_script(NULL, "deconfig"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 135 | case REQUESTING: |
| 136 | case RELEASED: |
| 137 | change_mode(LISTEN_RAW); |
| 138 | state = INIT_SELECTING; |
| 139 | break; |
| 140 | case INIT_SELECTING: |
| 141 | } |
| 142 | |
| 143 | /* start things over */ |
| 144 | packet_num = 0; |
| 145 | |
| 146 | /* Kill any timeouts because the user wants this to hurry along */ |
| 147 | timeout = 0; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /* perform a release */ |
| 152 | static void perform_release(void) |
| 153 | { |
| 154 | char buffer[16]; |
| 155 | struct in_addr temp_addr; |
| 156 | |
| 157 | /* send release packet */ |
| 158 | if (state == BOUND || state == RENEWING || state == REBINDING) { |
| 159 | temp_addr.s_addr = server_addr; |
| 160 | sprintf(buffer, "%s", inet_ntoa(temp_addr)); |
| 161 | temp_addr.s_addr = requested_ip; |
| 162 | LOG(LOG_INFO, "Unicasting a release of %s to %s", |
| 163 | inet_ntoa(temp_addr), buffer); |
| 164 | send_release(server_addr, requested_ip); /* unicast */ |
| 165 | run_script(NULL, "deconfig"); |
| 166 | } |
| 167 | LOG(LOG_INFO, "Entering released state"); |
| 168 | |
| 169 | change_mode(LISTEN_NONE); |
| 170 | state = RELEASED; |
| 171 | timeout = 0x7fffffff; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /* Exit and cleanup */ |
| 176 | static void exit_client(int retval) |
| 177 | { |
| 178 | pidfile_delete(client_config.pidfile); |
| 179 | CLOSE_LOG(); |
| 180 | exit(retval); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* Signal handler */ |
| 185 | static void signal_handler(int sig) |
| 186 | { |
| 187 | if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) { |
| 188 | LOG(LOG_ERR, "Could not send signal: %s", |
| 189 | strerror(errno)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | static void background(void) |
| 195 | { |
| 196 | int pid_fd; |
| 197 | |
| 198 | pid_fd = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */ |
| 199 | while (pid_fd >= 0 && pid_fd < 3) pid_fd = dup(pid_fd); /* don't let daemon close it */ |
| 200 | if (daemon(0, 0) == -1) { |
| 201 | perror("fork"); |
| 202 | exit_client(1); |
| 203 | } |
| 204 | client_config.foreground = 1; /* Do not fork again. */ |
Russ Dill | 6393d69 | 2003-01-21 22:39:34 +0000 | [diff] [blame] | 205 | client_config.background_if_no_lease = 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 206 | pidfile_write_release(pid_fd); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | #ifdef COMBINED_BINARY |
| 211 | int udhcpc_main(int argc, char *argv[]) |
| 212 | #else |
| 213 | int main(int argc, char *argv[]) |
| 214 | #endif |
| 215 | { |
| 216 | unsigned char *temp, *message; |
| 217 | unsigned long t1 = 0, t2 = 0, xid = 0; |
| 218 | unsigned long start = 0, lease; |
| 219 | fd_set rfds; |
| 220 | int retval; |
| 221 | struct timeval tv; |
| 222 | int c, len; |
| 223 | struct dhcpMessage packet; |
| 224 | struct in_addr temp_addr; |
| 225 | int pid_fd; |
| 226 | time_t now; |
| 227 | int max_fd; |
| 228 | int sig; |
| 229 | |
| 230 | static struct option arg_options[] = { |
| 231 | {"clientid", required_argument, 0, 'c'}, |
| 232 | {"foreground", no_argument, 0, 'f'}, |
| 233 | {"background", no_argument, 0, 'b'}, |
| 234 | {"hostname", required_argument, 0, 'H'}, |
| 235 | {"hostname", required_argument, 0, 'h'}, |
| 236 | {"interface", required_argument, 0, 'i'}, |
| 237 | {"now", no_argument, 0, 'n'}, |
| 238 | {"pidfile", required_argument, 0, 'p'}, |
| 239 | {"quit", no_argument, 0, 'q'}, |
| 240 | {"request", required_argument, 0, 'r'}, |
| 241 | {"script", required_argument, 0, 's'}, |
| 242 | {"version", no_argument, 0, 'v'}, |
| 243 | {"help", no_argument, 0, '?'}, |
| 244 | {0, 0, 0, 0} |
| 245 | }; |
| 246 | |
| 247 | /* get options */ |
| 248 | while (1) { |
| 249 | int option_index = 0; |
| 250 | c = getopt_long(argc, argv, "c:fbH:h:i:np:qr:s:v", arg_options, &option_index); |
| 251 | if (c == -1) break; |
| 252 | |
| 253 | switch (c) { |
| 254 | case 'c': |
| 255 | len = strlen(optarg) > 255 ? 255 : strlen(optarg); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame] | 256 | if (client_config.clientid) free(client_config.clientid); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 257 | client_config.clientid = xmalloc(len + 2); |
| 258 | client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID; |
| 259 | client_config.clientid[OPT_LEN] = len; |
| 260 | client_config.clientid[OPT_DATA] = '\0'; |
| 261 | strncpy(client_config.clientid + OPT_DATA, optarg, len); |
| 262 | break; |
| 263 | case 'f': |
| 264 | client_config.foreground = 1; |
| 265 | break; |
| 266 | case 'b': |
| 267 | client_config.background_if_no_lease = 1; |
| 268 | break; |
| 269 | case 'h': |
| 270 | case 'H': |
| 271 | len = strlen(optarg) > 255 ? 255 : strlen(optarg); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame] | 272 | if (client_config.hostname) free(client_config.hostname); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 273 | client_config.hostname = xmalloc(len + 2); |
| 274 | client_config.hostname[OPT_CODE] = DHCP_HOST_NAME; |
| 275 | client_config.hostname[OPT_LEN] = len; |
| 276 | strncpy(client_config.hostname + 2, optarg, len); |
| 277 | break; |
| 278 | case 'i': |
| 279 | client_config.interface = optarg; |
| 280 | break; |
| 281 | case 'n': |
| 282 | client_config.abort_if_no_lease = 1; |
| 283 | break; |
| 284 | case 'p': |
| 285 | client_config.pidfile = optarg; |
| 286 | break; |
| 287 | case 'q': |
| 288 | client_config.quit_after_lease = 1; |
| 289 | break; |
| 290 | case 'r': |
| 291 | requested_ip = inet_addr(optarg); |
| 292 | break; |
| 293 | case 's': |
| 294 | client_config.script = optarg; |
| 295 | break; |
| 296 | case 'v': |
| 297 | printf("udhcpcd, version %s\n\n", VERSION); |
| 298 | exit_client(0); |
| 299 | break; |
| 300 | default: |
| 301 | show_usage(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | OPEN_LOG("udhcpc"); |
| 306 | LOG(LOG_INFO, "udhcp client (v%s) started", VERSION); |
| 307 | |
| 308 | pid_fd = pidfile_acquire(client_config.pidfile); |
| 309 | pidfile_write_release(pid_fd); |
| 310 | |
| 311 | if (read_interface(client_config.interface, &client_config.ifindex, |
| 312 | NULL, client_config.arp) < 0) |
| 313 | exit_client(1); |
| 314 | |
| 315 | if (!client_config.clientid) { |
| 316 | client_config.clientid = xmalloc(6 + 3); |
| 317 | client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID; |
| 318 | client_config.clientid[OPT_LEN] = 7; |
| 319 | client_config.clientid[OPT_DATA] = 1; |
| 320 | memcpy(client_config.clientid + 3, client_config.arp, 6); |
| 321 | } |
| 322 | |
| 323 | /* setup signal handlers */ |
| 324 | socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe); |
| 325 | signal(SIGUSR1, signal_handler); |
| 326 | signal(SIGUSR2, signal_handler); |
| 327 | signal(SIGTERM, signal_handler); |
| 328 | |
| 329 | state = INIT_SELECTING; |
| 330 | run_script(NULL, "deconfig"); |
| 331 | change_mode(LISTEN_RAW); |
| 332 | |
| 333 | for (;;) { |
| 334 | |
| 335 | tv.tv_sec = timeout - time(0); |
| 336 | tv.tv_usec = 0; |
| 337 | FD_ZERO(&rfds); |
| 338 | |
| 339 | if (listen_mode != LISTEN_NONE && fd < 0) { |
| 340 | if (listen_mode == LISTEN_KERNEL) |
| 341 | fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface); |
| 342 | else |
| 343 | fd = raw_socket(client_config.ifindex); |
| 344 | if (fd < 0) { |
| 345 | LOG(LOG_ERR, "FATAL: couldn't listen on socket, %s", strerror(errno)); |
| 346 | exit_client(0); |
| 347 | } |
| 348 | } |
| 349 | if (fd >= 0) FD_SET(fd, &rfds); |
| 350 | FD_SET(signal_pipe[0], &rfds); |
| 351 | |
| 352 | if (tv.tv_sec > 0) { |
| 353 | DEBUG(LOG_INFO, "Waiting on select...\n"); |
| 354 | max_fd = signal_pipe[0] > fd ? signal_pipe[0] : fd; |
| 355 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); |
| 356 | } else retval = 0; /* If we already timed out, fall through */ |
| 357 | |
| 358 | now = time(0); |
| 359 | if (retval == 0) { |
| 360 | /* timeout dropped to zero */ |
| 361 | switch (state) { |
| 362 | case INIT_SELECTING: |
| 363 | if (packet_num < 3) { |
| 364 | if (packet_num == 0) |
| 365 | xid = random_xid(); |
| 366 | |
| 367 | /* send discover packet */ |
| 368 | send_discover(xid, requested_ip); /* broadcast */ |
| 369 | |
| 370 | timeout = now + ((packet_num == 2) ? 4 : 2); |
| 371 | packet_num++; |
| 372 | } else { |
| 373 | if (client_config.background_if_no_lease) { |
| 374 | LOG(LOG_INFO, "No lease, forking to background."); |
| 375 | background(); |
| 376 | } else if (client_config.abort_if_no_lease) { |
| 377 | LOG(LOG_INFO, "No lease, failing."); |
| 378 | exit_client(1); |
| 379 | } |
| 380 | /* wait to try again */ |
| 381 | packet_num = 0; |
| 382 | timeout = now + 60; |
| 383 | } |
| 384 | break; |
| 385 | case RENEW_REQUESTED: |
| 386 | case REQUESTING: |
| 387 | if (packet_num < 3) { |
| 388 | /* send request packet */ |
| 389 | if (state == RENEW_REQUESTED) |
| 390 | send_renew(xid, server_addr, requested_ip); /* unicast */ |
| 391 | else send_selecting(xid, server_addr, requested_ip); /* broadcast */ |
| 392 | |
| 393 | timeout = now + ((packet_num == 2) ? 10 : 2); |
| 394 | packet_num++; |
| 395 | } else { |
| 396 | /* timed out, go back to init state */ |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 397 | if (state == RENEW_REQUESTED) run_script(NULL, "deconfig"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 398 | state = INIT_SELECTING; |
| 399 | timeout = now; |
| 400 | packet_num = 0; |
| 401 | change_mode(LISTEN_RAW); |
| 402 | } |
| 403 | break; |
| 404 | case BOUND: |
| 405 | /* Lease is starting to run out, time to enter renewing state */ |
| 406 | state = RENEWING; |
| 407 | change_mode(LISTEN_KERNEL); |
| 408 | DEBUG(LOG_INFO, "Entering renew state"); |
| 409 | /* fall right through */ |
| 410 | case RENEWING: |
| 411 | /* Either set a new T1, or enter REBINDING state */ |
| 412 | if ((t2 - t1) <= (lease / 14400 + 1)) { |
| 413 | /* timed out, enter rebinding state */ |
| 414 | state = REBINDING; |
| 415 | timeout = now + (t2 - t1); |
| 416 | DEBUG(LOG_INFO, "Entering rebinding state"); |
| 417 | } else { |
| 418 | /* send a request packet */ |
| 419 | send_renew(xid, server_addr, requested_ip); /* unicast */ |
| 420 | |
| 421 | t1 = (t2 - t1) / 2 + t1; |
| 422 | timeout = t1 + start; |
| 423 | } |
| 424 | break; |
| 425 | case REBINDING: |
| 426 | /* Either set a new T2, or enter INIT state */ |
| 427 | if ((lease - t2) <= (lease / 14400 + 1)) { |
| 428 | /* timed out, enter init state */ |
| 429 | state = INIT_SELECTING; |
| 430 | LOG(LOG_INFO, "Lease lost, entering init state"); |
| 431 | run_script(NULL, "deconfig"); |
| 432 | timeout = now; |
| 433 | packet_num = 0; |
| 434 | change_mode(LISTEN_RAW); |
| 435 | } else { |
| 436 | /* send a request packet */ |
| 437 | send_renew(xid, 0, requested_ip); /* broadcast */ |
| 438 | |
| 439 | t2 = (lease - t2) / 2 + t2; |
| 440 | timeout = t2 + start; |
| 441 | } |
| 442 | break; |
| 443 | case RELEASED: |
| 444 | /* yah, I know, *you* say it would never happen */ |
| 445 | timeout = 0x7fffffff; |
| 446 | break; |
| 447 | } |
| 448 | } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) { |
| 449 | /* a packet is ready, read it */ |
| 450 | |
| 451 | if (listen_mode == LISTEN_KERNEL) |
| 452 | len = get_packet(&packet, fd); |
| 453 | else len = get_raw_packet(&packet, fd); |
| 454 | |
| 455 | if (len == -1 && errno != EINTR) { |
| 456 | DEBUG(LOG_INFO, "error on read, %s, reopening socket", strerror(errno)); |
| 457 | change_mode(listen_mode); /* just close and reopen */ |
| 458 | } |
| 459 | if (len < 0) continue; |
| 460 | |
| 461 | if (packet.xid != xid) { |
| 462 | DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)", |
| 463 | (unsigned long) packet.xid, xid); |
| 464 | continue; |
| 465 | } |
| 466 | |
| 467 | if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) { |
| 468 | DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring"); |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | switch (state) { |
| 473 | case INIT_SELECTING: |
| 474 | /* Must be a DHCPOFFER to one of our xid's */ |
| 475 | if (*message == DHCPOFFER) { |
| 476 | if ((temp = get_option(&packet, DHCP_SERVER_ID))) { |
| 477 | memcpy(&server_addr, temp, 4); |
| 478 | xid = packet.xid; |
| 479 | requested_ip = packet.yiaddr; |
| 480 | |
| 481 | /* enter requesting state */ |
| 482 | state = REQUESTING; |
| 483 | timeout = now; |
| 484 | packet_num = 0; |
| 485 | } else { |
| 486 | DEBUG(LOG_ERR, "No server ID in message"); |
| 487 | } |
| 488 | } |
| 489 | break; |
| 490 | case RENEW_REQUESTED: |
| 491 | case REQUESTING: |
| 492 | case RENEWING: |
| 493 | case REBINDING: |
| 494 | if (*message == DHCPACK) { |
| 495 | if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) { |
| 496 | LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease"); |
| 497 | lease = 60 * 60; |
| 498 | } else { |
| 499 | memcpy(&lease, temp, 4); |
| 500 | lease = ntohl(lease); |
| 501 | } |
| 502 | |
| 503 | /* enter bound state */ |
| 504 | t1 = lease / 2; |
| 505 | |
| 506 | /* little fixed point for n * .875 */ |
| 507 | t2 = (lease * 0x7) >> 3; |
| 508 | temp_addr.s_addr = packet.yiaddr; |
| 509 | LOG(LOG_INFO, "Lease of %s obtained, lease time %ld", |
| 510 | inet_ntoa(temp_addr), lease); |
| 511 | start = now; |
| 512 | timeout = t1 + start; |
| 513 | requested_ip = packet.yiaddr; |
| 514 | run_script(&packet, |
| 515 | ((state == RENEWING || state == REBINDING) ? "renew" : "bound")); |
| 516 | |
| 517 | state = BOUND; |
| 518 | change_mode(LISTEN_NONE); |
| 519 | if (client_config.quit_after_lease) |
| 520 | exit_client(0); |
| 521 | if (!client_config.foreground) |
| 522 | background(); |
| 523 | |
| 524 | } else if (*message == DHCPNAK) { |
| 525 | /* return to init state */ |
| 526 | LOG(LOG_INFO, "Received DHCP NAK"); |
| 527 | run_script(&packet, "nak"); |
| 528 | if (state != REQUESTING) |
| 529 | run_script(NULL, "deconfig"); |
| 530 | state = INIT_SELECTING; |
| 531 | timeout = now; |
| 532 | requested_ip = 0; |
| 533 | packet_num = 0; |
| 534 | change_mode(LISTEN_RAW); |
| 535 | sleep(3); /* avoid excessive network traffic */ |
| 536 | } |
| 537 | break; |
| 538 | /* case BOUND, RELEASED: - ignore all packets */ |
| 539 | } |
| 540 | } else if (retval > 0 && FD_ISSET(signal_pipe[0], &rfds)) { |
Russ Dill | 6393d69 | 2003-01-21 22:39:34 +0000 | [diff] [blame] | 541 | if (read(signal_pipe[0], &sig, sizeof(sig)) < 0) { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 542 | DEBUG(LOG_ERR, "Could not read signal: %s", |
| 543 | strerror(errno)); |
| 544 | continue; /* probably just EINTR */ |
| 545 | } |
| 546 | switch (sig) { |
| 547 | case SIGUSR1: |
| 548 | perform_renew(); |
| 549 | break; |
| 550 | case SIGUSR2: |
| 551 | perform_release(); |
| 552 | break; |
| 553 | case SIGTERM: |
| 554 | LOG(LOG_INFO, "Received SIGTERM"); |
| 555 | exit_client(0); |
| 556 | } |
| 557 | } else if (retval == -1 && errno == EINTR) { |
| 558 | /* a signal was caught */ |
| 559 | } else { |
| 560 | /* An error occured */ |
| 561 | DEBUG(LOG_ERR, "Error on select"); |
| 562 | } |
| 563 | |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | |