1 /*
2  * virdnsmasq.h: Helper APIs for managing dnsmasq
3  *
4  * Copyright (C) 2007-2012 Red Hat, Inc.
5  * Copyright (C) 2010 Satoru SATOH <satoru.satoh@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library.  If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  * based on iptables.h
22  */
23 
24 #pragma once
25 
26 #include "virobject.h"
27 #include "virsocketaddr.h"
28 
29 typedef struct
30 {
31     /*
32      * Each entry holds a string, "<mac_addr>,<hostname>,<ip_addr>" such as
33      * "01:23:45:67:89:0a,foo,10.0.0.3".
34      */
35     char *host;
36 
37 } dnsmasqDhcpHost;
38 
39 typedef struct
40 {
41     unsigned int     nhosts;
42     dnsmasqDhcpHost *hosts;
43 
44     char            *path;  /* Absolute path of dnsmasq's hostsfile. */
45 } dnsmasqHostsfile;
46 
47 typedef struct
48 {
49     unsigned int    nhostnames;
50     char            *ip;
51     char            **hostnames;
52 
53 } dnsmasqAddnHost;
54 
55 typedef struct
56 {
57     unsigned int     nhosts;
58     dnsmasqAddnHost *hosts;
59 
60     char            *path;  /* Absolute path of dnsmasq's hostsfile. */
61 } dnsmasqAddnHostsfile;
62 
63 typedef struct
64 {
65     char                 *config_dir;
66     dnsmasqHostsfile     *hostsfile;
67     dnsmasqAddnHostsfile *addnhostsfile;
68 } dnsmasqContext;
69 
70 typedef enum {
71    DNSMASQ_CAPS_BIND_DYNAMIC = 0, /* support for --bind-dynamic */
72    DNSMASQ_CAPS_BINDTODEVICE = 1, /* uses SO_BINDTODEVICE for --bind-interfaces */
73    DNSMASQ_CAPS_RA_PARAM = 2,     /* support for --ra-param */
74 
75    DNSMASQ_CAPS_LAST,             /* this must always be the last item */
76 } dnsmasqCapsFlags;
77 
78 typedef struct _dnsmasqCaps dnsmasqCaps;
79 
80 G_DEFINE_AUTOPTR_CLEANUP_FUNC(dnsmasqCaps, virObjectUnref);
81 
82 
83 dnsmasqContext * dnsmasqContextNew(const char *network_name,
84                                    const char *config_dir);
85 void             dnsmasqContextFree(dnsmasqContext *ctx);
86 G_DEFINE_AUTOPTR_CLEANUP_FUNC(dnsmasqContext, dnsmasqContextFree);
87 
88 int              dnsmasqAddDhcpHost(dnsmasqContext *ctx,
89                                     const char *mac,
90                                     virSocketAddr *ip,
91                                     const char *name,
92                                     const char *id,
93                                     const char *leasetime,
94                                     bool ipv6);
95 int              dnsmasqAddHost(dnsmasqContext *ctx,
96                                 virSocketAddr *ip,
97                                 const char *name);
98 int              dnsmasqSave(const dnsmasqContext *ctx);
99 int              dnsmasqDelete(const dnsmasqContext *ctx);
100 int              dnsmasqReload(pid_t pid);
101 
102 dnsmasqCaps *dnsmasqCapsNewFromBuffer(const char *buf);
103 dnsmasqCaps *dnsmasqCapsNewFromBinary(void);
104 bool dnsmasqCapsGet(dnsmasqCaps *caps, dnsmasqCapsFlags flag);
105 const char *dnsmasqCapsGetBinaryPath(dnsmasqCaps *caps);
106 unsigned long dnsmasqCapsGetVersion(dnsmasqCaps *caps);
107 char *dnsmasqDhcpHostsToString(dnsmasqDhcpHost *hosts,
108                                unsigned int nhosts);
109 
110 #define DNSMASQ_DHCPv6_MAJOR_REQD 2
111 #define DNSMASQ_DHCPv6_MINOR_REQD 64
112 #define DNSMASQ_RA_MAJOR_REQD 2
113 #define DNSMASQ_RA_MINOR_REQD 64
114 
115 #define DNSMASQ_DHCPv6_SUPPORT(CAPS) \
116     (dnsmasqCapsGetVersion(CAPS) >= \
117      (DNSMASQ_DHCPv6_MAJOR_REQD * 1000000) + \
118      (DNSMASQ_DHCPv6_MINOR_REQD * 1000))
119 #define DNSMASQ_RA_SUPPORT(CAPS) \
120     (dnsmasqCapsGetVersion(CAPS) >= \
121      (DNSMASQ_RA_MAJOR_REQD * 1000000) + \
122      (DNSMASQ_RA_MINOR_REQD * 1000))
123