summaryrefslogtreecommitdiff
path: root/tests/test-dhcp-client.c
blob: 3b35dac69b46a5c2541a274adaa1834b46ce0ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>
#include "dhcp-client.h"
#include <arpa/inet.h>
#include <getopt.h>
#include <stdlib.h>
#include <limits.h>
#include "command-line.h"
#include "dhcp.h"
#include "fatal-signal.h"
#include "poll-loop.h"
#include "util.h"
#include "vlog.h"

/* --request-ip: IP address to request from server.  If zero, then do not
 * request a specific IP address. */
static struct in_addr request_ip;

/* --vendor-class: Vendor class string to include in request.  If null, no
 * vendor class string is included. */
static const char *vendor_class;

/* --no-resolv-conf: Update /etc/resolv.conf to match DHCP reply? */
static bool update_resolv_conf = true;

static void parse_options(int argc, char *argv[]);
static void usage(void);
static void release(void *cli_);
static void modify_dhcp_request(struct dhcp_msg *, void *aux);

int
main(int argc, char *argv[])
{
    struct dhclient *cli;
    int error;

    set_program_name(argv[0]);
    vlog_init();
    parse_options(argc, argv);

    argc -= optind;
    argv += optind;
    if (argc != 1) {
        ovs_fatal(0, "exactly one non-option argument required; "
                  "use --help for help");
    }

    error = dhclient_create(argv[0], modify_dhcp_request, NULL, NULL, &cli);
    if (error) {
        ovs_fatal(error, "dhclient_create failed");
    }
    dhclient_init(cli, request_ip.s_addr);
    fatal_signal_add_hook(release, NULL, cli, true);

    for (;;) {
        dhclient_run(cli);
        if (dhclient_changed(cli)) {
            dhclient_configure_netdev(cli);
            if (update_resolv_conf) {
                dhclient_update_resolv_conf(cli);
            }
        }
        dhclient_wait(cli);
        poll_block();
    }
}

static void
release(void *cli_)
{
    struct dhclient *cli = cli_;
    dhclient_release(cli);
    if (dhclient_changed(cli)) {
        dhclient_configure_netdev(cli);
    }
}

static void
modify_dhcp_request(struct dhcp_msg *msg, void *aux UNUSED)
{
    if (vendor_class) {
        dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, vendor_class);
    }
}

static void
parse_options(int argc, char *argv[])
{
    enum {
        OPT_REQUEST_IP = UCHAR_MAX + 1,
        OPT_VENDOR_CLASS,
        OPT_NO_RESOLV_CONF
    };
    static struct option long_options[] = {
        {"request-ip",  required_argument, 0, OPT_REQUEST_IP },
        {"vendor-class", required_argument, 0, OPT_VENDOR_CLASS },
        {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
        {"verbose",     optional_argument, 0, 'v'},
        {"help",        no_argument, 0, 'h'},
        {"version",     no_argument, 0, 'V'},
        {0, 0, 0, 0},
    };
    char *short_options = long_options_to_short_options(long_options);

    for (;;) {
        int c;

        c = getopt_long(argc, argv, short_options, long_options, NULL);
        if (c == -1) {
            break;
        }

        switch (c) {
        case OPT_REQUEST_IP:
            if (!inet_aton(optarg, &request_ip)) {
                ovs_fatal(0,
                          "--request-ip argument is not a valid IP address");
            }
            break;

        case OPT_VENDOR_CLASS:
            vendor_class = optarg;
            break;

        case OPT_NO_RESOLV_CONF:
            update_resolv_conf = false;
            break;

        case 'h':
            usage();

        case 'V':
            printf("%s %s compiled "__DATE__" "__TIME__"\n",
                   program_name, VERSION BUILDNR);
            exit(EXIT_SUCCESS);

        case 'v':
            vlog_set_verbosity(optarg);
            break;

        case '?':
            exit(EXIT_FAILURE);

        default:
            abort();
        }
    }
    free(short_options);
}

static void
usage(void)
{
    printf("%s: standalone program for testing Open vSwitch DHCP client.\n"
           "usage: %s [OPTIONS] NETDEV\n"
           "where NETDEV is a network device (e.g. eth0).\n"
           "\nDHCP options:\n"
           "  --request-ip=IP         request specified IP address (default:\n"
           "                          do not request a specific IP)\n"
           "  --vendor-class=STRING   use STRING as vendor class; use\n"
           "                          OpenFlow to imitate ovs-openflowd\n"
           "  --no-resolv-conf        do not update /etc/resolv.conf\n",
           program_name, program_name);
    vlog_usage();
    printf("\nOther options:\n"
           "  -h, --help              display this help message\n"
           "  -V, --version           display version information\n");
    exit(EXIT_SUCCESS);
}