Lines Matching defs:lease
27 #include "sd-dhcp-lease.h"
30 #include "dhcp-lease-internal.h"
44 int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) {
45 assert_return(lease, -EINVAL);
48 if (lease->address == 0)
51 addr->s_addr = lease->address;
55 int sd_dhcp_lease_get_broadcast(sd_dhcp_lease *lease, struct in_addr *addr) {
56 assert_return(lease, -EINVAL);
59 if (!lease->have_broadcast)
62 addr->s_addr = lease->broadcast;
66 int sd_dhcp_lease_get_lifetime(sd_dhcp_lease *lease, uint32_t *lifetime) {
67 assert_return(lease, -EINVAL);
70 if (lease->lifetime <= 0)
73 *lifetime = lease->lifetime;
77 int sd_dhcp_lease_get_t1(sd_dhcp_lease *lease, uint32_t *t1) {
78 assert_return(lease, -EINVAL);
81 if (lease->t1 <= 0)
84 *t1 = lease->t1;
88 int sd_dhcp_lease_get_t2(sd_dhcp_lease *lease, uint32_t *t2) {
89 assert_return(lease, -EINVAL);
92 if (lease->t2 <= 0)
95 *t2 = lease->t2;
99 int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) {
100 assert_return(lease, -EINVAL);
103 if (lease->mtu <= 0)
106 *mtu = lease->mtu;
110 int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr) {
111 assert_return(lease, -EINVAL);
114 if (lease->dns_size <= 0)
117 *addr = lease->dns;
118 return (int) lease->dns_size;
121 int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr) {
122 assert_return(lease, -EINVAL);
125 if (lease->ntp_size <= 0)
128 *addr = lease->ntp;
129 return (int) lease->ntp_size;
132 int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) {
133 assert_return(lease, -EINVAL);
136 if (!lease->domainname)
139 *domainname = lease->domainname;
143 int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname) {
144 assert_return(lease, -EINVAL);
147 if (!lease->hostname)
150 *hostname = lease->hostname;
154 int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path) {
155 assert_return(lease, -EINVAL);
158 if (!lease->root_path)
161 *root_path = lease->root_path;
165 int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr) {
166 assert_return(lease, -EINVAL);
169 if (lease->router == 0)
172 addr->s_addr = lease->router;
176 int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr) {
177 assert_return(lease, -EINVAL);
180 if (!lease->have_subnet_mask)
183 addr->s_addr = lease->subnet_mask;
187 int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr) {
188 assert_return(lease, -EINVAL);
191 if (lease->server_address == 0)
194 addr->s_addr = lease->server_address;
198 int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr) {
199 assert_return(lease, -EINVAL);
202 if (lease->next_server == 0)
205 addr->s_addr = lease->next_server;
211 * Route objects have the same lifetime of the lease and must not be freed.
213 int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, sd_dhcp_route ***routes) {
217 assert_return(lease, -EINVAL);
220 if (lease->static_route_size <= 0)
223 ret = new(sd_dhcp_route *, lease->static_route_size);
227 for (i = 0; i < lease->static_route_size; i++)
228 ret[i] = &lease->static_route[i];
231 return (int) lease->static_route_size;
234 int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const void **data, size_t *data_len) {
235 assert_return(lease, -EINVAL);
239 if (lease->vendor_specific_len <= 0)
242 *data = lease->vendor_specific;
243 *data_len = lease->vendor_specific_len;
247 sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
249 if (!lease)
252 assert(lease->n_ref >= 1);
253 lease->n_ref++;
255 return lease;
258 sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
260 if (!lease)
263 assert(lease->n_ref >= 1);
264 lease->n_ref--;
266 if (lease->n_ref > 0)
269 while (lease->private_options) {
270 struct sd_dhcp_raw_option *option = lease->private_options;
272 LIST_REMOVE(options, lease->private_options, option);
278 free(lease->hostname);
279 free(lease->domainname);
280 free(lease->dns);
281 free(lease->ntp);
282 free(lease->static_route);
283 free(lease->client_id);
284 free(lease->vendor_specific);
285 free(lease);
513 sd_dhcp_lease *lease = userdata;
516 assert(lease);
521 r = lease_parse_u32(option, len, &lease->lifetime, 1);
523 log_debug_errno(r, "Failed to parse lease time, ignoring: %m");
528 r = lease_parse_be32(option, len, &lease->server_address);
535 r = lease_parse_be32(option, len, &lease->subnet_mask);
539 lease->have_subnet_mask = true;
543 r = lease_parse_be32(option, len, &lease->broadcast);
547 lease->have_broadcast = true;
552 r = lease_parse_be32(option, 4, &lease->router);
559 r = lease_parse_in_addrs(option, len, &lease->dns, &lease->dns_size);
565 r = lease_parse_in_addrs(option, len, &lease->ntp, &lease->ntp_size);
571 r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size, &lease->static_route_allocated);
577 r = lease_parse_u16(option, len, &lease->mtu, 68);
583 r = lease_parse_domain(option, len, &lease->domainname);
592 r = lease_parse_domain(option, len, &lease->hostname);
601 r = lease_parse_string(option, len, &lease->root_path);
607 r = lease_parse_u32(option, len, &lease->t1, 1);
613 r = lease_parse_u32(option, len, &lease->t2, 1);
621 &lease->static_route,
622 &lease->static_route_size,
623 &lease->static_route_allocated);
642 free(lease->timezone);
643 lease->timezone = tz;
652 lease->vendor_specific = mfree(lease->vendor_specific);
660 free(lease->vendor_specific);
661 lease->vendor_specific = p;
664 lease->vendor_specific_len = len;
668 r = dhcp_lease_insert_private_option(lease, code, option, len);
682 int dhcp_lease_insert_private_option(sd_dhcp_lease *lease, uint8_t tag, const void *data, uint8_t len) {
685 assert(lease);
687 LIST_FOREACH(options, cur, lease->private_options) {
708 LIST_INSERT_BEFORE(options, lease->private_options, cur, option);
713 sd_dhcp_lease *lease;
715 lease = new0(sd_dhcp_lease, 1);
716 if (!lease)
719 lease->router = INADDR_ANY;
720 lease->n_ref = 1;
722 *ret = lease;
726 int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
740 assert(lease);
752 r = sd_dhcp_lease_get_address(lease, &address);
756 r = sd_dhcp_lease_get_netmask(lease, &address);
760 r = sd_dhcp_lease_get_router(lease, &address);
764 r = sd_dhcp_lease_get_server_identifier(lease, &address);
768 r = sd_dhcp_lease_get_next_server(lease, &address);
772 r = sd_dhcp_lease_get_broadcast(lease, &address);
776 r = sd_dhcp_lease_get_mtu(lease, &mtu);
780 r = sd_dhcp_lease_get_t1(lease, &t1);
784 r = sd_dhcp_lease_get_t2(lease, &t2);
788 r = sd_dhcp_lease_get_lifetime(lease, &lifetime);
792 r = sd_dhcp_lease_get_dns(lease, &addresses);
799 r = sd_dhcp_lease_get_ntp(lease, &addresses);
806 r = sd_dhcp_lease_get_domainname(lease, &string);
810 r = sd_dhcp_lease_get_hostname(lease, &string);
814 r = sd_dhcp_lease_get_root_path(lease, &string);
818 r = sd_dhcp_lease_get_routes(lease, &routes);
822 r = sd_dhcp_lease_get_timezone(lease, &string);
826 r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len);
838 r = sd_dhcp_lease_get_vendor_specific(lease, &data, &data_len);
850 LIST_FOREACH(options, option, lease->private_options) {
874 return log_error_errno(r, "Failed to save lease data %s: %m", lease_file);
879 _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL;
903 r = dhcp_lease_new(&lease);
917 "DOMAINNAME", &lease->domainname,
918 "HOSTNAME", &lease->hostname,
919 "ROOT_PATH", &lease->root_path,
922 "TIMEZONE", &lease->timezone,
963 r = inet_pton(AF_INET, address, &lease->address);
969 r = inet_pton(AF_INET, router, &lease->router);
975 r = inet_pton(AF_INET, netmask, &lease->subnet_mask);
979 lease->have_subnet_mask = true;
983 r = inet_pton(AF_INET, server_address, &lease->server_address);
989 r = inet_pton(AF_INET, next_server, &lease->next_server);
995 r = inet_pton(AF_INET, broadcast, &lease->broadcast);
999 lease->have_broadcast = true;
1003 r = deserialize_in_addrs(&lease->dns, dns);
1007 lease->dns_size = r;
1011 r = deserialize_in_addrs(&lease->ntp, ntp);
1015 lease->ntp_size = r;
1019 r = safe_atou16(mtu, &lease->mtu);
1026 &lease->static_route,
1027 &lease->static_route_size,
1028 &lease->static_route_allocated,
1035 r = safe_atou32(lifetime, &lease->lifetime);
1041 r = safe_atou32(t1, &lease->t1);
1047 r = safe_atou32(t2, &lease->t2);
1053 r = deserialize_dhcp_option(&lease->client_id, &lease->client_id_len, client_id_hex);
1059 r = deserialize_dhcp_option(&lease->vendor_specific, &lease->vendor_specific_len, vendor_specific_hex);
1077 r = dhcp_lease_insert_private_option(lease, SD_DHCP_OPTION_PRIVATE_BASE + i, data, len);
1082 *ret = lease;
1083 lease = NULL;
1088 int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease) {
1092 assert(lease);
1094 if (lease->address == 0)
1097 address.s_addr = lease->address;
1104 lease->subnet_mask = mask.s_addr;
1105 lease->have_subnet_mask = true;
1110 int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const void **client_id, size_t *client_id_len) {
1111 assert_return(lease, -EINVAL);
1115 if (!lease->client_id)
1118 *client_id = lease->client_id;
1119 *client_id_len = lease->client_id_len;
1124 int dhcp_lease_set_client_id(sd_dhcp_lease *lease, const void *client_id, size_t client_id_len) {
1125 assert_return(lease, -EINVAL);
1129 lease->client_id = mfree(lease->client_id);
1137 free(lease->client_id);
1138 lease->client_id = p;
1139 lease->client_id_len = client_id_len;
1145 int sd_dhcp_lease_get_timezone(sd_dhcp_lease *lease, const char **tz) {
1146 assert_return(lease, -EINVAL);
1149 if (!lease->timezone)
1152 *tz = lease->timezone;