2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "dhcp-client.h"
19 #include <arpa/inet.h>
23 #include "command-line.h"
25 #include "fatal-signal.h"
27 #include "poll-loop.h"
31 /* --request-ip: IP address to request from server. If zero, then do not
32 * request a specific IP address. */
33 static struct in_addr request_ip;
35 /* --vendor-class: Vendor class string to include in request. If null, no
36 * vendor class string is included. */
37 static const char *vendor_class;
39 /* --no-resolv-conf: Update /etc/resolv.conf to match DHCP reply? */
40 static bool update_resolv_conf = true;
42 static void parse_options(int argc, char *argv[]);
43 static void usage(void);
44 static void release(void *cli_);
45 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
48 main(int argc, char *argv[])
53 set_program_name(argv[0]);
54 register_fault_handlers();
56 parse_options(argc, argv);
61 ovs_fatal(0, "exactly one non-option argument required; "
62 "use --help for help");
65 error = dhclient_create(argv[0], modify_dhcp_request, NULL, NULL, &cli);
67 ovs_fatal(error, "dhclient_create failed");
69 dhclient_init(cli, request_ip.s_addr);
70 fatal_signal_add_hook(release, cli, true);
74 if (dhclient_changed(cli)) {
75 dhclient_configure_netdev(cli);
76 if (update_resolv_conf) {
77 dhclient_update_resolv_conf(cli);
88 struct dhclient *cli = cli_;
89 dhclient_release(cli);
90 if (dhclient_changed(cli)) {
91 dhclient_configure_netdev(cli);
96 modify_dhcp_request(struct dhcp_msg *msg, void *aux UNUSED)
99 dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, vendor_class);
104 parse_options(int argc, char *argv[])
107 OPT_REQUEST_IP = UCHAR_MAX + 1,
111 static struct option long_options[] = {
112 {"request-ip", required_argument, 0, OPT_REQUEST_IP },
113 {"vendor-class", required_argument, 0, OPT_VENDOR_CLASS },
114 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
115 {"verbose", optional_argument, 0, 'v'},
116 {"help", no_argument, 0, 'h'},
117 {"version", no_argument, 0, 'V'},
120 char *short_options = long_options_to_short_options(long_options);
125 c = getopt_long(argc, argv, short_options, long_options, NULL);
132 if (!inet_aton(optarg, &request_ip)) {
134 "--request-ip argument is not a valid IP address");
138 case OPT_VENDOR_CLASS:
139 vendor_class = optarg;
142 case OPT_NO_RESOLV_CONF:
143 update_resolv_conf = false;
150 printf("%s %s compiled "__DATE__" "__TIME__"\n",
151 program_name, VERSION BUILDNR);
155 vlog_set_verbosity(optarg);
171 printf("%s: standalone program for testing Open vSwitch DHCP client.\n"
172 "usage: %s [OPTIONS] NETDEV\n"
173 "where NETDEV is a network device (e.g. eth0).\n"
175 " --request-ip=IP request specified IP address (default:\n"
176 " do not request a specific IP)\n"
177 " --vendor-class=STRING use STRING as vendor class; use\n"
178 " OpenFlow to imitate ovs-openflowd\n"
179 " --no-resolv-conf do not update /etc/resolv.conf\n",
180 program_name, program_name);
182 printf("\nOther options:\n"
183 " -h, --help display this help message\n"
184 " -V, --version display version information\n");