From 0 Mon Sep 17 00:00:00 2001 From: Mikhail Efremov Date: Wed, 30 Aug 2017 17:36:11 +0300 Subject: [PATCH 26] dhclient: Add --onetime and --nounicast options Thanks gremlin@. diff --git a/dhcp/client/dhclient.8 b/dhcp/client/dhclient.8 index defaced..defaced 100644 --- a/dhcp/client/dhclient.8 +++ b/dhcp/client/dhclient.8 @@ -94,6 +94,12 @@ dhclient - Dynamic Host Configuration Protocol Client .B -1 ] [ +.B --onetime +] +[ +.B --nounicast +] +[ .B -r | .B -x @@ -297,6 +303,9 @@ sets the maximum duration of the initial exchange to .I timeout (from dhclient.conf with a default of sixty seconds). .TP +.BI \--onetime,\ \-onetime +Similar to above, but on successful configuration exit with code 0. +.TP .BI \-d .\" This is not intuitive. Force @@ -398,6 +407,10 @@ combination with the .B -w flag. .TP +.BI \--nounicast,\ \-nounicast +Do not send unicast requests for refreshing the configuration. +Makes sense when connected to the switch acting as a DHCP Relay. +.TP .BI \-e \ VAR=value Define additional environment variables for the environment where .B dhclient-script diff --git a/dhcp/client/dhclient.c b/dhcp/client/dhclient.c index defaced..defaced 100644 --- a/dhcp/client/dhclient.c +++ b/dhcp/client/dhclient.c @@ -100,6 +100,8 @@ int dfd[2] = { -1, -1 }; struct string_list *client_env = NULL; int client_env_count = 0; int onetry = 0; +int onetime = 0; /* get address and exit; implies onetry */ +int do_unicast = 1; int quiet = 1; int nowait = 0; int stateless = 0; @@ -195,6 +197,7 @@ static const char use_v6command[] = "Command not used for DHCPv4: %s"; #endif #define DHCLIENT_USAGEC \ +" [--nounicast] [--onetime]\n" \ " [-s server-addr] [-cf config-file]\n" \ " [-df duid-file] [-lf lease-file]\n" \ " [-pf pid-file] [--no-pid] [-e VAR=val]\n" \ @@ -447,6 +450,10 @@ main(int argc, char **argv) { no_dhclient_script = 1; } else if (!strcmp(argv[i], "-1")) { onetry = 1; + } else if (!strcmp(argv[i], "--onetime") || !strcmp(argv[i], "-onetime")) { + onetime = onetry = 1; + } else if (!strcmp(argv[i], "--nounicast") || !strcmp(argv[i], "-nounicast")) { + do_unicast = 0; } else if (!strcmp(argv[i], "-q")) { quiet = 1; } else if (!strcmp(argv[i], "-s")) { @@ -1982,6 +1989,12 @@ void bind_lease (client) (long)(client->active->renewal - cur_time)); client->state = S_BOUND; reinitialize_interfaces(); + + if (onetime) + { + exit(0); + } + detach(); #if defined (NSUPDATE) if (client->config->do_forward_update) @@ -2016,7 +2029,7 @@ void state_bound (cpp) client, (struct option_state *)0, client -> active -> options, &global_scope, oc, MDL)) { - if (ds.len > 3) { + if (ds.len > 3 && do_unicast) { memcpy (client -> destination.iabuf, ds.data, 4); client -> destination.len = 4; } else @@ -3198,6 +3211,7 @@ void send_request (cpp) broadcast the DHCPREQUEST rather than unicasting. */ if (client -> state == S_REQUESTING || client -> state == S_REBOOTING || + !do_unicast || cur_time > client -> active -> rebind) destination.sin_addr = sockaddr_broadcast.sin_addr; else