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 5
49 #define REQUEST_TIMEOUT 3
50 #define REQUEST_RETRIES 5
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 debug(dhcp_client, "request timeout (retries %d)",
604 dhcp_client->retry_times);
606 dhcp_client->retry_times++;
608 start_request(dhcp_client);
613 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
616 static int switch_listening_mode(GDHCPClient *dhcp_client,
617 ListenMode listen_mode)
619 GIOChannel *listener_channel;
622 debug(dhcp_client, "switch listening mode (%d ==> %d)",
623 dhcp_client->listen_mode, listen_mode);
625 if (dhcp_client->listen_mode == listen_mode)
628 if (dhcp_client->listen_mode != L_NONE) {
629 g_source_remove(dhcp_client->listener_watch);
630 dhcp_client->listener_channel = NULL;
631 dhcp_client->listen_mode = L_NONE;
632 dhcp_client->listener_sockfd = -1;
633 dhcp_client->listener_watch = 0;
636 if (listen_mode == L_NONE)
639 if (listen_mode == L2)
640 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
641 else if (listen_mode == L3)
642 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
643 dhcp_client->interface);
647 if (listener_sockfd < 0)
650 listener_channel = g_io_channel_unix_new(listener_sockfd);
651 if (listener_channel == NULL) {
652 /* Failed to create listener channel */
653 close(listener_sockfd);
657 dhcp_client->listen_mode = listen_mode;
658 dhcp_client->listener_sockfd = listener_sockfd;
659 dhcp_client->listener_channel = listener_channel;
661 g_io_channel_set_close_on_unref(listener_channel, TRUE);
662 dhcp_client->listener_watch =
663 g_io_add_watch_full(listener_channel,
664 G_PRIORITY_HIGH, G_IO_IN,
665 listener_event, dhcp_client,
667 g_io_channel_unref(dhcp_client->listener_channel);
672 static void start_request(GDHCPClient *dhcp_client)
674 debug(dhcp_client, "start request (retries %d)",
675 dhcp_client->retry_times);
677 if (dhcp_client->retry_times == REQUEST_RETRIES) {
678 dhcp_client->state = INIT_SELECTING;
680 if (dhcp_client->no_lease_cb != NULL)
681 dhcp_client->no_lease_cb(dhcp_client,
682 dhcp_client->no_lease_data);
687 if (dhcp_client->retry_times == 0) {
688 dhcp_client->state = REQUESTING;
689 switch_listening_mode(dhcp_client, L2);
692 send_select(dhcp_client);
694 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
701 static uint32_t get_lease(struct dhcp_packet *packet)
704 uint32_t lease_seconds;
706 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
707 if (option_u8 == NULL)
710 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
711 lease_seconds = ntohl(lease_seconds);
712 /* paranoia: must not be prone to overflows */
713 lease_seconds &= 0x0fffffff;
714 if (lease_seconds < 10)
717 return lease_seconds;
720 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
722 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
724 if (dhcp_client->timeout > 0) {
725 g_source_remove(dhcp_client->timeout);
726 dhcp_client->timeout = 0;
729 dhcp_client->retry_times = retry_times;
730 dhcp_client->requested_ip = 0;
731 switch_listening_mode(dhcp_client, L2);
733 g_dhcp_client_start(dhcp_client);
736 static gboolean start_rebound_timeout(gpointer user_data)
738 GDHCPClient *dhcp_client = user_data;
740 debug(dhcp_client, "start rebound timeout");
742 switch_listening_mode(dhcp_client, L2);
744 dhcp_client->lease_seconds >>= 1;
746 /* We need to have enough time to receive ACK package*/
747 if (dhcp_client->lease_seconds <= 6) {
749 /* ip need to be cleared */
750 if (dhcp_client->lease_lost_cb != NULL)
751 dhcp_client->lease_lost_cb(dhcp_client,
752 dhcp_client->lease_lost_data);
754 restart_dhcp(dhcp_client, 0);
756 send_rebound(dhcp_client);
758 dhcp_client->timeout =
759 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
760 dhcp_client->lease_seconds >> 1,
761 start_rebound_timeout,
769 static void start_rebound(GDHCPClient *dhcp_client)
771 debug(dhcp_client, "start rebound");
773 dhcp_client->state = REBINDING;
775 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
776 dhcp_client->lease_seconds >> 1,
777 start_rebound_timeout,
782 static gboolean start_renew_timeout(gpointer user_data)
784 GDHCPClient *dhcp_client = user_data;
786 debug(dhcp_client, "start renew timeout");
788 dhcp_client->state = RENEWING;
790 dhcp_client->lease_seconds >>= 1;
792 switch_listening_mode(dhcp_client, L3);
793 if (dhcp_client->lease_seconds <= 60)
794 start_rebound(dhcp_client);
796 send_renew(dhcp_client);
798 dhcp_client->timeout =
799 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
800 dhcp_client->lease_seconds >> 1,
809 static void start_bound(GDHCPClient *dhcp_client)
811 debug(dhcp_client, "start bound");
813 dhcp_client->state = BOUND;
815 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
816 dhcp_client->lease_seconds >> 1,
817 start_renew_timeout, dhcp_client,
821 static gboolean restart_dhcp_timeout(gpointer user_data)
823 GDHCPClient *dhcp_client = user_data;
825 debug(dhcp_client, "restart DHCP timeout");
827 dhcp_client->ack_retry_times++;
829 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
834 static char *get_ip(uint32_t ip)
840 return g_strdup(inet_ntoa(addr));
843 /* get a rough idea of how long an option will be */
844 static const uint8_t len_of_option_as_string[] = {
845 [OPTION_IP] = sizeof("255.255.255.255 "),
847 [OPTION_U8] = sizeof("255 "),
848 [OPTION_U16] = sizeof("65535 "),
849 [OPTION_U32] = sizeof("4294967295 "),
852 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
854 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
857 /* Create "opt_value1 option_value2 ..." string */
858 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
860 unsigned upper_length;
864 len = option[OPT_LEN - OPT_DATA];
865 type &= OPTION_TYPE_MASK;
866 optlen = dhcp_option_lengths[type];
869 upper_length = len_of_option_as_string[type] *
870 ((unsigned)len / (unsigned)optlen);
871 dest = ret = malloc(upper_length + 1);
875 while (len >= optlen) {
878 dest += sprint_nip(dest, "", option);
881 uint16_t val_u16 = dhcp_get_unaligned(
882 (uint16_t *) option);
883 dest += sprintf(dest, "%u", ntohs(val_u16));
887 uint32_t val_u32 = dhcp_get_unaligned(
888 (uint32_t *) option);
889 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
890 "%ld", (unsigned long) ntohl(val_u32));
894 memcpy(dest, option, len);
911 static GList *get_option_value_list(char *value)
919 while ((pos = strchr(pos, ' ')) != NULL) {
922 list = g_list_append(list, g_strdup(value));
927 list = g_list_append(list, g_strdup(value));
932 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
934 GDHCPOptionType type;
935 GList *list, *value_list;
940 for (list = dhcp_client->request_list; list; list = list->next) {
941 code = (uint8_t) GPOINTER_TO_INT(list->data);
943 option = dhcp_get_option(packet, code);
944 if (option == NULL) {
945 g_hash_table_remove(dhcp_client->code_value_hash,
946 GINT_TO_POINTER((int) code));
950 type = dhcp_get_code_type(code);
952 option_value = malloc_option_value_string(option, type);
953 if (option_value == NULL)
954 g_hash_table_remove(dhcp_client->code_value_hash,
955 GINT_TO_POINTER((int) code));
957 value_list = get_option_value_list(option_value);
959 g_free(option_value);
961 if (value_list == NULL)
962 g_hash_table_remove(dhcp_client->code_value_hash,
963 GINT_TO_POINTER((int) code));
965 g_hash_table_insert(dhcp_client->code_value_hash,
966 GINT_TO_POINTER((int) code), value_list);
970 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
973 GDHCPClient *dhcp_client = user_data;
974 struct dhcp_packet packet;
975 uint8_t *message_type, *option_u8;
978 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
979 dhcp_client->listener_watch = 0;
983 if (dhcp_client->listen_mode == L_NONE)
986 if (dhcp_client->listen_mode == L2)
987 re = dhcp_recv_l2_packet(&packet, dhcp_client->listener_sockfd);
988 else if (dhcp_client->listen_mode == L3)
989 re = dhcp_recv_l3_packet(&packet, dhcp_client->listener_sockfd);
996 if (check_package_owner(dhcp_client, &packet) == FALSE)
999 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1000 if (message_type == NULL)
1001 /* No message type option, ignore pakcage */
1004 debug(dhcp_client, "received DHCP packet (current state %d)",
1005 dhcp_client->state);
1007 switch (dhcp_client->state) {
1008 case INIT_SELECTING:
1009 if (*message_type != DHCPOFFER)
1012 g_source_remove(dhcp_client->timeout);
1013 dhcp_client->timeout = 0;
1014 dhcp_client->retry_times = 0;
1016 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
1017 dhcp_client->server_ip =
1018 dhcp_get_unaligned((uint32_t *) option_u8);
1019 dhcp_client->requested_ip = packet.yiaddr;
1021 dhcp_client->state = REQUESTING;
1023 start_request(dhcp_client);
1029 if (*message_type == DHCPACK) {
1030 dhcp_client->retry_times = 0;
1032 if (dhcp_client->timeout > 0)
1033 g_source_remove(dhcp_client->timeout);
1034 dhcp_client->timeout = 0;
1036 dhcp_client->lease_seconds = get_lease(&packet);
1038 get_request(dhcp_client, &packet);
1040 switch_listening_mode(dhcp_client, L_NONE);
1042 g_free(dhcp_client->assigned_ip);
1043 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1045 /* Address should be set up here */
1046 if (dhcp_client->lease_available_cb != NULL)
1047 dhcp_client->lease_available_cb(dhcp_client,
1048 dhcp_client->lease_available_data);
1050 start_bound(dhcp_client);
1051 } else if (*message_type == DHCPNAK) {
1052 dhcp_client->retry_times = 0;
1054 if (dhcp_client->timeout > 0)
1055 g_source_remove(dhcp_client->timeout);
1057 dhcp_client->timeout = g_timeout_add_seconds_full(
1059 restart_dhcp_timeout,
1069 debug(dhcp_client, "processed DHCP packet (new state %d)",
1070 dhcp_client->state);
1075 static gboolean discover_timeout(gpointer user_data)
1077 GDHCPClient *dhcp_client = user_data;
1079 dhcp_client->retry_times++;
1081 g_dhcp_client_start(dhcp_client);
1086 int g_dhcp_client_start(GDHCPClient *dhcp_client)
1090 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
1091 if (dhcp_client->no_lease_cb != NULL)
1092 dhcp_client->no_lease_cb(dhcp_client,
1093 dhcp_client->no_lease_data);
1098 if (dhcp_client->retry_times == 0) {
1099 g_free(dhcp_client->assigned_ip);
1100 dhcp_client->assigned_ip = NULL;
1102 dhcp_client->state = INIT_SELECTING;
1103 re = switch_listening_mode(dhcp_client, L2);
1107 dhcp_client->xid = rand();
1110 send_discover(dhcp_client, 0);
1112 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1120 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
1122 switch_listening_mode(dhcp_client, L_NONE);
1124 if (dhcp_client->state == BOUND ||
1125 dhcp_client->state == RENEWING ||
1126 dhcp_client->state == REBINDING)
1127 send_release(dhcp_client, dhcp_client->server_ip,
1128 dhcp_client->requested_ip);
1130 if (dhcp_client->timeout > 0) {
1131 g_source_remove(dhcp_client->timeout);
1132 dhcp_client->timeout = 0;
1135 if (dhcp_client->listener_watch > 0) {
1136 g_source_remove(dhcp_client->listener_watch);
1137 dhcp_client->listener_watch = 0;
1140 dhcp_client->listener_channel = NULL;
1142 dhcp_client->retry_times = 0;
1143 dhcp_client->ack_retry_times = 0;
1145 dhcp_client->requested_ip = 0;
1146 dhcp_client->state = RELEASED;
1147 dhcp_client->lease_seconds = 0;
1150 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
1151 unsigned char option_code)
1153 return g_hash_table_lookup(dhcp_client->code_value_hash,
1154 GINT_TO_POINTER((int) option_code));
1157 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
1158 GDHCPClientEvent event,
1159 GDHCPClientEventFunc func,
1163 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
1164 dhcp_client->lease_available_cb = func;
1165 dhcp_client->lease_available_data = data;
1167 case G_DHCP_CLIENT_EVENT_NO_LEASE:
1168 dhcp_client->no_lease_cb = func;
1169 dhcp_client->no_lease_data = data;
1171 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
1172 dhcp_client->lease_lost_cb = func;
1173 dhcp_client->lease_lost_data = data;
1175 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
1176 dhcp_client->address_conflict_cb = func;
1177 dhcp_client->address_conflict_data = data;
1182 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
1184 return dhcp_client->ifindex;
1187 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
1189 return g_strdup(dhcp_client->assigned_ip);
1192 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
1193 unsigned char option_code)
1195 if (g_list_find(dhcp_client->request_list,
1196 GINT_TO_POINTER((int) option_code)) == NULL)
1197 dhcp_client->request_list = g_list_prepend(
1198 dhcp_client->request_list,
1199 (GINT_TO_POINTER((int) option_code)));
1201 return G_DHCP_CLIENT_ERROR_NONE;
1204 static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
1207 int len = strnlen(str, 255);
1209 storage = malloc(len + extra + OPT_DATA);
1210 storage[OPT_CODE] = code;
1211 storage[OPT_LEN] = len + extra;
1212 memcpy(storage + extra + OPT_DATA, str, len);
1217 /* Now only support send hostname */
1218 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
1219 unsigned char option_code, const char *option_value)
1221 uint8_t *binary_option;
1223 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
1224 binary_option = alloc_dhcp_option(option_code,
1227 g_hash_table_insert(dhcp_client->send_value_hash,
1228 GINT_TO_POINTER((int) option_code), binary_option);
1231 return G_DHCP_CLIENT_ERROR_NONE;
1234 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
1236 if (dhcp_client == NULL)
1239 g_atomic_int_inc(&dhcp_client->ref_count);
1244 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
1246 if (dhcp_client == NULL)
1249 if (g_atomic_int_dec_and_test(&dhcp_client->ref_count) == FALSE)
1252 g_dhcp_client_stop(dhcp_client);
1254 g_free(dhcp_client->interface);
1255 g_free(dhcp_client->assigned_ip);
1257 g_list_free(dhcp_client->request_list);
1258 g_list_free(dhcp_client->require_list);
1260 g_hash_table_destroy(dhcp_client->code_value_hash);
1261 g_hash_table_destroy(dhcp_client->send_value_hash);
1263 g_free(dhcp_client);
1266 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
1267 GDHCPDebugFunc func, gpointer user_data)
1269 if (dhcp_client == NULL)
1272 dhcp_client->debug_func = func;
1273 dhcp_client->debug_data = user_data;