+int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
+ unsigned char **duid, int *duid_len)
+{
+ time_t duid_time;
+
+ switch (duid_type) {
+ case G_DHCPV6_DUID_LLT:
+ *duid_len = 2 + 2 + 4 + ETH_ALEN;
+ *duid = g_try_malloc(*duid_len);
+ if (*duid == NULL)
+ return -ENOMEM;
+
+ (*duid)[0] = 0;
+ (*duid)[1] = 1;
+ get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
+ (*duid)[2] = 0;
+ (*duid)[3] = type;
+ duid_time = time(0) - DUID_TIME_EPOCH;
+ (*duid)[4] = duid_time >> 24;
+ (*duid)[5] = duid_time >> 16;
+ (*duid)[6] = duid_time >> 8;
+ (*duid)[7] = duid_time & 0xff;
+ break;
+ case G_DHCPV6_DUID_EN:
+ return -EINVAL;
+ case G_DHCPV6_DUID_LL:
+ *duid_len = 2 + 2 + ETH_ALEN;
+ *duid = g_try_malloc(*duid_len);
+ if (*duid == NULL)
+ return -ENOMEM;
+
+ (*duid)[0] = 0;
+ (*duid)[1] = 3;
+ get_interface_mac_address(index, &(*duid)[2 + 2]);
+ (*duid)[2] = 0;
+ (*duid)[3] = type;
+ break;
+ }
+
+ return 0;
+}
+
+int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
+ int duid_len)
+{
+ if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
+ return -EINVAL;
+
+ g_free(dhcp_client->duid);
+
+ dhcp_client->duid = duid;
+ dhcp_client->duid_len = duid_len;
+
+ return 0;
+}
+
+uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
+{
+ if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
+ return 0;
+
+ return dhcp_client->iaid;
+}
+
+void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
+ unsigned char *iaid)
+{
+ uint8_t buf[6];
+
+ get_interface_mac_address(index, buf);
+
+ memcpy(iaid, &buf[2], 4);
+ dhcp_client->iaid = iaid[0] << 24 |
+ iaid[1] << 16 | iaid[2] << 8 | iaid[3];
+}
+
+int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
+ uint32_t *T1, uint32_t *T2,
+ time_t *last_renew, time_t *last_rebind,
+ time_t *expire)
+{
+ if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
+ return -EINVAL;
+
+ if (T1 != NULL)
+ *T1 = dhcp_client->T1;
+
+ if (T2 != NULL)
+ *T2 = dhcp_client->T2;
+
+ if (last_renew != NULL)
+ *last_renew = dhcp_client->last_renew;
+
+ if (last_rebind != NULL)
+ *last_rebind = dhcp_client->last_rebind;
+
+ if (expire != NULL)
+ *expire = dhcp_client->expire;
+
+ return 0;
+}
+
+static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
+ uint16_t len)
+{
+ buf[0] = 0;
+ buf[1] = G_DHCPV6_IAADDR;
+ buf[2] = 0;
+ buf[3] = len;
+ memcpy(&buf[4], &dhcp_client->ia_na, 16);
+ memset(&buf[20], 0, 4); /* preferred */
+ memset(&buf[24], 0, 4); /* valid */
+ return buf;
+}
+
+static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
+{
+ uint32_t iaid;
+
+ iaid = g_dhcpv6_client_get_iaid(dhcp_client);
+ if (iaid == 0) {
+ g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
+ return;
+ }
+
+ buf[0] = iaid >> 24;
+ buf[1] = iaid >> 16;
+ buf[2] = iaid >> 8;
+ buf[3] = iaid;
+}
+
+int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
+ int code, uint32_t *T1, uint32_t *T2,
+ gboolean add_iaaddr)
+{
+ if (code == G_DHCPV6_IA_TA) {
+ uint8_t ia_options[4];
+
+ put_iaid(dhcp_client, index, ia_options);
+
+ g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
+ g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
+ ia_options, sizeof(ia_options));
+
+ } else if (code == G_DHCPV6_IA_NA) {
+
+ g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
+
+ if (add_iaaddr == TRUE) {
+#define IAADDR_LEN (16+4+4)
+ uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
+
+ put_iaid(dhcp_client, index, ia_options);
+
+ if (T1 != NULL) {
+ ia_options[4] = *T1 >> 24;
+ ia_options[5] = *T1 >> 16;
+ ia_options[6] = *T1 >> 8;
+ ia_options[7] = *T1;
+ } else
+ memset(&ia_options[4], 0x00, 4);
+
+ if (T2 != NULL) {
+ ia_options[8] = *T2 >> 24;
+ ia_options[9] = *T2 >> 16;
+ ia_options[10] = *T2 >> 8;
+ ia_options[11] = *T2;
+ } else
+ memset(&ia_options[8], 0x00, 4);
+
+ create_iaaddr(dhcp_client, &ia_options[12],
+ IAADDR_LEN);
+
+ g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
+ ia_options, sizeof(ia_options));
+ } else {
+ uint8_t ia_options[4+4+4];
+
+ put_iaid(dhcp_client, index, ia_options);
+
+ memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
+ memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
+
+ g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
+ ia_options, sizeof(ia_options));
+ }
+
+ } else
+ return -EINVAL;
+
+ return 0;
+}
+
+int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
+{
+ va_list va;
+ int i, j, len = sizeof(uint16_t) * args;
+ uint8_t *values;
+
+ values = g_try_malloc(len);
+ if (values == NULL)
+ return -ENOMEM;
+
+ va_start(va, args);
+ for (i = 0, j = 0; i < args; i++) {
+ uint16_t value = va_arg(va, int);
+ values[j++] = value >> 8;
+ values[j++] = value & 0xff;
+ }
+ va_end(va);
+
+ g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
+
+ return 0;
+}
+
+static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
+{
+ struct dhcpv6_packet *packet;
+ uint8_t buf[MAX_DHCPV6_PKT_SIZE];
+ unsigned char *ptr;
+ int ret, max_buf;
+
+ memset(buf, 0, sizeof(buf));
+ packet = (struct dhcpv6_packet *)&buf[0];
+ ptr = buf + sizeof(struct dhcpv6_packet);
+
+ debug(dhcp_client, "sending DHCPv6 %s message", msg);
+
+ init_packet(dhcp_client, packet, type);
+
+ dhcp_client->xid = packet->transaction_id[0] << 16 |
+ packet->transaction_id[1] << 8 |
+ packet->transaction_id[2];
+
+ max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
+
+ add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
+
+ add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
+
+ ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
+
+ debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
+ return ret;
+}
+
+static int send_solicitation(GDHCPClient *dhcp_client)
+{
+ return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
+}
+
+static int send_dhcpv6_request(GDHCPClient *dhcp_client)
+{
+ return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
+}
+
+static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
+{
+ return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
+}
+
+static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
+{
+ return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
+}
+
+static int send_information_req(GDHCPClient *dhcp_client)
+{
+ return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
+ "information-req");
+}
+