📄 dhcpc.c
字号:
sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface); else sockfd = udhcp_raw_socket(client_config.ifindex); } max_fd = udhcp_sp_fd_set(&rfds, sockfd); tv.tv_sec = timeout - already_waited_sec; tv.tv_usec = 0; retval = 0; /* If we already timed out, fall through, else... */ if (tv.tv_sec > 0) { timestamp_before_wait = (unsigned)monotonic_sec(); DEBUG("Waiting on select..."); retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); if (retval < 0) { /* EINTR? A signal was caught, don't panic */ if (errno == EINTR) continue; /* Else: an error occured, panic! */ bb_perror_msg_and_die("select"); } } /* If timeout dropped to zero, time to become active: * resend discover/renew/whatever */ if (retval == 0) { /* We will restart the wait in any case */ already_waited_sec = 0; switch (state) { case INIT_SELECTING: if (packet_num < discover_retries) { if (packet_num == 0) xid = random_xid(); send_discover(xid, requested_ip); /* broadcast */ timeout = discover_timeout; packet_num++; continue; } leasefail: udhcp_run_script(NULL, "leasefail");#if BB_MMU /* -b is not supported on NOMMU */ if (opt & OPT_b) { /* background if no lease */ bb_info_msg("No lease, forking to background"); client_background(); /* do not background again! */ opt = ((opt & ~OPT_b) | OPT_f); } else#endif if (opt & OPT_n) { /* abort if no lease */ bb_info_msg("No lease, failing"); retval = 1; goto ret; } /* wait before trying again */ timeout = tryagain_timeout; packet_num = 0; continue; case RENEW_REQUESTED: case REQUESTING: if (packet_num < discover_retries) { /* send request packet */ if (state == RENEW_REQUESTED) /* unicast */ send_renew(xid, server_addr, requested_ip); else /* broadcast */ send_select(xid, server_addr, requested_ip); timeout = discover_timeout; packet_num++; continue; } /* timed out, go back to init state */ if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig"); change_listen_mode(LISTEN_RAW); /* "discover...select...discover..." loops * were seen in the wild. Treat them similarly * to "no response to discover" case */ if (state == REQUESTING) { state = INIT_SELECTING; goto leasefail; } state = INIT_SELECTING; timeout = 0; packet_num = 0; continue; case BOUND: /* Half of the lease passed, time to enter renewing state */ change_listen_mode(LISTEN_KERNEL); DEBUG("Entering renew state"); state = RENEWING; /* fall right through */ case RENEWING: if (timeout > 60) { /* send a request packet */ send_renew(xid, server_addr, requested_ip); /* unicast */ timeout >>= 1; continue; } /* Timed out, enter rebinding state */ DEBUG("Entering rebinding state"); state = REBINDING; /* fall right through */ case REBINDING: /* Lease is *really* about to run out, * try to find DHCP server using broadcast */ if (timeout > 0) { /* send a request packet */ send_renew(xid, 0 /* INADDR_ANY*/, requested_ip); /* broadcast */ timeout >>= 1; continue; } /* Timed out, enter init state */ bb_info_msg("Lease lost, entering init state"); udhcp_run_script(NULL, "deconfig"); change_listen_mode(LISTEN_RAW); state = INIT_SELECTING; /*timeout = 0; - already is */ packet_num = 0; continue; /* case RELEASED: */ } /* yah, I know, *you* say it would never happen */ timeout = INT_MAX; continue; /* back to main loop */ } /* select() didn't timeout, something did happen. */ /* Is it a packet? */ if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) { int len; /* A packet is ready, read it */ if (listen_mode == LISTEN_KERNEL) len = udhcp_recv_kernel_packet(&packet, sockfd); else len = udhcp_recv_raw_packet(&packet, sockfd); if (len == -1) { /* error is severe, reopen socket */ DEBUG("error on read, %s, reopening socket", strerror(errno)); sleep(discover_timeout); /* 3 seconds by default */ change_listen_mode(listen_mode); /* just close and reopen */ } /* If this packet will turn out to be unrelated/bogus, * we will go back and wait for next one. * Be sure timeout is properly decreased. */ already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait; if (len < 0) continue; if (packet.xid != xid) { DEBUG("Ignoring xid %x (our xid is %x)", (unsigned)packet.xid, (unsigned)xid); continue; } /* Ignore packets that aren't for us */ if (memcmp(packet.chaddr, client_config.arp, 6)) { DEBUG("Packet does not have our chaddr - ignoring"); continue; } message = get_option(&packet, DHCP_MESSAGE_TYPE); if (message == NULL) { bb_error_msg("cannot get message type from packet - ignoring"); continue; } switch (state) { case INIT_SELECTING: /* Must be a DHCPOFFER to one of our xid's */ if (*message == DHCPOFFER) { /* TODO: why we don't just fetch server's IP from IP header? */ temp = get_option(&packet, DHCP_SERVER_ID); if (!temp) { bb_error_msg("no server ID in message"); continue; /* still selecting - this server looks bad */ } /* it IS unaligned sometimes, don't "optimize" */ server_addr = get_unaligned_u32p((uint32_t*)temp); xid = packet.xid; requested_ip = packet.yiaddr; /* enter requesting state */ state = REQUESTING; timeout = 0; packet_num = 0; already_waited_sec = 0; } continue; case RENEW_REQUESTED: case REQUESTING: case RENEWING: case REBINDING: if (*message == DHCPACK) { temp = get_option(&packet, DHCP_LEASE_TIME); if (!temp) { bb_error_msg("no lease time with ACK, using 1 hour lease"); lease_seconds = 60 * 60; } else { /* it IS unaligned sometimes, don't "optimize" */ lease_seconds = get_unaligned_u32p((uint32_t*)temp); lease_seconds = ntohl(lease_seconds); lease_seconds &= 0x0fffffff; /* paranoia: must not be prone to overflows */ if (lease_seconds < 10) /* and not too small */ lease_seconds = 10; }#if ENABLE_FEATURE_UDHCPC_ARPING if (opt & OPT_a) {/* RFC 2131 3.1 paragraph 5: * "The client receives the DHCPACK message with configuration * parameters. The client SHOULD perform a final check on the * parameters (e.g., ARP for allocated network address), and notes * the duration of the lease specified in the DHCPACK message. At this * point, the client is configured. If the client detects that the * address is already in use (e.g., through the use of ARP), * the client MUST send a DHCPDECLINE message to the server and restarts * the configuration process..." */ if (!arpping(packet.yiaddr, (uint32_t) 0, client_config.arp, client_config.interface) ) { bb_info_msg("offered address is in use " "(got ARP reply), declining"); send_decline(xid, server_addr, packet.yiaddr); if (state != REQUESTING) udhcp_run_script(NULL, "deconfig"); change_listen_mode(LISTEN_RAW); state = INIT_SELECTING; requested_ip = 0; timeout = tryagain_timeout; packet_num = 0; already_waited_sec = 0; continue; /* back to main loop */ } }#endif /* enter bound state */ timeout = lease_seconds / 2; { struct in_addr temp_addr; temp_addr.s_addr = packet.yiaddr; bb_info_msg("Lease of %s obtained, lease time %u", inet_ntoa(temp_addr), (unsigned)lease_seconds); } requested_ip = packet.yiaddr; udhcp_run_script(&packet, ((state == RENEWING || state == REBINDING) ? "renew" : "bound")); state = BOUND; change_listen_mode(LISTEN_NONE); if (opt & OPT_q) { /* quit after lease */ if (opt & OPT_R) /* release on quit */ perform_release(requested_ip, server_addr); goto ret0; }#if BB_MMU /* NOMMU case backgrounded earlier */ if (!(opt & OPT_f)) { client_background(); /* do not background again! */ opt = ((opt & ~OPT_b) | OPT_f); }#endif already_waited_sec = 0; continue; /* back to main loop */ } if (*message == DHCPNAK) { /* return to init state */ bb_info_msg("Received DHCP NAK"); udhcp_run_script(&packet, "nak"); if (state != REQUESTING) udhcp_run_script(NULL, "deconfig"); change_listen_mode(LISTEN_RAW); sleep(3); /* avoid excessive network traffic */ state = INIT_SELECTING; requested_ip = 0; timeout = 0; packet_num = 0; already_waited_sec = 0; } continue; /* case BOUND, RELEASED: - ignore all packets */ } continue; /* back to main loop */ } /* select() didn't timeout, something did happen. * But it wasn't a packet. It's a signal pipe then. */ { int signo = udhcp_sp_read(&rfds); switch (signo) { case SIGUSR1: perform_renew(); /* start things over */ packet_num = 0; /* Kill any timeouts because the user wants this to hurry along */ timeout = 0; break; case SIGUSR2: perform_release(requested_ip, server_addr); timeout = INT_MAX; break; case SIGTERM: bb_info_msg("Received SIGTERM"); if (opt & OPT_R) /* release on quit */ perform_release(requested_ip, server_addr); goto ret0; } } } /* for (;;) - main loop ends */ ret0: retval = 0; ret: /*if (client_config.pidfile) - remove_pidfile has its own check */ remove_pidfile(client_config.pidfile); return retval;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -