2010-04-26 13:52:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2010 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2010 Satoru SATOH <satoru.satoh@gmail.com>
|
|
|
|
*
|
|
|
|
* This library 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.
|
|
|
|
*
|
|
|
|
* This library 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 this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Based on iptables.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_PATHS_H
|
|
|
|
# include <paths.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "datatypes.h"
|
|
|
|
#include "dnsmasq.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
#include "logging.h"
|
2010-11-17 02:13:29 +00:00
|
|
|
#include "files.h"
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NETWORK
|
|
|
|
#define DNSMASQ_HOSTSFILE_SUFFIX "hostsfile"
|
|
|
|
|
|
|
|
static void
|
|
|
|
dhcphostFree(dnsmasqDhcpHost *host)
|
|
|
|
{
|
|
|
|
VIR_FREE(host->host);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hostsfileFree(dnsmasqHostsfile *hostsfile)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (hostsfile->hosts) {
|
|
|
|
for (i = 0; i < hostsfile->nhosts; i++)
|
|
|
|
dhcphostFree(&hostsfile->hosts[i]);
|
|
|
|
|
|
|
|
VIR_FREE(hostsfile->hosts);
|
|
|
|
|
|
|
|
hostsfile->nhosts = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(hostsfile->path);
|
|
|
|
|
|
|
|
VIR_FREE(hostsfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostsfileAdd(dnsmasqHostsfile *hostsfile,
|
|
|
|
const char *mac,
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
virSocketAddr *ip,
|
2010-04-26 13:52:02 +00:00
|
|
|
const char *name)
|
|
|
|
{
|
2010-10-22 16:22:20 +00:00
|
|
|
char *ipstr = NULL;
|
2010-04-26 13:52:02 +00:00
|
|
|
if (VIR_REALLOC_N(hostsfile->hosts, hostsfile->nhosts + 1) < 0)
|
|
|
|
goto alloc_error;
|
|
|
|
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
if (!(ipstr = virSocketFormatAddr(ip)))
|
|
|
|
return -1;
|
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
if (name) {
|
|
|
|
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s,%s",
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
mac, ipstr, name) < 0) {
|
2010-04-26 13:52:02 +00:00
|
|
|
goto alloc_error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s",
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
mac, ipstr) < 0) {
|
2010-04-26 13:52:02 +00:00
|
|
|
goto alloc_error;
|
|
|
|
}
|
|
|
|
}
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
VIR_FREE(ipstr);
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
hostsfile->nhosts++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
alloc_error:
|
2010-05-03 10:26:42 +00:00
|
|
|
virReportOOMError();
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
VIR_FREE(ipstr);
|
2010-04-26 13:52:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dnsmasqHostsfile *
|
|
|
|
hostsfileNew(const char *name,
|
|
|
|
const char *config_dir)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
dnsmasqHostsfile *hostsfile;
|
|
|
|
|
2010-05-03 10:26:42 +00:00
|
|
|
if (VIR_ALLOC(hostsfile) < 0) {
|
|
|
|
virReportOOMError();
|
2010-04-26 13:52:02 +00:00
|
|
|
return NULL;
|
2010-05-03 10:26:42 +00:00
|
|
|
}
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
hostsfile->hosts = NULL;
|
|
|
|
hostsfile->nhosts = 0;
|
|
|
|
|
|
|
|
if (virAsprintf(&hostsfile->path, "%s/%s.%s", config_dir, name,
|
2010-05-03 10:26:42 +00:00
|
|
|
DNSMASQ_HOSTSFILE_SUFFIX) < 0) {
|
|
|
|
virReportOOMError();
|
2010-04-26 13:52:02 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = virFileMakePath(config_dir))) {
|
|
|
|
virReportSystemError(err, _("cannot create config directory '%s'"),
|
2010-05-03 10:26:42 +00:00
|
|
|
config_dir);
|
2010-04-26 13:52:02 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostsfile;
|
|
|
|
|
|
|
|
error:
|
|
|
|
hostsfileFree(hostsfile);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostsfileWrite(const char *path,
|
|
|
|
dnsmasqDhcpHost *hosts,
|
|
|
|
unsigned int nhosts)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
FILE *f;
|
|
|
|
bool istmp = true;
|
|
|
|
unsigned int i;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (nhosts == 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (virAsprintf(&tmp, "%s.new", path) < 0)
|
2011-04-27 18:11:14 +00:00
|
|
|
return -ENOMEM;
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
if (!(f = fopen(tmp, "w"))) {
|
|
|
|
istmp = false;
|
|
|
|
if (!(f = fopen(path, "w"))) {
|
2011-04-27 18:11:14 +00:00
|
|
|
rc = -errno;
|
2010-04-26 13:52:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nhosts; i++) {
|
|
|
|
if (fputs(hosts[i].host, f) == EOF || fputc('\n', f) == EOF) {
|
2011-04-27 18:11:14 +00:00
|
|
|
rc = -errno;
|
2010-11-17 02:13:29 +00:00
|
|
|
VIR_FORCE_FCLOSE(f);
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
if (istmp)
|
|
|
|
unlink(tmp);
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-17 02:13:29 +00:00
|
|
|
if (VIR_FCLOSE(f) == EOF) {
|
2011-04-27 18:11:14 +00:00
|
|
|
rc = -errno;
|
2010-04-26 13:52:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (istmp) {
|
|
|
|
if (rename(tmp, path) < 0) {
|
2011-04-27 18:11:14 +00:00
|
|
|
rc = -errno;
|
2010-04-26 13:52:02 +00:00
|
|
|
unlink(tmp);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlink(tmp) < 0) {
|
2011-04-27 18:11:14 +00:00
|
|
|
rc = -errno;
|
2010-04-26 13:52:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(tmp);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostsfileSave(dnsmasqHostsfile *hostsfile)
|
|
|
|
{
|
|
|
|
int err = hostsfileWrite(hostsfile->path, hostsfile->hosts,
|
2010-05-03 10:26:42 +00:00
|
|
|
hostsfile->nhosts);
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
virReportSystemError(err, _("cannot write config file '%s'"),
|
2010-05-03 10:26:42 +00:00
|
|
|
hostsfile->path);
|
2010-04-26 13:52:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostsfileDelete(dnsmasqHostsfile *hostsfile)
|
|
|
|
{
|
|
|
|
if (!virFileExists(hostsfile->path))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (unlink(hostsfile->path) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot remove config file '%s'"),
|
2010-05-03 10:26:42 +00:00
|
|
|
hostsfile->path);
|
2010-04-26 13:52:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqContextNew:
|
|
|
|
*
|
|
|
|
* Create a new Dnsmasq context
|
|
|
|
*
|
|
|
|
* Returns a pointer to the new structure or NULL in case of error
|
|
|
|
*/
|
|
|
|
dnsmasqContext *
|
|
|
|
dnsmasqContextNew(const char *network_name,
|
|
|
|
const char *config_dir)
|
|
|
|
{
|
|
|
|
dnsmasqContext *ctx;
|
|
|
|
|
2010-05-03 10:26:42 +00:00
|
|
|
if (VIR_ALLOC(ctx) < 0) {
|
|
|
|
virReportOOMError();
|
2010-04-26 13:52:02 +00:00
|
|
|
return NULL;
|
2010-05-03 10:26:42 +00:00
|
|
|
}
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
if (!(ctx->hostsfile = hostsfileNew(network_name, config_dir)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
|
|
|
|
error:
|
|
|
|
dnsmasqContextFree(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqContextFree:
|
|
|
|
* @ctx: pointer to the dnsmasq context
|
|
|
|
*
|
|
|
|
* Free the resources associated with an dnsmasq context
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dnsmasqContextFree(dnsmasqContext *ctx)
|
|
|
|
{
|
|
|
|
if (!ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ctx->hostsfile)
|
|
|
|
hostsfileFree(ctx->hostsfile);
|
|
|
|
|
|
|
|
VIR_FREE(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqAddDhcpHost:
|
|
|
|
* @ctx: pointer to the dnsmasq context for each network
|
|
|
|
* @mac: pointer to the string contains mac address of the host
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
* @ip: pointer to the socket address contains ip of the host
|
2010-04-26 13:52:02 +00:00
|
|
|
* @name: pointer to the string contains hostname of the host or NULL
|
|
|
|
*
|
|
|
|
* Add dhcp-host entry.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dnsmasqAddDhcpHost(dnsmasqContext *ctx,
|
|
|
|
const char *mac,
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
virSocketAddr *ip,
|
2010-04-26 13:52:02 +00:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
if (ctx->hostsfile)
|
|
|
|
hostsfileAdd(ctx->hostsfile, mac, ip, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqSave:
|
|
|
|
* @ctx: pointer to the dnsmasq context for each network
|
|
|
|
*
|
|
|
|
* Saves all the configurations associated with a context to disk.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dnsmasqSave(const dnsmasqContext *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->hostsfile)
|
|
|
|
return hostsfileSave(ctx->hostsfile);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqDelete:
|
|
|
|
* @ctx: pointer to the dnsmasq context for each network
|
|
|
|
*
|
|
|
|
* Delete all the configuration files associated with a context.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dnsmasqDelete(const dnsmasqContext *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->hostsfile)
|
|
|
|
return hostsfileDelete(ctx->hostsfile);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqReload:
|
|
|
|
* @pid: the pid of the target dnsmasq process
|
|
|
|
*
|
|
|
|
* Reloads all the configurations associated to a context
|
|
|
|
*/
|
|
|
|
int
|
2010-05-03 18:23:50 +00:00
|
|
|
dnsmasqReload(pid_t pid ATTRIBUTE_UNUSED)
|
2010-04-26 13:52:02 +00:00
|
|
|
{
|
2010-05-03 18:23:50 +00:00
|
|
|
#ifndef WIN32
|
2010-04-26 13:52:02 +00:00
|
|
|
if (kill(pid, SIGHUP) != 0) {
|
|
|
|
virReportSystemError(errno,
|
2010-05-03 18:23:50 +00:00
|
|
|
_("Failed to make dnsmasq (PID: %d) reload config files."),
|
2010-04-26 13:52:02 +00:00
|
|
|
pid);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-03 18:23:50 +00:00
|
|
|
#endif /* WIN32 */
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|