From 011feef852de96a1adaba476037ce01b5efefc35 Mon Sep 17 00:00:00 2001 From: Patrik Flykt Date: Mon, 9 Dec 2013 23:43:08 +0200 Subject: dhcp: Add DHCP client initialization Provide functionality for initializing a DHCP client struct, setting interface index, last used address and additional options to request. On initialization the most useful options are added by default. --- src/dhcp/protocol.h | 104 ------------------------------ src/libsystemd-dhcp/dhcp-client.c | 125 ++++++++++++++++++++++++++++++++++++ src/libsystemd-dhcp/dhcp-protocol.h | 104 ++++++++++++++++++++++++++++++ src/systemd/sd-dhcp-client.h | 36 +++++++++++ 4 files changed, 265 insertions(+), 104 deletions(-) delete mode 100644 src/dhcp/protocol.h create mode 100644 src/libsystemd-dhcp/dhcp-client.c create mode 100644 src/libsystemd-dhcp/dhcp-protocol.h create mode 100644 src/systemd/sd-dhcp-client.h (limited to 'src') diff --git a/src/dhcp/protocol.h b/src/dhcp/protocol.h deleted file mode 100644 index d37546721a..0000000000 --- a/src/dhcp/protocol.h +++ /dev/null @@ -1,104 +0,0 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - -#pragma once - -/*** - This file is part of systemd. - - Copyright (C) 2013 Intel Corporation. All rights reserved. - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see . -***/ - -#include -#include -#include - -#include "macro.h" -#include "sparse-endian.h" - -struct DHCPMessage { - uint8_t op; - uint8_t htype; - uint8_t hlen; - uint8_t hops; - be32_t xid; - be16_t secs; - be16_t flags; - uint32_t ciaddr; - uint32_t yiaddr; - uint32_t siaddr; - uint32_t giaddr; - uint8_t chaddr[16]; - uint8_t sname[64]; - uint8_t file[128]; -} _packed_; - -typedef struct DHCPMessage DHCPMessage; - -struct DHCPPacket { - struct iphdr ip; - struct udphdr udp; - DHCPMessage dhcp; -} _packed_; - -typedef struct DHCPPacket DHCPPacket; - -enum DHCPState { - DHCP_STATE_INIT = 0, - DHCP_STATE_SELECTING = 1, - DHCP_STATE_INIT_REBOOT = 2, - DHCP_STATE_REBOOTING = 3, - DHCP_STATE_REQUESTING = 4, - DHCP_STATE_BOUND = 5, - DHCP_STATE_RENEWING = 6, - DHCP_STATE_REBINDING = 7, -}; - -typedef enum DHCPState DHCPState; - -enum { - BOOTREQUEST = 1, - BOOTREPLY = 2, -}; - -enum { - DHCP_DISCOVER = 1, - DHCP_OFFER = 2, - DHCP_REQUEST = 3, - DHCP_DECLINE = 4, - DHCP_ACK = 5, - DHCP_NAK = 6, - DHCP_RELEASE = 7, -}; - -enum { - DHCP_OVERLOAD_FILE = 1, - DHCP_OVERLOAD_SNAME = 2, -}; - -enum { - DHCP_OPTION_PAD = 0, - DHCP_OPTION_SUBNET_MASK = 1, - DHCP_OPTION_ROUTER = 3, - DHCP_OPTION_DOMAIN_NAME_SERVER = 6, - DHCP_OPTION_HOST_NAME = 12, - DHCP_OPTION_DOMAIN_NAME = 15, - DHCP_OPTION_NTP_SERVER = 42, - DHCP_OPTION_REQUESTED_IP_ADDRESS = 50, - DHCP_OPTION_OVERLOAD = 52, - DHCP_OPTION_MESSAGE_TYPE = 53, - DHCP_OPTION_PARAMETER_REQUEST_LIST = 55, - DHCP_OPTION_END = 255, -}; diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c new file mode 100644 index 0000000000..85c8040da4 --- /dev/null +++ b/src/libsystemd-dhcp/dhcp-client.c @@ -0,0 +1,125 @@ +/*** + This file is part of systemd. + + Copyright (C) 2013 Intel Corporation. All rights reserved. + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include + +#include "util.h" +#include "list.h" + +#include "dhcp-protocol.h" +#include "sd-dhcp-client.h" + +struct sd_dhcp_client { + DHCPState state; + int index; + uint8_t *req_opts; + size_t req_opts_size; + uint32_t last_addr; +}; + +static const uint8_t default_req_opts[] = { + DHCP_OPTION_SUBNET_MASK, + DHCP_OPTION_ROUTER, + DHCP_OPTION_HOST_NAME, + DHCP_OPTION_DOMAIN_NAME, + DHCP_OPTION_DOMAIN_NAME_SERVER, + DHCP_OPTION_NTP_SERVER, +}; + +int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) +{ + size_t i; + + assert_return(client, -EINVAL); + assert_return (client->state == DHCP_STATE_INIT, -EBUSY); + + switch(option) { + case DHCP_OPTION_PAD: + case DHCP_OPTION_OVERLOAD: + case DHCP_OPTION_MESSAGE_TYPE: + case DHCP_OPTION_PARAMETER_REQUEST_LIST: + case DHCP_OPTION_END: + return -EINVAL; + + default: + break; + } + + for (i = 0; i < client->req_opts_size; i++) + if (client->req_opts[i] == option) + return -EEXIST; + + if (!GREEDY_REALLOC(client->req_opts, client->req_opts_size, + client->req_opts_size + 1)) + return -ENOMEM; + + client->req_opts[client->req_opts_size - 1] = option; + + return 0; +} + +int sd_dhcp_client_set_request_address(sd_dhcp_client *client, + const struct in_addr *last_addr) +{ + assert_return(client, -EINVAL); + assert_return(client->state == DHCP_STATE_INIT, -EBUSY); + + if (last_addr) + client->last_addr = last_addr->s_addr; + else + client->last_addr = INADDR_ANY; + + return 0; +} + +int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) +{ + assert_return(client, -EINVAL); + assert_return(client->state == DHCP_STATE_INIT, -EBUSY); + assert_return(interface_index >= -1, -EINVAL); + + client->index = interface_index; + + return 0; +} + +sd_dhcp_client *sd_dhcp_client_new(void) +{ + sd_dhcp_client *client; + + client = new0(sd_dhcp_client, 1); + if (!client) + return NULL; + + client->state = DHCP_STATE_INIT; + client->index = -1; + + client->req_opts_size = ELEMENTSOF(default_req_opts); + + client->req_opts = memdup(default_req_opts, client->req_opts_size); + if (!client->req_opts) { + free(client); + return NULL; + } + + return client; +} diff --git a/src/libsystemd-dhcp/dhcp-protocol.h b/src/libsystemd-dhcp/dhcp-protocol.h new file mode 100644 index 0000000000..d37546721a --- /dev/null +++ b/src/libsystemd-dhcp/dhcp-protocol.h @@ -0,0 +1,104 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright (C) 2013 Intel Corporation. All rights reserved. + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include + +#include "macro.h" +#include "sparse-endian.h" + +struct DHCPMessage { + uint8_t op; + uint8_t htype; + uint8_t hlen; + uint8_t hops; + be32_t xid; + be16_t secs; + be16_t flags; + uint32_t ciaddr; + uint32_t yiaddr; + uint32_t siaddr; + uint32_t giaddr; + uint8_t chaddr[16]; + uint8_t sname[64]; + uint8_t file[128]; +} _packed_; + +typedef struct DHCPMessage DHCPMessage; + +struct DHCPPacket { + struct iphdr ip; + struct udphdr udp; + DHCPMessage dhcp; +} _packed_; + +typedef struct DHCPPacket DHCPPacket; + +enum DHCPState { + DHCP_STATE_INIT = 0, + DHCP_STATE_SELECTING = 1, + DHCP_STATE_INIT_REBOOT = 2, + DHCP_STATE_REBOOTING = 3, + DHCP_STATE_REQUESTING = 4, + DHCP_STATE_BOUND = 5, + DHCP_STATE_RENEWING = 6, + DHCP_STATE_REBINDING = 7, +}; + +typedef enum DHCPState DHCPState; + +enum { + BOOTREQUEST = 1, + BOOTREPLY = 2, +}; + +enum { + DHCP_DISCOVER = 1, + DHCP_OFFER = 2, + DHCP_REQUEST = 3, + DHCP_DECLINE = 4, + DHCP_ACK = 5, + DHCP_NAK = 6, + DHCP_RELEASE = 7, +}; + +enum { + DHCP_OVERLOAD_FILE = 1, + DHCP_OVERLOAD_SNAME = 2, +}; + +enum { + DHCP_OPTION_PAD = 0, + DHCP_OPTION_SUBNET_MASK = 1, + DHCP_OPTION_ROUTER = 3, + DHCP_OPTION_DOMAIN_NAME_SERVER = 6, + DHCP_OPTION_HOST_NAME = 12, + DHCP_OPTION_DOMAIN_NAME = 15, + DHCP_OPTION_NTP_SERVER = 42, + DHCP_OPTION_REQUESTED_IP_ADDRESS = 50, + DHCP_OPTION_OVERLOAD = 52, + DHCP_OPTION_MESSAGE_TYPE = 53, + DHCP_OPTION_PARAMETER_REQUEST_LIST = 55, + DHCP_OPTION_END = 255, +}; diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h new file mode 100644 index 0000000000..9b19a1dc97 --- /dev/null +++ b/src/systemd/sd-dhcp-client.h @@ -0,0 +1,36 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#ifndef foosddhcpclienthfoo +#define foosddhcpclienthfoo + +/*** + This file is part of systemd. + + Copyright (C) 2013 Intel Corporation. All rights reserved. + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include + +typedef struct sd_dhcp_client sd_dhcp_client; + +int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option); +int sd_dhcp_client_set_request_address(sd_dhcp_client *client, + const struct in_addr *last_address); +int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index); + +sd_dhcp_client *sd_dhcp_client_new(void); + +#endif -- cgit v1.2.3-54-g00ecf