From b1e1ccf7c33d971c2a05a2d607c76e155cb384b0 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Wed, 19 Mar 2014 01:13:11 -0400 Subject: [PATCH] Add support for setting the metric for the default GW route. --- ndhc/ifset.c | 3 ++- ndhc/ndhc.c | 23 ++++++++++++++++++++++- ndhc/ndhc.h | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ndhc/ifset.c b/ndhc/ifset.c index 2cef4be..11221ca 100644 --- a/ndhc/ifset.c +++ b/ndhc/ifset.c @@ -595,7 +595,8 @@ void perform_router(const char *str_router, size_t len) return; } - if (rtnl_set_default_gw_v4(fd, router.s_addr, 0) < 0) + if (rtnl_set_default_gw_v4(fd, router.s_addr, + client_config.metric) < 0) log_line("%s: (%s) failed to set route: %s", client_config.interface, __func__, strerror(errno)); else diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index 9278fdb..39c0a88 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -48,6 +48,7 @@ #include #include #include +#include #include "ndhc.h" #include "ndhc-defines.h" @@ -89,6 +90,7 @@ struct client_config_t client_config = { .interface = "eth0", .arp = "\0\0\0\0\0\0", .clientid_len = 0, + .metric = 0, }; static void show_usage(void) @@ -124,6 +126,7 @@ static void show_usage(void) " -W, --arp-probe-num Number of ARP probes before lease is ok\n" " -m, --arp-probe-min Min ms to wait for ARP response\n" " -M, --arp-probe-max Max ms to wait for ARP response\n" +" -t, --gw-metric Route metric for default gw (default: 0)\n" " -R, --resolve-conf=FILE Path to resolv.conf or equivalent\n" " -H, --dhcp-hostname Allow DHCP to set machine hostname\n" " -v, --version Display version\n" @@ -422,6 +425,7 @@ int main(int argc, char **argv) {"arp-probe-num", required_argument, 0, 'W'}, {"arp-probe-min", required_argument, 0, 'm'}, {"arp-probe-max", required_argument, 0, 'M'}, + {"gw-metric", required_argument, 0, 't'}, {"resolv-conf", required_argument, 0, 'R'}, {"dhcp-set-hostname", no_argument, 0, 'H'}, {"version", no_argument, 0, 'v'}, @@ -431,7 +435,7 @@ int main(int argc, char **argv) while (1) { int c; - c = getopt_long(argc, argv, "c:fbp:P:l:h:i:nqr:V:u:U:C:s:Sdw:W:m:M:R:Hv?", + c = getopt_long(argc, argv, "c:fbp:P:l:h:i:nqr:V:u:U:C:s:Sdw:W:m:M:t:R:Hv?", arg_options, NULL); if (c == -1) break; @@ -572,6 +576,23 @@ int main(int argc, char **argv) strnkcpy(client_config.vendor, optarg, sizeof client_config.vendor); break; + case 't': { + char *p; + long mt = strtol(optarg, &p, 10); + if (p == optarg) { + log_error("gw-metric arg '%s' isn't a valid number", + optarg); + exit(EXIT_FAILURE); + } + if (mt > INT_MAX) { + log_error("gw-metric arg '%s' is too large", optarg); + exit(EXIT_FAILURE); + } + if (mt < 0) + mt = 0; + client_config.metric = (int)mt; + break; + } case 'R': strnkcpy(resolv_conf_d, optarg, sizeof resolv_conf_d); break; diff --git a/ndhc/ndhc.h b/ndhc/ndhc.h index 0fcf3a1..bd271de 100644 --- a/ndhc/ndhc.h +++ b/ndhc/ndhc.h @@ -60,6 +60,7 @@ struct client_config_t { uint8_t clientid_len; // Length of the clientid char hostname[64]; // Optional hostname to use char vendor[64]; // Vendor identification that will be sent + int metric; // Metric for the default route int ifindex; // Index number of the interface to use uint8_t arp[6]; // Our arp address };