For SNAT, don't store the pre-fragment L2 header before actions are applied.
[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 <config.h>
35 #include "dhcp-client.h"
36 #include <arpa/inet.h>
37 #include <getopt.h>
38 #include <stdlib.h>
39 #include <limits.h>
40 #include "command-line.h"
41 #include "dhcp.h"
42 #include "fatal-signal.h"
43 #include "fault.h"
44 #include "poll-loop.h"
45 #include "util.h"
46 #include "vlog.h"
47
48 /* --request-ip: IP address to request from server.  If zero, then do not
49  * request a specific IP address. */
50 static struct in_addr request_ip;
51
52 /* --vendor-class: Vendor class string to include in request.  If null, no
53  * vendor class string is included. */
54 static const char *vendor_class;
55
56 /* --no-resolv-conf: Update /etc/resolv.conf to match DHCP reply? */
57 static bool update_resolv_conf = true;
58
59 static void parse_options(int argc, char *argv[]);
60 static void usage(void);
61 static void release(void *cli_);
62 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
63
64 int
65 main(int argc, char *argv[])
66 {
67     struct dhclient *cli;
68     int error;
69
70     set_program_name(argv[0]);
71     register_fault_handlers();
72     vlog_init();
73     parse_options(argc, argv);
74
75     argc -= optind;
76     argv += optind;
77     if (argc != 1) {
78         ofp_fatal(0, "exactly one non-option argument required; "
79                   "use --help for help");
80     }
81
82     error = dhclient_create(argv[0], modify_dhcp_request, NULL, NULL, &cli);
83     if (error) {
84         ofp_fatal(error, "dhclient_create failed");
85     }
86     dhclient_init(cli, request_ip.s_addr);
87     fatal_signal_add_hook(release, cli, true);
88
89     for (;;) {
90         fatal_signal_block();
91         dhclient_run(cli);
92         if (dhclient_changed(cli)) {
93             dhclient_configure_netdev(cli);
94             if (update_resolv_conf) {
95                 dhclient_update_resolv_conf(cli);
96             }
97         }
98         dhclient_wait(cli);
99         fatal_signal_unblock();
100         poll_block();
101     }
102 }
103
104 static void
105 release(void *cli_)
106 {
107     struct dhclient *cli = cli_;
108     dhclient_release(cli);
109     if (dhclient_changed(cli)) {
110         dhclient_configure_netdev(cli);
111     }
112 }
113
114 static void
115 modify_dhcp_request(struct dhcp_msg *msg, void *aux UNUSED)
116 {
117     if (vendor_class) {
118         dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, vendor_class);
119     }
120 }
121
122 static void
123 parse_options(int argc, char *argv[])
124 {
125     enum {
126         OPT_REQUEST_IP = UCHAR_MAX + 1,
127         OPT_VENDOR_CLASS,
128         OPT_NO_RESOLV_CONF
129     };
130     static struct option long_options[] = {
131         {"request-ip",  required_argument, 0, OPT_REQUEST_IP },
132         {"vendor-class", required_argument, 0, OPT_VENDOR_CLASS },
133         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
134         {"verbose",     optional_argument, 0, 'v'},
135         {"help",        no_argument, 0, 'h'},
136         {"version",     no_argument, 0, 'V'},
137         {0, 0, 0, 0},
138     };
139     char *short_options = long_options_to_short_options(long_options);
140
141     for (;;) {
142         int c;
143
144         c = getopt_long(argc, argv, short_options, long_options, NULL);
145         if (c == -1) {
146             break;
147         }
148
149         switch (c) {
150         case OPT_REQUEST_IP:
151             if (!inet_aton(optarg, &request_ip)) {
152                 ofp_fatal(0,
153                           "--request-ip argument is not a valid IP address");
154             }
155             break;
156
157         case OPT_VENDOR_CLASS:
158             vendor_class = optarg;
159             break;
160
161         case OPT_NO_RESOLV_CONF:
162             update_resolv_conf = false;
163             break;
164
165         case 'h':
166             usage();
167
168         case 'V':
169             printf("%s %s compiled "__DATE__" "__TIME__"\n",
170                    program_name, VERSION BUILDNR);
171             exit(EXIT_SUCCESS);
172
173         case 'v':
174             vlog_set_verbosity(optarg);
175             break;
176
177         case '?':
178             exit(EXIT_FAILURE);
179
180         default:
181             abort();
182         }
183     }
184     free(short_options);
185 }
186
187 static void
188 usage(void)
189 {
190     printf("%s: standalone program for testing OpenFlow DHCP client.\n"
191            "usage: %s [OPTIONS] NETDEV\n"
192            "where NETDEV is a network device (e.g. eth0).\n"
193            "\nDHCP options:\n"
194            "  --request-ip=IP         request specified IP address (default:\n"
195            "                          do not request a specific IP)\n"
196            "  --vendor-class=STRING   use STRING as vendor class (default:\n"
197            "                          none); use OpenFlow to imitate secchan\n"
198            "  --no-resolv-conf        do not update /etc/resolv.conf\n",
199            program_name, program_name);
200     vlog_usage();
201     printf("\nOther options:\n"
202            "  -h, --help              display this help message\n"
203            "  -V, --version           display version information\n");
204     exit(EXIT_SUCCESS);
205 }
206