2022-02-07 06:35:29 +05:30
|
|
|
// Copyright 2011-2018 Nicholas J. Kain <njkain at gmail dot com>
|
|
|
|
// SPDX-License-Identifier: MIT
|
2011-04-20 02:07:43 +05:30
|
|
|
#include <stdlib.h>
|
2014-03-19 13:42:24 +05:30
|
|
|
#include <stdio.h>
|
2011-04-20 02:07:43 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2014-03-31 02:32:48 +05:30
|
|
|
#include "nk/log.h"
|
|
|
|
#include "nk/io.h"
|
2014-03-11 05:12:52 +05:30
|
|
|
#include "leasefile.h"
|
2014-03-19 13:42:24 +05:30
|
|
|
#include "ndhc.h"
|
2011-04-20 02:07:43 +05:30
|
|
|
|
|
|
|
static int leasefilefd = -1;
|
|
|
|
|
2014-03-19 13:42:24 +05:30
|
|
|
static void get_leasefile_path(char *leasefile, size_t dlen, char *ifname)
|
2011-04-20 02:07:43 +05:30
|
|
|
{
|
2014-03-21 08:56:19 +05:30
|
|
|
int splen = snprintf(leasefile, dlen, "%s/LEASE-%s",
|
2014-03-19 13:42:24 +05:30
|
|
|
state_dir, ifname);
|
2014-03-31 02:51:27 +05:30
|
|
|
if (splen < 0)
|
|
|
|
suicide("%s: (%s) snprintf failed; return=%d",
|
|
|
|
client_config.interface, __func__, splen);
|
|
|
|
if ((size_t)splen >= dlen)
|
2020-11-25 07:32:51 +05:30
|
|
|
suicide("%s: (%s) snprintf dest buffer too small %d >= %zu",
|
2014-03-31 02:51:27 +05:30
|
|
|
client_config.interface, __func__, splen, sizeof dlen);
|
2011-04-20 02:07:43 +05:30
|
|
|
}
|
|
|
|
|
2014-03-11 05:12:52 +05:30
|
|
|
void open_leasefile(void)
|
2011-04-20 02:07:43 +05:30
|
|
|
{
|
2014-03-19 13:42:24 +05:30
|
|
|
char leasefile[PATH_MAX];
|
|
|
|
get_leasefile_path(leasefile, sizeof leasefile, client_config.interface);
|
|
|
|
leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
|
2014-03-31 02:51:27 +05:30
|
|
|
if (leasefilefd < 0)
|
2020-11-07 05:17:51 +05:30
|
|
|
suicide("%s: (%s) Failed to create lease file '%s': %s",
|
|
|
|
client_config.interface, __func__, leasefile, strerror(errno));
|
2011-04-20 02:07:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void write_leasefile(struct in_addr ipnum)
|
|
|
|
{
|
2014-03-20 13:37:12 +05:30
|
|
|
char ip[INET_ADDRSTRLEN];
|
|
|
|
char out[INET_ADDRSTRLEN*2];
|
|
|
|
if (leasefilefd < 0) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) leasefile fd < 0; no leasefile will be written",
|
|
|
|
client_config.interface, __func__);
|
2011-04-20 02:07:43 +05:30
|
|
|
return;
|
2014-03-20 13:37:12 +05:30
|
|
|
}
|
2011-04-20 02:07:43 +05:30
|
|
|
inet_ntop(AF_INET, &ipnum, ip, sizeof ip);
|
2014-03-20 13:37:12 +05:30
|
|
|
ssize_t olen = snprintf(out, sizeof out, "%s\n", ip);
|
|
|
|
if (olen < 0 || (size_t)olen >= sizeof ip) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) snprintf failed; return=%zd",
|
|
|
|
client_config.interface, __func__, olen);
|
2014-03-20 13:37:12 +05:30
|
|
|
return;
|
|
|
|
}
|
2020-11-07 05:17:51 +05:30
|
|
|
if (safe_ftruncate(leasefilefd, 0)) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) Failed to truncate lease file: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
2020-11-07 05:17:51 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lseek(leasefilefd, 0, SEEK_SET) == (off_t)-1) {
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) Failed to seek to start of lease file: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
2020-11-07 05:17:51 +05:30
|
|
|
return;
|
2011-04-20 02:07:43 +05:30
|
|
|
}
|
2014-04-04 13:31:49 +05:30
|
|
|
size_t outlen = strlen(out);
|
2020-11-07 05:17:51 +05:30
|
|
|
ssize_t ret = safe_write(leasefilefd, out, outlen);
|
2014-04-04 13:31:49 +05:30
|
|
|
if (ret < 0 || (size_t)ret != outlen)
|
2020-11-25 07:32:51 +05:30
|
|
|
log_line("%s: (%s) Failed to write ip to lease file.",
|
|
|
|
client_config.interface, __func__);
|
2011-06-28 23:17:36 +05:30
|
|
|
else
|
|
|
|
fsync(leasefilefd);
|
2011-04-20 02:07:43 +05:30
|
|
|
}
|
2013-05-06 16:37:54 +05:30
|
|
|
|