netdev: Fix file descriptor leak.
[sliver-openvswitch.git] / lib / dhcp-client.c
index 5e54017..7704c8a 100644 (file)
@@ -103,6 +103,7 @@ struct dhclient {
     bool changed;
 
     unsigned int retransmit, delay; /* Used by send_reliably(). */
+    unsigned int max_timeout;
 
     unsigned int init_delay;    /* Used by S_INIT. */
 
@@ -199,6 +200,7 @@ dhclient_create(const char *netdev_name,
     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;
@@ -206,6 +208,15 @@ dhclient_create(const char *netdev_name,
     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)
@@ -345,7 +356,12 @@ dhclient_get_state_elapsed(const struct dhclient *cli)
 unsigned int
 dhclient_get_lease_remaining(const struct dhclient *cli)
 {
-    return dhclient_is_bound(cli) ? cli->lease_expiration - time_now() : 0;
+    if (dhclient_is_bound(cli)) {
+        time_t now = time_now();
+        return cli->lease_expiration > now ? cli->lease_expiration - now : 0;
+    } else {
+        return 0;
+    }
 }
 
 /* If 'cli' is bound to an IP address, returns that IP address; otherwise,
@@ -867,7 +883,7 @@ send_reliably(struct dhclient *cli,
             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);
@@ -943,8 +959,12 @@ do_receive_msg(struct dhclient *cli, struct dhcp_msg *msg)
         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));
+            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;
         }
@@ -1019,7 +1039,11 @@ do_send_msg(struct dhclient *cli, const struct dhcp_msg *msg)
      * 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",