summaryrefslogtreecommitdiff
path: root/tests/test-dhcp-client.c
blob: 2fee3fc1119363d875b150bd341432b329522d35 (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
186
187
188
189
/*
 * Copyright (c) 2008, 2009 Nicira Networks.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#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 "fault.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]);
    register_fault_handlers();
    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, cli, true);

    for (;;) {
        fatal_signal_block();
        dhclient_run(cli);
        if (dhclient_changed(cli)) {
            dhclient_configure_netdev(cli);
            if (update_resolv_conf) {
                dhclient_update_resolv_conf(cli);
            }
        }
        dhclient_wait(cli);
        fatal_signal_unblock();
        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 (default:\n"
           "                          none); use OpenFlow to imitate secchan\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);
}