#include <sys/types.h>
#include <time.h>
#include <unistd.h>
-#include "buffer.h"
#include "csum.h"
#include "dhcp.h"
#include "dynamic-string.h"
#include "flow.h"
#include "netdev.h"
-#include "ofp-print.h"
+#include "ofpbuf.h"
#include "poll-loop.h"
#include "sat-math.h"
#include "timeval.h"
bool changed;
unsigned int retransmit, delay; /* Used by send_reliably(). */
+ unsigned int max_timeout;
unsigned int init_delay; /* Used by S_INIT. */
cli->ipaddr = 0;
cli->server_ip = 0;
cli->retransmit = cli->delay = 0;
+ cli->max_timeout = 64;
cli->min_timeout = 1;
ds_init(&cli->s);
cli->changed = true;
return 0;
}
+/* Sets the maximum amount of timeout that 'cli' will wait for a reply from
+ * the DHCP server before retransmitting, in seconds, to 'max_timeout'. The
+ * default is 64 seconds. */
+void
+dhclient_set_max_timeout(struct dhclient *cli, unsigned int max_timeout)
+{
+ cli->max_timeout = MAX(2, max_timeout);
+}
+
+/* Destroys 'cli' and frees all related resources. */
+void
+dhclient_destroy(struct dhclient *cli)
+{
+ if (cli) {
+ dhcp_msg_uninit(cli->binding);
+ free(cli->binding);
+ netdev_close(cli->netdev);
+ ds_destroy(&cli->s);
+ free(cli);
+ }
+}
+
+/* Returns the network device in use by 'cli'. The caller must not destroy
+ * the returned device. */
+struct netdev *
+dhclient_get_netdev(struct dhclient *cli)
+{
+ return cli->netdev;
+}
+
/* Forces 'cli' into a (re)initialization state, in which no address is bound
* but the client is advertising to obtain one. If 'requested_ip' is nonzero,
* then the client will attempt to re-bind to that IP address; otherwise, it
dhclient_wait(struct dhclient *cli)
{
if (cli->min_timeout != UINT_MAX) {
- poll_timer_wait(sat_mul(cli->min_timeout, 1000));
+ time_t now = time_now();
+ unsigned int wake = sat_add(cli->state_entered, cli->min_timeout);
+ if (wake <= now) {
+ poll_immediate_wake();
+ } else {
+ poll_timer_wait(sat_mul(sat_sub(wake, now), 1000));
+ }
}
/* Reset timeout to 1 second. This will have no effect ordinarily, because
* dhclient_run() will typically set it back to a higher value. If,
cli->modify_request(&msg, cli->aux);
}
do_send_msg(cli, &msg);
- cli->delay = MIN(64, MAX(4, cli->delay * 2));
+ cli->delay = MIN(cli->max_timeout, MAX(4, cli->delay * 2));
cli->retransmit += fuzz(cli->delay, 1);
timeout(cli, cli->retransmit);
dhcp_msg_uninit(&msg);
static bool
do_receive_msg(struct dhclient *cli, struct dhcp_msg *msg)
{
- struct buffer b;
+ struct ofpbuf b;
- buffer_init(&b, netdev_get_mtu(cli->netdev) + VLAN_ETH_HEADER_LEN);
+ ofpbuf_init(&b, netdev_get_mtu(cli->netdev) + VLAN_ETH_HEADER_LEN);
for (; cli->received < 50; cli->received++) {
const struct ip_header *ip;
const struct dhcp_header *dhcp;
struct flow flow;
int error;
- buffer_clear(&b);
+ ofpbuf_clear(&b);
error = netdev_recv(cli->netdev, &b);
if (error) {
goto drained;
continue;
}
- buffer_pull(&b, b.l7 - b.data);
+ ofpbuf_pull(&b, (char *)b.l7 - (char*)b.data);
error = dhcp_parse(msg, &b);
if (!error) {
- VLOG_DBG_RL(&rl, "received %s",
- dhcp_msg_to_string(msg, false, &cli->s));
- buffer_uninit(&b);
+ if (VLOG_IS_DBG_ENABLED()) {
+ VLOG_DBG_RL(&rl, "received %s",
+ dhcp_msg_to_string(msg, false, &cli->s));
+ } else {
+ VLOG_WARN_RL(&rl, "received %s", dhcp_type_name(msg->type));
+ }
+ ofpbuf_uninit(&b);
return true;
}
}
netdev_drain(cli->netdev);
drained:
- buffer_uninit(&b);
+ ofpbuf_uninit(&b);
return false;
}
static void
do_send_msg(struct dhclient *cli, const struct dhcp_msg *msg)
{
- struct buffer b;
+ struct ofpbuf b;
struct eth_header eh;
struct ip_header nh;
struct udp_header th;
uint32_t udp_csum;
int error;
- buffer_init(&b, ETH_TOTAL_MAX);
- buffer_reserve(&b, ETH_HEADER_LEN + IP_HEADER_LEN + UDP_HEADER_LEN);
+ ofpbuf_init(&b, ETH_TOTAL_MAX);
+ ofpbuf_reserve(&b, ETH_HEADER_LEN + IP_HEADER_LEN + UDP_HEADER_LEN);
dhcp_assemble(msg, &b);
udp_csum = csum_continue(udp_csum, &th, sizeof th);
th.udp_csum = csum_finish(csum_continue(udp_csum, b.data, b.size));
- buffer_push(&b, &th, sizeof th);
- buffer_push(&b, &nh, sizeof nh);
- buffer_push(&b, &eh, sizeof eh);
+ ofpbuf_push(&b, &th, sizeof th);
+ ofpbuf_push(&b, &nh, sizeof nh);
+ ofpbuf_push(&b, &eh, sizeof eh);
/* Don't try to send the frame if it's too long for an Ethernet frame. We
* disregard the network device's actual MTU because we don't want the
* frame to have to be discarded or fragmented if it travels over a regular
* Ethernet at some point. 1500 bytes should be enough for anyone. */
if (b.size <= ETH_TOTAL_MAX) {
- VLOG_DBG("sending %s", dhcp_msg_to_string(msg, false, &cli->s));
+ if (VLOG_IS_DBG_ENABLED()) {
+ VLOG_DBG("sending %s", dhcp_msg_to_string(msg, false, &cli->s));
+ } else {
+ VLOG_WARN("sending %s", dhcp_type_name(msg->type));
+ }
error = netdev_send(cli->netdev, &b);
if (error) {
VLOG_ERR("send failed on %s: %s",
VLOG_ERR("cannot send %zu-byte Ethernet frame", b.size);
}
- buffer_uninit(&b);
+ ofpbuf_uninit(&b);
}
static unsigned int