3 * DHCP client library with GLib integration
5 * Copyright (C) 2009-2012 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
32 #include <sys/ioctl.h>
33 #include <arpa/inet.h>
36 #include <netpacket/packet.h>
37 #include <netinet/if_ether.h>
38 #include <net/ethernet.h>
41 #include <linux/filter.h>
49 #define DISCOVER_TIMEOUT 3
50 #define DISCOVER_RETRIES 10
52 #define REQUEST_TIMEOUT 3
53 #define REQUEST_RETRIES 5
55 typedef enum _listen_mode {
62 typedef enum _dhcp_client_state {
87 uint8_t mac_address[6];
90 uint32_t requested_ip;
93 uint32_t lease_seconds;
94 ListenMode listen_mode;
97 uint8_t ack_retry_times;
100 guint listener_watch;
101 GIOChannel *listener_channel;
104 GHashTable *code_value_hash;
105 GHashTable *send_value_hash;
106 GDHCPClientEventFunc lease_available_cb;
107 gpointer lease_available_data;
108 GDHCPClientEventFunc ipv4ll_available_cb;
109 gpointer ipv4ll_available_data;
110 GDHCPClientEventFunc no_lease_cb;
111 gpointer no_lease_data;
112 GDHCPClientEventFunc lease_lost_cb;
113 gpointer lease_lost_data;
114 GDHCPClientEventFunc ipv4ll_lost_cb;
115 gpointer ipv4ll_lost_data;
116 GDHCPClientEventFunc address_conflict_cb;
117 gpointer address_conflict_data;
118 GDHCPDebugFunc debug_func;
120 GDHCPClientEventFunc information_req_cb;
121 gpointer information_req_data;
122 GDHCPClientEventFunc solicitation_cb;
123 gpointer solicitation_data;
124 GDHCPClientEventFunc advertise_cb;
125 gpointer advertise_data;
126 GDHCPClientEventFunc request_cb;
127 gpointer request_data;
128 GDHCPClientEventFunc renew_cb;
130 GDHCPClientEventFunc rebind_cb;
131 gpointer rebind_data;
132 GDHCPClientEventFunc release_cb;
133 gpointer release_data;
137 unsigned char *server_duid;
139 uint16_t status_code;
142 struct in6_addr ia_na;
143 struct in6_addr ia_ta;
148 struct timeval start_time;
151 static inline void debug(GDHCPClient *client, const char *format, ...)
156 if (client->debug_func == NULL)
159 va_start(ap, format);
161 if (vsnprintf(str, sizeof(str), format, ap) > 0)
162 client->debug_func(str, client->debug_data);
167 /* Initialize the packet with the proper defaults */
168 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
170 if (dhcp_client->type == G_DHCP_IPV6)
171 dhcpv6_init_header(pkt, type);
173 struct dhcp_packet *packet = pkt;
175 dhcp_init_header(packet, type);
176 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
180 static void add_request_options(GDHCPClient *dhcp_client,
181 struct dhcp_packet *packet)
186 int end = dhcp_end_option(packet->options);
188 for (list = dhcp_client->request_list; list; list = list->next) {
189 code = (uint8_t) GPOINTER_TO_INT(list->data);
191 packet->options[end + OPT_DATA + len] = code;
196 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
197 packet->options[end + OPT_LEN] = len;
198 packet->options[end + OPT_DATA + len] = DHCP_END;
205 unsigned char **ptr_buf;
208 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
211 uint8_t *option = value;
213 struct hash_params *params = user_data;
215 /* option[0][1] contains option code */
216 len = option[2] << 8 | option[3];
218 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
221 memcpy(*params->ptr_buf, option, len + 2 + 2);
222 (*params->ptr_buf) += len + 2 + 2;
225 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
226 unsigned char *buf, int max_buf,
227 unsigned char **ptr_buf)
229 struct hash_params params = {
235 if (dhcp_client->type == G_DHCP_IPV4)
238 g_hash_table_foreach(dhcp_client->send_value_hash,
239 add_dhcpv6_binary_option, ¶ms);
241 *ptr_buf = *params.ptr_buf;
244 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
248 buf[1] = code & 0xff;
251 if (len > 0 && msg != NULL)
252 memcpy(&buf[4], msg, len);
255 static int32_t get_time_diff(struct timeval *tv)
260 gettimeofday(&now, NULL);
262 hsec = (now.tv_sec - tv->tv_sec) * 100;
263 hsec += (now.tv_usec - tv->tv_usec) / 10000;
268 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
269 struct dhcpv6_packet *packet,
270 unsigned char *buf, int max_buf,
271 unsigned char **ptr_buf)
274 uint16_t code, value;
278 if (dhcp_client->type == G_DHCP_IPV4)
281 for (list = dhcp_client->request_list; list; list = list->next) {
282 code = (uint16_t) GPOINTER_TO_INT(list->data);
285 case G_DHCPV6_CLIENTID:
286 if (dhcp_client->duid == NULL)
289 len = 2 + 2 + dhcp_client->duid_len;
290 if ((*ptr_buf + len) > (buf + max_buf)) {
291 debug(dhcp_client, "Too long dhcpv6 message "
292 "when writing client id option");
296 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
297 dhcp_client->duid_len, dhcp_client->duid);
301 case G_DHCPV6_SERVERID:
302 if (dhcp_client->server_duid == NULL)
305 len = 2 + 2 + dhcp_client->server_duid_len;
306 if ((*ptr_buf + len) > (buf + max_buf)) {
307 debug(dhcp_client, "Too long dhcpv6 message "
308 "when writing server id option");
312 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
313 dhcp_client->server_duid_len,
314 dhcp_client->server_duid);
318 case G_DHCPV6_RAPID_COMMIT:
320 if ((*ptr_buf + len) > (buf + max_buf)) {
321 debug(dhcp_client, "Too long dhcpv6 message "
322 "when writing rapid commit option");
326 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
333 case G_DHCPV6_ELAPSED_TIME:
334 if (dhcp_client->retransmit == FALSE) {
336 * Initial message, elapsed time is 0.
340 diff = get_time_diff(&dhcp_client->start_time);
341 if (diff < 0 || diff > 0xffff)
346 if ((*ptr_buf + len) > (buf + max_buf)) {
347 debug(dhcp_client, "Too long dhcpv6 message "
348 "when writing elapsed time option");
352 value = htons((uint16_t)diff);
353 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
354 2, (uint8_t *)&value);
358 case G_DHCPV6_DNS_SERVERS:
361 case G_DHCPV6_SNTP_SERVERS:
370 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
372 uint8_t *option = value;
373 struct dhcp_packet *packet = user_data;
375 dhcp_add_binary_option(packet, option);
378 static void add_send_options(GDHCPClient *dhcp_client,
379 struct dhcp_packet *packet)
381 g_hash_table_foreach(dhcp_client->send_value_hash,
382 add_binary_option, packet);
386 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
387 * represents the number of seconds elapsed from the start of
388 * attempting DHCP to satisfy some DHCP servers that allow for an
389 * "authoritative" reply before responding.
391 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
393 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
396 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
398 struct dhcp_packet packet;
400 debug(dhcp_client, "sending DHCP discover request");
402 init_packet(dhcp_client, &packet, DHCPDISCOVER);
404 packet.xid = dhcp_client->xid;
405 packet.secs = dhcp_attempt_secs(dhcp_client);
408 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
410 /* Explicitly saying that we want RFC-compliant packets helps
411 * some buggy DHCP servers to NOT send bigger packets */
412 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
414 add_request_options(dhcp_client, &packet);
416 add_send_options(dhcp_client, &packet);
418 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
419 INADDR_BROADCAST, SERVER_PORT,
420 MAC_BCAST_ADDR, dhcp_client->ifindex);
423 static int send_select(GDHCPClient *dhcp_client)
425 struct dhcp_packet packet;
427 debug(dhcp_client, "sending DHCP select request");
429 init_packet(dhcp_client, &packet, DHCPREQUEST);
431 packet.xid = dhcp_client->xid;
432 packet.secs = dhcp_attempt_secs(dhcp_client);
434 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
435 dhcp_client->requested_ip);
436 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
437 dhcp_client->server_ip);
439 add_request_options(dhcp_client, &packet);
441 add_send_options(dhcp_client, &packet);
443 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
444 INADDR_BROADCAST, SERVER_PORT,
445 MAC_BCAST_ADDR, dhcp_client->ifindex);
448 static int send_renew(GDHCPClient *dhcp_client)
450 struct dhcp_packet packet;
452 debug(dhcp_client, "sending DHCP renew request");
454 init_packet(dhcp_client , &packet, DHCPREQUEST);
455 packet.xid = dhcp_client->xid;
456 packet.ciaddr = htonl(dhcp_client->requested_ip);
458 add_request_options(dhcp_client, &packet);
460 add_send_options(dhcp_client, &packet);
462 return dhcp_send_kernel_packet(&packet,
463 dhcp_client->requested_ip, CLIENT_PORT,
464 dhcp_client->server_ip, SERVER_PORT);
467 static int send_rebound(GDHCPClient *dhcp_client)
469 struct dhcp_packet packet;
471 debug(dhcp_client, "sending DHCP rebound request");
473 init_packet(dhcp_client , &packet, DHCPREQUEST);
474 packet.xid = dhcp_client->xid;
475 packet.ciaddr = htonl(dhcp_client->requested_ip);
477 add_request_options(dhcp_client, &packet);
479 add_send_options(dhcp_client, &packet);
481 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
482 INADDR_BROADCAST, SERVER_PORT,
483 MAC_BCAST_ADDR, dhcp_client->ifindex);
486 static int send_release(GDHCPClient *dhcp_client,
487 uint32_t server, uint32_t ciaddr)
489 struct dhcp_packet packet;
491 debug(dhcp_client, "sending DHCP release request");
493 init_packet(dhcp_client, &packet, DHCPRELEASE);
495 packet.ciaddr = htonl(ciaddr);
497 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
499 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
500 server, SERVER_PORT);
503 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
504 static int switch_listening_mode(GDHCPClient *dhcp_client,
505 ListenMode listen_mode);
507 static gboolean send_probe_packet(gpointer dhcp_data)
509 GDHCPClient *dhcp_client;
512 dhcp_client = dhcp_data;
513 /* if requested_ip is not valid, pick a new address*/
514 if (dhcp_client->requested_ip == 0) {
515 debug(dhcp_client, "pick a new random address");
516 dhcp_client->requested_ip = ipv4ll_random_ip(0);
519 debug(dhcp_client, "sending IPV4LL probe request");
521 if (dhcp_client->retry_times == 1) {
522 dhcp_client->state = IPV4LL_PROBE;
523 switch_listening_mode(dhcp_client, L_ARP);
525 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
526 dhcp_client->requested_ip, dhcp_client->ifindex);
528 if (dhcp_client->retry_times < PROBE_NUM) {
529 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
530 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
531 timeout += PROBE_MIN*1000;
533 timeout = (ANNOUNCE_WAIT * 1000);
535 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
537 ipv4ll_probe_timeout,
543 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
544 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
546 static gboolean send_announce_packet(gpointer dhcp_data)
548 GDHCPClient *dhcp_client;
550 dhcp_client = dhcp_data;
552 debug(dhcp_client, "sending IPV4LL announce request");
554 ipv4ll_send_arp_packet(dhcp_client->mac_address,
555 dhcp_client->requested_ip,
556 dhcp_client->requested_ip,
557 dhcp_client->ifindex);
559 if (dhcp_client->timeout > 0)
560 g_source_remove(dhcp_client->timeout);
561 dhcp_client->timeout = 0;
563 if (dhcp_client->state == IPV4LL_DEFEND) {
564 dhcp_client->timeout =
565 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
567 ipv4ll_defend_timeout,
572 dhcp_client->timeout =
573 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
575 ipv4ll_announce_timeout,
581 static void get_interface_mac_address(int index, uint8_t *mac_address)
586 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
588 perror("Open socket error");
592 memset(&ifr, 0, sizeof(ifr));
593 ifr.ifr_ifindex = index;
595 err = ioctl(sk, SIOCGIFNAME, &ifr);
597 perror("Get interface name error");
601 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
603 perror("Get mac address error");
607 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
613 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
615 if (dhcp_client == NULL)
618 dhcp_client->retransmit = TRUE;
621 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
623 if (dhcp_client == NULL)
626 dhcp_client->retransmit = FALSE;
629 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
630 unsigned char **duid, int *duid_len)
635 case G_DHCPV6_DUID_LLT:
636 *duid_len = 2 + 2 + 4 + ETH_ALEN;
637 *duid = g_try_malloc(*duid_len);
643 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
646 duid_time = time(NULL) - DUID_TIME_EPOCH;
647 (*duid)[4] = duid_time >> 24;
648 (*duid)[5] = duid_time >> 16;
649 (*duid)[6] = duid_time >> 8;
650 (*duid)[7] = duid_time & 0xff;
652 case G_DHCPV6_DUID_EN:
654 case G_DHCPV6_DUID_LL:
655 *duid_len = 2 + 2 + ETH_ALEN;
656 *duid = g_try_malloc(*duid_len);
662 get_interface_mac_address(index, &(*duid)[2 + 2]);
671 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
674 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
677 g_free(dhcp_client->duid);
679 dhcp_client->duid = duid;
680 dhcp_client->duid_len = duid_len;
685 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
687 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
690 return dhcp_client->iaid;
693 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
698 get_interface_mac_address(index, buf);
700 memcpy(iaid, &buf[2], 4);
701 dhcp_client->iaid = iaid[0] << 24 |
702 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
705 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
706 uint32_t *T1, uint32_t *T2,
707 time_t *last_renew, time_t *last_rebind,
710 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
714 *T1 = dhcp_client->T1;
717 *T2 = dhcp_client->T2;
719 if (last_renew != NULL)
720 *last_renew = dhcp_client->last_renew;
722 if (last_rebind != NULL)
723 *last_rebind = dhcp_client->last_rebind;
726 *expire = dhcp_client->expire;
731 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
735 buf[1] = G_DHCPV6_IAADDR;
738 memcpy(&buf[4], &dhcp_client->ia_na, 16);
739 memset(&buf[20], 0, 4); /* preferred */
740 memset(&buf[24], 0, 4); /* valid */
744 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
748 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
750 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
760 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
761 int code, uint32_t *T1, uint32_t *T2,
764 if (code == G_DHCPV6_IA_TA) {
765 uint8_t ia_options[4];
767 put_iaid(dhcp_client, index, ia_options);
769 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
770 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
771 ia_options, sizeof(ia_options));
773 } else if (code == G_DHCPV6_IA_NA) {
775 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
777 if (add_iaaddr == TRUE) {
778 #define IAADDR_LEN (16+4+4)
779 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
781 put_iaid(dhcp_client, index, ia_options);
784 ia_options[4] = *T1 >> 24;
785 ia_options[5] = *T1 >> 16;
786 ia_options[6] = *T1 >> 8;
789 memset(&ia_options[4], 0x00, 4);
792 ia_options[8] = *T2 >> 24;
793 ia_options[9] = *T2 >> 16;
794 ia_options[10] = *T2 >> 8;
795 ia_options[11] = *T2;
797 memset(&ia_options[8], 0x00, 4);
799 create_iaaddr(dhcp_client, &ia_options[12],
802 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
803 ia_options, sizeof(ia_options));
805 uint8_t ia_options[4+4+4];
807 put_iaid(dhcp_client, index, ia_options);
809 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
810 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
812 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
813 ia_options, sizeof(ia_options));
822 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
825 int i, j, len = sizeof(uint16_t) * args;
828 values = g_try_malloc(len);
833 for (i = 0, j = 0; i < args; i++) {
834 uint16_t value = va_arg(va, int);
835 values[j++] = value >> 8;
836 values[j++] = value & 0xff;
840 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
847 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
849 struct dhcpv6_packet *packet;
850 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
854 memset(buf, 0, sizeof(buf));
855 packet = (struct dhcpv6_packet *)&buf[0];
856 ptr = buf + sizeof(struct dhcpv6_packet);
858 init_packet(dhcp_client, packet, type);
860 if (dhcp_client->retransmit == FALSE) {
861 dhcp_client->xid = packet->transaction_id[0] << 16 |
862 packet->transaction_id[1] << 8 |
863 packet->transaction_id[2];
864 gettimeofday(&dhcp_client->start_time, NULL);
866 packet->transaction_id[0] = dhcp_client->xid >> 16;
867 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
868 packet->transaction_id[2] = dhcp_client->xid;
871 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
873 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
876 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
878 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
880 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
882 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
884 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
888 static int send_solicitation(GDHCPClient *dhcp_client)
890 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
893 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
895 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
898 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
900 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
903 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
905 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
908 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
910 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
913 static int send_information_req(GDHCPClient *dhcp_client)
915 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
919 static void remove_value(gpointer data, gpointer user_data)
925 static void remove_option_value(gpointer data)
927 GList *option_value = data;
929 g_list_foreach(option_value, remove_value, NULL);
932 GDHCPClient *g_dhcp_client_new(GDHCPType type,
933 int ifindex, GDHCPClientError *error)
935 GDHCPClient *dhcp_client;
938 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
942 dhcp_client = g_try_new0(GDHCPClient, 1);
943 if (dhcp_client == NULL) {
944 *error = G_DHCP_CLIENT_ERROR_NOMEM;
948 dhcp_client->interface = get_interface_name(ifindex);
949 if (dhcp_client->interface == NULL) {
950 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
954 if (interface_is_up(ifindex) == FALSE) {
955 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
959 get_interface_mac_address(ifindex, dhcp_client->mac_address);
961 dhcp_client->listener_sockfd = -1;
962 dhcp_client->listener_channel = NULL;
963 dhcp_client->listen_mode = L_NONE;
964 dhcp_client->ref_count = 1;
965 dhcp_client->type = type;
966 dhcp_client->ifindex = ifindex;
967 dhcp_client->lease_available_cb = NULL;
968 dhcp_client->ipv4ll_available_cb = NULL;
969 dhcp_client->no_lease_cb = NULL;
970 dhcp_client->lease_lost_cb = NULL;
971 dhcp_client->ipv4ll_lost_cb = NULL;
972 dhcp_client->address_conflict_cb = NULL;
973 dhcp_client->listener_watch = 0;
974 dhcp_client->retry_times = 0;
975 dhcp_client->ack_retry_times = 0;
976 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
977 g_direct_equal, NULL, remove_option_value);
978 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
979 g_direct_equal, NULL, g_free);
980 dhcp_client->request_list = NULL;
981 dhcp_client->require_list = NULL;
982 dhcp_client->duid = NULL;
983 dhcp_client->duid_len = 0;
984 dhcp_client->last_renew = dhcp_client->last_rebind = time(NULL);
985 dhcp_client->expire = 0;
987 *error = G_DHCP_CLIENT_ERROR_NONE;
992 g_free(dhcp_client->interface);
997 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
999 static int dhcp_l2_socket(int ifindex)
1002 struct sockaddr_ll sock;
1007 * I've selected not to see LL header, so BPF doesn't see it, too.
1008 * The filter may also pass non-IP and non-ARP packets, but we do
1009 * a more complete check when receiving the message in userspace.
1011 * and filter shamelessly stolen from:
1013 * http://www.flamewarmaster.de/software/dhcpclient/
1015 * There are a few other interesting ideas on that page (look under
1016 * "Motivation"). Use of netlink events is most interesting. Think
1017 * of various network servers listening for events and reconfiguring.
1018 * That would obsolete sending HUP signals and/or make use of restarts.
1020 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1023 * TODO: make conditional?
1025 static const struct sock_filter filter_instr[] = {
1027 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1028 /* L5, L1, is UDP? */
1029 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1030 /* ugly check for arp on ethernet-like and IPv4 */
1031 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1032 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1033 /* skip IP header */
1034 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1035 /* check udp source and destination ports */
1036 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1038 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1040 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1041 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1044 static const struct sock_fprog filter_prog = {
1045 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1046 /* casting const away: */
1047 .filter = (struct sock_filter *) filter_instr,
1050 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
1054 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1055 /* Use only if standard ports are in use */
1056 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1057 sizeof(filter_prog));
1059 memset(&sock, 0, sizeof(sock));
1060 sock.sll_family = AF_PACKET;
1061 sock.sll_protocol = htons(ETH_P_IP);
1062 sock.sll_ifindex = ifindex;
1064 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1072 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1074 if (packet->ip.protocol != IPPROTO_UDP)
1077 if (packet->ip.version != IPVERSION)
1080 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1083 if (packet->udp.dest != htons(CLIENT_PORT))
1086 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1092 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
1095 struct ip_udp_dhcp_packet packet;
1098 memset(&packet, 0, sizeof(packet));
1100 bytes = read(fd, &packet, sizeof(packet));
1104 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1107 if (bytes < ntohs(packet.ip.tot_len))
1108 /* packet is bigger than sizeof(packet), we did partial read */
1111 /* ignore any extra garbage bytes */
1112 bytes = ntohs(packet.ip.tot_len);
1114 if (sanity_check(&packet, bytes) == FALSE)
1117 check = packet.ip.check;
1118 packet.ip.check = 0;
1119 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1122 /* verify UDP checksum. IP header has to be modified for this */
1123 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1124 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1125 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1126 check = packet.udp.check;
1127 packet.udp.check = 0;
1128 if (check && check != dhcp_checksum(&packet, bytes))
1131 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1132 sizeof(packet.udp)));
1134 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1137 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1140 static void ipv4ll_start(GDHCPClient *dhcp_client)
1145 if (dhcp_client->timeout > 0) {
1146 g_source_remove(dhcp_client->timeout);
1147 dhcp_client->timeout = 0;
1150 switch_listening_mode(dhcp_client, L_NONE);
1151 dhcp_client->type = G_DHCP_IPV4LL;
1152 dhcp_client->retry_times = 0;
1153 dhcp_client->requested_ip = 0;
1155 /*try to start with a based mac address ip*/
1156 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1157 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1159 /*first wait a random delay to avoid storm of arp request on boot*/
1160 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1162 dhcp_client->retry_times++;
1163 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1170 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1173 switch_listening_mode(dhcp_client, L_NONE);
1175 if (dhcp_client->timeout > 0)
1176 g_source_remove(dhcp_client->timeout);
1178 if (dhcp_client->listener_watch > 0) {
1179 g_source_remove(dhcp_client->listener_watch);
1180 dhcp_client->listener_watch = 0;
1183 dhcp_client->state = IPV4LL_PROBE;
1184 dhcp_client->retry_times = 0;
1185 dhcp_client->requested_ip = 0;
1187 g_free(dhcp_client->assigned_ip);
1188 dhcp_client->assigned_ip = NULL;
1191 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1194 struct ether_arp arp;
1195 uint32_t ip_requested;
1196 int source_conflict;
1197 int target_conflict;
1199 memset(&arp, 0, sizeof(arp));
1200 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1204 if (arp.arp_op != htons(ARPOP_REPLY) &&
1205 arp.arp_op != htons(ARPOP_REQUEST))
1208 ip_requested = htonl(dhcp_client->requested_ip);
1209 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1210 sizeof(ip_requested));
1212 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1213 sizeof(ip_requested));
1215 if (!source_conflict && !target_conflict)
1218 dhcp_client->conflicts++;
1220 debug(dhcp_client, "IPV4LL conflict detected");
1222 if (dhcp_client->state == IPV4LL_MONITOR) {
1223 if (!source_conflict)
1225 dhcp_client->state = IPV4LL_DEFEND;
1226 debug(dhcp_client, "DEFEND mode conflicts : %d",
1227 dhcp_client->conflicts);
1228 /*Try to defend with a single announce*/
1229 send_announce_packet(dhcp_client);
1233 if (dhcp_client->state == IPV4LL_DEFEND) {
1234 if (!source_conflict)
1236 else if (dhcp_client->ipv4ll_lost_cb != NULL)
1237 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1238 dhcp_client->ipv4ll_lost_data);
1241 ipv4ll_stop(dhcp_client);
1243 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1244 /*restart whole state machine*/
1245 dhcp_client->retry_times++;
1246 dhcp_client->timeout =
1247 g_timeout_add_full(G_PRIORITY_HIGH,
1248 ipv4ll_random_delay_ms(PROBE_WAIT),
1253 /* Here we got a lot of conflicts, RFC3927 states that we have
1254 * to wait RATE_LIMIT_INTERVAL before retrying,
1255 * but we just report failure.
1257 else if (dhcp_client->no_lease_cb != NULL)
1258 dhcp_client->no_lease_cb(dhcp_client,
1259 dhcp_client->no_lease_data);
1264 static gboolean check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1266 if (dhcp_client->type == G_DHCP_IPV6) {
1267 struct dhcpv6_packet *packet6 = pkt;
1270 if (packet6 == NULL)
1273 xid = packet6->transaction_id[0] << 16 |
1274 packet6->transaction_id[1] << 8 |
1275 packet6->transaction_id[2];
1277 if (xid != dhcp_client->xid)
1280 struct dhcp_packet *packet = pkt;
1282 if (packet->xid != dhcp_client->xid)
1285 if (packet->hlen != 6)
1288 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1295 static void start_request(GDHCPClient *dhcp_client);
1297 static gboolean request_timeout(gpointer user_data)
1299 GDHCPClient *dhcp_client = user_data;
1301 debug(dhcp_client, "request timeout (retries %d)",
1302 dhcp_client->retry_times);
1304 dhcp_client->retry_times++;
1306 start_request(dhcp_client);
1311 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1312 gpointer user_data);
1314 static int switch_listening_mode(GDHCPClient *dhcp_client,
1315 ListenMode listen_mode)
1317 GIOChannel *listener_channel;
1318 int listener_sockfd;
1320 if (dhcp_client->listen_mode == listen_mode)
1323 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1324 dhcp_client->listen_mode, listen_mode);
1326 if (dhcp_client->listen_mode != L_NONE) {
1327 if (dhcp_client->listener_watch > 0)
1328 g_source_remove(dhcp_client->listener_watch);
1329 dhcp_client->listener_channel = NULL;
1330 dhcp_client->listen_mode = L_NONE;
1331 dhcp_client->listener_sockfd = -1;
1332 dhcp_client->listener_watch = 0;
1335 if (listen_mode == L_NONE)
1338 if (listen_mode == L2)
1339 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1340 else if (listen_mode == L3) {
1341 if (dhcp_client->type == G_DHCP_IPV6)
1342 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1343 dhcp_client->interface,
1346 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1347 dhcp_client->interface,
1349 } else if (listen_mode == L_ARP)
1350 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1354 if (listener_sockfd < 0)
1357 listener_channel = g_io_channel_unix_new(listener_sockfd);
1358 if (listener_channel == NULL) {
1359 /* Failed to create listener channel */
1360 close(listener_sockfd);
1364 dhcp_client->listen_mode = listen_mode;
1365 dhcp_client->listener_sockfd = listener_sockfd;
1366 dhcp_client->listener_channel = listener_channel;
1368 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1369 dhcp_client->listener_watch =
1370 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1371 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1372 listener_event, dhcp_client,
1374 g_io_channel_unref(dhcp_client->listener_channel);
1379 static void start_request(GDHCPClient *dhcp_client)
1381 debug(dhcp_client, "start request (retries %d)",
1382 dhcp_client->retry_times);
1384 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1385 dhcp_client->state = INIT_SELECTING;
1386 ipv4ll_start(dhcp_client);
1391 if (dhcp_client->retry_times == 0) {
1392 dhcp_client->state = REQUESTING;
1393 switch_listening_mode(dhcp_client, L2);
1396 send_select(dhcp_client);
1398 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1405 static uint32_t get_lease(struct dhcp_packet *packet)
1408 uint32_t lease_seconds;
1410 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1414 lease_seconds = get_be32(option);
1415 /* paranoia: must not be prone to overflows */
1416 lease_seconds &= 0x0fffffff;
1417 if (lease_seconds < 10)
1420 return lease_seconds;
1423 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1425 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1427 if (dhcp_client->timeout > 0) {
1428 g_source_remove(dhcp_client->timeout);
1429 dhcp_client->timeout = 0;
1432 dhcp_client->retry_times = retry_times;
1433 dhcp_client->requested_ip = 0;
1434 dhcp_client->state = INIT_SELECTING;
1435 switch_listening_mode(dhcp_client, L2);
1437 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1440 static gboolean start_rebound_timeout(gpointer user_data)
1442 GDHCPClient *dhcp_client = user_data;
1444 debug(dhcp_client, "start rebound timeout");
1446 switch_listening_mode(dhcp_client, L2);
1448 dhcp_client->lease_seconds >>= 1;
1450 /* We need to have enough time to receive ACK package*/
1451 if (dhcp_client->lease_seconds <= 6) {
1453 /* ip need to be cleared */
1454 if (dhcp_client->lease_lost_cb != NULL)
1455 dhcp_client->lease_lost_cb(dhcp_client,
1456 dhcp_client->lease_lost_data);
1458 restart_dhcp(dhcp_client, 0);
1460 send_rebound(dhcp_client);
1462 dhcp_client->timeout =
1463 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1464 dhcp_client->lease_seconds >> 1,
1465 start_rebound_timeout,
1473 static void start_rebound(GDHCPClient *dhcp_client)
1475 debug(dhcp_client, "start rebound");
1477 dhcp_client->state = REBINDING;
1479 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1480 dhcp_client->lease_seconds >> 1,
1481 start_rebound_timeout,
1486 static gboolean start_renew_timeout(gpointer user_data)
1488 GDHCPClient *dhcp_client = user_data;
1490 debug(dhcp_client, "start renew timeout");
1492 dhcp_client->state = RENEWING;
1494 dhcp_client->lease_seconds >>= 1;
1496 switch_listening_mode(dhcp_client, L3);
1497 if (dhcp_client->lease_seconds <= 60)
1498 start_rebound(dhcp_client);
1500 send_renew(dhcp_client);
1502 if (dhcp_client->timeout > 0)
1503 g_source_remove(dhcp_client->timeout);
1505 dhcp_client->timeout =
1506 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1507 dhcp_client->lease_seconds >> 1,
1508 start_renew_timeout,
1516 static void start_bound(GDHCPClient *dhcp_client)
1518 debug(dhcp_client, "start bound");
1520 dhcp_client->state = BOUND;
1522 if (dhcp_client->timeout > 0)
1523 g_source_remove(dhcp_client->timeout);
1525 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1526 dhcp_client->lease_seconds >> 1,
1527 start_renew_timeout, dhcp_client,
1531 static gboolean restart_dhcp_timeout(gpointer user_data)
1533 GDHCPClient *dhcp_client = user_data;
1535 debug(dhcp_client, "restart DHCP timeout");
1537 dhcp_client->ack_retry_times++;
1539 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1544 static char *get_ip(uint32_t ip)
1546 struct in_addr addr;
1550 return g_strdup(inet_ntoa(addr));
1553 /* get a rough idea of how long an option will be */
1554 static const uint8_t len_of_option_as_string[] = {
1555 [OPTION_IP] = sizeof("255.255.255.255 "),
1556 [OPTION_STRING] = 1,
1557 [OPTION_U8] = sizeof("255 "),
1558 [OPTION_U16] = sizeof("65535 "),
1559 [OPTION_U32] = sizeof("4294967295 "),
1562 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1564 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1567 /* Create "opt_value1 option_value2 ..." string */
1568 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1570 unsigned upper_length;
1574 len = option[OPT_LEN - OPT_DATA];
1575 type &= OPTION_TYPE_MASK;
1576 optlen = dhcp_option_lengths[type];
1579 upper_length = len_of_option_as_string[type] *
1580 ((unsigned)len / (unsigned)optlen);
1581 dest = ret = g_malloc(upper_length + 1);
1585 while (len >= optlen) {
1588 dest += sprint_nip(dest, "", option);
1591 uint16_t val_u16 = get_be16(option);
1592 dest += sprintf(dest, "%u", val_u16);
1596 uint32_t val_u32 = get_be32(option);
1597 dest += sprintf(dest, "%u", val_u32);
1601 memcpy(dest, option, len);
1618 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1626 if (type == OPTION_STRING)
1627 return g_list_append(list, g_strdup(value));
1629 while ((pos = strchr(pos, ' ')) != NULL) {
1632 list = g_list_append(list, g_strdup(value));
1637 list = g_list_append(list, g_strdup(value));
1642 static inline uint32_t get_uint32(unsigned char *value)
1644 return value[0] << 24 | value[1] << 16 |
1645 value[2] << 8 | value[3];
1648 static inline uint16_t get_uint16(unsigned char *value)
1650 return value[0] << 8 | value[1];
1653 static GList *get_addresses(GDHCPClient *dhcp_client,
1655 unsigned char *value,
1659 struct in6_addr addr;
1660 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1661 uint16_t option_len, option_code, st = 0, max_len;
1662 int addr_count = 0, i, pos;
1666 if (value == NULL || len < 4)
1669 iaid = get_uint32(&value[0]);
1670 if (dhcp_client->iaid != iaid)
1673 if (code == G_DHCPV6_IA_NA) {
1674 T1 = get_uint32(&value[4]);
1675 T2 = get_uint32(&value[8]);
1678 /* RFC 3315, 22.4 */
1688 max_len = len - pos;
1690 /* We have more sub-options in this packet. */
1692 option = dhcpv6_get_sub_option(&value[pos], max_len,
1693 &option_code, &option_len);
1695 debug(dhcp_client, "pos %d option %p code %d len %d",
1696 pos, option, option_code, option_len);
1704 switch (option_code) {
1705 case G_DHCPV6_IAADDR:
1707 memcpy(&addr, &option[0], sizeof(addr));
1709 preferred = get_uint32(&option[i]);
1711 valid = get_uint32(&option[i]);
1716 case G_DHCPV6_STATUS_CODE:
1717 st = get_uint16(&option[0]);
1718 debug(dhcp_client, "error code %d", st);
1719 if (option_len > 2) {
1720 str = g_strndup((gchar *)&option[2],
1722 debug(dhcp_client, "error text: %s", str);
1730 pos += 2 + 2 + option_len;
1732 } while (option != NULL);
1734 if (addr_count > 0 && st == 0) {
1735 /* We only support one address atm */
1736 char addr_str[INET6_ADDRSTRLEN + 1];
1738 if (preferred > valid)
1739 /* RFC 3315, 22.6 */
1742 dhcp_client->T1 = T1;
1743 dhcp_client->T2 = T2;
1745 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
1746 debug(dhcp_client, "count %d addr %s T1 %u T2 %u",
1747 addr_count, addr_str, T1, T2);
1749 list = g_list_append(list, g_strdup(addr_str));
1751 if (code == G_DHCPV6_IA_NA)
1752 memcpy(&dhcp_client->ia_na, &addr,
1753 sizeof(struct in6_addr));
1755 memcpy(&dhcp_client->ia_ta, &addr,
1756 sizeof(struct in6_addr));
1758 g_dhcpv6_client_set_expire(dhcp_client, valid);
1764 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
1766 unsigned char *value,
1777 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
1778 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
1781 "%s server list length (%d) is invalid",
1782 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
1786 for (i = 0; i < len; i += 16) {
1788 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
1792 if (inet_ntop(AF_INET6, &value[i], str,
1793 INET6_ADDRSTRLEN) == NULL)
1796 list = g_list_append(list, str);
1800 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
1801 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
1802 list = get_addresses(dhcp_client, code, len, value, status);
1812 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
1813 struct dhcpv6_packet *packet,
1814 uint16_t pkt_len, uint16_t *status)
1816 GList *list, *value_list;
1819 uint16_t option_len;
1821 for (list = dhcp_client->request_list; list; list = list->next) {
1822 code = (uint16_t) GPOINTER_TO_INT(list->data);
1824 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
1826 if (option == NULL) {
1827 g_hash_table_remove(dhcp_client->code_value_hash,
1828 GINT_TO_POINTER((int) code));
1832 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
1833 option_len, option, status);
1835 debug(dhcp_client, "code %d %p len %d list %p", code, option,
1836 option_len, value_list);
1838 if (value_list == NULL)
1839 g_hash_table_remove(dhcp_client->code_value_hash,
1840 GINT_TO_POINTER((int) code));
1842 g_hash_table_insert(dhcp_client->code_value_hash,
1843 GINT_TO_POINTER((int) code), value_list);
1847 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
1849 GDHCPOptionType type;
1850 GList *list, *value_list;
1855 for (list = dhcp_client->request_list; list; list = list->next) {
1856 code = (uint8_t) GPOINTER_TO_INT(list->data);
1858 option = dhcp_get_option(packet, code);
1859 if (option == NULL) {
1860 g_hash_table_remove(dhcp_client->code_value_hash,
1861 GINT_TO_POINTER((int) code));
1865 type = dhcp_get_code_type(code);
1867 option_value = malloc_option_value_string(option, type);
1868 if (option_value == NULL)
1869 g_hash_table_remove(dhcp_client->code_value_hash,
1870 GINT_TO_POINTER((int) code));
1872 value_list = get_option_value_list(option_value, type);
1874 g_free(option_value);
1876 if (value_list == NULL)
1877 g_hash_table_remove(dhcp_client->code_value_hash,
1878 GINT_TO_POINTER((int) code));
1880 g_hash_table_insert(dhcp_client->code_value_hash,
1881 GINT_TO_POINTER((int) code), value_list);
1885 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1888 GDHCPClient *dhcp_client = user_data;
1889 struct dhcp_packet packet;
1890 struct dhcpv6_packet *packet6 = NULL;
1891 uint8_t *message_type = NULL, *client_id = NULL, *option,
1893 uint16_t option_len = 0, status = 0;
1896 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
1897 uint16_t pkt_len = 0;
1901 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1902 dhcp_client->listener_watch = 0;
1906 if (dhcp_client->listen_mode == L_NONE)
1911 if (dhcp_client->listen_mode == L2)
1912 re = dhcp_recv_l2_packet(&packet,
1913 dhcp_client->listener_sockfd);
1914 else if (dhcp_client->listen_mode == L3) {
1915 if (dhcp_client->type == G_DHCP_IPV6) {
1916 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
1917 dhcp_client->listener_sockfd);
1920 xid = packet6->transaction_id[0] << 16 |
1921 packet6->transaction_id[1] << 8 |
1922 packet6->transaction_id[2];
1924 re = dhcp_recv_l3_packet(&packet,
1925 dhcp_client->listener_sockfd);
1928 } else if (dhcp_client->listen_mode == L_ARP) {
1929 ipv4ll_recv_arp_packet(dhcp_client);
1938 if (check_package_owner(dhcp_client, pkt) == FALSE)
1941 if (dhcp_client->type == G_DHCP_IPV6) {
1942 if (packet6 == NULL)
1946 client_id = dhcpv6_get_option(packet6, pkt_len,
1947 G_DHCPV6_CLIENTID, &option_len, &count);
1949 if (client_id == NULL || count == 0 || option_len == 0 ||
1950 memcmp(dhcp_client->duid, client_id,
1951 dhcp_client->duid_len) != 0) {
1953 "client duid error, discarding msg %p/%d/%d",
1954 client_id, option_len, count);
1958 option = dhcpv6_get_option(packet6, pkt_len,
1959 G_DHCPV6_STATUS_CODE, &option_len, NULL);
1960 if (option != 0 && option_len > 0) {
1961 status = option[0]<<8 | option[1];
1963 debug(dhcp_client, "error code %d", status);
1964 if (option_len > 2) {
1965 gchar *txt = g_strndup(
1966 (gchar *)&option[2],
1968 debug(dhcp_client, "error text: %s",
1973 dhcp_client->status_code = status;
1975 dhcp_client->status_code = 0;
1978 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1979 if (message_type == NULL)
1983 if (message_type == NULL && client_id == NULL)
1984 /* No message type / client id option, ignore package */
1987 debug(dhcp_client, "received DHCP packet xid 0x%04x "
1988 "(current state %d)", xid, dhcp_client->state);
1990 switch (dhcp_client->state) {
1991 case INIT_SELECTING:
1992 if (*message_type != DHCPOFFER)
1995 g_source_remove(dhcp_client->timeout);
1996 dhcp_client->timeout = 0;
1997 dhcp_client->retry_times = 0;
1999 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2000 dhcp_client->server_ip = get_be32(option);
2001 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2003 dhcp_client->state = REQUESTING;
2005 start_request(dhcp_client);
2011 if (*message_type == DHCPACK) {
2012 dhcp_client->retry_times = 0;
2014 if (dhcp_client->timeout > 0)
2015 g_source_remove(dhcp_client->timeout);
2016 dhcp_client->timeout = 0;
2018 dhcp_client->lease_seconds = get_lease(&packet);
2020 get_request(dhcp_client, &packet);
2022 switch_listening_mode(dhcp_client, L_NONE);
2024 g_free(dhcp_client->assigned_ip);
2025 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2027 /* Address should be set up here */
2028 if (dhcp_client->lease_available_cb != NULL)
2029 dhcp_client->lease_available_cb(dhcp_client,
2030 dhcp_client->lease_available_data);
2032 start_bound(dhcp_client);
2033 } else if (*message_type == DHCPNAK) {
2034 dhcp_client->retry_times = 0;
2036 if (dhcp_client->timeout > 0)
2037 g_source_remove(dhcp_client->timeout);
2039 dhcp_client->timeout = g_timeout_add_seconds_full(
2041 restart_dhcp_timeout,
2048 if (dhcp_client->type != G_DHCP_IPV6)
2051 if (packet6->message != DHCPV6_REPLY &&
2052 packet6->message != DHCPV6_ADVERTISE)
2056 server_id = dhcpv6_get_option(packet6, pkt_len,
2057 G_DHCPV6_SERVERID, &option_len, &count);
2058 if (server_id == NULL || count != 1 || option_len == 0) {
2059 /* RFC 3315, 15.10 */
2061 "server duid error, discarding msg %p/%d/%d",
2062 server_id, option_len, count);
2065 dhcp_client->server_duid = g_try_malloc(option_len);
2066 if (dhcp_client->server_duid == NULL)
2068 memcpy(dhcp_client->server_duid, server_id, option_len);
2069 dhcp_client->server_duid_len = option_len;
2071 if (packet6->message == DHCPV6_REPLY) {
2072 uint8_t *rapid_commit;
2075 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2076 G_DHCPV6_RAPID_COMMIT,
2077 &option_len, &count);
2078 if (rapid_commit == NULL || option_len == 0 ||
2080 /* RFC 3315, 17.1.4 */
2084 switch_listening_mode(dhcp_client, L_NONE);
2086 if (dhcp_client->status_code == 0)
2087 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2088 &dhcp_client->status_code);
2090 if (packet6->message == DHCPV6_ADVERTISE) {
2091 if (dhcp_client->advertise_cb != NULL)
2092 dhcp_client->advertise_cb(dhcp_client,
2093 dhcp_client->advertise_data);
2097 if (dhcp_client->solicitation_cb != NULL) {
2099 * The dhcp_client might not be valid after the
2100 * callback call so just return immediately.
2102 dhcp_client->solicitation_cb(dhcp_client,
2103 dhcp_client->solicitation_data);
2107 case INFORMATION_REQ:
2112 if (dhcp_client->type != G_DHCP_IPV6)
2115 if (packet6->message != DHCPV6_REPLY)
2120 server_id = dhcpv6_get_option(packet6, pkt_len,
2121 G_DHCPV6_SERVERID, &option_len, &count);
2122 if (server_id == NULL || count != 1 || option_len == 0 ||
2123 (dhcp_client->server_duid_len > 0 &&
2124 memcmp(dhcp_client->server_duid, server_id,
2125 dhcp_client->server_duid_len) != 0)) {
2126 /* RFC 3315, 15.10 */
2128 "server duid error, discarding msg %p/%d/%d",
2129 server_id, option_len, count);
2133 switch_listening_mode(dhcp_client, L_NONE);
2135 dhcp_client->status_code = 0;
2136 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2137 &dhcp_client->status_code);
2139 if (dhcp_client->information_req_cb != NULL) {
2141 * The dhcp_client might not be valid after the
2142 * callback call so just return immediately.
2144 dhcp_client->information_req_cb(dhcp_client,
2145 dhcp_client->information_req_data);
2148 if (dhcp_client->request_cb != NULL) {
2149 dhcp_client->request_cb(dhcp_client,
2150 dhcp_client->request_data);
2153 if (dhcp_client->renew_cb != NULL) {
2154 dhcp_client->renew_cb(dhcp_client,
2155 dhcp_client->renew_data);
2158 if (dhcp_client->rebind_cb != NULL) {
2159 dhcp_client->rebind_cb(dhcp_client,
2160 dhcp_client->rebind_data);
2163 if (dhcp_client->release_cb != NULL) {
2164 dhcp_client->release_cb(dhcp_client,
2165 dhcp_client->release_data);
2173 debug(dhcp_client, "processed DHCP packet (new state %d)",
2174 dhcp_client->state);
2179 static gboolean discover_timeout(gpointer user_data)
2181 GDHCPClient *dhcp_client = user_data;
2183 dhcp_client->retry_times++;
2186 * We do not send the REQUESTED IP option if we are retrying because
2187 * if the server is non-authoritative it will ignore the request if the
2188 * option is present.
2190 g_dhcp_client_start(dhcp_client, NULL);
2195 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2197 GDHCPClient *dhcp_client = dhcp_data;
2199 debug(dhcp_client, "back to MONITOR mode");
2201 dhcp_client->conflicts = 0;
2202 dhcp_client->state = IPV4LL_MONITOR;
2207 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2209 GDHCPClient *dhcp_client = dhcp_data;
2212 debug(dhcp_client, "request timeout (retries %d)",
2213 dhcp_client->retry_times);
2215 if (dhcp_client->retry_times != ANNOUNCE_NUM){
2216 dhcp_client->retry_times++;
2217 send_announce_packet(dhcp_client);
2221 ip = htonl(dhcp_client->requested_ip);
2222 debug(dhcp_client, "switching to monitor mode");
2223 dhcp_client->state = IPV4LL_MONITOR;
2224 dhcp_client->assigned_ip = get_ip(ip);
2226 if (dhcp_client->ipv4ll_available_cb != NULL)
2227 dhcp_client->ipv4ll_available_cb(dhcp_client,
2228 dhcp_client->ipv4ll_available_data);
2229 dhcp_client->conflicts = 0;
2234 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2237 GDHCPClient *dhcp_client = dhcp_data;
2239 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2240 dhcp_client->retry_times);
2242 if (dhcp_client->retry_times == PROBE_NUM) {
2243 dhcp_client->state = IPV4LL_ANNOUNCE;
2244 dhcp_client->retry_times = 0;
2246 dhcp_client->retry_times++;
2247 send_announce_packet(dhcp_client);
2250 dhcp_client->retry_times++;
2251 send_probe_packet(dhcp_client);
2256 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2261 if (dhcp_client->type == G_DHCP_IPV6) {
2262 if (dhcp_client->information_req_cb) {
2263 dhcp_client->state = INFORMATION_REQ;
2264 re = switch_listening_mode(dhcp_client, L3);
2266 switch_listening_mode(dhcp_client, L_NONE);
2267 dhcp_client->state = 0;
2270 send_information_req(dhcp_client);
2272 } else if (dhcp_client->solicitation_cb) {
2273 dhcp_client->state = SOLICITATION;
2274 re = switch_listening_mode(dhcp_client, L3);
2276 switch_listening_mode(dhcp_client, L_NONE);
2277 dhcp_client->state = 0;
2280 send_solicitation(dhcp_client);
2282 } else if (dhcp_client->request_cb) {
2283 dhcp_client->state = REQUEST;
2284 re = switch_listening_mode(dhcp_client, L3);
2286 switch_listening_mode(dhcp_client, L_NONE);
2287 dhcp_client->state = 0;
2290 send_dhcpv6_request(dhcp_client);
2292 } else if (dhcp_client->renew_cb) {
2293 dhcp_client->state = RENEW;
2294 re = switch_listening_mode(dhcp_client, L3);
2296 switch_listening_mode(dhcp_client, L_NONE);
2297 dhcp_client->state = 0;
2300 send_dhcpv6_renew(dhcp_client);
2302 } else if (dhcp_client->rebind_cb) {
2303 dhcp_client->state = REBIND;
2304 re = switch_listening_mode(dhcp_client, L3);
2306 switch_listening_mode(dhcp_client, L_NONE);
2307 dhcp_client->state = 0;
2310 send_dhcpv6_rebind(dhcp_client);
2312 } else if (dhcp_client->release_cb) {
2313 dhcp_client->state = RENEW;
2314 re = switch_listening_mode(dhcp_client, L3);
2316 switch_listening_mode(dhcp_client, L_NONE);
2317 dhcp_client->state = 0;
2320 send_dhcpv6_release(dhcp_client);
2326 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2327 ipv4ll_start(dhcp_client);
2331 if (dhcp_client->retry_times == 0) {
2332 g_free(dhcp_client->assigned_ip);
2333 dhcp_client->assigned_ip = NULL;
2335 dhcp_client->state = INIT_SELECTING;
2336 re = switch_listening_mode(dhcp_client, L2);
2340 dhcp_client->xid = rand();
2341 dhcp_client->start = time(NULL);
2344 if (last_address == NULL) {
2347 addr = inet_addr(last_address);
2348 if (addr == 0xFFFFFFFF) {
2351 g_free(dhcp_client->last_address);
2352 dhcp_client->last_address = g_strdup(last_address);
2355 send_discover(dhcp_client, addr);
2357 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2365 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2367 switch_listening_mode(dhcp_client, L_NONE);
2369 if (dhcp_client->state == BOUND ||
2370 dhcp_client->state == RENEWING ||
2371 dhcp_client->state == REBINDING)
2372 send_release(dhcp_client, dhcp_client->server_ip,
2373 dhcp_client->requested_ip);
2375 if (dhcp_client->timeout > 0) {
2376 g_source_remove(dhcp_client->timeout);
2377 dhcp_client->timeout = 0;
2380 if (dhcp_client->listener_watch > 0) {
2381 g_source_remove(dhcp_client->listener_watch);
2382 dhcp_client->listener_watch = 0;
2385 dhcp_client->listener_channel = NULL;
2387 dhcp_client->retry_times = 0;
2388 dhcp_client->ack_retry_times = 0;
2390 dhcp_client->requested_ip = 0;
2391 dhcp_client->state = RELEASED;
2392 dhcp_client->lease_seconds = 0;
2395 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2396 unsigned char option_code)
2398 return g_hash_table_lookup(dhcp_client->code_value_hash,
2399 GINT_TO_POINTER((int) option_code));
2402 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2403 GDHCPClientEvent event,
2404 GDHCPClientEventFunc func,
2408 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2409 dhcp_client->lease_available_cb = func;
2410 dhcp_client->lease_available_data = data;
2412 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2413 if (dhcp_client->type == G_DHCP_IPV6)
2415 dhcp_client->ipv4ll_available_cb = func;
2416 dhcp_client->ipv4ll_available_data = data;
2418 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2419 dhcp_client->no_lease_cb = func;
2420 dhcp_client->no_lease_data = data;
2422 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2423 dhcp_client->lease_lost_cb = func;
2424 dhcp_client->lease_lost_data = data;
2426 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2427 if (dhcp_client->type == G_DHCP_IPV6)
2429 dhcp_client->ipv4ll_lost_cb = func;
2430 dhcp_client->ipv4ll_lost_data = data;
2432 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2433 dhcp_client->address_conflict_cb = func;
2434 dhcp_client->address_conflict_data = data;
2436 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2437 if (dhcp_client->type == G_DHCP_IPV4)
2439 dhcp_client->information_req_cb = func;
2440 dhcp_client->information_req_data = data;
2442 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2443 if (dhcp_client->type == G_DHCP_IPV4)
2445 dhcp_client->solicitation_cb = func;
2446 dhcp_client->solicitation_data = data;
2448 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2449 if (dhcp_client->type == G_DHCP_IPV4)
2451 dhcp_client->advertise_cb = func;
2452 dhcp_client->advertise_data = data;
2454 case G_DHCP_CLIENT_EVENT_REQUEST:
2455 if (dhcp_client->type == G_DHCP_IPV4)
2457 dhcp_client->request_cb = func;
2458 dhcp_client->request_data = data;
2460 case G_DHCP_CLIENT_EVENT_RENEW:
2461 if (dhcp_client->type == G_DHCP_IPV4)
2463 dhcp_client->renew_cb = func;
2464 dhcp_client->renew_data = data;
2466 case G_DHCP_CLIENT_EVENT_REBIND:
2467 if (dhcp_client->type == G_DHCP_IPV4)
2469 dhcp_client->rebind_cb = func;
2470 dhcp_client->rebind_data = data;
2472 case G_DHCP_CLIENT_EVENT_RELEASE:
2473 if (dhcp_client->type == G_DHCP_IPV4)
2475 dhcp_client->release_cb = func;
2476 dhcp_client->release_data = data;
2481 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2483 return dhcp_client->ifindex;
2486 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2488 return g_strdup(dhcp_client->assigned_ip);
2491 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2493 GList *option = NULL;
2495 if (dhcp_client->type == G_DHCP_IPV6)
2498 switch (dhcp_client->state) {
2500 case IPV4LL_MONITOR:
2501 return g_strdup("255.255.0.0");
2505 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2507 return g_strdup(option->data);
2508 case INIT_SELECTING:
2512 case IPV4LL_ANNOUNCE:
2513 case INFORMATION_REQ:
2524 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2525 unsigned int option_code)
2527 if (g_list_find(dhcp_client->request_list,
2528 GINT_TO_POINTER((int) option_code)) == NULL)
2529 dhcp_client->request_list = g_list_prepend(
2530 dhcp_client->request_list,
2531 (GINT_TO_POINTER((int) option_code)));
2533 return G_DHCP_CLIENT_ERROR_NONE;
2536 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2538 g_list_free(dhcp_client->request_list);
2539 dhcp_client->request_list = NULL;
2542 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2544 g_hash_table_remove_all(dhcp_client->send_value_hash);
2547 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
2551 storage = g_try_malloc(size + OPT_DATA);
2552 if (storage == NULL)
2555 storage[OPT_CODE] = code;
2556 storage[OPT_LEN] = size;
2557 memcpy(&storage[OPT_DATA], data, size);
2562 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data, unsigned size)
2564 return alloc_dhcp_option(code, data, MIN(size, 255));
2567 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
2569 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
2572 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
2574 const unsigned maclen = 6;
2575 const unsigned idlen = maclen + 1;
2576 const uint8_t option_code = G_DHCP_CLIENT_ID;
2577 uint8_t idbuf[idlen];
2578 uint8_t *data_option;
2580 idbuf[0] = ARPHRD_ETHER;
2582 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
2584 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
2585 if (data_option == NULL)
2586 return G_DHCP_CLIENT_ERROR_NOMEM;
2588 g_hash_table_insert(dhcp_client->send_value_hash,
2589 GINT_TO_POINTER((int) option_code), data_option);
2591 return G_DHCP_CLIENT_ERROR_NONE;
2594 /* Now only support send hostname */
2595 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
2596 unsigned char option_code, const char *option_value)
2598 uint8_t *binary_option;
2600 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
2601 binary_option = alloc_dhcp_string_option(option_code,
2603 if (binary_option == NULL)
2604 return G_DHCP_CLIENT_ERROR_NOMEM;
2606 g_hash_table_insert(dhcp_client->send_value_hash,
2607 GINT_TO_POINTER((int) option_code), binary_option);
2610 return G_DHCP_CLIENT_ERROR_NONE;
2613 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
2618 storage = g_malloc(2 + 2 + len);
2619 if (storage == NULL)
2622 storage[0] = code >> 8;
2623 storage[1] = code & 0xff;
2624 storage[2] = len >> 8;
2625 storage[3] = len & 0xff;
2626 memcpy(storage + 2 + 2, option, len);
2631 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
2632 uint16_t option_code,
2633 uint8_t *option_value,
2634 uint16_t option_len)
2636 if (option_value != NULL) {
2637 uint8_t *binary_option;
2639 debug(dhcp_client, "setting option %d to %p len %d",
2640 option_code, option_value, option_len);
2642 binary_option = alloc_dhcpv6_option(option_code, option_value,
2644 if (binary_option != NULL)
2645 g_hash_table_insert(dhcp_client->send_value_hash,
2646 GINT_TO_POINTER((int) option_code),
2651 void g_dhcpv6_client_reset_renew(GDHCPClient *dhcp_client)
2653 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2656 dhcp_client->last_renew = time(NULL);
2659 void g_dhcpv6_client_reset_rebind(GDHCPClient *dhcp_client)
2661 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2664 dhcp_client->last_rebind = time(NULL);
2667 void g_dhcpv6_client_set_expire(GDHCPClient *dhcp_client, uint32_t timeout)
2669 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2672 dhcp_client->expire = time(NULL) + timeout;
2675 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
2677 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2680 return dhcp_client->status_code;
2683 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
2685 if (dhcp_client == NULL)
2688 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
2693 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
2695 if (dhcp_client == NULL)
2698 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
2701 g_dhcp_client_stop(dhcp_client);
2703 g_free(dhcp_client->interface);
2704 g_free(dhcp_client->assigned_ip);
2705 g_free(dhcp_client->last_address);
2706 g_free(dhcp_client->duid);
2707 g_free(dhcp_client->server_duid);
2709 g_list_free(dhcp_client->request_list);
2710 g_list_free(dhcp_client->require_list);
2712 g_hash_table_destroy(dhcp_client->code_value_hash);
2713 g_hash_table_destroy(dhcp_client->send_value_hash);
2715 g_free(dhcp_client);
2718 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
2719 GDHCPDebugFunc func, gpointer user_data)
2721 if (dhcp_client == NULL)
2724 dhcp_client->debug_func = func;
2725 dhcp_client->debug_data = user_data;