udhcpc: rename server_addr to server_id, fix id comparison if it's not specified

Even though it is _meant to be_ an IP address, in the wild servers sometimes
give bogus server ids, like 1.1.1.1

function                                             old     new   delta
udhcpc_main                                         2551    2542      -9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2021-06-15 01:15:44 +02:00
parent 827b690fa7
commit 9e27fed6b9

View File

@ -1113,7 +1113,7 @@ static void change_listen_mode(int new_mode)
/* else LISTEN_NONE: client_data.sockfd stays closed */ /* else LISTEN_NONE: client_data.sockfd stays closed */
} }
static void perform_release(uint32_t server_addr, uint32_t requested_ip) static void perform_release(uint32_t server_id, uint32_t requested_ip)
{ {
char buffer[sizeof("255.255.255.255")]; char buffer[sizeof("255.255.255.255")];
struct in_addr temp_addr; struct in_addr temp_addr;
@ -1126,13 +1126,13 @@ static void perform_release(uint32_t server_addr, uint32_t requested_ip)
|| client_data.state == REBINDING || client_data.state == REBINDING
|| client_data.state == RENEW_REQUESTED || client_data.state == RENEW_REQUESTED
) { ) {
temp_addr.s_addr = server_addr; temp_addr.s_addr = server_id;
strcpy(buffer, inet_ntoa(temp_addr)); strcpy(buffer, inet_ntoa(temp_addr));
temp_addr.s_addr = requested_ip; temp_addr.s_addr = requested_ip;
bb_info_msg("unicasting a release of %s to %s", bb_info_msg("unicasting a release of %s to %s",
inet_ntoa(temp_addr), buffer); inet_ntoa(temp_addr), buffer);
client_data.xid = random_xid(); //TODO: can omit? client_data.xid = random_xid(); //TODO: can omit?
send_release(server_addr, requested_ip); /* unicast */ send_release(server_id, requested_ip); /* unicast */
} }
bb_simple_info_msg("entering released state"); bb_simple_info_msg("entering released state");
/* /*
@ -1218,7 +1218,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
int tryagain_timeout = 20; int tryagain_timeout = 20;
int discover_timeout = 3; int discover_timeout = 3;
int discover_retries = 3; int discover_retries = 3;
uint32_t server_addr = server_addr; /* for compiler */ uint32_t server_id = server_id; /* for compiler */
uint32_t requested_ip = 0; uint32_t requested_ip = 0;
int packet_num; int packet_num;
int timeout; /* must be signed */ int timeout; /* must be signed */
@ -1481,7 +1481,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
case REQUESTING: case REQUESTING:
if (packet_num < 3) { if (packet_num < 3) {
/* send broadcast select packet */ /* send broadcast select packet */
send_select(server_addr, requested_ip); send_select(server_id, requested_ip);
timeout = discover_timeout; timeout = discover_timeout;
packet_num++; packet_num++;
continue; continue;
@ -1512,7 +1512,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
* Anyway, it does recover by eventually failing through * Anyway, it does recover by eventually failing through
* into INIT_SELECTING state. * into INIT_SELECTING state.
*/ */
if (send_renew(server_addr, requested_ip) >= 0) { if (send_renew(server_id, requested_ip) >= 0) {
timeout = discover_timeout; timeout = discover_timeout;
packet_num++; packet_num++;
continue; continue;
@ -1603,7 +1603,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
timeout = 0; timeout = 0;
continue; continue;
case SIGUSR2: case SIGUSR2:
perform_release(server_addr, requested_ip); perform_release(server_id, requested_ip);
/* ^^^ switches to LISTEN_NONE */ /* ^^^ switches to LISTEN_NONE */
timeout = INT_MAX; timeout = INT_MAX;
continue; continue;
@ -1688,13 +1688,13 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
* They either supply DHCP_SERVER_ID of 0.0.0.0 or don't supply it at all. * They either supply DHCP_SERVER_ID of 0.0.0.0 or don't supply it at all.
* They say ISC DHCP client supports this case. * They say ISC DHCP client supports this case.
*/ */
server_addr = 0; server_id = 0;
temp = udhcp_get_option32(&packet, DHCP_SERVER_ID); temp = udhcp_get_option32(&packet, DHCP_SERVER_ID);
if (!temp) { if (!temp) {
bb_simple_info_msg("no server ID, using 0.0.0.0"); bb_simple_info_msg("no server ID, using 0.0.0.0");
} else { } else {
/* it IS unaligned sometimes, don't "optimize" */ /* it IS unaligned sometimes, don't "optimize" */
move_from_unaligned32(server_addr, temp); move_from_unaligned32(server_id, temp);
} }
/*xid = packet.xid; - already is */ /*xid = packet.xid; - already is */
temp_addr.s_addr = requested_ip = packet.yiaddr; temp_addr.s_addr = requested_ip = packet.yiaddr;
@ -1718,7 +1718,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
change_listen_mode(LISTEN_NONE); change_listen_mode(LISTEN_NONE);
temp_addr.s_addr = server_addr; temp_addr.s_addr = server_id;
strcpy(server_str, inet_ntoa(temp_addr)); strcpy(server_str, inet_ntoa(temp_addr));
temp_addr.s_addr = packet.yiaddr; temp_addr.s_addr = packet.yiaddr;
@ -1766,7 +1766,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
bb_simple_info_msg("offered address is in use " bb_simple_info_msg("offered address is in use "
"(got ARP reply), declining"); "(got ARP reply), declining");
client_data.xid = random_xid(); //TODO: can omit? client_data.xid = random_xid(); //TODO: can omit?
send_decline(server_addr, packet.yiaddr); send_decline(server_id, packet.yiaddr);
if (client_data.state != REQUESTING) if (client_data.state != REQUESTING)
d4_run_script_deconfig(); d4_run_script_deconfig();
@ -1810,20 +1810,14 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
* "wrong" server can reply first, with a NAK. * "wrong" server can reply first, with a NAK.
* Do not interpret it as a NAK from "our" server. * Do not interpret it as a NAK from "our" server.
*/ */
if (server_addr != 0) { uint32_t svid = 0; /* we treat no server id as 0.0.0.0 */
uint32_t svid; uint8_t *temp = udhcp_get_option32(&packet, DHCP_SERVER_ID);
uint8_t *temp; if (temp)
temp = udhcp_get_option32(&packet, DHCP_SERVER_ID);
if (!temp) {
non_matching_svid:
log1("received DHCP NAK with wrong"
" server ID%s", ", ignoring packet");
continue;
}
move_from_unaligned32(svid, temp); move_from_unaligned32(svid, temp);
if (svid != server_addr) if (svid != server_id) {
goto non_matching_svid; log1("received DHCP NAK with wrong"
" server ID%s", ", ignoring packet");
continue;
} }
/* return to init state */ /* return to init state */
change_listen_mode(LISTEN_NONE); change_listen_mode(LISTEN_NONE);
@ -1847,7 +1841,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
ret0: ret0:
if (opt & OPT_R) /* release on quit */ if (opt & OPT_R) /* release on quit */
perform_release(server_addr, requested_ip); perform_release(server_id, requested_ip);
retval = 0; retval = 0;
ret: ret:
/*if (client_data.pidfile) - remove_pidfile has its own check */ /*if (client_data.pidfile) - remove_pidfile has its own check */