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;
51 typedef struct DHCPLease DHCPLease;
53 struct sd_dhcp_client {
56 sd_event_source *timeout_resend;
59 union sockaddr_union link;
60 sd_event_source *receive_message;
62 size_t req_opts_allocated;
65 struct ether_addr mac_addr;
70 sd_event_source *timeout_t1;
71 sd_event_source *timeout_t2;
72 sd_event_source *timeout_expire;
73 sd_dhcp_client_cb_t cb;
78 static const uint8_t default_req_opts[] = {
79 DHCP_OPTION_SUBNET_MASK,
81 DHCP_OPTION_HOST_NAME,
82 DHCP_OPTION_DOMAIN_NAME,
83 DHCP_OPTION_DOMAIN_NAME_SERVER,
84 DHCP_OPTION_NTP_SERVER,
87 static int client_receive_message(sd_event_source *s, int fd,
88 uint32_t revents, void *userdata);
90 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) {
103 assert_return(client, -EINVAL);
104 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
107 case DHCP_OPTION_PAD:
108 case DHCP_OPTION_OVERLOAD:
109 case DHCP_OPTION_MESSAGE_TYPE:
110 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
111 case DHCP_OPTION_END:
118 for (i = 0; i < client->req_opts_size; i++)
119 if (client->req_opts[i] == option)
122 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
123 client->req_opts_size + 1))
126 client->req_opts[client->req_opts_size++] = option;
131 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
132 const struct in_addr *last_addr) {
133 assert_return(client, -EINVAL);
134 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
137 client->last_addr = last_addr->s_addr;
139 client->last_addr = INADDR_ANY;
144 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
145 assert_return(client, -EINVAL);
146 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
147 assert_return(interface_index >= -1, -EINVAL);
149 client->index = interface_index;
154 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
155 const struct ether_addr *addr) {
156 assert_return(client, -EINVAL);
157 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
159 memcpy(&client->mac_addr, addr, ETH_ALEN);
164 int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr) {
165 assert_return(client, -EINVAL);
166 assert_return(addr, -EINVAL);
168 switch (client->state) {
169 case DHCP_STATE_INIT:
170 case DHCP_STATE_SELECTING:
171 case DHCP_STATE_INIT_REBOOT:
172 case DHCP_STATE_REBOOTING:
173 case DHCP_STATE_REQUESTING:
174 return -EADDRNOTAVAIL;
176 case DHCP_STATE_BOUND:
177 case DHCP_STATE_RENEWING:
178 case DHCP_STATE_REBINDING:
179 addr->s_addr = client->lease->address;
187 int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu) {
188 assert_return(client, -EINVAL);
189 assert_return(mtu, -EINVAL);
191 switch (client->state) {
192 case DHCP_STATE_INIT:
193 case DHCP_STATE_SELECTING:
194 case DHCP_STATE_INIT_REBOOT:
195 case DHCP_STATE_REBOOTING:
196 case DHCP_STATE_REQUESTING:
197 return -EADDRNOTAVAIL;
199 case DHCP_STATE_BOUND:
200 case DHCP_STATE_RENEWING:
201 case DHCP_STATE_REBINDING:
202 if (client->lease->mtu)
203 *mtu = client->lease->mtu;
213 int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size) {
214 assert_return(client, -EINVAL);
215 assert_return(addr, -EINVAL);
216 assert_return(addr_size, -EINVAL);
218 switch (client->state) {
219 case DHCP_STATE_INIT:
220 case DHCP_STATE_SELECTING:
221 case DHCP_STATE_INIT_REBOOT:
222 case DHCP_STATE_REBOOTING:
223 case DHCP_STATE_REQUESTING:
224 return -EADDRNOTAVAIL;
226 case DHCP_STATE_BOUND:
227 case DHCP_STATE_RENEWING:
228 case DHCP_STATE_REBINDING:
229 if (client->lease->dns_size) {
230 *addr_size = client->lease->dns_size;
231 *addr = client->lease->dns;
241 int sd_dhcp_client_get_domainname(sd_dhcp_client *client, const char **domainname) {
242 assert_return(client, -EINVAL);
243 assert_return(domainname, -EINVAL);
245 switch (client->state) {
246 case DHCP_STATE_INIT:
247 case DHCP_STATE_SELECTING:
248 case DHCP_STATE_INIT_REBOOT:
249 case DHCP_STATE_REBOOTING:
250 case DHCP_STATE_REQUESTING:
251 return -EADDRNOTAVAIL;
253 case DHCP_STATE_BOUND:
254 case DHCP_STATE_RENEWING:
255 case DHCP_STATE_REBINDING:
256 if (client->lease->domainname)
257 *domainname = client->lease->domainname;
267 int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname) {
268 assert_return(client, -EINVAL);
269 assert_return(hostname, -EINVAL);
271 switch (client->state) {
272 case DHCP_STATE_INIT:
273 case DHCP_STATE_SELECTING:
274 case DHCP_STATE_INIT_REBOOT:
275 case DHCP_STATE_REBOOTING:
276 case DHCP_STATE_REQUESTING:
277 return -EADDRNOTAVAIL;
279 case DHCP_STATE_BOUND:
280 case DHCP_STATE_RENEWING:
281 case DHCP_STATE_REBINDING:
282 if (client->lease->hostname)
283 *hostname = client->lease->hostname;
293 int sd_dhcp_client_prefixlen(const struct in_addr *addr) {
297 assert_return(addr, -EADDRNOTAVAIL);
299 mask = be32toh(addr->s_addr);
308 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr) {
309 assert_return(client, -EINVAL);
310 assert_return(addr, -EINVAL);
312 switch (client->state) {
313 case DHCP_STATE_INIT:
314 case DHCP_STATE_SELECTING:
315 case DHCP_STATE_INIT_REBOOT:
316 case DHCP_STATE_REBOOTING:
317 case DHCP_STATE_REQUESTING:
318 return -EADDRNOTAVAIL;
320 case DHCP_STATE_BOUND:
321 case DHCP_STATE_RENEWING:
322 case DHCP_STATE_REBINDING:
323 addr->s_addr = client->lease->router;
331 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr) {
332 assert_return(client, -EINVAL);
333 assert_return(addr, -EINVAL);
335 switch (client->state) {
336 case DHCP_STATE_INIT:
337 case DHCP_STATE_SELECTING:
338 case DHCP_STATE_INIT_REBOOT:
339 case DHCP_STATE_REBOOTING:
340 case DHCP_STATE_REQUESTING:
341 return -EADDRNOTAVAIL;
343 case DHCP_STATE_BOUND:
344 case DHCP_STATE_RENEWING:
345 case DHCP_STATE_REBINDING:
346 addr->s_addr = client->lease->subnet_mask;
354 static int client_notify(sd_dhcp_client *client, int event) {
356 client->cb(client, event, client->userdata);
361 static void lease_free(DHCPLease *lease) {
365 free(lease->hostname);
366 free(lease->domainname);
371 DEFINE_TRIVIAL_CLEANUP_FUNC(DHCPLease*, lease_free);
372 #define _cleanup_lease_free_ _cleanup_(lease_freep)
374 static int client_stop(sd_dhcp_client *client, int error) {
375 assert_return(client, -EINVAL);
377 client->receive_message =
378 sd_event_source_unref(client->receive_message);
384 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
386 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
387 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
388 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
392 client_notify(client, error);
394 switch (client->state) {
396 case DHCP_STATE_INIT:
397 case DHCP_STATE_SELECTING:
398 case DHCP_STATE_REQUESTING:
399 case DHCP_STATE_BOUND:
401 client->start_time = 0;
402 client->state = DHCP_STATE_INIT;
405 case DHCP_STATE_INIT_REBOOT:
406 case DHCP_STATE_REBOOTING:
407 case DHCP_STATE_RENEWING:
408 case DHCP_STATE_REBINDING:
414 lease_free(client->lease);
415 client->lease = NULL;
421 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
422 DHCPMessage *message, uint16_t secs,
423 uint8_t **opt, size_t *optlen) {
427 *opt = (uint8_t *)(message + 1);
433 message->op = BOOTREQUEST;
435 message->hlen = ETHER_ADDR_LEN;
436 message->xid = htobe32(client->xid);
438 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
439 refuse to issue an DHCP lease if 'secs' is set to zero */
440 message->secs = htobe16(secs);
442 if (client->state == DHCP_STATE_RENEWING ||
443 client->state == DHCP_STATE_REBINDING)
444 message->ciaddr = client->lease->address;
446 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
454 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
459 /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
460 Identifier option is not set */
461 err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
462 ETH_ALEN, &client->mac_addr);
466 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
467 err = dhcp_option_append(opt, optlen,
468 DHCP_OPTION_PARAMETER_REQUEST_LIST,
469 client->req_opts_size,
474 /* Some DHCP servers will send bigger DHCP packets than the
475 defined default size unless the Maximum Messge Size option
476 is explicitely set */
477 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
478 DHCP_CLIENT_MIN_OPTIONS_SIZE);
479 err = dhcp_option_append(opt, optlen,
480 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
489 static uint16_t client_checksum(void *buf, int len) {
498 for (i = 0; i < len / 2 ; i++)
507 sum = (sum & 0xffff) + (sum >> 16);
512 static void client_append_ip_headers(DHCPPacket *packet, uint16_t len) {
513 packet->ip.version = IPVERSION;
514 packet->ip.ihl = DHCP_IP_SIZE / 4;
515 packet->ip.tot_len = htobe16(len);
517 packet->ip.protocol = IPPROTO_UDP;
518 packet->ip.saddr = INADDR_ANY;
519 packet->ip.daddr = INADDR_BROADCAST;
521 packet->udp.source = htobe16(DHCP_PORT_CLIENT);
522 packet->udp.dest = htobe16(DHCP_PORT_SERVER);
523 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
525 packet->ip.check = packet->udp.len;
526 packet->udp.check = client_checksum(&packet->ip.ttl, len - 8);
528 packet->ip.ttl = IPDEFTTL;
529 packet->ip.check = 0;
530 packet->ip.check = client_checksum(&packet->ip, DHCP_IP_SIZE);
533 static int client_send_discover(sd_dhcp_client *client, uint16_t secs) {
535 _cleanup_free_ DHCPPacket *discover;
539 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
540 len = sizeof(DHCPPacket) + optlen;
542 discover = malloc0(len);
547 err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
548 secs, &opt, &optlen);
552 if (client->last_addr != INADDR_ANY) {
553 err = dhcp_option_append(&opt, &optlen,
554 DHCP_OPTION_REQUESTED_IP_ADDRESS,
555 4, &client->last_addr);
560 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
564 client_append_ip_headers(discover, len);
566 err = dhcp_network_send_raw_socket(client->fd, &client->link,
572 static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
573 _cleanup_free_ DHCPPacket *request;
578 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
579 len = DHCP_MESSAGE_SIZE + optlen;
581 request = malloc0(len);
585 err = client_packet_init(client, DHCP_REQUEST, &request->dhcp, secs,
590 if (client->state == DHCP_STATE_REQUESTING) {
591 err = dhcp_option_append(&opt, &optlen,
592 DHCP_OPTION_REQUESTED_IP_ADDRESS,
593 4, &client->lease->address);
597 err = dhcp_option_append(&opt, &optlen,
598 DHCP_OPTION_SERVER_IDENTIFIER,
599 4, &client->lease->server_address);
604 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
608 if (client->state == DHCP_STATE_RENEWING) {
609 err = dhcp_network_send_udp_socket(client->fd,
610 client->lease->server_address,
612 len - DHCP_IP_UDP_SIZE);
614 client_append_ip_headers(request, len);
616 err = dhcp_network_send_raw_socket(client->fd, &client->link,
623 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
625 sd_dhcp_client *client = userdata;
626 usec_t next_timeout = 0;
631 switch (client->state) {
632 case DHCP_STATE_RENEWING:
634 time_left = (client->lease->t2 - client->lease->t1)/2;
638 next_timeout = usec + time_left * USEC_PER_SEC;
642 case DHCP_STATE_REBINDING:
644 time_left = (client->lease->lifetime - client->lease->t2)/2;
648 next_timeout = usec + time_left * USEC_PER_SEC;
651 case DHCP_STATE_INIT:
652 case DHCP_STATE_INIT_REBOOT:
653 case DHCP_STATE_REBOOTING:
654 case DHCP_STATE_SELECTING:
655 case DHCP_STATE_REQUESTING:
656 case DHCP_STATE_BOUND:
658 if (client->attempt < 64)
659 client->attempt *= 2;
661 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
666 next_timeout += (random_u32() & 0x1fffff);
668 err = sd_event_add_monotonic(client->event, next_timeout,
670 client_timeout_resend, client,
671 &client->timeout_resend);
675 secs = (usec - client->start_time) / USEC_PER_SEC;
677 switch (client->state) {
678 case DHCP_STATE_INIT:
679 err = client_send_discover(client, secs);
681 client->state = DHCP_STATE_SELECTING;
684 if (client->attempt >= 64)
690 case DHCP_STATE_SELECTING:
691 err = client_send_discover(client, secs);
692 if (err < 0 && client->attempt >= 64)
697 case DHCP_STATE_REQUESTING:
698 case DHCP_STATE_RENEWING:
699 case DHCP_STATE_REBINDING:
700 err = client_send_request(client, secs);
701 if (err < 0 && client->attempt >= 64)
704 client->request_sent = usec;
708 case DHCP_STATE_INIT_REBOOT:
709 case DHCP_STATE_REBOOTING:
710 case DHCP_STATE_BOUND:
718 client_stop(client, err);
720 /* Errors were dealt with when stopping the client, don't spill
721 errors into the event loop handler */
725 static int client_initialize_events(sd_dhcp_client *client, usec_t usec) {
728 r = sd_event_add_io(client->event, client->fd, EPOLLIN,
729 client_receive_message, client,
730 &client->receive_message);
734 r = sd_event_add_monotonic(client->event, usec, 0,
735 client_timeout_resend, client,
736 &client->timeout_resend);
740 client_stop(client, r);
746 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
748 sd_dhcp_client *client = userdata;
750 client_stop(client, DHCP_EVENT_EXPIRED);
755 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
756 sd_dhcp_client *client = userdata;
759 if (client->fd >= 0) {
760 client->receive_message =
761 sd_event_source_unref(client->receive_message);
766 client->state = DHCP_STATE_REBINDING;
769 r = dhcp_network_bind_raw_socket(client->index, &client->link);
771 client_stop(client, r);
777 return client_initialize_events(client, usec);
780 static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata) {
781 sd_dhcp_client *client = userdata;
784 client->state = DHCP_STATE_RENEWING;
787 r = dhcp_network_bind_udp_socket(client->index,
788 client->lease->address);
790 client_stop(client, r);
796 return client_initialize_events(client, usec);
799 static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
801 DHCPLease *lease = user_data;
806 case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
808 memcpy(&val, option, 4);
809 lease->lifetime = be32toh(val);
814 case DHCP_OPTION_SERVER_IDENTIFIER:
816 memcpy(&lease->server_address, option, 4);
820 case DHCP_OPTION_SUBNET_MASK:
822 memcpy(&lease->subnet_mask, option, 4);
826 case DHCP_OPTION_ROUTER:
828 memcpy(&lease->router, option, 4);
832 case DHCP_OPTION_DOMAIN_NAME_SERVER:
836 lease->dns_size = len / 4;
839 lease->dns = new0(struct in_addr, lease->dns_size);
843 for (i = 0; i < lease->dns_size; i++) {
844 memcpy(&lease->dns[i].s_addr, option + 4 * i, 4);
850 case DHCP_OPTION_INTERFACE_MTU:
854 memcpy(&mtu, option, 2);
855 lease->mtu = be16toh(mtu);
863 case DHCP_OPTION_DOMAIN_NAME:
865 free(lease->domainname);
866 lease->domainname = strndup((const char *)option, len);
871 case DHCP_OPTION_HOST_NAME:
873 free(lease->hostname);
874 lease->hostname = strndup((const char *)option, len);
879 case DHCP_OPTION_RENEWAL_T1_TIME:
881 memcpy(&val, option, 4);
882 lease->t1 = be32toh(val);
887 case DHCP_OPTION_REBINDING_T2_TIME:
889 memcpy(&val, option, 4);
890 lease->t2 = be32toh(val);
899 static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
903 if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
906 hdrlen = message->ip.ihl * 4;
907 if (hdrlen < 20 || hdrlen > len || client_checksum(&message->ip,
911 message->ip.check = message->udp.len;
914 if (hdrlen + be16toh(message->udp.len) > len ||
915 client_checksum(&message->ip.ttl, be16toh(message->udp.len) + 12))
918 if (be16toh(message->udp.source) != DHCP_PORT_SERVER ||
919 be16toh(message->udp.dest) != DHCP_PORT_CLIENT)
922 if (message->dhcp.op != BOOTREPLY)
925 if (be32toh(message->dhcp.xid) != client->xid)
928 if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
935 static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
937 _cleanup_lease_free_ DHCPLease *lease = NULL;
940 r = client_verify_headers(client, offer, len);
944 lease = new0(DHCPLease, 1);
948 len = len - DHCP_IP_UDP_SIZE;
949 r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
954 lease->address = offer->dhcp.yiaddr;
956 if (lease->address == INADDR_ANY ||
957 lease->server_address == INADDR_ANY ||
958 lease->subnet_mask == INADDR_ANY ||
959 lease->lifetime == 0)
962 client->lease = lease;
968 static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
972 _cleanup_lease_free_ DHCPLease *lease = NULL;
975 if (client->state == DHCP_STATE_RENEWING) {
976 dhcp = (DHCPMessage *)buf;
978 ack = (DHCPPacket *)buf;
980 r = client_verify_headers(client, ack, len);
985 len -= DHCP_IP_UDP_SIZE;
988 lease = new0(DHCPLease, 1);
992 r = dhcp_option_parse(dhcp, len, client_parse_offer, lease);
994 return DHCP_EVENT_NO_LEASE;
999 lease->address = dhcp->yiaddr;
1001 if (lease->address == INADDR_ANY ||
1002 lease->server_address == INADDR_ANY ||
1003 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
1006 r = DHCP_EVENT_IP_ACQUIRE;
1007 if (client->lease) {
1008 if (client->lease->address != lease->address ||
1009 client->lease->subnet_mask != lease->subnet_mask ||
1010 client->lease->router != lease->router) {
1011 r = DHCP_EVENT_IP_CHANGE;
1014 lease_free(client->lease);
1017 client->lease = lease;
1023 static uint64_t client_compute_timeout(uint64_t request_sent,
1024 uint32_t lifetime) {
1025 return request_sent + (lifetime - 3) * USEC_PER_SEC +
1026 + (random_u32() & 0x1fffff);
1029 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
1030 uint64_t next_timeout;
1033 if (client->lease->lifetime < 10)
1036 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
1037 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
1038 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
1040 if (!client->lease->t1)
1041 client->lease->t1 = client->lease->lifetime / 2;
1043 next_timeout = client_compute_timeout(client->request_sent,
1045 if (next_timeout < usec)
1048 r = sd_event_add_monotonic(client->event, next_timeout,
1050 client_timeout_t1, client,
1051 &client->timeout_t1);
1055 if (!client->lease->t2)
1056 client->lease->t2 = client->lease->lifetime * 7 / 8;
1058 if (client->lease->t2 < client->lease->t1)
1061 if (client->lease->lifetime < client->lease->t2)
1064 next_timeout = client_compute_timeout(client->request_sent,
1066 if (next_timeout < usec)
1069 r = sd_event_add_monotonic(client->event, next_timeout,
1071 client_timeout_t2, client,
1072 &client->timeout_t2);
1076 next_timeout = client_compute_timeout(client->request_sent,
1077 client->lease->lifetime);
1078 if (next_timeout < usec)
1081 r = sd_event_add_monotonic(client->event, next_timeout,
1083 client_timeout_expire, client,
1084 &client->timeout_expire);
1091 static int client_receive_message(sd_event_source *s, int fd,
1092 uint32_t revents, void *userdata) {
1093 sd_dhcp_client *client = userdata;
1094 uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
1095 int buflen = sizeof(buf);
1096 int len, r = 0, notify_event = 0;
1097 DHCPPacket *message;
1100 len = read(fd, &buf, buflen);
1104 r = sd_event_get_now_monotonic(client->event, &time_now);
1108 switch (client->state) {
1109 case DHCP_STATE_SELECTING:
1111 message = (DHCPPacket *)&buf;
1113 if (client_receive_offer(client, message, len) >= 0) {
1115 client->timeout_resend =
1116 sd_event_source_unref(client->timeout_resend);
1118 client->state = DHCP_STATE_REQUESTING;
1119 client->attempt = 1;
1121 r = sd_event_add_monotonic(client->event, time_now, 0,
1122 client_timeout_resend,
1124 &client->timeout_resend);
1131 case DHCP_STATE_REQUESTING:
1132 case DHCP_STATE_RENEWING:
1133 case DHCP_STATE_REBINDING:
1135 r = client_receive_ack(client, buf, len);
1137 if (r == DHCP_EVENT_NO_LEASE)
1141 client->timeout_resend =
1142 sd_event_source_unref(client->timeout_resend);
1144 if (client->state == DHCP_STATE_REQUESTING)
1145 notify_event = DHCP_EVENT_IP_ACQUIRE;
1146 else if (r != DHCP_EVENT_IP_ACQUIRE)
1149 client->state = DHCP_STATE_BOUND;
1150 client->attempt = 1;
1152 client->last_addr = client->lease->address;
1154 r = client_set_lease_timeouts(client, time_now);
1159 client_notify(client, notify_event);
1161 client->receive_message =
1162 sd_event_source_unref(client->receive_message);
1171 case DHCP_STATE_INIT:
1172 case DHCP_STATE_INIT_REBOOT:
1173 case DHCP_STATE_REBOOTING:
1174 case DHCP_STATE_BOUND:
1180 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
1181 return client_stop(client, r);
1186 int sd_dhcp_client_start(sd_dhcp_client *client) {
1189 assert_return(client, -EINVAL);
1190 assert_return(client->index > 0, -EINVAL);
1191 assert_return(client->state == DHCP_STATE_INIT ||
1192 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
1194 client->xid = random_u32();
1196 r = dhcp_network_bind_raw_socket(client->index, &client->link);
1199 client_stop(client, r);
1204 client->start_time = now(CLOCK_MONOTONIC);
1206 return client_initialize_events(client, client->start_time);
1209 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) {
1214 assert_return(client, NULL);
1216 sd_dhcp_client_stop(client);
1218 sd_event_unref(client->event);
1219 free(client->req_opts);
1225 sd_dhcp_client *sd_dhcp_client_new(sd_event *event) {
1226 sd_dhcp_client *client;
1228 assert_return(event, NULL);
1230 client = new0(sd_dhcp_client, 1);
1234 client->event = sd_event_ref(event);
1235 client->state = DHCP_STATE_INIT;
1238 client->attempt = 1;
1240 client->req_opts_size = ELEMENTSOF(default_req_opts);
1242 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1243 if (!client->req_opts) {