Merge "master" into "next".
[cascardo/ovs.git] / tests / test-dhcp-client.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "dhcp-client.h"
19 #include <arpa/inet.h>
20 #include <getopt.h>
21 #include <stdlib.h>
22 #include <limits.h>
23 #include "command-line.h"
24 #include "dhcp.h"
25 #include "fatal-signal.h"
26 #include "poll-loop.h"
27 #include "util.h"
28 #include "vlog.h"
29
30 /* --request-ip: IP address to request from server.  If zero, then do not
31  * request a specific IP address. */
32 static struct in_addr request_ip;
33
34 /* --vendor-class: Vendor class string to include in request.  If null, no
35  * vendor class string is included. */
36 static const char *vendor_class;
37
38 /* --no-resolv-conf: Update /etc/resolv.conf to match DHCP reply? */
39 static bool update_resolv_conf = true;
40
41 static void parse_options(int argc, char *argv[]);
42 static void usage(void);
43 static void release(void *cli_);
44 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
45
46 int
47 main(int argc, char *argv[])
48 {
49     struct dhclient *cli;
50     int error;
51
52     set_program_name(argv[0]);
53     vlog_init();
54     parse_options(argc, argv);
55
56     argc -= optind;
57     argv += optind;
58     if (argc != 1) {
59         ovs_fatal(0, "exactly one non-option argument required; "
60                   "use --help for help");
61     }
62
63     error = dhclient_create(argv[0], modify_dhcp_request, NULL, NULL, &cli);
64     if (error) {
65         ovs_fatal(error, "dhclient_create failed");
66     }
67     dhclient_init(cli, request_ip.s_addr);
68     fatal_signal_add_hook(release, NULL, cli, true);
69
70     for (;;) {
71         dhclient_run(cli);
72         if (dhclient_changed(cli)) {
73             dhclient_configure_netdev(cli);
74             if (update_resolv_conf) {
75                 dhclient_update_resolv_conf(cli);
76             }
77         }
78         dhclient_wait(cli);
79         poll_block();
80     }
81 }
82
83 static void
84 release(void *cli_)
85 {
86     struct dhclient *cli = cli_;
87     dhclient_release(cli);
88     if (dhclient_changed(cli)) {
89         dhclient_configure_netdev(cli);
90     }
91 }
92
93 static void
94 modify_dhcp_request(struct dhcp_msg *msg, void *aux OVS_UNUSED)
95 {
96     if (vendor_class) {
97         dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, vendor_class);
98     }
99 }
100
101 static void
102 parse_options(int argc, char *argv[])
103 {
104     enum {
105         OPT_REQUEST_IP = UCHAR_MAX + 1,
106         OPT_VENDOR_CLASS,
107         OPT_NO_RESOLV_CONF
108     };
109     static struct option long_options[] = {
110         {"request-ip",  required_argument, 0, OPT_REQUEST_IP },
111         {"vendor-class", required_argument, 0, OPT_VENDOR_CLASS },
112         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
113         {"verbose",     optional_argument, 0, 'v'},
114         {"help",        no_argument, 0, 'h'},
115         {"version",     no_argument, 0, 'V'},
116         {0, 0, 0, 0},
117     };
118     char *short_options = long_options_to_short_options(long_options);
119
120     for (;;) {
121         int c;
122
123         c = getopt_long(argc, argv, short_options, long_options, NULL);
124         if (c == -1) {
125             break;
126         }
127
128         switch (c) {
129         case OPT_REQUEST_IP:
130             if (!inet_aton(optarg, &request_ip)) {
131                 ovs_fatal(0,
132                           "--request-ip argument is not a valid IP address");
133             }
134             break;
135
136         case OPT_VENDOR_CLASS:
137             vendor_class = optarg;
138             break;
139
140         case OPT_NO_RESOLV_CONF:
141             update_resolv_conf = false;
142             break;
143
144         case 'h':
145             usage();
146
147         case 'V':
148             printf("%s %s compiled "__DATE__" "__TIME__"\n",
149                    program_name, VERSION BUILDNR);
150             exit(EXIT_SUCCESS);
151
152         case 'v':
153             vlog_set_verbosity(optarg);
154             break;
155
156         case '?':
157             exit(EXIT_FAILURE);
158
159         default:
160             abort();
161         }
162     }
163     free(short_options);
164 }
165
166 static void
167 usage(void)
168 {
169     printf("%s: standalone program for testing Open vSwitch DHCP client.\n"
170            "usage: %s [OPTIONS] NETDEV\n"
171            "where NETDEV is a network device (e.g. eth0).\n"
172            "\nDHCP options:\n"
173            "  --request-ip=IP         request specified IP address (default:\n"
174            "                          do not request a specific IP)\n"
175            "  --vendor-class=STRING   use STRING as vendor class; use\n"
176            "                          OpenFlow to imitate ovs-openflowd\n"
177            "  --no-resolv-conf        do not update /etc/resolv.conf\n",
178            program_name, program_name);
179     vlog_usage();
180     printf("\nOther options:\n"
181            "  -h, --help              display this help message\n"
182            "  -V, --version           display version information\n");
183     exit(EXIT_SUCCESS);
184 }
185