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