chiark / gitweb /
libsystemd-dhcp: expose received DNS server
authorTom Gundersen <teg@jklm.no>
Sun, 5 Jan 2014 22:01:32 +0000 (23:01 +0100)
committerTom Gundersen <teg@jklm.no>
Sun, 5 Jan 2014 22:01:38 +0000 (23:01 +0100)
src/libsystemd-dhcp/dhcp-client.c
src/systemd/sd-dhcp-client.h

index f6a621102219c395460de96177b05fc9bee4e3ad..dbec869816532fc93ebef5e1f199bf0284ae7610 100644 (file)
@@ -41,6 +41,7 @@ struct DHCPLease {
         be32_t server_address;
         be32_t subnet_mask;
         be32_t router;
+        be32_t dns;
 };
 
 typedef struct DHCPLease DHCPLease;
@@ -209,6 +210,33 @@ int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr)
         return 0;
 }
 
+int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr *addr)
+{
+        assert_return(client, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        switch (client->state) {
+        case DHCP_STATE_INIT:
+        case DHCP_STATE_SELECTING:
+        case DHCP_STATE_INIT_REBOOT:
+        case DHCP_STATE_REBOOTING:
+        case DHCP_STATE_REQUESTING:
+                return -EADDRNOTAVAIL;
+
+        case DHCP_STATE_BOUND:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
+                if (client->lease->dns)
+                        addr->s_addr = client->lease->dns;
+                else
+                        return -ENOENT;
+
+                break;
+        }
+
+        return 0;
+}
+
 int sd_dhcp_client_prefixlen(const struct in_addr *addr)
 {
         int len = 0;
@@ -727,6 +755,12 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
 
                 break;
 
+        case DHCP_OPTION_DOMAIN_NAME_SERVER:
+                if (len >= 4)
+                        memcpy(&lease->dns, option, 4);
+
+                break;
+
         case DHCP_OPTION_RENEWAL_T1_TIME:
                 if (len == 4) {
                         memcpy(&val, option, 4);
index fdc5b2dcba303aa6ddf8448c24182eb1e41b397c..68fcba5495b9152fa09ca7b6926f6c2f5496002e 100644 (file)
@@ -54,6 +54,7 @@ int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_prefixlen(const struct in_addr *addr);
 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr);
+int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr *addr);
 
 int sd_dhcp_client_stop(sd_dhcp_client *client);
 int sd_dhcp_client_start(sd_dhcp_client *client);