chiark / gitweb /
dhcp: Add function to stop the DHCP client
[elogind.git] / src / libsystemd-dhcp / dhcp-client.c
index 4d1722f5b36f56d1cc01c435630b5fee537cc26f..bd2882476e6955a8b2da8cc6de73106be2362a46 100644 (file)
@@ -119,9 +119,36 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client,
         return 0;
 }
 
+static int client_stop(sd_dhcp_client *client, int error)
+{
+        assert_return(client, -EINVAL);
+        assert_return(client->state != DHCP_STATE_INIT &&
+                      client->state != DHCP_STATE_INIT_REBOOT, -EALREADY);
+
+        switch (client->state) {
+
+        case DHCP_STATE_INIT:
+        case DHCP_STATE_SELECTING:
+
+                client->state = DHCP_STATE_INIT;
+                break;
+
+        case DHCP_STATE_INIT_REBOOT:
+        case DHCP_STATE_REBOOTING:
+        case DHCP_STATE_REQUESTING:
+        case DHCP_STATE_BOUND:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
+
+                break;
+        }
+
+        return 0;
+}
+
 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
-                              DHCPMessage *message, uint8_t **opt,
-                              size_t *optlen)
+                              DHCPMessage *message, uint16_t secs,
+                              uint8_t **opt, size_t *optlen)
 {
         int err;
 
@@ -136,6 +163,10 @@ static int client_packet_init(sd_dhcp_client *client, uint8_t type,
         message->hlen = ETHER_ADDR_LEN;
         message->xid = htobe32(client->xid);
 
+        /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
+           refuse to issue an DHCP lease if 'secs' is set to zero */
+        message->secs = htobe16(secs);
+
         memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
         (*opt)[0] = 0x63;
         (*opt)[1] = 0x82;
@@ -189,7 +220,7 @@ static uint16_t client_checksum(void *buf, int len)
         return ~((sum & 0xffff) + (sum >> 16));
 }
 
-static int client_send_discover(sd_dhcp_client *client)
+static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
 {
         int err = 0;
         _cleanup_free_ DHCPPacket *discover;
@@ -205,7 +236,7 @@ static int client_send_discover(sd_dhcp_client *client)
                 return -ENOMEM;
 
         err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
-                                 &opt, &optlen);
+                                 secs, &opt, &optlen);
         if (err < 0)
                 return err;
 
@@ -256,7 +287,12 @@ int sd_dhcp_client_start(sd_dhcp_client *client)
 
         client->xid = random_u();
 
-        return client_send_discover(client);
+        return client_send_discover(client, 0);
+}
+
+int sd_dhcp_client_stop(sd_dhcp_client *client)
+{
+        return client_stop(client, 0);
 }
 
 sd_dhcp_client *sd_dhcp_client_new(void)