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