Make facility and level optional in -v, --verbose options.
[sliver-openvswitch.git] / tests / test-dhcp-client.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include "dhcp-client.h"
35 #include <arpa/inet.h>
36 #include <getopt.h>
37 #include <stdlib.h>
38 #include <limits.h>
39 #include "command-line.h"
40 #include "dhcp.h"
41 #include "fatal-signal.h"
42 #include "fault.h"
43 #include "poll-loop.h"
44 #include "util.h"
45 #include "vlog.h"
46
47 /* --request-ip: IP address to request from server.  If zero, then do not
48  * request a specific IP address. */
49 static struct in_addr request_ip;
50
51 /* --vendor-class: Vendor class string to include in request.  If null, no
52  * vendor class string is included. */
53 static const char *vendor_class;
54
55 static void parse_options(int argc, char *argv[]);
56 static void usage(void);
57 static void release(void *cli_);
58 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
59
60 int
61 main(int argc, char *argv[])
62 {
63     struct dhclient *cli;
64     int error;
65
66     set_program_name(argv[0]);
67     register_fault_handlers();
68     vlog_init();
69     parse_options(argc, argv);
70
71     argc -= optind;
72     argv += optind;
73     if (argc != 1) {
74         fatal(0, "exactly one non-option argument required; "
75               "use --help for help");
76     }
77
78     error = dhclient_create(argv[0], modify_dhcp_request, NULL, NULL, &cli);
79     if (error) {
80         fatal(error, "dhclient_create failed");
81     }
82     dhclient_init(cli, request_ip.s_addr);
83     fatal_signal_add_hook(release, cli);
84
85     for (;;) {
86         fatal_signal_block();
87         dhclient_run(cli);
88         fatal_signal_unblock();
89         dhclient_wait(cli);
90         poll_block();
91     }
92 }
93
94 static void
95 release(void *cli_)
96 {
97     struct dhclient *cli = cli_;
98     dhclient_release(cli);
99 }
100
101 static void
102 modify_dhcp_request(struct dhcp_msg *msg, void *aux UNUSED)
103 {
104     if (vendor_class) {
105         dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, vendor_class);
106     }
107 }
108
109 static void
110 parse_options(int argc, char *argv[])
111 {
112     enum {
113         OPT_REQUEST_IP = UCHAR_MAX + 1,
114         OPT_VENDOR_CLASS
115     };
116     static struct option long_options[] = {
117         {"request-ip",  required_argument, 0, OPT_REQUEST_IP },
118         {"vendor-class", required_argument, 0, OPT_VENDOR_CLASS },
119         {"verbose",     optional_argument, 0, 'v'},
120         {"help",        no_argument, 0, 'h'},
121         {"version",     no_argument, 0, 'V'},
122         {0, 0, 0, 0},
123     };
124     char *short_options = long_options_to_short_options(long_options);
125
126     for (;;) {
127         int c;
128
129         c = getopt_long(argc, argv, short_options, long_options, NULL);
130         if (c == -1) {
131             break;
132         }
133
134         switch (c) {
135         case OPT_REQUEST_IP:
136             if (!inet_aton(optarg, &request_ip)) {
137                 fatal(0, "--request-ip argument is not a valid IP address");
138             }
139             break;
140
141         case OPT_VENDOR_CLASS:
142             vendor_class = optarg;
143             break;
144
145         case 'h':
146             usage();
147
148         case 'V':
149             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
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 OpenFlow 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 (default:\n"
176            "                          none); use OpenFlow to imitate secchan\n"
177            "\nOther options:\n"
178            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
179            "  -v, --verbose           set maximum verbosity level\n"
180            "  -h, --help              display this help message\n"
181            "  -V, --version           display version information\n",
182            program_name, program_name);
183     exit(EXIT_SUCCESS);
184 }
185