3 * DHCP client library with GLib integration
5 * Copyright (C) 2009-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
34 #include <netpacket/packet.h>
35 #include <net/ethernet.h>
38 #include <linux/filter.h>
45 #define DISCOVER_TIMEOUT 3
46 #define DISCOVER_RETRIES 10
48 #define REQUEST_TIMEOUT 3
49 #define REQUEST_RETRIES 5
51 typedef enum _listen_mode {
57 typedef enum _dhcp_client_state {
72 uint8_t mac_address[6];
75 uint32_t requested_ip;
77 uint32_t lease_seconds;
78 ListenMode listen_mode;
81 uint8_t ack_retry_times;
84 GIOChannel *listener_channel;
87 GHashTable *code_value_hash;
88 GHashTable *send_value_hash;
89 GDHCPClientEventFunc lease_available_cb;
90 gpointer lease_available_data;
91 GDHCPClientEventFunc no_lease_cb;
92 gpointer no_lease_data;
93 GDHCPClientEventFunc lease_lost_cb;
94 gpointer lease_lost_data;
95 GDHCPClientEventFunc address_conflict_cb;
96 gpointer address_conflict_data;
97 GDHCPDebugFunc debug_func;
101 static inline void debug(GDHCPClient *client, const char *format, ...)
106 if (client->debug_func == NULL)
109 va_start(ap, format);
111 if (vsnprintf(str, sizeof(str), format, ap) > 0)
112 client->debug_func(str, client->debug_data);
117 /* Initialize the packet with the proper defaults */
118 static void init_packet(GDHCPClient *dhcp_client,
119 struct dhcp_packet *packet, char type)
121 dhcp_init_header(packet, type);
123 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
126 static void add_request_options(GDHCPClient *dhcp_client,
127 struct dhcp_packet *packet)
132 int end = dhcp_end_option(packet->options);
134 for (list = dhcp_client->request_list; list; list = list->next) {
135 code = (uint8_t) GPOINTER_TO_INT(list->data);
137 packet->options[end + OPT_DATA + len] = code;
142 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
143 packet->options[end + OPT_LEN] = len;
144 packet->options[end + OPT_DATA + len] = DHCP_END;
148 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
150 uint8_t *option = value;
151 struct dhcp_packet *packet = user_data;
153 dhcp_add_binary_option(packet, option);
156 static void add_send_options(GDHCPClient *dhcp_client,
157 struct dhcp_packet *packet)
159 g_hash_table_foreach(dhcp_client->send_value_hash,
160 add_binary_option, packet);
163 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
165 struct dhcp_packet packet;
167 debug(dhcp_client, "sending DHCP discover request");
169 init_packet(dhcp_client, &packet, DHCPDISCOVER);
171 packet.xid = dhcp_client->xid;
174 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
176 /* Explicitly saying that we want RFC-compliant packets helps
177 * some buggy DHCP servers to NOT send bigger packets */
178 dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
180 add_request_options(dhcp_client, &packet);
182 add_send_options(dhcp_client, &packet);
184 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
185 INADDR_BROADCAST, SERVER_PORT,
186 MAC_BCAST_ADDR, dhcp_client->ifindex);
189 static int send_select(GDHCPClient *dhcp_client)
191 struct dhcp_packet packet;
194 debug(dhcp_client, "sending DHCP select request");
196 init_packet(dhcp_client, &packet, DHCPREQUEST);
198 packet.xid = dhcp_client->xid;
200 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
201 dhcp_client->requested_ip);
202 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
204 add_request_options(dhcp_client, &packet);
206 add_send_options(dhcp_client, &packet);
208 addr.s_addr = dhcp_client->requested_ip;
210 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
211 INADDR_BROADCAST, SERVER_PORT,
212 MAC_BCAST_ADDR, dhcp_client->ifindex);
215 static int send_renew(GDHCPClient *dhcp_client)
217 struct dhcp_packet packet;
219 debug(dhcp_client, "sending DHCP renew request");
221 init_packet(dhcp_client , &packet, DHCPREQUEST);
222 packet.xid = dhcp_client->xid;
223 packet.ciaddr = dhcp_client->requested_ip;
225 add_request_options(dhcp_client, &packet);
227 add_send_options(dhcp_client, &packet);
229 return dhcp_send_kernel_packet(&packet,
230 dhcp_client->requested_ip, CLIENT_PORT,
231 dhcp_client->server_ip, SERVER_PORT);
234 static int send_rebound(GDHCPClient *dhcp_client)
236 struct dhcp_packet packet;
238 debug(dhcp_client, "sending DHCP rebound request");
240 init_packet(dhcp_client , &packet, DHCPREQUEST);
241 packet.xid = dhcp_client->xid;
242 packet.ciaddr = dhcp_client->requested_ip;
244 add_request_options(dhcp_client, &packet);
246 add_send_options(dhcp_client, &packet);
248 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
249 INADDR_BROADCAST, SERVER_PORT,
250 MAC_BCAST_ADDR, dhcp_client->ifindex);
253 static int send_release(GDHCPClient *dhcp_client,
254 uint32_t server, uint32_t ciaddr)
256 struct dhcp_packet packet;
258 debug(dhcp_client, "sending DHCP release request");
260 init_packet(dhcp_client, &packet, DHCPRELEASE);
262 packet.ciaddr = ciaddr;
264 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
266 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
267 server, SERVER_PORT);
272 static void get_interface_mac_address(int index, uint8_t *mac_address)
277 sk = socket(PF_INET, SOCK_DGRAM, 0);
279 perror("Open socket error");
283 memset(&ifr, 0, sizeof(ifr));
284 ifr.ifr_ifindex = index;
286 err = ioctl(sk, SIOCGIFNAME, &ifr);
288 perror("Get interface name error");
292 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
294 perror("Get mac address error");
298 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
304 static void remove_value(gpointer data, gpointer user_data)
310 static void remove_option_value(gpointer data)
312 GList *option_value = data;
314 g_list_foreach(option_value, remove_value, NULL);
317 GDHCPClient *g_dhcp_client_new(GDHCPType type,
318 int ifindex, GDHCPClientError *error)
320 GDHCPClient *dhcp_client;
323 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
327 dhcp_client = g_try_new0(GDHCPClient, 1);
328 if (dhcp_client == NULL) {
329 *error = G_DHCP_CLIENT_ERROR_NOMEM;
333 dhcp_client->interface = get_interface_name(ifindex);
334 if (dhcp_client->interface == NULL) {
335 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
339 if (interface_is_up(ifindex) == FALSE) {
340 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
344 get_interface_mac_address(ifindex, dhcp_client->mac_address);
346 dhcp_client->listener_sockfd = -1;
347 dhcp_client->listener_channel = NULL;
348 dhcp_client->listen_mode = L_NONE;
349 dhcp_client->ref_count = 1;
350 dhcp_client->type = type;
351 dhcp_client->ifindex = ifindex;
352 dhcp_client->lease_available_cb = NULL;
353 dhcp_client->no_lease_cb = NULL;
354 dhcp_client->lease_lost_cb = NULL;
355 dhcp_client->address_conflict_cb = NULL;
356 dhcp_client->listener_watch = 0;
357 dhcp_client->retry_times = 0;
358 dhcp_client->ack_retry_times = 0;
359 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
360 g_direct_equal, NULL, remove_option_value);
361 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
362 g_direct_equal, NULL, g_free);
363 dhcp_client->request_list = NULL;
364 dhcp_client->require_list = NULL;
366 *error = G_DHCP_CLIENT_ERROR_NONE;
371 g_free(dhcp_client->interface);
376 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
378 static int dhcp_l2_socket(int ifindex)
381 struct sockaddr_ll sock;
386 * I've selected not to see LL header, so BPF doesn't see it, too.
387 * The filter may also pass non-IP and non-ARP packets, but we do
388 * a more complete check when receiving the message in userspace.
390 * and filter shamelessly stolen from:
392 * http://www.flamewarmaster.de/software/dhcpclient/
394 * There are a few other interesting ideas on that page (look under
395 * "Motivation"). Use of netlink events is most interesting. Think
396 * of various network servers listening for events and reconfiguring.
397 * That would obsolete sending HUP signals and/or make use of restarts.
399 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
402 * TODO: make conditional?
404 static const struct sock_filter filter_instr[] = {
406 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
407 /* L5, L1, is UDP? */
408 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
409 /* ugly check for arp on ethernet-like and IPv4 */
410 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
411 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
413 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
414 /* check udp source and destination ports */
415 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
417 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
419 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
420 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
423 static const struct sock_fprog filter_prog = {
424 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
425 /* casting const away: */
426 .filter = (struct sock_filter *) filter_instr,
429 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
433 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
434 /* Use only if standard ports are in use */
435 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
436 sizeof(filter_prog));
438 sock.sll_family = AF_PACKET;
439 sock.sll_protocol = htons(ETH_P_IP);
440 sock.sll_ifindex = ifindex;
442 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
450 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
452 if (packet->ip.protocol != IPPROTO_UDP)
455 if (packet->ip.version != IPVERSION)
458 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
461 if (packet->udp.dest != htons(CLIENT_PORT))
464 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
470 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
473 struct ip_udp_dhcp_packet packet;
476 memset(&packet, 0, sizeof(packet));
478 bytes = read(fd, &packet, sizeof(packet));
482 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
485 if (bytes < ntohs(packet.ip.tot_len))
486 /* packet is bigger than sizeof(packet), we did partial read */
489 /* ignore any extra garbage bytes */
490 bytes = ntohs(packet.ip.tot_len);
492 if (sanity_check(&packet, bytes) == FALSE)
495 check = packet.ip.check;
497 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
500 /* verify UDP checksum. IP header has to be modified for this */
501 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
502 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
503 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
504 check = packet.udp.check;
505 packet.udp.check = 0;
506 if (check && check != dhcp_checksum(&packet, bytes))
509 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
510 sizeof(packet.udp)));
512 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
515 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
518 static gboolean check_package_owner(GDHCPClient *dhcp_client,
519 struct dhcp_packet *packet)
521 if (packet->xid != dhcp_client->xid)
524 if (packet->hlen != 6)
527 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
533 static void start_request(GDHCPClient *dhcp_client);
535 static gboolean request_timeout(gpointer user_data)
537 GDHCPClient *dhcp_client = user_data;
539 debug(dhcp_client, "request timeout (retries %d)",
540 dhcp_client->retry_times);
542 dhcp_client->retry_times++;
544 start_request(dhcp_client);
549 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
552 static int switch_listening_mode(GDHCPClient *dhcp_client,
553 ListenMode listen_mode)
555 GIOChannel *listener_channel;
558 debug(dhcp_client, "switch listening mode (%d ==> %d)",
559 dhcp_client->listen_mode, listen_mode);
561 if (dhcp_client->listen_mode == listen_mode)
564 if (dhcp_client->listen_mode != L_NONE) {
565 g_source_remove(dhcp_client->listener_watch);
566 dhcp_client->listener_channel = NULL;
567 dhcp_client->listen_mode = L_NONE;
568 dhcp_client->listener_sockfd = -1;
569 dhcp_client->listener_watch = 0;
572 if (listen_mode == L_NONE)
575 if (listen_mode == L2)
576 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
577 else if (listen_mode == L3)
578 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
579 dhcp_client->interface);
583 if (listener_sockfd < 0)
586 listener_channel = g_io_channel_unix_new(listener_sockfd);
587 if (listener_channel == NULL) {
588 /* Failed to create listener channel */
589 close(listener_sockfd);
593 dhcp_client->listen_mode = listen_mode;
594 dhcp_client->listener_sockfd = listener_sockfd;
595 dhcp_client->listener_channel = listener_channel;
597 g_io_channel_set_close_on_unref(listener_channel, TRUE);
598 dhcp_client->listener_watch =
599 g_io_add_watch_full(listener_channel,
600 G_PRIORITY_HIGH, G_IO_IN,
601 listener_event, dhcp_client,
603 g_io_channel_unref(dhcp_client->listener_channel);
608 static void start_request(GDHCPClient *dhcp_client)
610 debug(dhcp_client, "start request (retries %d)",
611 dhcp_client->retry_times);
613 if (dhcp_client->retry_times == REQUEST_RETRIES) {
614 dhcp_client->state = INIT_SELECTING;
616 if (dhcp_client->no_lease_cb != NULL)
617 dhcp_client->no_lease_cb(dhcp_client,
618 dhcp_client->no_lease_data);
623 if (dhcp_client->retry_times == 0) {
624 dhcp_client->state = REQUESTING;
625 switch_listening_mode(dhcp_client, L2);
628 send_select(dhcp_client);
630 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
637 static uint32_t get_lease(struct dhcp_packet *packet)
640 uint32_t lease_seconds;
642 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
643 if (option_u8 == NULL)
646 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
647 lease_seconds = ntohl(lease_seconds);
648 /* paranoia: must not be prone to overflows */
649 lease_seconds &= 0x0fffffff;
650 if (lease_seconds < 10)
653 return lease_seconds;
656 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
658 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
660 if (dhcp_client->timeout > 0) {
661 g_source_remove(dhcp_client->timeout);
662 dhcp_client->timeout = 0;
665 dhcp_client->retry_times = retry_times;
666 dhcp_client->requested_ip = 0;
667 switch_listening_mode(dhcp_client, L2);
669 g_dhcp_client_start(dhcp_client);
672 static gboolean start_rebound_timeout(gpointer user_data)
674 GDHCPClient *dhcp_client = user_data;
676 debug(dhcp_client, "start rebound timeout");
678 switch_listening_mode(dhcp_client, L2);
680 dhcp_client->lease_seconds >>= 1;
682 /* We need to have enough time to receive ACK package*/
683 if (dhcp_client->lease_seconds <= 6) {
685 /* ip need to be cleared */
686 if (dhcp_client->lease_lost_cb != NULL)
687 dhcp_client->lease_lost_cb(dhcp_client,
688 dhcp_client->lease_lost_data);
690 restart_dhcp(dhcp_client, 0);
692 send_rebound(dhcp_client);
694 dhcp_client->timeout =
695 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
696 dhcp_client->lease_seconds >> 1,
697 start_rebound_timeout,
705 static void start_rebound(GDHCPClient *dhcp_client)
707 debug(dhcp_client, "start rebound");
709 dhcp_client->state = REBINDING;
711 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
712 dhcp_client->lease_seconds >> 1,
713 start_rebound_timeout,
718 static gboolean start_renew_timeout(gpointer user_data)
720 GDHCPClient *dhcp_client = user_data;
722 debug(dhcp_client, "start renew timeout");
724 dhcp_client->state = RENEWING;
726 dhcp_client->lease_seconds >>= 1;
728 switch_listening_mode(dhcp_client, L3);
729 if (dhcp_client->lease_seconds <= 60)
730 start_rebound(dhcp_client);
732 send_renew(dhcp_client);
734 dhcp_client->timeout =
735 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
736 dhcp_client->lease_seconds >> 1,
745 static void start_bound(GDHCPClient *dhcp_client)
747 debug(dhcp_client, "start bound");
749 dhcp_client->state = BOUND;
751 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
752 dhcp_client->lease_seconds >> 1,
753 start_renew_timeout, dhcp_client,
757 static gboolean restart_dhcp_timeout(gpointer user_data)
759 GDHCPClient *dhcp_client = user_data;
761 debug(dhcp_client, "restart DHCP timeout");
763 dhcp_client->ack_retry_times++;
765 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
770 static char *get_ip(uint32_t ip)
776 return g_strdup(inet_ntoa(addr));
779 /* get a rough idea of how long an option will be */
780 static const uint8_t len_of_option_as_string[] = {
781 [OPTION_IP] = sizeof("255.255.255.255 "),
783 [OPTION_U8] = sizeof("255 "),
784 [OPTION_U16] = sizeof("65535 "),
785 [OPTION_U32] = sizeof("4294967295 "),
788 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
790 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
793 /* Create "opt_value1 option_value2 ..." string */
794 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
796 unsigned upper_length;
800 len = option[OPT_LEN - OPT_DATA];
801 type &= OPTION_TYPE_MASK;
802 optlen = dhcp_option_lengths[type];
805 upper_length = len_of_option_as_string[type] *
806 ((unsigned)len / (unsigned)optlen);
807 dest = ret = malloc(upper_length + 1);
811 while (len >= optlen) {
814 dest += sprint_nip(dest, "", option);
817 uint16_t val_u16 = dhcp_get_unaligned(
818 (uint16_t *) option);
819 dest += sprintf(dest, "%u", ntohs(val_u16));
823 uint32_t val_u32 = dhcp_get_unaligned(
824 (uint32_t *) option);
825 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
826 "%ld", (unsigned long) ntohl(val_u32));
830 memcpy(dest, option, len);
847 static GList *get_option_value_list(char *value)
855 while ((pos = strchr(pos, ' ')) != NULL) {
858 list = g_list_append(list, g_strdup(value));
863 list = g_list_append(list, g_strdup(value));
868 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
870 GDHCPOptionType type;
871 GList *list, *value_list;
876 for (list = dhcp_client->request_list; list; list = list->next) {
877 code = (uint8_t) GPOINTER_TO_INT(list->data);
879 option = dhcp_get_option(packet, code);
880 if (option == NULL) {
881 g_hash_table_remove(dhcp_client->code_value_hash,
882 GINT_TO_POINTER((int) code));
886 type = dhcp_get_code_type(code);
888 option_value = malloc_option_value_string(option, type);
889 if (option_value == NULL)
890 g_hash_table_remove(dhcp_client->code_value_hash,
891 GINT_TO_POINTER((int) code));
893 value_list = get_option_value_list(option_value);
895 g_free(option_value);
897 if (value_list == NULL)
898 g_hash_table_remove(dhcp_client->code_value_hash,
899 GINT_TO_POINTER((int) code));
901 g_hash_table_insert(dhcp_client->code_value_hash,
902 GINT_TO_POINTER((int) code), value_list);
906 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
909 GDHCPClient *dhcp_client = user_data;
910 struct dhcp_packet packet;
911 uint8_t *message_type, *option_u8;
914 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
915 dhcp_client->listener_watch = 0;
919 if (dhcp_client->listen_mode == L_NONE)
922 if (dhcp_client->listen_mode == L2)
923 re = dhcp_recv_l2_packet(&packet, dhcp_client->listener_sockfd);
924 else if (dhcp_client->listen_mode == L3)
925 re = dhcp_recv_l3_packet(&packet, dhcp_client->listener_sockfd);
932 if (check_package_owner(dhcp_client, &packet) == FALSE)
935 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
936 if (message_type == NULL)
937 /* No message type option, ignore pakcage */
940 debug(dhcp_client, "received DHCP packet (current state %d)",
943 switch (dhcp_client->state) {
945 if (*message_type != DHCPOFFER)
948 g_source_remove(dhcp_client->timeout);
949 dhcp_client->timeout = 0;
950 dhcp_client->retry_times = 0;
952 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
953 dhcp_client->server_ip =
954 dhcp_get_unaligned((uint32_t *) option_u8);
955 dhcp_client->requested_ip = packet.yiaddr;
957 dhcp_client->state = REQUESTING;
959 start_request(dhcp_client);
965 if (*message_type == DHCPACK) {
966 dhcp_client->retry_times = 0;
968 if (dhcp_client->timeout > 0)
969 g_source_remove(dhcp_client->timeout);
970 dhcp_client->timeout = 0;
972 dhcp_client->lease_seconds = get_lease(&packet);
974 get_request(dhcp_client, &packet);
976 switch_listening_mode(dhcp_client, L_NONE);
978 g_free(dhcp_client->assigned_ip);
979 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
981 /* Address should be set up here */
982 if (dhcp_client->lease_available_cb != NULL)
983 dhcp_client->lease_available_cb(dhcp_client,
984 dhcp_client->lease_available_data);
986 start_bound(dhcp_client);
987 } else if (*message_type == DHCPNAK) {
988 dhcp_client->retry_times = 0;
990 if (dhcp_client->timeout > 0)
991 g_source_remove(dhcp_client->timeout);
993 dhcp_client->timeout = g_timeout_add_seconds_full(
995 restart_dhcp_timeout,
1005 debug(dhcp_client, "processed DHCP packet (new state %d)",
1006 dhcp_client->state);
1011 static gboolean discover_timeout(gpointer user_data)
1013 GDHCPClient *dhcp_client = user_data;
1015 dhcp_client->retry_times++;
1017 g_dhcp_client_start(dhcp_client);
1022 int g_dhcp_client_start(GDHCPClient *dhcp_client)
1026 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
1027 if (dhcp_client->no_lease_cb != NULL)
1028 dhcp_client->no_lease_cb(dhcp_client,
1029 dhcp_client->no_lease_data);
1034 if (dhcp_client->retry_times == 0) {
1035 g_free(dhcp_client->assigned_ip);
1036 dhcp_client->assigned_ip = NULL;
1038 dhcp_client->state = INIT_SELECTING;
1039 re = switch_listening_mode(dhcp_client, L2);
1043 dhcp_client->xid = rand();
1046 send_discover(dhcp_client, 0);
1048 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1056 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
1058 switch_listening_mode(dhcp_client, L_NONE);
1060 if (dhcp_client->state == BOUND ||
1061 dhcp_client->state == RENEWING ||
1062 dhcp_client->state == REBINDING)
1063 send_release(dhcp_client, dhcp_client->server_ip,
1064 dhcp_client->requested_ip);
1066 if (dhcp_client->timeout > 0) {
1067 g_source_remove(dhcp_client->timeout);
1068 dhcp_client->timeout = 0;
1071 if (dhcp_client->listener_watch > 0) {
1072 g_source_remove(dhcp_client->listener_watch);
1073 dhcp_client->listener_watch = 0;
1076 dhcp_client->listener_channel = NULL;
1078 dhcp_client->retry_times = 0;
1079 dhcp_client->ack_retry_times = 0;
1081 dhcp_client->requested_ip = 0;
1082 dhcp_client->state = RELEASED;
1083 dhcp_client->lease_seconds = 0;
1086 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
1087 unsigned char option_code)
1089 return g_hash_table_lookup(dhcp_client->code_value_hash,
1090 GINT_TO_POINTER((int) option_code));
1093 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
1094 GDHCPClientEvent event,
1095 GDHCPClientEventFunc func,
1099 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
1100 dhcp_client->lease_available_cb = func;
1101 dhcp_client->lease_available_data = data;
1103 case G_DHCP_CLIENT_EVENT_NO_LEASE:
1104 dhcp_client->no_lease_cb = func;
1105 dhcp_client->no_lease_data = data;
1107 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
1108 dhcp_client->lease_lost_cb = func;
1109 dhcp_client->lease_lost_data = data;
1111 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
1112 dhcp_client->address_conflict_cb = func;
1113 dhcp_client->address_conflict_data = data;
1118 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
1120 return dhcp_client->ifindex;
1123 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
1125 return g_strdup(dhcp_client->assigned_ip);
1128 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
1129 unsigned char option_code)
1131 if (g_list_find(dhcp_client->request_list,
1132 GINT_TO_POINTER((int) option_code)) == NULL)
1133 dhcp_client->request_list = g_list_prepend(
1134 dhcp_client->request_list,
1135 (GINT_TO_POINTER((int) option_code)));
1137 return G_DHCP_CLIENT_ERROR_NONE;
1140 static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
1143 int len = strnlen(str, 255);
1145 storage = malloc(len + extra + OPT_DATA);
1146 storage[OPT_CODE] = code;
1147 storage[OPT_LEN] = len + extra;
1148 memcpy(storage + extra + OPT_DATA, str, len);
1153 /* Now only support send hostname */
1154 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
1155 unsigned char option_code, const char *option_value)
1157 uint8_t *binary_option;
1159 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
1160 binary_option = alloc_dhcp_option(option_code,
1163 g_hash_table_insert(dhcp_client->send_value_hash,
1164 GINT_TO_POINTER((int) option_code), binary_option);
1167 return G_DHCP_CLIENT_ERROR_NONE;
1170 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
1172 if (dhcp_client == NULL)
1175 g_atomic_int_inc(&dhcp_client->ref_count);
1180 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
1182 if (dhcp_client == NULL)
1185 if (g_atomic_int_dec_and_test(&dhcp_client->ref_count) == FALSE)
1188 g_dhcp_client_stop(dhcp_client);
1190 g_free(dhcp_client->interface);
1191 g_free(dhcp_client->assigned_ip);
1193 g_list_free(dhcp_client->request_list);
1194 g_list_free(dhcp_client->require_list);
1196 g_hash_table_destroy(dhcp_client->code_value_hash);
1197 g_hash_table_destroy(dhcp_client->send_value_hash);
1199 g_free(dhcp_client);
1202 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
1203 GDHCPDebugFunc func, gpointer user_data)
1205 if (dhcp_client == NULL)
1208 dhcp_client->debug_func = func;
1209 dhcp_client->debug_data = user_data;