sd-dhcp-lease.c revision 3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8
/***
This file is part of systemd.
Copyright (C) 2013 Intel Corporation. All rights reserved.
Copyright (C) 2014 Tom Gundersen
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 <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include "fileio.h"
#include "unaligned.h"
#include "in-addr-util.h"
#include "hostname-util.h"
#include "dhcp-protocol.h"
#include "dhcp-lease-internal.h"
#include "sd-dhcp-lease.h"
#include "network-internal.h"
#include "dns-domain.h"
return 0;
}
return 0;
}
else
return -ENOENT;
return 0;
}
} else
return -ENOENT;
return 0;
}
} else
return -ENOENT;
return 0;
}
if (lease->domainname)
else
return -ENOENT;
return 0;
}
else
return -ENOENT;
return 0;
}
else
return -ENOENT;
return 0;
}
else
return -ENOENT;
return 0;
}
return 0;
}
return 0;
}
return 0;
}
if (lease->static_route_size) {
return lease->static_route_size;
} else
return -ENOENT;
return 0;
}
if (!lease->vendor_specific)
return -ENOENT;
return 0;
}
if (!lease)
return NULL;
return lease;
}
if (!lease)
return NULL;
return NULL;
while (lease->private_options) {
}
return NULL;
}
if (len == 4) {
}
}
}
if (len == 2) {
}
}
if (len == 4)
}
if (len == 1)
}
if (len == 1) {
}
}
if (len >= 1) {
char *string;
return -EINVAL;
if (!string)
return -ENOMEM;
} else
return 0;
}
static int lease_parse_in_addrs_aux(const uint8_t *option, size_t len, struct in_addr **ret, size_t *ret_size, size_t mult) {
if (!addresses)
return -ENOMEM;
}
return 0;
}
static int lease_parse_in_addrs(const uint8_t *option, size_t len, struct in_addr **ret, size_t *ret_size) {
}
static int lease_parse_in_addrs_pairs(const uint8_t *option, size_t len, struct in_addr **ret, size_t *ret_size) {
}
if (!len)
return 0;
if (len % 8 != 0)
return -EINVAL;
return -ENOMEM;
while (len >= 8) {
int r;
if (r < 0) {
log_error("Failed to determine destination prefix length from class based IP, ignoring");
continue;
}
option += 4;
option += 4;
len -= 8;
(*routes_size)++;
}
return 0;
}
/* parses RFC3442 Classless Static Route Option */
static int lease_parse_classless_routes(const uint8_t *option, size_t len, struct sd_dhcp_route **routes,
/* option format: (subnet-mask-width significant-subnet-octets gateway-ip)* */
while (len > 0) {
struct sd_dhcp_route *route;
return -ENOMEM;
option++;
len--;
/* can't have more than 4 octets in IPv4 */
return -EINVAL;
option += dst_octets;
len -= dst_octets;
if (len < 4)
return -EINVAL;
option += 4;
len -= 4;
(*routes_size)++;
}
return 0;
}
int r;
switch(code) {
case DHCP_OPTION_TIME_OFFSET:
break;
break;
break;
break;
case DHCP_OPTION_SUBNET_MASK:
break;
case DHCP_OPTION_BROADCAST:
break;
case DHCP_OPTION_ROUTER:
if (len >= 4)
break;
case DHCP_OPTION_NTP_SERVER:
case DHCP_OPTION_STATIC_ROUTE:
return lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size, &lease->static_route_allocated);
break;
break;
break;
break;
case DHCP_OPTION_DOMAIN_NAME: {
if (r < 0)
return r;
if (r < 0) {
return 0;
}
if (is_localhost(normalized)) {
log_debug_errno(r, "Detected 'localhost' as suggested hostname, ignoring.");
break;
}
normalized = NULL;
break;
}
case DHCP_OPTION_HOST_NAME: {
if (r < 0)
return r;
if (r < 0) {
return 0;
}
if (is_localhost(normalized)) {
log_debug_errno(r, "Detected 'localhost' as suggested hostname, ignoring.");
return 0;
}
normalized = NULL;
break;
}
case DHCP_OPTION_ROOT_PATH:
break;
break;
break;
break;
return lease_parse_classless_routes(
case DHCP_OPTION_NEW_TZDB_TIMEZONE: {
if (r < 0)
return r;
if (!timezone_is_valid(tz))
return -EINVAL;
break;
}
if (len >= 1) {
if (!lease->vendor_specific)
return -ENOMEM;
}
break;
default:
break;
if (r < 0)
return r;
}
return 0;
}
break;
return 0;
}
}
if (!option)
return -ENOMEM;
return -ENOMEM;
}
return 0;
}
if (!lease)
return -ENOMEM;
return 0;
}
struct sd_dhcp_raw_option *option;
const char *string;
struct sd_dhcp_route *routes;
int r;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
fprintf(f,
"# This is private data. Do not parse.\n"
if (r < 0)
goto fail;
if (r >= 0)
if (r >= 0)
fprintf(f, "SERVER_ADDRESS=%s\n",
if (r >= 0)
if (r >= 0)
fputs("DNS=", f);
if (r >= 0)
serialize_in_addrs(f, addresses, r);
fputs("\n", f);
fputs("NTP=", f);
if (r >= 0)
serialize_in_addrs(f, addresses, r);
fputs("\n", f);
if (r >= 0)
if (r >= 0)
if (r >= 0)
if (r >= 0)
if (r >= 0)
if (r >= 0) {
_cleanup_free_ char *client_id_hex;
if (!client_id_hex) {
r = -ENOMEM;
goto fail;
}
}
if (r >= 0) {
if (!option_hex) {
r = -ENOMEM;
goto fail;
}
}
if (r < 0)
goto fail;
}
r = fflush_and_check(f);
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
return 0;
fail:
if (temp_path)
}
int r, i;
r = dhcp_lease_new(&lease);
if (r < 0)
return r;
"ADDRESS", &address,
"ROUTER", &router,
"NETMASK", &netmask,
"SERVER_IDENTIFIER", &server_address,
"NEXT_SERVER", &next_server,
"DNS", &dns,
"NTP", &ntp,
"MTU", &mtu,
"ROUTES", &routes,
"CLIENTID", &client_id_hex,
"VENDOR_SPECIFIC", &vendor_specific_hex,
"OPTION_224", &options[0],
NULL);
if (r < 0) {
if (r == -ENOENT)
return 0;
}
if (r < 0)
return r;
if (router) {
if (r < 0)
return r;
}
if (r < 0)
return r;
if (server_address) {
if (r < 0)
return r;
}
if (next_server) {
if (r < 0)
return r;
}
if (dns) {
if (r < 0)
return r;
}
if (ntp) {
if (r < 0)
return r;
}
if (mtu) {
uint16_t u;
}
if (routes) {
if (r < 0)
return r;
}
if (client_id_hex) {
if (r < 0)
return r;
}
if (vendor_specific_hex) {
r = deserialize_dhcp_option(&lease->vendor_specific, &lease->vendor_specific_len, vendor_specific_hex);
if (r < 0)
return r;
}
for (i = 0; i <= DHCP_OPTION_PRIVATE_LAST - DHCP_OPTION_PRIVATE_BASE; i++) {
if (!options[i])
continue;
if (r < 0)
return r;
if (r < 0)
return r;
}
return 0;
}
int r;
/* fall back to the default subnet masks based on address class */
if (r < 0)
return r;
return 0;
}
size_t *client_id_len) {
return 0;
}
lease->client_id_len = 0;
if (client_id) {
}
return 0;
}
return -ENXIO;
return 0;
}