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>
36 #include <net/if_arp.h>
39 #include <linux/filter.h>
46 #define DISCOVER_TIMEOUT 3
47 #define DISCOVER_RETRIES 3
49 #define REQUEST_TIMEOUT 3
50 #define REQUEST_RETRIES 3
52 typedef enum _listen_mode {
58 typedef enum _dhcp_client_state {
73 uint8_t mac_address[6];
76 uint32_t requested_ip;
78 uint32_t lease_seconds;
79 ListenMode listen_mode;
82 uint8_t ack_retry_times;
85 GIOChannel *listener_channel;
88 GHashTable *code_value_hash;
89 GHashTable *send_value_hash;
90 GDHCPClientEventFunc lease_available_cb;
91 gpointer lease_available_data;
92 GDHCPClientEventFunc no_lease_cb;
93 gpointer no_lease_data;
94 GDHCPClientEventFunc lease_lost_cb;
95 gpointer lease_lost_data;
96 GDHCPClientEventFunc address_conflict_cb;
97 gpointer address_conflict_data;
98 GDHCPDebugFunc debug_func;
102 static inline void debug(GDHCPClient *client, const char *format, ...)
107 if (client->debug_func == NULL)
110 va_start(ap, format);
112 if (vsnprintf(str, sizeof(str), format, ap) > 0)
113 client->debug_func(str, client->debug_data);
118 /* Initialize the packet with the proper defaults */
119 static void init_packet(GDHCPClient *dhcp_client,
120 struct dhcp_packet *packet, char type)
122 dhcp_init_header(packet, type);
124 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
127 static void add_request_options(GDHCPClient *dhcp_client,
128 struct dhcp_packet *packet)
133 int end = dhcp_end_option(packet->options);
135 for (list = dhcp_client->request_list; list; list = list->next) {
136 code = (uint8_t) GPOINTER_TO_INT(list->data);
138 packet->options[end + OPT_DATA + len] = code;
143 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
144 packet->options[end + OPT_LEN] = len;
145 packet->options[end + OPT_DATA + len] = DHCP_END;
149 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
151 uint8_t *option = value;
152 struct dhcp_packet *packet = user_data;
154 dhcp_add_binary_option(packet, option);
157 static void add_send_options(GDHCPClient *dhcp_client,
158 struct dhcp_packet *packet)
160 g_hash_table_foreach(dhcp_client->send_value_hash,
161 add_binary_option, packet);
164 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
166 struct dhcp_packet packet;
168 debug(dhcp_client, "sending DHCP discover request");
170 init_packet(dhcp_client, &packet, DHCPDISCOVER);
172 packet.xid = dhcp_client->xid;
175 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
177 /* Explicitly saying that we want RFC-compliant packets helps
178 * some buggy DHCP servers to NOT send bigger packets */
179 dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
181 add_request_options(dhcp_client, &packet);
183 add_send_options(dhcp_client, &packet);
185 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
186 INADDR_BROADCAST, SERVER_PORT,
187 MAC_BCAST_ADDR, dhcp_client->ifindex);
190 static int send_select(GDHCPClient *dhcp_client)
192 struct dhcp_packet packet;
195 debug(dhcp_client, "sending DHCP select request");
197 init_packet(dhcp_client, &packet, DHCPREQUEST);
199 packet.xid = dhcp_client->xid;
201 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
202 dhcp_client->requested_ip);
203 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
205 add_request_options(dhcp_client, &packet);
207 add_send_options(dhcp_client, &packet);
209 addr.s_addr = dhcp_client->requested_ip;
211 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
212 INADDR_BROADCAST, SERVER_PORT,
213 MAC_BCAST_ADDR, dhcp_client->ifindex);
216 static int send_renew(GDHCPClient *dhcp_client)
218 struct dhcp_packet packet;
220 debug(dhcp_client, "sending DHCP renew request");
222 init_packet(dhcp_client , &packet, DHCPREQUEST);
223 packet.xid = dhcp_client->xid;
224 packet.ciaddr = dhcp_client->requested_ip;
226 add_request_options(dhcp_client, &packet);
228 add_send_options(dhcp_client, &packet);
230 return dhcp_send_kernel_packet(&packet,
231 dhcp_client->requested_ip, CLIENT_PORT,
232 dhcp_client->server_ip, SERVER_PORT);
235 static int send_rebound(GDHCPClient *dhcp_client)
237 struct dhcp_packet packet;
239 debug(dhcp_client, "sending DHCP rebound request");
241 init_packet(dhcp_client , &packet, DHCPREQUEST);
242 packet.xid = dhcp_client->xid;
243 packet.ciaddr = dhcp_client->requested_ip;
245 add_request_options(dhcp_client, &packet);
247 add_send_options(dhcp_client, &packet);
249 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
250 INADDR_BROADCAST, SERVER_PORT,
251 MAC_BCAST_ADDR, dhcp_client->ifindex);
254 static int send_release(GDHCPClient *dhcp_client,
255 uint32_t server, uint32_t ciaddr)
257 struct dhcp_packet packet;
259 debug(dhcp_client, "sending DHCP release request");
261 init_packet(dhcp_client, &packet, DHCPRELEASE);
263 packet.ciaddr = ciaddr;
265 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
267 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
268 server, SERVER_PORT);
271 static gboolean interface_is_up(int index)
275 gboolean ret = FALSE;
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, SIOCGIFFLAGS, &ifr);
294 perror("Get interface flags error");
298 if (ifr.ifr_flags & IFF_UP)
307 static char *get_interface_name(int index)
315 sk = socket(PF_INET, SOCK_DGRAM, 0);
317 perror("Open socket error");
321 memset(&ifr, 0, sizeof(ifr));
322 ifr.ifr_ifindex = index;
324 err = ioctl(sk, SIOCGIFNAME, &ifr);
326 perror("Get interface name error");
333 return g_strdup(ifr.ifr_name);
336 static void get_interface_mac_address(int index, uint8_t *mac_address)
341 sk = socket(PF_INET, SOCK_DGRAM, 0);
343 perror("Open socket error");
347 memset(&ifr, 0, sizeof(ifr));
348 ifr.ifr_ifindex = index;
350 err = ioctl(sk, SIOCGIFNAME, &ifr);
352 perror("Get interface name error");
356 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
358 perror("Get mac address error");
362 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
368 static void remove_value(gpointer data, gpointer user_data)
374 static void remove_option_value(gpointer data)
376 GList *option_value = data;
378 g_list_foreach(option_value, remove_value, NULL);
381 GDHCPClient *g_dhcp_client_new(GDHCPType type,
382 int ifindex, GDHCPClientError *error)
384 GDHCPClient *dhcp_client;
387 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
391 dhcp_client = g_try_new0(GDHCPClient, 1);
392 if (dhcp_client == NULL) {
393 *error = G_DHCP_CLIENT_ERROR_NOMEM;
397 dhcp_client->interface = get_interface_name(ifindex);
398 if (dhcp_client->interface == NULL) {
399 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
403 if (interface_is_up(ifindex) == FALSE) {
404 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
408 get_interface_mac_address(ifindex, dhcp_client->mac_address);
410 dhcp_client->listener_sockfd = -1;
411 dhcp_client->listener_channel = NULL;
412 dhcp_client->listen_mode = L_NONE;
413 dhcp_client->ref_count = 1;
414 dhcp_client->type = type;
415 dhcp_client->ifindex = ifindex;
416 dhcp_client->lease_available_cb = NULL;
417 dhcp_client->no_lease_cb = NULL;
418 dhcp_client->lease_lost_cb = NULL;
419 dhcp_client->address_conflict_cb = NULL;
420 dhcp_client->listener_watch = 0;
421 dhcp_client->retry_times = 0;
422 dhcp_client->ack_retry_times = 0;
423 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
424 g_direct_equal, NULL, remove_option_value);
425 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
426 g_direct_equal, NULL, g_free);
427 dhcp_client->request_list = NULL;
428 dhcp_client->require_list = NULL;
430 *error = G_DHCP_CLIENT_ERROR_NONE;
435 g_free(dhcp_client->interface);
440 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
442 static int dhcp_l2_socket(int ifindex)
445 struct sockaddr_ll sock;
450 * I've selected not to see LL header, so BPF doesn't see it, too.
451 * The filter may also pass non-IP and non-ARP packets, but we do
452 * a more complete check when receiving the message in userspace.
454 * and filter shamelessly stolen from:
456 * http://www.flamewarmaster.de/software/dhcpclient/
458 * There are a few other interesting ideas on that page (look under
459 * "Motivation"). Use of netlink events is most interesting. Think
460 * of various network servers listening for events and reconfiguring.
461 * That would obsolete sending HUP signals and/or make use of restarts.
463 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
466 * TODO: make conditional?
468 static const struct sock_filter filter_instr[] = {
470 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
471 /* L5, L1, is UDP? */
472 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
473 /* ugly check for arp on ethernet-like and IPv4 */
474 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
475 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
477 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
478 /* check udp source and destination ports */
479 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
481 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
483 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
484 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
487 static const struct sock_fprog filter_prog = {
488 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
489 /* casting const away: */
490 .filter = (struct sock_filter *) filter_instr,
493 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
497 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
498 /* Use only if standard ports are in use */
499 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
500 sizeof(filter_prog));
502 sock.sll_family = AF_PACKET;
503 sock.sll_protocol = htons(ETH_P_IP);
504 sock.sll_ifindex = ifindex;
506 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
514 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
516 if (packet->ip.protocol != IPPROTO_UDP)
519 if (packet->ip.version != IPVERSION)
522 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
525 if (packet->udp.dest != htons(CLIENT_PORT))
528 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
534 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
537 struct ip_udp_dhcp_packet packet;
540 memset(&packet, 0, sizeof(packet));
542 bytes = read(fd, &packet, sizeof(packet));
546 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
549 if (bytes < ntohs(packet.ip.tot_len))
550 /* packet is bigger than sizeof(packet), we did partial read */
553 /* ignore any extra garbage bytes */
554 bytes = ntohs(packet.ip.tot_len);
556 if (sanity_check(&packet, bytes) == FALSE)
559 check = packet.ip.check;
561 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
564 /* verify UDP checksum. IP header has to be modified for this */
565 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
566 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
567 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
568 check = packet.udp.check;
569 packet.udp.check = 0;
570 if (check && check != dhcp_checksum(&packet, bytes))
573 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
574 sizeof(packet.udp)));
576 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
579 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
582 static gboolean check_package_owner(GDHCPClient *dhcp_client,
583 struct dhcp_packet *packet)
585 if (packet->xid != dhcp_client->xid)
588 if (packet->hlen != 6)
591 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
597 static void start_request(GDHCPClient *dhcp_client);
599 static gboolean request_timeout(gpointer user_data)
601 GDHCPClient *dhcp_client = user_data;
603 dhcp_client->retry_times++;
605 start_request(dhcp_client);
610 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
613 static int switch_listening_mode(GDHCPClient *dhcp_client,
614 ListenMode listen_mode)
616 GIOChannel *listener_channel;
619 if (dhcp_client->listen_mode == listen_mode)
622 if (dhcp_client->listen_mode != L_NONE) {
623 g_source_remove(dhcp_client->listener_watch);
624 dhcp_client->listener_channel = NULL;
625 dhcp_client->listen_mode = L_NONE;
626 dhcp_client->listener_sockfd = -1;
627 dhcp_client->listener_watch = 0;
630 if (listen_mode == L_NONE)
633 if (listen_mode == L2)
634 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
635 else if (listen_mode == L3)
636 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
637 dhcp_client->interface);
641 if (listener_sockfd < 0)
644 listener_channel = g_io_channel_unix_new(listener_sockfd);
645 if (listener_channel == NULL) {
646 /* Failed to create listener channel */
647 close(listener_sockfd);
651 dhcp_client->listen_mode = listen_mode;
652 dhcp_client->listener_sockfd = listener_sockfd;
653 dhcp_client->listener_channel = listener_channel;
655 g_io_channel_set_close_on_unref(listener_channel, TRUE);
656 dhcp_client->listener_watch =
657 g_io_add_watch_full(listener_channel,
658 G_PRIORITY_HIGH, G_IO_IN,
659 listener_event, dhcp_client,
661 g_io_channel_unref(dhcp_client->listener_channel);
666 static void start_request(GDHCPClient *dhcp_client)
668 if (dhcp_client->retry_times == REQUEST_RETRIES) {
669 dhcp_client->state = INIT_SELECTING;
671 if (dhcp_client->no_lease_cb != NULL)
672 dhcp_client->no_lease_cb(dhcp_client,
673 dhcp_client->no_lease_data);
678 if (dhcp_client->retry_times == 0) {
679 dhcp_client->state = REQUESTING;
680 switch_listening_mode(dhcp_client, L2);
683 send_select(dhcp_client);
685 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
692 static uint32_t get_lease(struct dhcp_packet *packet)
695 uint32_t lease_seconds;
697 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
698 if (option_u8 == NULL)
701 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
702 lease_seconds = ntohl(lease_seconds);
703 /* paranoia: must not be prone to overflows */
704 lease_seconds &= 0x0fffffff;
705 if (lease_seconds < 10)
708 return lease_seconds;
711 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
713 if (dhcp_client->timeout > 0) {
714 g_source_remove(dhcp_client->timeout);
715 dhcp_client->timeout = 0;
718 dhcp_client->retry_times = retry_times;
719 dhcp_client->requested_ip = 0;
720 switch_listening_mode(dhcp_client, L2);
722 g_dhcp_client_start(dhcp_client);
725 static gboolean start_rebound_timeout(gpointer user_data)
727 GDHCPClient *dhcp_client = user_data;
729 switch_listening_mode(dhcp_client, L2);
731 dhcp_client->lease_seconds >>= 1;
733 /* We need to have enough time to receive ACK package*/
734 if (dhcp_client->lease_seconds <= 6) {
736 /* ip need to be cleared */
737 if (dhcp_client->lease_lost_cb != NULL)
738 dhcp_client->lease_lost_cb(dhcp_client,
739 dhcp_client->lease_lost_data);
741 restart_dhcp(dhcp_client, 0);
743 send_rebound(dhcp_client);
745 dhcp_client->timeout =
746 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
747 dhcp_client->lease_seconds >> 1,
748 start_rebound_timeout,
756 static void start_rebound(GDHCPClient *dhcp_client)
758 dhcp_client->state = REBINDING;
760 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
761 dhcp_client->lease_seconds >> 1,
762 start_rebound_timeout,
767 static gboolean start_renew_timeout(gpointer user_data)
769 GDHCPClient *dhcp_client = user_data;
771 dhcp_client->state = RENEWING;
773 dhcp_client->lease_seconds >>= 1;
775 switch_listening_mode(dhcp_client, L3);
776 if (dhcp_client->lease_seconds <= 60)
777 start_rebound(dhcp_client);
779 send_renew(dhcp_client);
781 dhcp_client->timeout =
782 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
783 dhcp_client->lease_seconds >> 1,
792 static void start_bound(GDHCPClient *dhcp_client)
794 dhcp_client->state = BOUND;
796 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
797 dhcp_client->lease_seconds >> 1,
798 start_renew_timeout, dhcp_client,
802 static gboolean restart_dhcp_timeout(gpointer user_data)
804 GDHCPClient *dhcp_client = user_data;
806 dhcp_client->ack_retry_times++;
808 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
813 static char *get_ip(uint32_t ip)
819 return g_strdup(inet_ntoa(addr));
822 /* get a rough idea of how long an option will be */
823 static const uint8_t len_of_option_as_string[] = {
824 [OPTION_IP] = sizeof("255.255.255.255 "),
826 [OPTION_U8] = sizeof("255 "),
827 [OPTION_U16] = sizeof("65535 "),
828 [OPTION_U32] = sizeof("4294967295 "),
831 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
833 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
836 /* Create "opt_value1 option_value2 ..." string */
837 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
839 unsigned upper_length;
843 len = option[OPT_LEN - OPT_DATA];
844 type &= OPTION_TYPE_MASK;
845 optlen = dhcp_option_lengths[type];
848 upper_length = len_of_option_as_string[type] *
849 ((unsigned)len / (unsigned)optlen);
850 dest = ret = malloc(upper_length + 1);
854 while (len >= optlen) {
857 dest += sprint_nip(dest, "", option);
860 uint16_t val_u16 = dhcp_get_unaligned(
861 (uint16_t *) option);
862 dest += sprintf(dest, "%u", ntohs(val_u16));
866 uint32_t val_u32 = dhcp_get_unaligned(
867 (uint32_t *) option);
868 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
869 "%ld", (unsigned long) ntohl(val_u32));
873 memcpy(dest, option, len);
890 static GList *get_option_value_list(char *value)
895 while ((pos = strchr(pos, ' ')) != NULL) {
898 list = g_list_append(list, g_strdup(value));
903 list = g_list_append(list, g_strdup(value));
908 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
910 GDHCPOptionType type;
911 GList *list, *value_list;
916 for (list = dhcp_client->request_list; list; list = list->next) {
917 code = (uint8_t) GPOINTER_TO_INT(list->data);
919 option = dhcp_get_option(packet, code);
920 if (option == NULL) {
921 g_hash_table_remove(dhcp_client->code_value_hash,
922 GINT_TO_POINTER((int) code));
926 type = dhcp_get_code_type(code);
928 option_value = malloc_option_value_string(option, type);
929 if (option_value == NULL)
930 g_hash_table_remove(dhcp_client->code_value_hash,
931 GINT_TO_POINTER((int) code));
933 value_list = get_option_value_list(option_value);
935 g_free(option_value);
937 if (value_list == NULL)
938 g_hash_table_remove(dhcp_client->code_value_hash,
939 GINT_TO_POINTER((int) code));
941 g_hash_table_insert(dhcp_client->code_value_hash,
942 GINT_TO_POINTER((int) code), value_list);
946 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
949 GDHCPClient *dhcp_client = user_data;
950 struct dhcp_packet packet;
951 uint8_t *message_type, *option_u8;
954 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
955 dhcp_client->listener_watch = 0;
959 if (dhcp_client->listen_mode == L_NONE)
962 if (dhcp_client->listen_mode == L2)
963 re = dhcp_recv_l2_packet(&packet, dhcp_client->listener_sockfd);
964 else if (dhcp_client->listen_mode == L3)
965 re = dhcp_recv_l3_packet(&packet, dhcp_client->listener_sockfd);
972 if (check_package_owner(dhcp_client, &packet) == FALSE)
975 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
976 if (message_type == NULL)
977 /* No message type option, ignore pakcage */
980 switch (dhcp_client->state) {
982 if (*message_type != DHCPOFFER)
985 g_source_remove(dhcp_client->timeout);
986 dhcp_client->timeout = 0;
987 dhcp_client->retry_times = 0;
989 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
990 dhcp_client->server_ip =
991 dhcp_get_unaligned((uint32_t *) option_u8);
992 dhcp_client->requested_ip = packet.yiaddr;
994 dhcp_client->state = REQUESTING;
996 start_request(dhcp_client);
1002 if (*message_type == DHCPACK) {
1003 dhcp_client->retry_times = 0;
1005 if (dhcp_client->timeout > 0)
1006 g_source_remove(dhcp_client->timeout);
1007 dhcp_client->timeout = 0;
1009 dhcp_client->lease_seconds = get_lease(&packet);
1011 get_request(dhcp_client, &packet);
1013 switch_listening_mode(dhcp_client, L_NONE);
1015 g_free(dhcp_client->assigned_ip);
1016 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1018 /* Address should be set up here */
1019 if (dhcp_client->lease_available_cb != NULL)
1020 dhcp_client->lease_available_cb(dhcp_client,
1021 dhcp_client->lease_available_data);
1023 start_bound(dhcp_client);
1024 } else if (*message_type == DHCPNAK) {
1025 dhcp_client->retry_times = 0;
1027 if (dhcp_client->timeout > 0)
1028 g_source_remove(dhcp_client->timeout);
1030 dhcp_client->timeout = g_timeout_add_seconds_full(
1032 restart_dhcp_timeout,
1045 static gboolean discover_timeout(gpointer user_data)
1047 GDHCPClient *dhcp_client = user_data;
1049 dhcp_client->retry_times++;
1051 g_dhcp_client_start(dhcp_client);
1056 int g_dhcp_client_start(GDHCPClient *dhcp_client)
1060 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
1061 if (dhcp_client->no_lease_cb != NULL)
1062 dhcp_client->no_lease_cb(dhcp_client,
1063 dhcp_client->no_lease_data);
1068 if (dhcp_client->retry_times == 0) {
1069 g_free(dhcp_client->assigned_ip);
1070 dhcp_client->assigned_ip = NULL;
1072 dhcp_client->state = INIT_SELECTING;
1073 re = switch_listening_mode(dhcp_client, L2);
1077 dhcp_client->xid = rand();
1080 send_discover(dhcp_client, 0);
1082 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1090 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
1092 switch_listening_mode(dhcp_client, L_NONE);
1094 if (dhcp_client->state == BOUND ||
1095 dhcp_client->state == RENEWING ||
1096 dhcp_client->state == REBINDING)
1097 send_release(dhcp_client, dhcp_client->server_ip,
1098 dhcp_client->requested_ip);
1100 if (dhcp_client->timeout > 0) {
1101 g_source_remove(dhcp_client->timeout);
1102 dhcp_client->timeout = 0;
1105 if (dhcp_client->listener_watch > 0) {
1106 g_source_remove(dhcp_client->listener_watch);
1107 dhcp_client->listener_watch = 0;
1110 dhcp_client->listener_channel = NULL;
1112 dhcp_client->retry_times = 0;
1113 dhcp_client->ack_retry_times = 0;
1115 dhcp_client->requested_ip = 0;
1116 dhcp_client->state = RELEASED;
1117 dhcp_client->lease_seconds = 0;
1120 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
1121 unsigned char option_code)
1123 return g_hash_table_lookup(dhcp_client->code_value_hash,
1124 GINT_TO_POINTER((int) option_code));
1127 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
1128 GDHCPClientEvent event,
1129 GDHCPClientEventFunc func,
1133 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
1134 dhcp_client->lease_available_cb = func;
1135 dhcp_client->lease_available_data = data;
1137 case G_DHCP_CLIENT_EVENT_NO_LEASE:
1138 dhcp_client->no_lease_cb = func;
1139 dhcp_client->no_lease_data = data;
1141 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
1142 dhcp_client->lease_lost_cb = func;
1143 dhcp_client->lease_lost_data = data;
1145 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
1146 dhcp_client->address_conflict_cb = func;
1147 dhcp_client->address_conflict_data = data;
1152 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
1154 return dhcp_client->ifindex;
1157 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
1159 return g_strdup(dhcp_client->assigned_ip);
1162 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
1163 unsigned char option_code)
1165 if (g_list_find(dhcp_client->request_list,
1166 GINT_TO_POINTER((int) option_code)) == NULL)
1167 dhcp_client->request_list = g_list_prepend(
1168 dhcp_client->request_list,
1169 (GINT_TO_POINTER((int) option_code)));
1171 return G_DHCP_CLIENT_ERROR_NONE;
1174 static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
1177 int len = strnlen(str, 255);
1179 storage = malloc(len + extra + OPT_DATA);
1180 storage[OPT_CODE] = code;
1181 storage[OPT_LEN] = len + extra;
1182 memcpy(storage + extra + OPT_DATA, str, len);
1187 static const char *get_hostname(const char *host)
1189 char local_host_name[HOST_NAME_MAX + 1];
1191 if (g_strcmp0("<hostname>", host) != 0)
1192 return g_strdup(host);
1194 if (gethostname(local_host_name, HOST_NAME_MAX) != 0)
1197 local_host_name[HOST_NAME_MAX] = 0;
1199 return g_strdup(local_host_name);
1202 /* Now only support send hostname */
1203 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
1204 unsigned char option_code, const char *option_value)
1206 uint8_t *binary_option;
1207 const char *hostname;
1209 if (option_code == G_DHCP_HOST_NAME) {
1210 hostname = get_hostname(option_value);
1212 binary_option = alloc_dhcp_option(option_code, hostname, 0);
1214 g_hash_table_insert(dhcp_client->send_value_hash,
1215 GINT_TO_POINTER((int) option_code), binary_option);
1218 return G_DHCP_CLIENT_ERROR_NONE;
1221 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
1223 if (dhcp_client == NULL)
1226 g_atomic_int_inc(&dhcp_client->ref_count);
1231 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
1233 if (dhcp_client == NULL)
1236 if (g_atomic_int_dec_and_test(&dhcp_client->ref_count) == FALSE)
1239 g_dhcp_client_stop(dhcp_client);
1241 g_free(dhcp_client->interface);
1242 g_free(dhcp_client->assigned_ip);
1244 g_list_free(dhcp_client->request_list);
1245 g_list_free(dhcp_client->require_list);
1247 g_hash_table_destroy(dhcp_client->code_value_hash);
1248 g_hash_table_destroy(dhcp_client->send_value_hash);
1250 g_free(dhcp_client);
1253 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
1254 GDHCPDebugFunc func, gpointer user_data)
1256 if (dhcp_client == NULL)
1259 dhcp_client->debug_func = func;
1260 dhcp_client->debug_data = user_data;