2 This file is part of systemd.
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <net/ethernet.h>
25 #include <sys/param.h>
30 #include "dhcp-protocol.h"
31 #include "dhcp-internal.h"
32 #include "sd-dhcp-client.h"
34 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
41 be32_t server_address;
50 typedef struct DHCPLease DHCPLease;
52 struct sd_dhcp_client {
55 sd_event_source *timeout_resend;
58 union sockaddr_union link;
59 sd_event_source *receive_message;
61 size_t req_opts_allocated;
64 struct ether_addr mac_addr;
69 sd_event_source *timeout_t1;
70 sd_event_source *timeout_t2;
71 sd_event_source *timeout_expire;
72 sd_dhcp_client_cb_t cb;
77 static const uint8_t default_req_opts[] = {
78 DHCP_OPTION_SUBNET_MASK,
80 DHCP_OPTION_HOST_NAME,
81 DHCP_OPTION_DOMAIN_NAME,
82 DHCP_OPTION_DOMAIN_NAME_SERVER,
83 DHCP_OPTION_NTP_SERVER,
86 static int client_receive_message(sd_event_source *s, int fd,
87 uint32_t revents, void *userdata);
89 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
92 assert_return(client, -EINVAL);
95 client->userdata = userdata;
100 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
104 assert_return(client, -EINVAL);
105 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
108 case DHCP_OPTION_PAD:
109 case DHCP_OPTION_OVERLOAD:
110 case DHCP_OPTION_MESSAGE_TYPE:
111 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
112 case DHCP_OPTION_END:
119 for (i = 0; i < client->req_opts_size; i++)
120 if (client->req_opts[i] == option)
123 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
124 client->req_opts_size + 1))
127 client->req_opts[client->req_opts_size++] = option;
132 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
133 const struct in_addr *last_addr)
135 assert_return(client, -EINVAL);
136 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
139 client->last_addr = last_addr->s_addr;
141 client->last_addr = INADDR_ANY;
146 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index)
148 assert_return(client, -EINVAL);
149 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
150 assert_return(interface_index >= -1, -EINVAL);
152 client->index = interface_index;
157 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
158 const struct ether_addr *addr)
160 assert_return(client, -EINVAL);
161 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
163 memcpy(&client->mac_addr, addr, ETH_ALEN);
168 int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr)
170 assert_return(client, -EINVAL);
171 assert_return(addr, -EINVAL);
173 switch (client->state) {
174 case DHCP_STATE_INIT:
175 case DHCP_STATE_SELECTING:
176 case DHCP_STATE_INIT_REBOOT:
177 case DHCP_STATE_REBOOTING:
178 case DHCP_STATE_REQUESTING:
179 return -EADDRNOTAVAIL;
181 case DHCP_STATE_BOUND:
182 case DHCP_STATE_RENEWING:
183 case DHCP_STATE_REBINDING:
184 addr->s_addr = client->lease->address;
192 int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu)
194 assert_return(client, -EINVAL);
195 assert_return(mtu, -EINVAL);
197 switch (client->state) {
198 case DHCP_STATE_INIT:
199 case DHCP_STATE_SELECTING:
200 case DHCP_STATE_INIT_REBOOT:
201 case DHCP_STATE_REBOOTING:
202 case DHCP_STATE_REQUESTING:
203 return -EADDRNOTAVAIL;
205 case DHCP_STATE_BOUND:
206 case DHCP_STATE_RENEWING:
207 case DHCP_STATE_REBINDING:
208 if (client->lease->mtu)
209 *mtu = client->lease->mtu;
219 int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size)
221 assert_return(client, -EINVAL);
222 assert_return(addr, -EINVAL);
223 assert_return(addr_size, -EINVAL);
225 switch (client->state) {
226 case DHCP_STATE_INIT:
227 case DHCP_STATE_SELECTING:
228 case DHCP_STATE_INIT_REBOOT:
229 case DHCP_STATE_REBOOTING:
230 case DHCP_STATE_REQUESTING:
231 return -EADDRNOTAVAIL;
233 case DHCP_STATE_BOUND:
234 case DHCP_STATE_RENEWING:
235 case DHCP_STATE_REBINDING:
236 if (client->lease->dns_size) {
237 *addr_size = client->lease->dns_size;
238 *addr = client->lease->dns;
248 int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname)
250 assert_return(client, -EINVAL);
251 assert_return(hostname, -EINVAL);
253 switch (client->state) {
254 case DHCP_STATE_INIT:
255 case DHCP_STATE_SELECTING:
256 case DHCP_STATE_INIT_REBOOT:
257 case DHCP_STATE_REBOOTING:
258 case DHCP_STATE_REQUESTING:
259 return -EADDRNOTAVAIL;
261 case DHCP_STATE_BOUND:
262 case DHCP_STATE_RENEWING:
263 case DHCP_STATE_REBINDING:
264 if (client->lease->hostname)
265 *hostname = client->lease->hostname;
275 int sd_dhcp_client_prefixlen(const struct in_addr *addr)
280 assert_return(addr, -EADDRNOTAVAIL);
282 mask = be32toh(addr->s_addr);
291 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr)
293 assert_return(client, -EINVAL);
294 assert_return(addr, -EINVAL);
296 switch (client->state) {
297 case DHCP_STATE_INIT:
298 case DHCP_STATE_SELECTING:
299 case DHCP_STATE_INIT_REBOOT:
300 case DHCP_STATE_REBOOTING:
301 case DHCP_STATE_REQUESTING:
302 return -EADDRNOTAVAIL;
304 case DHCP_STATE_BOUND:
305 case DHCP_STATE_RENEWING:
306 case DHCP_STATE_REBINDING:
307 addr->s_addr = client->lease->router;
315 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr)
317 assert_return(client, -EINVAL);
318 assert_return(addr, -EINVAL);
320 switch (client->state) {
321 case DHCP_STATE_INIT:
322 case DHCP_STATE_SELECTING:
323 case DHCP_STATE_INIT_REBOOT:
324 case DHCP_STATE_REBOOTING:
325 case DHCP_STATE_REQUESTING:
326 return -EADDRNOTAVAIL;
328 case DHCP_STATE_BOUND:
329 case DHCP_STATE_RENEWING:
330 case DHCP_STATE_REBINDING:
331 addr->s_addr = client->lease->subnet_mask;
339 static int client_notify(sd_dhcp_client *client, int event)
342 client->cb(client, event, client->userdata);
347 static int client_stop(sd_dhcp_client *client, int error)
349 assert_return(client, -EINVAL);
351 client->receive_message =
352 sd_event_source_unref(client->receive_message);
358 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
360 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
361 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
362 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
366 client_notify(client, error);
368 switch (client->state) {
370 case DHCP_STATE_INIT:
371 case DHCP_STATE_SELECTING:
372 case DHCP_STATE_REQUESTING:
373 case DHCP_STATE_BOUND:
375 client->start_time = 0;
376 client->state = DHCP_STATE_INIT;
379 case DHCP_STATE_INIT_REBOOT:
380 case DHCP_STATE_REBOOTING:
381 case DHCP_STATE_RENEWING:
382 case DHCP_STATE_REBINDING:
388 free(client->lease->dns);
390 client->lease = NULL;
396 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
397 DHCPMessage *message, uint16_t secs,
398 uint8_t **opt, size_t *optlen)
403 *opt = (uint8_t *)(message + 1);
409 message->op = BOOTREQUEST;
411 message->hlen = ETHER_ADDR_LEN;
412 message->xid = htobe32(client->xid);
414 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
415 refuse to issue an DHCP lease if 'secs' is set to zero */
416 message->secs = htobe16(secs);
418 if (client->state == DHCP_STATE_RENEWING ||
419 client->state == DHCP_STATE_REBINDING)
420 message->ciaddr = client->lease->address;
422 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
430 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
435 /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
436 Identifier option is not set */
437 err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
438 ETH_ALEN, &client->mac_addr);
442 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
443 err = dhcp_option_append(opt, optlen,
444 DHCP_OPTION_PARAMETER_REQUEST_LIST,
445 client->req_opts_size,
450 /* Some DHCP servers will send bigger DHCP packets than the
451 defined default size unless the Maximum Messge Size option
452 is explicitely set */
453 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
454 DHCP_CLIENT_MIN_OPTIONS_SIZE);
455 err = dhcp_option_append(opt, optlen,
456 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
465 static uint16_t client_checksum(void *buf, int len)
475 for (i = 0; i < len / 2 ; i++)
484 sum = (sum & 0xffff) + (sum >> 16);
489 static void client_append_ip_headers(DHCPPacket *packet, uint16_t len)
491 packet->ip.version = IPVERSION;
492 packet->ip.ihl = DHCP_IP_SIZE / 4;
493 packet->ip.tot_len = htobe16(len);
495 packet->ip.protocol = IPPROTO_UDP;
496 packet->ip.saddr = INADDR_ANY;
497 packet->ip.daddr = INADDR_BROADCAST;
499 packet->udp.source = htobe16(DHCP_PORT_CLIENT);
500 packet->udp.dest = htobe16(DHCP_PORT_SERVER);
501 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
503 packet->ip.check = packet->udp.len;
504 packet->udp.check = client_checksum(&packet->ip.ttl, len - 8);
506 packet->ip.ttl = IPDEFTTL;
507 packet->ip.check = 0;
508 packet->ip.check = client_checksum(&packet->ip, DHCP_IP_SIZE);
511 static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
514 _cleanup_free_ DHCPPacket *discover;
518 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
519 len = sizeof(DHCPPacket) + optlen;
521 discover = malloc0(len);
526 err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
527 secs, &opt, &optlen);
531 if (client->last_addr != INADDR_ANY) {
532 err = dhcp_option_append(&opt, &optlen,
533 DHCP_OPTION_REQUESTED_IP_ADDRESS,
534 4, &client->last_addr);
539 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
543 client_append_ip_headers(discover, len);
545 err = dhcp_network_send_raw_socket(client->fd, &client->link,
551 static int client_send_request(sd_dhcp_client *client, uint16_t secs)
553 _cleanup_free_ DHCPPacket *request;
558 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
559 len = DHCP_MESSAGE_SIZE + optlen;
561 request = malloc0(len);
565 err = client_packet_init(client, DHCP_REQUEST, &request->dhcp, secs,
570 if (client->state == DHCP_STATE_REQUESTING) {
571 err = dhcp_option_append(&opt, &optlen,
572 DHCP_OPTION_REQUESTED_IP_ADDRESS,
573 4, &client->lease->address);
577 err = dhcp_option_append(&opt, &optlen,
578 DHCP_OPTION_SERVER_IDENTIFIER,
579 4, &client->lease->server_address);
584 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
588 if (client->state == DHCP_STATE_RENEWING) {
589 err = dhcp_network_send_udp_socket(client->fd,
590 client->lease->server_address,
592 len - DHCP_IP_UDP_SIZE);
594 client_append_ip_headers(request, len);
596 err = dhcp_network_send_raw_socket(client->fd, &client->link,
603 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
606 sd_dhcp_client *client = userdata;
607 usec_t next_timeout = 0;
612 switch (client->state) {
613 case DHCP_STATE_RENEWING:
615 time_left = (client->lease->t2 - client->lease->t1)/2;
619 next_timeout = usec + time_left * USEC_PER_SEC;
623 case DHCP_STATE_REBINDING:
625 time_left = (client->lease->lifetime - client->lease->t2)/2;
629 next_timeout = usec + time_left * USEC_PER_SEC;
632 case DHCP_STATE_INIT:
633 case DHCP_STATE_INIT_REBOOT:
634 case DHCP_STATE_REBOOTING:
635 case DHCP_STATE_SELECTING:
636 case DHCP_STATE_REQUESTING:
637 case DHCP_STATE_BOUND:
639 if (client->attempt < 64)
640 client->attempt *= 2;
642 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
647 next_timeout += (random_u32() & 0x1fffff);
649 err = sd_event_add_monotonic(client->event, next_timeout,
651 client_timeout_resend, client,
652 &client->timeout_resend);
656 secs = (usec - client->start_time) / USEC_PER_SEC;
658 switch (client->state) {
659 case DHCP_STATE_INIT:
660 err = client_send_discover(client, secs);
662 client->state = DHCP_STATE_SELECTING;
665 if (client->attempt >= 64)
671 case DHCP_STATE_SELECTING:
672 err = client_send_discover(client, secs);
673 if (err < 0 && client->attempt >= 64)
678 case DHCP_STATE_REQUESTING:
679 case DHCP_STATE_RENEWING:
680 case DHCP_STATE_REBINDING:
681 err = client_send_request(client, secs);
682 if (err < 0 && client->attempt >= 64)
685 client->request_sent = usec;
689 case DHCP_STATE_INIT_REBOOT:
690 case DHCP_STATE_REBOOTING:
691 case DHCP_STATE_BOUND:
699 client_stop(client, err);
701 /* Errors were dealt with when stopping the client, don't spill
702 errors into the event loop handler */
706 static int client_initialize_events(sd_dhcp_client *client, usec_t usec)
710 r = sd_event_add_io(client->event, client->fd, EPOLLIN,
711 client_receive_message, client,
712 &client->receive_message);
716 r = sd_event_add_monotonic(client->event, usec, 0,
717 client_timeout_resend, client,
718 &client->timeout_resend);
722 client_stop(client, r);
728 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
731 sd_dhcp_client *client = userdata;
733 client_stop(client, DHCP_EVENT_EXPIRED);
738 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
740 sd_dhcp_client *client = userdata;
743 if (client->fd >= 0) {
744 client->receive_message =
745 sd_event_source_unref(client->receive_message);
750 client->state = DHCP_STATE_REBINDING;
753 r = dhcp_network_bind_raw_socket(client->index, &client->link);
755 client_stop(client, r);
761 return client_initialize_events(client, usec);
764 static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
766 sd_dhcp_client *client = userdata;
769 client->state = DHCP_STATE_RENEWING;
772 r = dhcp_network_bind_udp_socket(client->index,
773 client->lease->address);
775 client_stop(client, r);
781 return client_initialize_events(client, usec);
784 static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
787 DHCPLease *lease = user_data;
792 case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
794 memcpy(&val, option, 4);
795 lease->lifetime = be32toh(val);
800 case DHCP_OPTION_SERVER_IDENTIFIER:
802 memcpy(&lease->server_address, option, 4);
806 case DHCP_OPTION_SUBNET_MASK:
808 memcpy(&lease->subnet_mask, option, 4);
812 case DHCP_OPTION_ROUTER:
814 memcpy(&lease->router, option, 4);
818 case DHCP_OPTION_DOMAIN_NAME_SERVER:
822 lease->dns_size = len / 4;
825 lease->dns = new0(struct in_addr, lease->dns_size);
829 for (i = 0; i < lease->dns_size; i++) {
830 memcpy(&lease->dns[i].s_addr, option + 4 * i, 4);
836 case DHCP_OPTION_INTERFACE_MTU:
840 memcpy(&mtu, option, 2);
841 lease->mtu = be16toh(mtu);
849 case DHCP_OPTION_HOST_NAME:
851 free(lease->hostname);
852 lease->hostname = strndup((const char *)option, len);
857 case DHCP_OPTION_RENEWAL_T1_TIME:
859 memcpy(&val, option, 4);
860 lease->t1 = be32toh(val);
865 case DHCP_OPTION_REBINDING_T2_TIME:
867 memcpy(&val, option, 4);
868 lease->t2 = be32toh(val);
877 static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
882 if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
885 hdrlen = message->ip.ihl * 4;
886 if (hdrlen < 20 || hdrlen > len || client_checksum(&message->ip,
890 message->ip.check = message->udp.len;
893 if (hdrlen + be16toh(message->udp.len) > len ||
894 client_checksum(&message->ip.ttl, be16toh(message->udp.len) + 12))
897 if (be16toh(message->udp.source) != DHCP_PORT_SERVER ||
898 be16toh(message->udp.dest) != DHCP_PORT_CLIENT)
901 if (message->dhcp.op != BOOTREPLY)
904 if (be32toh(message->dhcp.xid) != client->xid)
907 if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
914 static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
920 err = client_verify_headers(client, offer, len);
924 lease = new0(DHCPLease, 1);
928 len = len - DHCP_IP_UDP_SIZE;
929 if (dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
930 lease) != DHCP_OFFER)
933 lease->address = offer->dhcp.yiaddr;
935 if (lease->address == INADDR_ANY ||
936 lease->server_address == INADDR_ANY ||
937 lease->subnet_mask == INADDR_ANY ||
938 lease->lifetime == 0)
941 client->lease = lease;
951 static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
959 if (client->state == DHCP_STATE_RENEWING) {
960 dhcp = (DHCPMessage *)buf;
962 ack = (DHCPPacket *)buf;
964 r = client_verify_headers(client, ack, len);
969 len -= DHCP_IP_UDP_SIZE;
972 lease = new0(DHCPLease, 1);
976 r = dhcp_option_parse(dhcp, len, client_parse_offer, lease);
979 r = DHCP_EVENT_NO_LEASE;
988 lease->address = dhcp->yiaddr;
990 if (lease->address == INADDR_ANY ||
991 lease->server_address == INADDR_ANY ||
992 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0) {
997 r = DHCP_EVENT_IP_ACQUIRE;
999 if (client->lease->address != lease->address ||
1000 client->lease->subnet_mask != lease->subnet_mask ||
1001 client->lease->router != lease->router) {
1002 r = DHCP_EVENT_IP_CHANGE;
1005 free(client->lease);
1008 client->lease = lease;
1018 static uint64_t client_compute_timeout(uint64_t request_sent,
1021 return request_sent + (lifetime - 3) * USEC_PER_SEC +
1022 + (random_u32() & 0x1fffff);
1025 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
1028 uint64_t next_timeout;
1030 if (client->lease->lifetime < 10)
1033 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
1034 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
1035 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
1037 if (!client->lease->t1)
1038 client->lease->t1 = client->lease->lifetime / 2;
1040 next_timeout = client_compute_timeout(client->request_sent,
1042 if (next_timeout < usec)
1045 err = sd_event_add_monotonic(client->event, next_timeout,
1047 client_timeout_t1, client,
1048 &client->timeout_t1);
1052 if (!client->lease->t2)
1053 client->lease->t2 = client->lease->lifetime * 7 / 8;
1055 if (client->lease->t2 < client->lease->t1)
1058 if (client->lease->lifetime < client->lease->t2)
1061 next_timeout = client_compute_timeout(client->request_sent,
1063 if (next_timeout < usec)
1066 err = sd_event_add_monotonic(client->event, next_timeout,
1068 client_timeout_t2, client,
1069 &client->timeout_t2);
1073 next_timeout = client_compute_timeout(client->request_sent,
1074 client->lease->lifetime);
1075 if (next_timeout < usec)
1078 err = sd_event_add_monotonic(client->event, next_timeout,
1080 client_timeout_expire, client,
1081 &client->timeout_expire);
1088 static int client_receive_message(sd_event_source *s, int fd,
1089 uint32_t revents, void *userdata)
1091 sd_dhcp_client *client = userdata;
1092 uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
1093 int buflen = sizeof(buf);
1094 int len, r = 0, notify_event = 0;
1095 DHCPPacket *message;
1098 len = read(fd, &buf, buflen);
1102 r = sd_event_get_now_monotonic(client->event, &time_now);
1106 switch (client->state) {
1107 case DHCP_STATE_SELECTING:
1109 message = (DHCPPacket *)&buf;
1111 if (client_receive_offer(client, message, len) >= 0) {
1113 client->timeout_resend =
1114 sd_event_source_unref(client->timeout_resend);
1116 client->state = DHCP_STATE_REQUESTING;
1117 client->attempt = 1;
1119 r = sd_event_add_monotonic(client->event, time_now, 0,
1120 client_timeout_resend,
1122 &client->timeout_resend);
1129 case DHCP_STATE_REQUESTING:
1130 case DHCP_STATE_RENEWING:
1131 case DHCP_STATE_REBINDING:
1133 r = client_receive_ack(client, buf, len);
1135 if (r == DHCP_EVENT_NO_LEASE)
1139 client->timeout_resend =
1140 sd_event_source_unref(client->timeout_resend);
1142 if (client->state == DHCP_STATE_REQUESTING)
1143 notify_event = DHCP_EVENT_IP_ACQUIRE;
1144 else if (r != DHCP_EVENT_IP_ACQUIRE)
1147 client->state = DHCP_STATE_BOUND;
1148 client->attempt = 1;
1150 client->last_addr = client->lease->address;
1152 r = client_set_lease_timeouts(client, time_now);
1157 client_notify(client, notify_event);
1159 client->receive_message =
1160 sd_event_source_unref(client->receive_message);
1169 case DHCP_STATE_INIT:
1170 case DHCP_STATE_INIT_REBOOT:
1171 case DHCP_STATE_REBOOTING:
1172 case DHCP_STATE_BOUND:
1178 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
1179 return client_stop(client, r);
1184 int sd_dhcp_client_start(sd_dhcp_client *client)
1188 assert_return(client, -EINVAL);
1189 assert_return(client->index > 0, -EINVAL);
1190 assert_return(client->state == DHCP_STATE_INIT ||
1191 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
1193 client->xid = random_u32();
1195 r = dhcp_network_bind_raw_socket(client->index, &client->link);
1198 client_stop(client, r);
1203 client->start_time = now(CLOCK_MONOTONIC);
1205 return client_initialize_events(client, client->start_time);
1208 int sd_dhcp_client_stop(sd_dhcp_client *client)
1210 return client_stop(client, DHCP_EVENT_STOP);
1213 sd_dhcp_client *sd_dhcp_client_free(sd_dhcp_client *client)
1215 assert_return(client, NULL);
1217 sd_dhcp_client_stop(client);
1219 sd_event_unref(client->event);
1220 free(client->req_opts);
1226 sd_dhcp_client *sd_dhcp_client_new(sd_event *event)
1228 sd_dhcp_client *client;
1230 assert_return(event, NULL);
1232 client = new0(sd_dhcp_client, 1);
1236 client->event = sd_event_ref(event);
1237 client->state = DHCP_STATE_INIT;
1240 client->attempt = 1;
1242 client->req_opts_size = ELEMENTSOF(default_req_opts);
1244 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1245 if (!client->req_opts) {