2010-04-26 13:52:02 +00:00
|
|
|
/*
|
2012-12-12 16:43:54 +00:00
|
|
|
* virdnsmasq.h: Helper APIs for managing dnsmasq
|
|
|
|
*
|
util: capabilities detection for dnsmasq
In order to optionally take advantage of new features in dnsmasq when
the host's version of dnsmasq supports them, but still be able to run
on hosts that don't support the new features, we need to be able to
detect the version of dnsmasq running on the host, and possibly
determine from the help output what options are in this dnsmasq.
This patch implements a greatly simplified version of the capabilities
code we already have for qemu. A dnsmasqCaps device can be created and
populated either from running a program on disk, reading a file with
the concatenated output of "dnsmasq --version; dnsmasq --help", or
examining a buffer in memory that contains the concatenated output of
those two commands. Simple functions to retrieve capabilities flags,
the version number, and the path of the binary are also included.
bridge_driver.c creates a single dnsmasqCaps object at driver startup,
and disposes of it at driver shutdown. Any time it must be used, the
dnsmasqCapsRefresh method is called - it checks the mtime of the
binary, and re-runs the checks if the binary has changed.
networkxml2argvtest.c creates 2 "artificial" dnsmasqCaps objects at
startup - one "restricted" (doesn't support --bind-dynamic) and one
"full" (does support --bind-dynamic). Some of the test cases use one
and some the other, to make sure both code pathes are tested.
2012-11-20 17:22:15 +00:00
|
|
|
* Copyright (C) 2007-2012 Red Hat, Inc.
|
2010-04-26 13:52:02 +00:00
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-04-26 13:52:02 +00:00
|
|
|
*
|
|
|
|
* based on iptables.h
|
|
|
|
*/
|
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#pragma once
|
2010-04-26 13:52:02 +00:00
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#include "virobject.h"
|
|
|
|
#include "virsocketaddr.h"
|
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
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Each entry holds a string, "<mac_addr>,<hostname>,<ip_addr>" such as
|
|
|
|
* "01:23:45:67:89:0a,foo,10.0.0.3".
|
|
|
|
*/
|
|
|
|
char *host;
|
|
|
|
|
|
|
|
} dnsmasqDhcpHost;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned int nhosts;
|
|
|
|
dnsmasqDhcpHost *hosts;
|
|
|
|
|
|
|
|
char *path; /* Absolute path of dnsmasq's hostsfile. */
|
|
|
|
} dnsmasqHostsfile;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2011-06-24 10:04:39 +00:00
|
|
|
unsigned int nhostnames;
|
|
|
|
char *ip;
|
|
|
|
char **hostnames;
|
|
|
|
|
|
|
|
} dnsmasqAddnHost;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned int nhosts;
|
|
|
|
dnsmasqAddnHost *hosts;
|
|
|
|
|
|
|
|
char *path; /* Absolute path of dnsmasq's hostsfile. */
|
|
|
|
} dnsmasqAddnHostsfile;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
network: Fix dnsmasq hostsfile creation logic and related tests
networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010).
It has a force flag. If the dnsmasq hostsfile already exists force
needs to be true to overwrite it. networkBuildDnsmasqArgv sets force
to false, networkDefine sets it to true. This results in the
hostsfile being written only in networkDefine in the common case.
If no error occurred networkSaveDnsmasqHostsfile returns true and
networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq
command line.
networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011)
to return a new dnsmasqContext instead of reusing one. This change broke
the logic of the force flag as now networkSaveDnsmasqHostsfile returns
NULL on error, but the early return -- if force was not set and the
hostsfile exists -- returns 0. This turned the early return in an error
case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option
anymore if the hostsfile already exists. It did because networkDefine
created the hostsfile already.
Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile
but didn't apply the force option correctly to the new addnhosts file.
Now force doesn't control an early return anymore, but influences the
handling of the hostsfile context creation and dnsmasqSave is always
called now. This commit also added test cases that reveal several
problems. First, the tests now calls functions that try to write the
dnsmasq config files to disk. If someone runs this tests as root this
might overwrite actively used dnsmasq config files, this is a no-go. Also
the tests depend on configure --localstatedir, this needs to be fixed as
well, because it makes the tests fail when localstatedir is different
from /var.
This patch does several things to fix this:
1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv
to the caller to separate the command line generation from the config
file writing. This makes the command line generation testable without the
risk of interfering with system files, because the tests just don't call
dnsmasqSave.
2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag
useless as the saving happens somewhere else now. This fixes the wrong
usage of the force flag in combination with then newly added addnhosts
file by removing the force flag.
3) Adapt the wrong test cases to the correct behavior, by adding the
missing --dhcp-hostsfile option. Both affected tests contain DHCP host
elements but missed the necessary --dhcp-hostsfile option.
4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile,
because it doesn't save the dnsmasqContext anymore.
5) Move all directory creations in dnsmasq context handling code from
the *New functions to dnsmasqSave to avoid directory creations in system
paths in the test cases.
6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext
anymore the test case can create one with the localstatedir that is
expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
|
|
|
char *config_dir;
|
2011-06-24 10:04:39 +00:00
|
|
|
dnsmasqHostsfile *hostsfile;
|
|
|
|
dnsmasqAddnHostsfile *addnhostsfile;
|
2010-04-26 13:52:02 +00:00
|
|
|
} dnsmasqContext;
|
|
|
|
|
util: capabilities detection for dnsmasq
In order to optionally take advantage of new features in dnsmasq when
the host's version of dnsmasq supports them, but still be able to run
on hosts that don't support the new features, we need to be able to
detect the version of dnsmasq running on the host, and possibly
determine from the help output what options are in this dnsmasq.
This patch implements a greatly simplified version of the capabilities
code we already have for qemu. A dnsmasqCaps device can be created and
populated either from running a program on disk, reading a file with
the concatenated output of "dnsmasq --version; dnsmasq --help", or
examining a buffer in memory that contains the concatenated output of
those two commands. Simple functions to retrieve capabilities flags,
the version number, and the path of the binary are also included.
bridge_driver.c creates a single dnsmasqCaps object at driver startup,
and disposes of it at driver shutdown. Any time it must be used, the
dnsmasqCapsRefresh method is called - it checks the mtime of the
binary, and re-runs the checks if the binary has changed.
networkxml2argvtest.c creates 2 "artificial" dnsmasqCaps objects at
startup - one "restricted" (doesn't support --bind-dynamic) and one
"full" (does support --bind-dynamic). Some of the test cases use one
and some the other, to make sure both code pathes are tested.
2012-11-20 17:22:15 +00:00
|
|
|
typedef enum {
|
|
|
|
DNSMASQ_CAPS_BIND_DYNAMIC = 0, /* support for --bind-dynamic */
|
2012-12-17 17:49:18 +00:00
|
|
|
DNSMASQ_CAPS_BINDTODEVICE = 1, /* uses SO_BINDTODEVICE for --bind-interfaces */
|
2016-07-01 11:50:18 +00:00
|
|
|
DNSMASQ_CAPS_RA_PARAM = 2, /* support for --ra-param */
|
util: capabilities detection for dnsmasq
In order to optionally take advantage of new features in dnsmasq when
the host's version of dnsmasq supports them, but still be able to run
on hosts that don't support the new features, we need to be able to
detect the version of dnsmasq running on the host, and possibly
determine from the help output what options are in this dnsmasq.
This patch implements a greatly simplified version of the capabilities
code we already have for qemu. A dnsmasqCaps device can be created and
populated either from running a program on disk, reading a file with
the concatenated output of "dnsmasq --version; dnsmasq --help", or
examining a buffer in memory that contains the concatenated output of
those two commands. Simple functions to retrieve capabilities flags,
the version number, and the path of the binary are also included.
bridge_driver.c creates a single dnsmasqCaps object at driver startup,
and disposes of it at driver shutdown. Any time it must be used, the
dnsmasqCapsRefresh method is called - it checks the mtime of the
binary, and re-runs the checks if the binary has changed.
networkxml2argvtest.c creates 2 "artificial" dnsmasqCaps objects at
startup - one "restricted" (doesn't support --bind-dynamic) and one
"full" (does support --bind-dynamic). Some of the test cases use one
and some the other, to make sure both code pathes are tested.
2012-11-20 17:22:15 +00:00
|
|
|
|
|
|
|
DNSMASQ_CAPS_LAST, /* this must always be the last item */
|
|
|
|
} dnsmasqCapsFlags;
|
|
|
|
|
|
|
|
typedef struct _dnsmasqCaps dnsmasqCaps;
|
|
|
|
typedef dnsmasqCaps *dnsmasqCapsPtr;
|
|
|
|
|
2020-07-03 03:40:13 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(dnsmasqCaps, virObjectUnref);
|
|
|
|
|
util: capabilities detection for dnsmasq
In order to optionally take advantage of new features in dnsmasq when
the host's version of dnsmasq supports them, but still be able to run
on hosts that don't support the new features, we need to be able to
detect the version of dnsmasq running on the host, and possibly
determine from the help output what options are in this dnsmasq.
This patch implements a greatly simplified version of the capabilities
code we already have for qemu. A dnsmasqCaps device can be created and
populated either from running a program on disk, reading a file with
the concatenated output of "dnsmasq --version; dnsmasq --help", or
examining a buffer in memory that contains the concatenated output of
those two commands. Simple functions to retrieve capabilities flags,
the version number, and the path of the binary are also included.
bridge_driver.c creates a single dnsmasqCaps object at driver startup,
and disposes of it at driver shutdown. Any time it must be used, the
dnsmasqCapsRefresh method is called - it checks the mtime of the
binary, and re-runs the checks if the binary has changed.
networkxml2argvtest.c creates 2 "artificial" dnsmasqCaps objects at
startup - one "restricted" (doesn't support --bind-dynamic) and one
"full" (does support --bind-dynamic). Some of the test cases use one
and some the other, to make sure both code pathes are tested.
2012-11-20 17:22:15 +00:00
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
dnsmasqContext * dnsmasqContextNew(const char *network_name,
|
|
|
|
const char *config_dir);
|
|
|
|
void dnsmasqContextFree(dnsmasqContext *ctx);
|
2020-07-03 03:40:13 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(dnsmasqContext, dnsmasqContextFree);
|
|
|
|
|
2011-06-28 12:07:46 +00:00
|
|
|
int dnsmasqAddDhcpHost(dnsmasqContext *ctx,
|
2010-04-26 13:52:02 +00:00
|
|
|
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,
|
2012-12-06 17:20:38 +00:00
|
|
|
const char *name,
|
2013-02-15 19:02:26 +00:00
|
|
|
const char *id,
|
2020-04-22 20:05:57 +00:00
|
|
|
const char *leasetime,
|
2012-12-06 17:20:38 +00:00
|
|
|
bool ipv6);
|
2011-06-28 12:07:46 +00:00
|
|
|
int dnsmasqAddHost(dnsmasqContext *ctx,
|
2011-06-24 10:04:39 +00:00
|
|
|
virSocketAddr *ip,
|
|
|
|
const char *name);
|
2010-04-26 13:52:02 +00:00
|
|
|
int dnsmasqSave(const dnsmasqContext *ctx);
|
|
|
|
int dnsmasqDelete(const dnsmasqContext *ctx);
|
|
|
|
int dnsmasqReload(pid_t pid);
|
|
|
|
|
util: capabilities detection for dnsmasq
In order to optionally take advantage of new features in dnsmasq when
the host's version of dnsmasq supports them, but still be able to run
on hosts that don't support the new features, we need to be able to
detect the version of dnsmasq running on the host, and possibly
determine from the help output what options are in this dnsmasq.
This patch implements a greatly simplified version of the capabilities
code we already have for qemu. A dnsmasqCaps device can be created and
populated either from running a program on disk, reading a file with
the concatenated output of "dnsmasq --version; dnsmasq --help", or
examining a buffer in memory that contains the concatenated output of
those two commands. Simple functions to retrieve capabilities flags,
the version number, and the path of the binary are also included.
bridge_driver.c creates a single dnsmasqCaps object at driver startup,
and disposes of it at driver shutdown. Any time it must be used, the
dnsmasqCapsRefresh method is called - it checks the mtime of the
binary, and re-runs the checks if the binary has changed.
networkxml2argvtest.c creates 2 "artificial" dnsmasqCaps objects at
startup - one "restricted" (doesn't support --bind-dynamic) and one
"full" (does support --bind-dynamic). Some of the test cases use one
and some the other, to make sure both code pathes are tested.
2012-11-20 17:22:15 +00:00
|
|
|
dnsmasqCapsPtr dnsmasqCapsNewFromBuffer(const char *buf,
|
|
|
|
const char *binaryPath);
|
|
|
|
dnsmasqCapsPtr dnsmasqCapsNewFromFile(const char *dataPath,
|
|
|
|
const char *binaryPath);
|
|
|
|
dnsmasqCapsPtr dnsmasqCapsNewFromBinary(const char *binaryPath);
|
|
|
|
int dnsmasqCapsRefresh(dnsmasqCapsPtr *caps, const char *binaryPath);
|
|
|
|
bool dnsmasqCapsGet(dnsmasqCapsPtr caps, dnsmasqCapsFlags flag);
|
|
|
|
const char *dnsmasqCapsGetBinaryPath(dnsmasqCapsPtr caps);
|
|
|
|
unsigned long dnsmasqCapsGetVersion(dnsmasqCapsPtr caps);
|
2020-04-22 20:05:57 +00:00
|
|
|
char *dnsmasqDhcpHostsToString(dnsmasqDhcpHost *hosts,
|
|
|
|
unsigned int nhosts);
|
2012-12-06 17:20:38 +00:00
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#define DNSMASQ_DHCPv6_MAJOR_REQD 2
|
|
|
|
#define DNSMASQ_DHCPv6_MINOR_REQD 64
|
|
|
|
#define DNSMASQ_RA_MAJOR_REQD 2
|
|
|
|
#define DNSMASQ_RA_MINOR_REQD 64
|
2012-12-06 17:20:38 +00:00
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#define DNSMASQ_DHCPv6_SUPPORT(CAPS) \
|
2017-11-03 12:09:47 +00:00
|
|
|
(dnsmasqCapsGetVersion(CAPS) >= \
|
2012-12-06 17:20:38 +00:00
|
|
|
(DNSMASQ_DHCPv6_MAJOR_REQD * 1000000) + \
|
|
|
|
(DNSMASQ_DHCPv6_MINOR_REQD * 1000))
|
2019-06-18 16:13:08 +00:00
|
|
|
#define DNSMASQ_RA_SUPPORT(CAPS) \
|
2017-11-03 12:09:47 +00:00
|
|
|
(dnsmasqCapsGetVersion(CAPS) >= \
|
|
|
|
(DNSMASQ_RA_MAJOR_REQD * 1000000) + \
|
2012-12-06 17:20:38 +00:00
|
|
|
(DNSMASQ_RA_MINOR_REQD * 1000))
|