test-dhcp-client.c revision d47e1de40e410ab2149918c3269038d2c264b01d
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/***
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright (C) 2013 Intel Corporation. All rights reserved.
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is free software; you can redistribute it and/or modify it
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering under the terms of the GNU Lesser General Public License as published by
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (at your option) any later version.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is distributed in the hope that it will be useful, but
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Lesser General Public License for more details.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering You should have received a copy of the GNU Lesser General Public License
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering***/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stdlib.h>
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner#include <assert.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <errno.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <stdio.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/types.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <sys/socket.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <unistd.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "util.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "socket-util.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#include "dhcp-protocol.h"
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#include "dhcp-internal.h"
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers#include "sd-dhcp-client.h"
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic struct ether_addr mac_addr = {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering};
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringtypedef int (*test_callback_recv_t)(size_t size, DHCPMessage *dhcp);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic bool verbose = false;
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poetteringstatic int test_fd[2];
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic test_callback_recv_t callback_recv;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic be32_t xid;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic sd_event_source *test_hangcheck;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallacestatic int test_dhcp_hangcheck(sd_event_source *s, uint64_t usec,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering void *userdata)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering{
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_not_reached("Test case should have completed in 2 seconds");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyerstatic void test_request_basic(sd_event *e)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering{
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_dhcp_client *client;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (verbose)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("* %s\n", __FUNCTION__);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_dhcp_client_new(&client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_dhcp_client_attach_event(client, e, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(NULL, 0) == -EINVAL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_address(NULL, NULL) == -EINVAL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_index(NULL, 0) == -EINVAL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_index(client, 15) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_index(client, -42) == -EINVAL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_index(client, -1) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_SUBNET_MASK) == -EEXIST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_ROUTER) == -EEXIST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_HOST_NAME) == -EEXIST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_DOMAIN_NAME) == -EEXIST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_DOMAIN_NAME_SERVER)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering == -EEXIST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_NTP_SERVER) == -EEXIST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_PAD) == -EINVAL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_END) == -EINVAL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_MESSAGE_TYPE) == -EINVAL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DHCP_OPTION_OVERLOAD) == -EINVAL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client,
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers DHCP_OPTION_PARAMETER_REQUEST_LIST)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers == -EINVAL);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client, 33) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client, 44) == 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic void test_checksum(void)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers{
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers uint8_t buf[20] = {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers 0x45, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x40, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers 0xff, 0xff, 0xff, 0xff
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers };
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (verbose)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf("* %s\n", __FUNCTION__);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(dhcp_packet_checksum(&buf, 20) == be16toh(0x78ae));
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic int check_options(uint8_t code, uint8_t len, const uint8_t *option,
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers void *user_data)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers{
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers switch(code) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers case DHCP_OPTION_CLIENT_IDENTIFIER:
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(len == 7);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(option[0] == 0x01);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(memcmp(&option[1], &mac_addr, ETH_ALEN) == 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers break;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers default:
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers break;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversint dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers const void *packet, size_t len)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers{
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers size_t size;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering _cleanup_free_ DHCPPacket *discover;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers uint16_t ip_check, udp_check;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(s >= 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(packet);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers size = sizeof(DHCPPacket);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(len > size);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers discover = memdup(packet, len);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(discover->ip.ttl == IPDEFTTL);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(discover->ip.protocol == IPPROTO_UDP);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(discover->ip.saddr == INADDR_ANY);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(discover->ip.daddr == INADDR_BROADCAST);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(discover->udp.source == be16toh(DHCP_PORT_CLIENT));
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(discover->udp.dest == be16toh(DHCP_PORT_SERVER));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ip_check = discover->ip.check;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers discover->ip.ttl = 0;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers discover->ip.check = discover->udp.len;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek udp_check = ~dhcp_packet_checksum(&discover->ip.ttl, len - 8);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(udp_check == 0xffff);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering discover->ip.ttl = IPDEFTTL;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers discover->ip.check = ip_check;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ip_check = ~dhcp_packet_checksum(&discover->ip, sizeof(discover->ip));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(ip_check == 0xffff);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(discover->dhcp.xid);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(memcmp(discover->dhcp.chaddr,
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers &mac_addr.ether_addr_octet, 6) == 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers size = len - sizeof(struct iphdr) - sizeof(struct udphdr);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(callback_recv);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering callback_recv(size, &discover->dhcp);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return 575;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversint dhcp_network_bind_raw_socket(int index, union sockaddr_union *link, uint32_t id)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering{
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (socketpair(AF_UNIX, SOCK_STREAM, 0, test_fd) < 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return -errno;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return test_fd[0];
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversint dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers{
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversint dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers const void *packet, size_t len)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering{
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic int test_discover_message_verify(size_t size, struct DHCPMessage *dhcp)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering{
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int res;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers res = dhcp_option_parse(dhcp, size, check_options, NULL);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(res == DHCP_DISCOVER);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (verbose)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers printf(" recv DHCP Discover 0x%08x\n", be32toh(dhcp->xid));
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic void test_discover_message(sd_event *e)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers{
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_dhcp_client *client;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers int res, r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (verbose)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers printf("* %s\n", __FUNCTION__);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers r = sd_dhcp_client_new(&client);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_dhcp_client_attach_event(client, e, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_index(client, 42) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_mac(client, &mac_addr) >= 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_request_option(client, 248) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering callback_recv = test_discover_message_verify;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers res = sd_dhcp_client_start(client);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(res == 0 || res == -EINPROGRESS);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers sd_event_run(e, (uint64_t) -1);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_dhcp_client_stop(client);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers sd_dhcp_client_free(client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers test_fd[1] = safe_close(test_fd[1]);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers callback_recv = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic uint8_t test_addr_acq_offer[] = {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x80, 0x11, 0xb3, 0x84, 0xc0, 0xa8, 0x02, 0x01,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0xc0, 0xa8, 0x02, 0xbf, 0x00, 0x43, 0x00, 0x44,
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers 0x01, 0x34, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x6f, 0x95, 0x2f, 0x30, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x02, 0xbf,
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna 0xc0, 0xa8, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x02, 0x36,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x33, 0x04, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x02, 0x58, 0x01, 0x04, 0xff, 0xff, 0xff,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x2a, 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x0f,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x09, 0x6c, 0x61, 0x62, 0x2e, 0x69, 0x6e, 0x74,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x72, 0x61, 0x03, 0x04, 0xc0, 0xa8, 0x02, 0x01,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering};
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic uint8_t test_addr_acq_ack[] = {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x80, 0x11, 0xb3, 0x84, 0xc0, 0xa8, 0x02, 0x01,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0xc0, 0xa8, 0x02, 0xbf, 0x00, 0x43, 0x00, 0x44,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x01, 0x34, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x02, 0xbf,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0xc0, 0xa8, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00,
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x36,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x33, 0x04, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x02, 0x58, 0x01, 0x04, 0xff, 0xff, 0xff,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x2a, 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x0f,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x09, 0x6c, 0x61, 0x62, 0x2e, 0x69, 0x6e, 0x74,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x72, 0x61, 0x03, 0x04, 0xc0, 0xa8, 0x02, 0x01,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering};
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic void test_addr_acq_acquired(sd_dhcp_client *client, int event,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering void *userdata) {
2f7a4867babd3fd382e5495f21724358f30fa67dMichal Sekletar sd_event *e = userdata;
2f7a4867babd3fd382e5495f21724358f30fa67dMichal Sekletar sd_dhcp_lease *lease;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct in_addr addr;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(event == DHCP_EVENT_IP_ACQUIRE);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_get_lease(client, &lease) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(lease);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_lease_get_address(lease, &addr) >= 0);
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[44],
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering sizeof(addr.s_addr)) == 0);
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_lease_get_netmask(lease, &addr) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[285],
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sizeof(addr.s_addr)) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_lease_get_router(lease, &addr) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[308],
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek sizeof(addr.s_addr)) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (verbose)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering printf(" DHCP address acquired\n");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_event_exit(e, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnastatic int test_addr_acq_recv_request(size_t size, DHCPMessage *request) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna uint16_t udp_check = 0;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna uint8_t *msg_bytes = (uint8_t *)request;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna int res;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna res = dhcp_option_parse(request, size, check_options, NULL);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert_se(res == DHCP_REQUEST);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert_se(xid == request->xid);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer assert_se(msg_bytes[size - 1] == DHCP_OPTION_END);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (verbose)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna printf(" recv DHCP Request 0x%08x\n", be32toh(xid));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna memcpy(&test_addr_acq_ack[26], &udp_check, sizeof(udp_check));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna memcpy(&test_addr_acq_ack[32], &xid, sizeof(xid));
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek memcpy(&test_addr_acq_ack[56], &mac_addr.ether_addr_octet,
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna ETHER_ADDR_LEN);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna callback_recv = NULL;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna res = write(test_fd[1], test_addr_acq_ack,
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna sizeof(test_addr_acq_ack));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert_se(res == sizeof(test_addr_acq_ack));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (verbose)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna printf(" send DHCP Ack\n");
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return 0;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna};
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmekstatic int test_addr_acq_recv_discover(size_t size, DHCPMessage *discover) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna uint16_t udp_check = 0;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek uint8_t *msg_bytes = (uint8_t *)discover;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek int res;
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek res = dhcp_option_parse(discover, size, check_options, NULL);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert_se(res == DHCP_DISCOVER);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert_se(msg_bytes[size - 1] == DHCP_OPTION_END);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna xid = discover->xid;
8333c77edf8fd1654cd96f3f6ee0f078dd64b58bZbigniew Jędrzejewski-Szmek
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (verbose)
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek printf(" recv DHCP Discover 0x%08x\n", be32toh(xid));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna memcpy(&test_addr_acq_offer[26], &udp_check, sizeof(udp_check));
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek memcpy(&test_addr_acq_offer[32], &xid, sizeof(xid));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna memcpy(&test_addr_acq_offer[56], &mac_addr.ether_addr_octet,
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna ETHER_ADDR_LEN);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek callback_recv = test_addr_acq_recv_request;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna res = write(test_fd[1], test_addr_acq_offer,
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna sizeof(test_addr_acq_offer));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna assert_se(res == sizeof(test_addr_acq_offer));
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna if (verbose)
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna printf(" sent DHCP Offer\n");
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return 0;
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek}
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagnastatic void test_addr_acq(sd_event *e) {
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna usec_t time_now = now(CLOCK_MONOTONIC);
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek sd_dhcp_client *client;
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna int res, r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek if (verbose)
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek printf("* %s\n", __FUNCTION__);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = sd_dhcp_client_new(&client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen r = sd_dhcp_client_attach_event(client, e, 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(r >= 0);
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_index(client, 42) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_dhcp_client_set_mac(client, &mac_addr) >= 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(sd_dhcp_client_set_callback(client, test_addr_acq_acquired, e)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering callback_recv = test_addr_acq_recv_discover;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_event_add_time(e, &test_hangcheck,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering CLOCK_MONOTONIC,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering time_now + 2 * USEC_PER_SEC, 0,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_dhcp_hangcheck, NULL) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering res = sd_dhcp_client_start(client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(res == 0 || res == -EINPROGRESS);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_event_loop(e);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_hangcheck = sd_event_source_unref(test_hangcheck);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers sd_dhcp_client_set_callback(client, NULL, NULL);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_dhcp_client_stop(client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_dhcp_client_free(client);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_fd[1] = safe_close(test_fd[1]);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering callback_recv = NULL;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers xid = 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint main(int argc, char *argv[]) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering sd_event *e;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(sd_event_new(&e) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_request_basic(e);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_checksum();
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_discover_message(e);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_addr_acq(e);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering