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