test-dhcp6-client.c revision 5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53e
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek/***
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek This file is part of systemd.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek Copyright (C) 2014 Intel Corporation. All rights reserved.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek (at your option) any later version.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek***/
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <stdbool.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <stdio.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <sys/types.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <sys/socket.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <unistd.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include <net/ethernet.h>
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "socket-util.h"
73f860db9893deab6aebceb53dd7d0deb662e832Zbigniew Jędrzejewski-Szmek#include "macro.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "sd-event.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "event-util.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "virt.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "sd-dhcp6-client.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "dhcp6-protocol.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "dhcp6-internal.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "dhcp6-lease-internal.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic struct ether_addr mac_addr = {
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek};
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poetteringstatic bool verbose = false;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poetteringstatic sd_event_source *hangcheck;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_dhcp_fd[2];
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_index = 42;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_client_message_num;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic be32_t test_iaid = 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic uint8_t test_duid[14] = { };
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poetteringstatic sd_event *e_solicit;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poetteringstatic int test_client_basic(sd_event *e) {
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering sd_dhcp6_client *client;
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering if (verbose)
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering printf("* %s\n", __FUNCTION__);
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_client_new(&client) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(client);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(sd_dhcp6_client_attach_event(client, e, 0) >= 0);
29a5ca9baa58e55c4d9e1d008cdd014aa9c3c3e1Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_client_set_index(client, 15) == 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_client_set_index(client, -42) == -EINVAL);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_client_set_index(client, -1) == 0);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt assert_se(sd_dhcp6_client_set_index(client, 42) >= 0);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(sd_dhcp6_client_set_mac(client, &mac_addr) >= 0);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_client_set_callback(client, NULL, NULL) >= 0);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(sd_dhcp6_client_detach_event(client) >= 0);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(!sd_dhcp6_client_unref(client));
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 0;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering}
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_option(sd_event *e) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint8_t packet[] = {
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek 'F', 'O', 'O',
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek 0x00, DHCP6_OPTION_ORO, 0x00, 0x07,
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek 'A', 'B', 'C', 'D', 'E', 'F', 'G',
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek 0x00, DHCP6_OPTION_VENDOR_CLASS, 0x00, 0x09,
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek '1', '2', '3', '4', '5', '6', '7', '8', '9',
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek 'B', 'A', 'R',
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek };
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek uint8_t result[] = {
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek 'F', 'O', 'O',
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek 'B', 'A', 'R',
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek };
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek uint16_t optcode;
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek size_t optlen;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint8_t *optval, *buf, *out;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek size_t zero = 0, pos = 3;
e0aa3726103448097e5ad7cc6f427e142103a321Zbigniew Jędrzejewski-Szmek size_t buflen = sizeof(packet), outlen = sizeof(result);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek if (verbose)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt printf("* %s\n", __FUNCTION__);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(buflen == outlen);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
175a3d25d0e8596d4ba0759aea3f89ee228e7d6dLennart Poettering assert_se(dhcp6_option_parse(&buf, &zero, &optcode, &optlen,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &optval) == -ENOMSG);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek buflen -= 3;
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek buf = &packet[3];
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek outlen -= 3;
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek out = &result[3];
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_option_parse(&buf, &buflen, &optcode, &optlen,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &optval) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek pos += 4 + optlen;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(buf == &packet[pos]);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optcode == DHCP6_OPTION_ORO);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optlen == 7);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(buflen + pos == sizeof(packet));
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(dhcp6_option_append(&out, &outlen, optcode, optlen,
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering optval) >= 0);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(out == &result[pos]);
29a5ca9baa58e55c4d9e1d008cdd014aa9c3c3e1Lennart Poettering assert_se(*out == 0x00);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_option_parse(&buf, &buflen, &optcode, &optlen,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &optval) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek pos += 4 + optlen;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(buf == &packet[pos]);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optcode == DHCP6_OPTION_VENDOR_CLASS);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optlen == 9);
23ea3dab15181a3966ec45fa2ccbb81e59d2e0a7Zbigniew Jędrzejewski-Szmek assert_se(buflen + pos == sizeof(packet));
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_option_append(&out, &outlen, optcode, optlen,
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering optval) >= 0);
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer assert_se(out == &result[pos]);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(*out == 'B');
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(memcmp(packet, result, sizeof(packet)) == 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek return 0;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering}
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
5b84559a76f30755019d6a0e6b2ccc9bbe0b3c56Lennart Poetteringstatic uint8_t msg_advertise[198] = {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering 0x02, 0x0f, 0xb4, 0xe5, 0x00, 0x01, 0x00, 0x0e,
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering 0x00, 0x01, 0x00, 0x01, 0x1a, 0x6b, 0xf3, 0x30,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x3c, 0x97, 0x0e, 0xcf, 0xa3, 0x7d, 0x00, 0x03,
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek 0x00, 0x5e, 0x0e, 0xcf, 0xa3, 0x7d, 0x00, 0x00,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x00, 0x50, 0x00, 0x00, 0x00, 0x78, 0x00, 0x05,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x00, 0x18, 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0xbe, 0xef, 0x78, 0xee, 0x1c, 0xf3, 0x09, 0x3c,
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer 0x55, 0xad, 0x00, 0x00, 0x00, 0x96, 0x00, 0x00,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x00, 0xb4, 0x00, 0x0d, 0x00, 0x32, 0x00, 0x00,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x28,
23ea3dab15181a3966ec45fa2ccbb81e59d2e0a7Zbigniew Jędrzejewski-Szmek 0x65, 0x73, 0x29, 0x20, 0x72, 0x65, 0x6e, 0x65,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x77, 0x65, 0x64, 0x2e, 0x20, 0x47, 0x72, 0x65,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x65, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x20, 0x66,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x72, 0x6f, 0x6d, 0x20, 0x70, 0x6c, 0x61, 0x6e,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x65, 0x74, 0x20, 0x45, 0x61, 0x72, 0x74, 0x68,
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek 0x00, 0x17, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00,
23ea3dab15181a3966ec45fa2ccbb81e59d2e0a7Zbigniew Jędrzejewski-Szmek 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x0b,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0x72, 0x61, 0x00, 0x00, 0x1f, 0x00, 0x10, 0x20,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0x02, 0x00, 0x0e, 0x00, 0x01, 0x00, 0x01, 0x19,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0x40, 0x5c, 0x53, 0x78, 0x2b, 0xcb, 0xb3, 0x6d,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0x53, 0x00, 0x07, 0x00, 0x01, 0x00
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek};
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_advertise_option(sd_event *e) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _cleanup_dhcp6_lease_free_ sd_dhcp6_lease *lease = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek DHCP6Message *advertise = (DHCP6Message *)msg_advertise;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint8_t *optval, *opt = &msg_advertise[sizeof(DHCP6Message)];
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint16_t optcode;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek size_t optlen, len = sizeof(msg_advertise);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering be32_t val;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint8_t preference = 255;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek struct in6_addr addr;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint32_t lt_pref, lt_valid;
23ea3dab15181a3966ec45fa2ccbb81e59d2e0a7Zbigniew Jędrzejewski-Szmek int r;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering bool opt_clientid = false;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (verbose)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek printf("* %s\n", __FUNCTION__);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_lease_new(&lease) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(advertise->type == DHCP6_ADVERTISE);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se((be32toh(advertise->transaction_id) & 0x00ffffff) ==
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek 0x0fb4e5);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek while ((r = dhcp6_option_parse(&opt, &len, &optcode, &optlen,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &optval)) >= 0) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek switch(optcode) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DHCP6_OPTION_CLIENTID:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optlen == 14);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek opt_clientid = true;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DHCP6_OPTION_IA_NA:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optlen == 94);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval, &msg_advertise[26], optlen));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek val = htobe32(0x0ecfa37d);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval, &val, sizeof(val)));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek val = htobe32(80);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval + 4, &val, sizeof(val)));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek val = htobe32(120);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval + 8, &val, sizeof(val)));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_option_parse_ia(&optval, &optlen,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek optcode,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &lease->ia) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DHCP6_OPTION_SERVERID:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optlen == 14);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval, &msg_advertise[179], optlen));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_lease_set_serverid(lease, optval,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek optlen) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DHCP6_OPTION_PREFERENCE:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optlen == 1);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!*optval);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_lease_set_preference(lease,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek *optval) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering default:
aa44499da15a8fa7026463555a7a27e55e4e24a8Philippe De Swert break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(r == -ENOMSG);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(opt_clientid);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(sd_dhcp6_lease_get_first_address(lease, &addr, &lt_pref,
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering &lt_valid) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr)));
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert_se(lt_pref == 150);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(lt_valid == 180);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_lease_get_next_address(lease, &addr, &lt_pref,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &lt_valid) == -ENOMSG);
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. John
9d458c0902cdc5d993fd2f78d36fb83a8130c81bLennart Poettering assert_se(sd_dhcp6_lease_get_first_address(lease, &addr, &lt_pref,
9d458c0902cdc5d993fd2f78d36fb83a8130c81bLennart Poettering &lt_valid) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr)));
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. John assert_se(sd_dhcp6_lease_get_next_address(lease, &addr, &lt_pref,
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. John &lt_valid) == -ENOMSG);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_lease_get_next_address(lease, &addr, &lt_pref,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &lt_valid) == -ENOMSG);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_lease_get_first_address(lease, &addr, &lt_pref,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &lt_valid) >= 0);
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr)));
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_lease_get_next_address(lease, &addr, &lt_pref,
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek &lt_valid) == -ENOMSG);
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_lease_get_serverid(lease, &opt, &len) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(len == 14);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(opt, &msg_advertise[179], len));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_lease_get_preference(lease, &preference) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(preference == 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek return 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_hangcheck(sd_event_source *s, uint64_t usec, void *userdata) {
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek assert_not_reached("Test case should have completed in 2 seconds");
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek return 0;
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek}
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekint detect_vm(const char **id) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 1;
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekint detect_container(const char **id) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 1;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekint detect_virtualization(const char **id) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 1;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmekint dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek assert_se(index == test_index);
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek if (socketpair(AF_UNIX, SOCK_STREAM, 0, test_dhcp_fd) < 0)
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek return -errno;
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return test_dhcp_fd[0];
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_client_send_reply(DHCP6Message *request) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
ed6556920c1a6cdfe0bb04e806bc1f54ea191545Mantas Mikulėnasstatic int test_client_verify_request(DHCP6Message *request, uint8_t *option,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek size_t len) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _cleanup_dhcp6_lease_free_ sd_dhcp6_lease *lease = NULL;
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek uint8_t *optval;
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek uint16_t optcode;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek size_t optlen;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek bool found_clientid = false, found_iana = false, found_serverid = false;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek struct in6_addr addr;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek be32_t val;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint32_t lt_pref, lt_valid;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(request->type == DHCP6_REQUEST);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(dhcp6_lease_new(&lease) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek while ((r = dhcp6_option_parse(&option, &len,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &optcode, &optlen, &optval)) >= 0) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek switch(optcode) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DHCP6_OPTION_CLIENTID:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!found_clientid);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek found_clientid = true;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval, &test_duid,
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek sizeof(test_duid)));
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek break;
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek case DHCP6_OPTION_IA_NA:
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek assert_se(!found_iana);
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek found_iana = true;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optlen == 40);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval, &test_iaid, sizeof(test_iaid)));
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek val = htobe32(80);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval + 4, &val, sizeof(val)));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek val = htobe32(120);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(optval + 8, &val, sizeof(val)));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!dhcp6_option_parse_ia(&optval, &optlen,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek optcode, &lease->ia));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DHCP6_OPTION_SERVERID:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!found_serverid);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek found_serverid = true;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(optlen == 14);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(&msg_advertise[179], optval, optlen));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eceb8483e5a02e8e337486b89719a3b99cfcb7ceLennart Poettering break;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(r == -ENOMSG);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(found_clientid && found_iana && found_serverid);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp6_lease_get_first_address(lease, &addr, &lt_pref,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek &lt_valid) >= 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr)));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(lt_pref == 150);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(lt_valid == 180);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering assert_se(sd_dhcp6_lease_get_next_address(lease, &addr, &lt_pref,
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering &lt_valid) == -ENOMSG);
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering sd_event_exit(e_solicit, 0);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
eceb8483e5a02e8e337486b89719a3b99cfcb7ceLennart Poettering return 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_client_send_advertise(DHCP6Message *solicit)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek{
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek DHCP6Message advertise;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek advertise.transaction_id = solicit->transaction_id;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek advertise.type = DHCP6_ADVERTISE;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek memcpy(msg_advertise, &advertise.transaction_id, 4);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering memcpy(&msg_advertise[8], test_duid, sizeof(test_duid));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek memcpy(&msg_advertise[26], &test_iaid, sizeof(test_iaid));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert_se(write(test_dhcp_fd[1], msg_advertise, sizeof(msg_advertise))
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek == sizeof(msg_advertise));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 0;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int test_client_verify_solicit(DHCP6Message *solicit, uint8_t *option,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek size_t len) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint8_t *optval;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek uint16_t optcode;
size_t optlen;
bool found_clientid = false, found_iana = false;
int r;
assert_se(solicit->type == DHCP6_SOLICIT);
while ((r = dhcp6_option_parse(&option, &len,
&optcode, &optlen, &optval)) >= 0) {
switch(optcode) {
case DHCP6_OPTION_CLIENTID:
assert_se(!found_clientid);
found_clientid = true;
assert_se(optlen == sizeof(test_duid));
memcpy(&test_duid, optval, sizeof(test_duid));
break;
case DHCP6_OPTION_IA_NA:
assert_se(!found_iana);
found_iana = true;
assert_se(optlen == 12);
memcpy(&test_iaid, optval, sizeof(test_iaid));
break;
}
}
assert_se(r == -ENOMSG);
assert_se(found_clientid && found_iana);
return 0;
}
int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,
const void *packet, size_t len) {
struct in6_addr mcast =
IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT;
DHCP6Message *message;
uint8_t *option;
assert_se(s == test_dhcp_fd[0]);
assert_se(server_address);
assert_se(packet);
assert_se(len > sizeof(DHCP6Message) + 4);
assert_se(IN6_ARE_ADDR_EQUAL(server_address, &mcast));
message = (DHCP6Message *)packet;
option = (uint8_t *)(message + 1);
len -= sizeof(DHCP6Message);
assert_se(message->transaction_id & 0x00ffffff);
if (test_client_message_num == 0) {
test_client_verify_solicit(message, option, len);
test_client_send_advertise(message);
test_client_message_num++;
} else if (test_client_message_num == 1) {
test_client_verify_request(message, option, len);
test_client_send_reply(message);
test_client_message_num++;
}
return len;
}
static void test_client_solicit_cb(sd_dhcp6_client *client, int event,
void *userdata) {
sd_event *e = userdata;
assert_se(e);
if (verbose)
printf(" got DHCPv6 event %d\n", event);
sd_event_exit(e, 0);
}
static int test_client_solicit(sd_event *e) {
sd_dhcp6_client *client;
usec_t time_now = now(CLOCK_MONOTONIC);
if (verbose)
printf("* %s\n", __FUNCTION__);
assert_se(sd_dhcp6_client_new(&client) >= 0);
assert_se(client);
assert_se(sd_dhcp6_client_attach_event(client, e, 0) >= 0);
assert_se(sd_dhcp6_client_set_index(client, test_index) == 0);
assert_se(sd_dhcp6_client_set_mac(client, &mac_addr) >= 0);
assert_se(sd_dhcp6_client_set_callback(client,
test_client_solicit_cb, e) >= 0);
assert_se(sd_event_add_time(e, &hangcheck, CLOCK_MONOTONIC,
time_now + 2 * USEC_PER_SEC, 0,
test_hangcheck, NULL) >= 0);
e_solicit = e;
assert_se(sd_dhcp6_client_start(client) >= 0);
sd_event_loop(e);
hangcheck = sd_event_source_unref(hangcheck);
assert_se(!sd_dhcp6_client_unref(client));
test_dhcp_fd[1] = safe_close(test_dhcp_fd[1]);
return 0;
}
int main(int argc, char *argv[]) {
_cleanup_event_unref_ sd_event *e;
assert_se(sd_event_new(&e) >= 0);
log_set_max_level(LOG_DEBUG);
log_parse_environment();
log_open();
test_client_basic(e);
test_option(e);
test_advertise_option(e);
test_client_solicit(e);
assert_se(!sd_event_unref(e));
return 0;
}