1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-10 01:17:44 +03:00

sd-dhcp-lease: add a meta-getter to simplify querying of server lists

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-04-02 15:42:31 +02:00
parent 562ffaca26
commit 8541db8f59
2 changed files with 65 additions and 44 deletions

View File

@ -96,59 +96,70 @@ int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) {
return 0;
}
int sd_dhcp_lease_get_servers(
sd_dhcp_lease *lease,
sd_dhcp_lease_info what,
const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
switch (what) {
case SD_DHCP_LEASE_DNS_SERVERS:
if (lease->dns_size <= 0)
return -ENODATA;
*addr = lease->dns;
return (int) lease->dns_size;
case SD_DHCP_LEASE_NTP_SERVERS:
if (lease->ntp_size <= 0)
return -ENODATA;
*addr = lease->ntp;
return (int) lease->ntp_size;
case SD_DHCP_LEASE_SIP_SERVERS:
if (lease->sip_size <= 0)
return -ENODATA;
*addr = lease->sip;
return (int) lease->sip_size;
case SD_DHCP_LEASE_POP3_SERVERS:
if (lease->pop3_server_size <= 0)
return -ENODATA;
*addr = lease->pop3_server;
return (int) lease->pop3_server_size;
case SD_DHCP_LEASE_SMTP_SERVERS:
if (lease->smtp_server_size <= 0)
return -ENODATA;
*addr = lease->smtp_server;
return (int) lease->smtp_server_size;
default:
log_debug("Uknown DHCP lease info item %d.", what);
return -ENXIO;
}
}
int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
if (lease->dns_size <= 0)
return -ENODATA;
*addr = lease->dns;
return (int) lease->dns_size;
return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_DNS_SERVERS, addr);
}
int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
if (lease->ntp_size <= 0)
return -ENODATA;
*addr = lease->ntp;
return (int) lease->ntp_size;
return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_NTP_SERVERS, addr);
}
int sd_dhcp_lease_get_sip(sd_dhcp_lease *lease, const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
if (lease->sip_size <= 0)
return -ENODATA;
*addr = lease->sip;
return (int) lease->sip_size;
return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_SIP_SERVERS, addr);
}
int sd_dhcp_lease_get_pop3_server(sd_dhcp_lease *lease, const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
if (lease->pop3_server_size <= 0)
return -ENODATA;
*addr = lease->pop3_server;
return (int) lease->pop3_server_size;
return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_POP3_SERVERS, addr);
}
int sd_dhcp_lease_get_smtp_server(sd_dhcp_lease *lease, const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
if (lease->smtp_server_size <= 0)
return -ENODATA;
*addr = lease->smtp_server;
return (int) lease->smtp_server_size;
return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_SMTP_SERVERS, addr);
}
int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) {

View File

@ -33,6 +33,15 @@ typedef struct sd_dhcp_route sd_dhcp_route;
sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease);
sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease);
typedef enum sd_dhcp_lease_info {
SD_DHCP_LEASE_DNS_SERVERS = 0,
SD_DHCP_LEASE_NTP_SERVERS,
SD_DHCP_LEASE_SIP_SERVERS,
SD_DHCP_LEASE_POP3_SERVERS,
SD_DHCP_LEASE_SMTP_SERVERS,
_SD_DHCP_LEASE_INFO_MAX,
} sd_dhcp_lease_info;
int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr);
int sd_dhcp_lease_get_lifetime(sd_dhcp_lease *lease, uint32_t *lifetime);
int sd_dhcp_lease_get_t1(sd_dhcp_lease *lease, uint32_t *t1);
@ -42,6 +51,7 @@ int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr);
int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr);
int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr);
int sd_dhcp_lease_get_servers(sd_dhcp_lease *lease, sd_dhcp_lease_info what, const struct in_addr **addr);
int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_sip(sd_dhcp_lease *lease, const struct in_addr **addr);