2 This file is part of systemd.
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
5 Copyright (C) 2014 Tom Gundersen
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <net/ethernet.h>
26 #include <sys/param.h>
31 #include "dhcp-protocol.h"
32 #include "dhcp-internal.h"
33 #include "dhcp-lease.h"
34 #include "sd-dhcp-client.h"
36 int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) {
37 assert_return(lease, -EINVAL);
38 assert_return(addr, -EINVAL);
40 addr->s_addr = lease->address;
45 int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) {
46 assert_return(lease, -EINVAL);
47 assert_return(mtu, -EINVAL);
57 int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, struct in_addr **addr, size_t *addr_size) {
58 assert_return(lease, -EINVAL);
59 assert_return(addr, -EINVAL);
60 assert_return(addr_size, -EINVAL);
62 if (lease->dns_size) {
63 *addr_size = lease->dns_size;
71 int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) {
72 assert_return(lease, -EINVAL);
73 assert_return(domainname, -EINVAL);
75 if (lease->domainname)
76 *domainname = lease->domainname;
83 int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname) {
84 assert_return(lease, -EINVAL);
85 assert_return(hostname, -EINVAL);
88 *hostname = lease->hostname;
95 int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr) {
96 assert_return(lease, -EINVAL);
97 assert_return(addr, -EINVAL);
99 addr->s_addr = lease->router;
104 int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr) {
105 assert_return(lease, -EINVAL);
106 assert_return(addr, -EINVAL);
108 addr->s_addr = lease->subnet_mask;
113 sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
115 assert_se(REFCNT_INC(lease->n_ref) >= 2);
120 sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
121 if (lease && REFCNT_DEC(lease->n_ref) <= 0) {
122 free(lease->hostname);
123 free(lease->domainname);
131 int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
133 sd_dhcp_lease *lease = user_data;
138 case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
140 memcpy(&val, option, 4);
141 lease->lifetime = be32toh(val);
146 case DHCP_OPTION_SERVER_IDENTIFIER:
148 memcpy(&lease->server_address, option, 4);
152 case DHCP_OPTION_SUBNET_MASK:
154 memcpy(&lease->subnet_mask, option, 4);
158 case DHCP_OPTION_ROUTER:
160 memcpy(&lease->router, option, 4);
164 case DHCP_OPTION_DOMAIN_NAME_SERVER:
165 if (len && !(len % 4)) {
168 lease->dns_size = len / 4;
171 lease->dns = new0(struct in_addr, lease->dns_size);
175 for (i = 0; i < lease->dns_size; i++) {
176 memcpy(&lease->dns[i].s_addr, option + 4 * i, 4);
182 case DHCP_OPTION_INTERFACE_MTU:
186 memcpy(&mtu, option, 2);
187 lease->mtu = be16toh(mtu);
195 case DHCP_OPTION_DOMAIN_NAME:
197 free(lease->domainname);
198 lease->domainname = strndup((const char *)option, len);
203 case DHCP_OPTION_HOST_NAME:
205 free(lease->hostname);
206 lease->hostname = strndup((const char *)option, len);
211 case DHCP_OPTION_RENEWAL_T1_TIME:
213 memcpy(&val, option, 4);
214 lease->t1 = be32toh(val);
219 case DHCP_OPTION_REBINDING_T2_TIME:
221 memcpy(&val, option, 4);
222 lease->t2 = be32toh(val);
231 int dhcp_lease_new(sd_dhcp_lease **ret) {
232 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
234 lease = new0(sd_dhcp_lease, 1);
238 lease->n_ref = REFCNT_INIT;