summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile28
-rw-r--r--include/netplan.h29
-rw-r--r--include/parse-nm.h (renamed from src/parse-nm.h)6
-rw-r--r--include/parse.h66
-rw-r--r--include/util.h33
-rw-r--r--src/dbus.c2
-rw-r--r--src/error.c1
-rw-r--r--src/generate.c2
-rw-r--r--src/names.c4
-rw-r--r--src/names.h3
-rw-r--r--src/netplan.c6
-rw-r--r--src/networkd.c2
-rw-r--r--src/networkd.h17
-rw-r--r--src/nm.c3
-rw-r--r--src/nm.h13
-rw-r--r--src/openvswitch.c2
-rw-r--r--src/openvswitch.h13
-rw-r--r--src/parse-globals.h51
-rw-r--r--src/parse-nm.c1
-rw-r--r--src/parse.c14
-rw-r--r--src/sriov.c3
-rw-r--r--src/sriov.h8
-rw-r--r--src/types.c (renamed from src/netdef.c)7
-rw-r--r--src/types.h (renamed from src/parse.h)120
-rw-r--r--src/util-internal.h70
-rw-r--r--src/util.c10
-rw-r--r--src/util.h41
-rw-r--r--src/validation.c6
-rw-r--r--src/validation.h5
-rw-r--r--src/yaml-helpers.h (renamed from src/netplan.h)8
30 files changed, 383 insertions, 191 deletions
diff --git a/Makefile b/Makefile
index 0c67710..ca3dea3 100644
--- a/Makefile
+++ b/Makefile
@@ -6,10 +6,26 @@ BUILDFLAGS = \
-std=c99 \
-D_XOPEN_SOURCE=500 \
-DSBINDIR=\"$(SBINDIR)\" \
+ -I${CURDIR}/include \
-Wall \
-Werror \
$(NULL)
+SRCS = \
+ src/error.c \
+ src/names.c \
+ src/netplan.c \
+ src/networkd.c \
+ src/nm.c \
+ src/openvswitch.c \
+ src/parse.c \
+ src/parse-nm.c \
+ src/sriov.c \
+ src/types.c \
+ src/util.c \
+ src/validation.c \
+ $(NULL)
+
SYSTEMD_GENERATOR_DIR=$(shell pkg-config --variable=systemdsystemgeneratordir systemd)
SYSTEMD_UNIT_DIR=$(shell pkg-config --variable=systemdsystemunitdir systemd)
BASH_COMPLETIONS_DIR=$(shell pkg-config --variable=completionsdir bash-completion || echo "/etc/bash_completion.d")
@@ -38,15 +54,15 @@ default: netplan/_features.py generate netplan-dbus dbus/io.netplan.Netplan.serv
%.o: src/%.c
$(CC) $(BUILDFLAGS) $(CFLAGS) $(LDFLAGS) -c $^ `pkg-config --cflags --libs glib-2.0 gio-2.0 yaml-0.1 uuid`
-libnetplan.so.$(NETPLAN_SOVER): parse.o netdef.o netplan.o util.o validation.o error.o parse-nm.o names.o
- $(CC) -shared -Wl,-soname,libnetplan.so.$(NETPLAN_SOVER) $(BUILDFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ `pkg-config --libs glib-2.0 gio-2.0 yaml-0.1`
+libnetplan.so.$(NETPLAN_SOVER): $(SRCS)
+ $(CC) -shared -Wl,-soname,libnetplan.so.$(NETPLAN_SOVER) $(BUILDFLAGS) $(CFLAGS) -fvisibility=hidden $(LDFLAGS) -o $@ $^ `pkg-config --cflags --libs glib-2.0 gio-2.0 yaml-0.1 uuid`
ln -snf libnetplan.so.$(NETPLAN_SOVER) libnetplan.so
-generate: libnetplan.so.$(NETPLAN_SOVER) nm.o networkd.o openvswitch.o generate.o sriov.o names.o
- $(CC) $(BUILDFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ -L. -lnetplan `pkg-config --cflags --libs glib-2.0 gio-2.0 yaml-0.1 uuid`
+generate: libnetplan.so.$(NETPLAN_SOVER) generate.o
+ $(CC) $(BUILDFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(filter-out $<,$^) -L. -lnetplan `pkg-config --cflags --libs glib-2.0 gio-2.0 yaml-0.1 uuid`
-netplan-dbus: src/dbus.c src/_features.h netdef.o parse.o util.o validation.o error.o names.o
- $(CC) $(BUILDFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(patsubst %.h,,$^) `pkg-config --cflags --libs libsystemd glib-2.0 gio-2.0 yaml-0.1`
+netplan-dbus: libnetplan.so.$(NETPLAN_SOVER) src/_features.h dbus.o
+ $(CC) $(BUILDFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(filter-out $<,$(patsubst %.h,,$^)) -L. -lnetplan `pkg-config --cflags --libs libsystemd glib-2.0 gio-2.0 yaml-0.1 uuid`
src/_features.h: src/[^_]*.[hc]
printf "#include <stddef.h>\nstatic const char *feature_flags[] __attribute__((__unused__)) = {\n" > $@
diff --git a/include/netplan.h b/include/netplan.h
new file mode 100644
index 0000000..9d30c26
--- /dev/null
+++ b/include/netplan.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2021 Canonical, Ltd.
+ * Author: Lukas Märdian <slyon@ubuntu.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 3.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+#define NETPLAN_PUBLIC __attribute__ ((visibility("default")))
+#define NETPLAN_INTERNAL __attribute__ ((visibility("default")))
+#define NETPLAN_ABI __attribute__ ((visibility("default")))
+
+/**
+ * Represent a configuration stanza
+ */
+typedef struct net_definition NetplanNetDefinition;
+
+NETPLAN_PUBLIC void
+write_netplan_conf(const NetplanNetDefinition* def, const char* rootdir);
diff --git a/src/parse-nm.h b/include/parse-nm.h
index 53973f7..d83d665 100644
--- a/src/parse-nm.h
+++ b/include/parse-nm.h
@@ -17,6 +17,10 @@
#pragma once
+#include "netplan.h"
+#include <glib.h>
+
#define NETPLAN_NM_EMPTY_GROUP "_"
-gboolean netplan_parse_keyfile(const char* filename, GError** error);
+NETPLAN_PUBLIC gboolean
+netplan_parse_keyfile(const char* filename, GError** error);
diff --git a/include/parse.h b/include/parse.h
new file mode 100644
index 0000000..eda0260
--- /dev/null
+++ b/include/parse.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 Canonical, Ltd.
+ * Author: Martin Pitt <martin.pitt@ubuntu.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 3.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+#include "netplan.h"
+#include <glib.h>
+
+/****************************************************
+ * Parsed definitions
+ ****************************************************/
+
+typedef enum {
+ NETPLAN_DEF_TYPE_NONE,
+ /* physical devices */
+ NETPLAN_DEF_TYPE_ETHERNET,
+ NETPLAN_DEF_TYPE_WIFI,
+ NETPLAN_DEF_TYPE_MODEM,
+ /* virtual devices */
+ NETPLAN_DEF_TYPE_VIRTUAL,
+ NETPLAN_DEF_TYPE_BRIDGE = NETPLAN_DEF_TYPE_VIRTUAL,
+ NETPLAN_DEF_TYPE_BOND,
+ NETPLAN_DEF_TYPE_VLAN,
+ NETPLAN_DEF_TYPE_TUNNEL,
+ NETPLAN_DEF_TYPE_PORT,
+ /* Type fallback/passthrough */
+ NETPLAN_DEF_TYPE_NM,
+ NETPLAN_DEF_TYPE_MAX_
+} NetplanDefType;
+
+typedef enum {
+ NETPLAN_BACKEND_NONE,
+ NETPLAN_BACKEND_NETWORKD,
+ NETPLAN_BACKEND_NM,
+ NETPLAN_BACKEND_OVS,
+ NETPLAN_BACKEND_MAX_,
+} NetplanBackend;
+
+/****************************************************
+ * Functions
+ ****************************************************/
+
+NETPLAN_PUBLIC gboolean
+netplan_parse_yaml(const char* filename, GError** error);
+
+NETPLAN_PUBLIC GHashTable*
+netplan_finish_parse(GError** error);
+
+NETPLAN_PUBLIC guint
+netplan_clear_netdefs();
+
+NETPLAN_PUBLIC NetplanBackend
+netplan_get_global_backend();
diff --git a/include/util.h b/include/util.h
new file mode 100644
index 0000000..e7e1a0f
--- /dev/null
+++ b/include/util.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2016 Canonical, Ltd.
+ * Author: Martin Pitt <martin.pitt@ubuntu.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 3.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <glib.h>
+#include "netplan.h"
+
+NETPLAN_PUBLIC gboolean
+netplan_delete_connection(const char* id, const char* rootdir);
+
+NETPLAN_PUBLIC gboolean
+netplan_generate(const char* rootdir);
+
+NETPLAN_PUBLIC gchar*
+netplan_get_id_from_nm_filename(const char* filename, const char* ssid);
+
+NETPLAN_PUBLIC gchar*
+netplan_get_filename_by_id(const char* netdef_id, const char* rootdir);
diff --git a/src/dbus.c b/src/dbus.c
index a486b54..1442af3 100644
--- a/src/dbus.c
+++ b/src/dbus.c
@@ -15,7 +15,7 @@
#include <systemd/sd-event.h>
#include "_features.h"
-#include "util.h"
+#include "util-internal.h"
typedef struct {
sd_bus_slot *slot;
diff --git a/src/error.c b/src/error.c
index 0c34e17..ff28be9 100644
--- a/src/error.c
+++ b/src/error.c
@@ -22,6 +22,7 @@
#include <yaml.h>
#include "parse.h"
+#include "parse-globals.h"
/****************************************************
diff --git a/src/generate.c b/src/generate.c
index caff955..00c7035 100644
--- a/src/generate.c
+++ b/src/generate.c
@@ -27,7 +27,9 @@
#include <gio/gio.h>
#include "util.h"
+#include "util-internal.h"
#include "parse.h"
+#include "parse-globals.h"
#include "names.h"
#include "networkd.h"
#include "nm.h"
diff --git a/src/names.c b/src/names.c
index 378c6b4..3a085ba 100644
--- a/src/names.c
+++ b/src/names.c
@@ -107,8 +107,8 @@ NAME_FUNCTION(wifi_mode, NetplanWifiMode);
/* ABI compatibility definitions */
-const char*
+NETPLAN_ABI const char*
tunnel_mode_to_string(NetplanTunnelMode val) __attribute__ ((alias ("netplan_tunnel_mode_name")));
-extern const char*
+NETPLAN_ABI extern const char*
netplan_backend_to_name __attribute__((alias("netplan_backend_to_str")));
diff --git a/src/names.h b/src/names.h
index 256a600..c1b108b 100644
--- a/src/names.h
+++ b/src/names.h
@@ -18,8 +18,9 @@
#pragma once
#include "netplan.h"
+#include "types.h"
-const char*
+NETPLAN_INTERNAL const char*
netplan_backend_name(NetplanBackend val);
const char*
diff --git a/src/netplan.c b/src/netplan.c
index 7a7bc43..6285aa2 100644
--- a/src/netplan.c
+++ b/src/netplan.c
@@ -20,6 +20,8 @@
#include "netplan.h"
#include "parse.h"
+#include "parse-globals.h"
+#include "yaml-helpers.h"
#include "names.h"
gchar *tmp = NULL;
@@ -885,7 +887,7 @@ contains_netdef_type(gpointer key, gpointer value, gpointer user_data)
* @rootdir: If not %NULL, generate configuration in this root directory
* (useful for testing).
*/
-void
+NETPLAN_INTERNAL void
write_netplan_conf_full(const char* file_hint, const char* rootdir)
{
g_autofree gchar *path = NULL;
@@ -980,7 +982,7 @@ void cleanup_netplan_conf(const char* rootdir)
/**
* Helper function for testing only
*/
-void
+NETPLAN_INTERNAL void
_write_netplan_conf(const char* netdef_id, const char* rootdir)
{
GHashTable* ht = NULL;
diff --git a/src/networkd.c b/src/networkd.c
index e8e680e..f6c9ec9 100644
--- a/src/networkd.c
+++ b/src/networkd.c
@@ -27,8 +27,10 @@
#include "networkd.h"
#include "parse.h"
+#include "parse-globals.h"
#include "names.h"
#include "util.h"
+#include "util-internal.h"
#include "validation.h"
/**
diff --git a/src/networkd.h b/src/networkd.h
index 41ab125..be2bb29 100644
--- a/src/networkd.h
+++ b/src/networkd.h
@@ -17,10 +17,17 @@
#pragma once
-#include "parse.h"
+#include "netplan.h"
+#include <glib.h>
-gboolean write_networkd_conf(const NetplanNetDefinition* def, const char* rootdir);
-void cleanup_networkd_conf(const char* rootdir);
-void enable_networkd(const char* generator_dir);
+NETPLAN_INTERNAL gboolean
+write_networkd_conf(const NetplanNetDefinition* def, const char* rootdir);
-void write_network_file(const NetplanNetDefinition* def, const char* rootdir, const char* path);
+NETPLAN_INTERNAL void
+write_network_file(const NetplanNetDefinition* def, const char* rootdir, const char* path);
+
+NETPLAN_INTERNAL void
+cleanup_networkd_conf(const char* rootdir);
+
+NETPLAN_INTERNAL void
+enable_networkd(const char* generator_dir);
diff --git a/src/nm.c b/src/nm.c
index aef15ac..8e84a99 100644
--- a/src/nm.c
+++ b/src/nm.c
@@ -26,9 +26,12 @@
#include <glib/gprintf.h>
#include <uuid.h>
+#include "netplan.h"
#include "nm.h"
#include "parse.h"
+#include "parse-globals.h"
#include "util.h"
+#include "util-internal.h"
#include "validation.h"
#include "parse-nm.h"
diff --git a/src/nm.h b/src/nm.h
index 9f8f9ca..ac86f00 100644
--- a/src/nm.h
+++ b/src/nm.h
@@ -17,8 +17,13 @@
#pragma once
-#include "parse.h"
+#include "netplan.h"
-void write_nm_conf(NetplanNetDefinition* def, const char* rootdir);
-void write_nm_conf_finish(const char* rootdir);
-void cleanup_nm_conf(const char* rootdir);
+NETPLAN_INTERNAL void
+write_nm_conf(NetplanNetDefinition* def, const char* rootdir);
+
+NETPLAN_INTERNAL void
+write_nm_conf_finish(const char* rootdir);
+
+NETPLAN_INTERNAL void
+cleanup_nm_conf(const char* rootdir);
diff --git a/src/openvswitch.c b/src/openvswitch.c
index 2088fbe..3bf8631 100644
--- a/src/openvswitch.c
+++ b/src/openvswitch.c
@@ -25,7 +25,9 @@
#include "openvswitch.h"
#include "networkd.h"
#include "parse.h"
+#include "parse-globals.h"
#include "util.h"
+#include "util-internal.h"
static void
write_ovs_systemd_unit(const char* id, const GString* cmds, const char* rootdir, gboolean physical, gboolean cleanup, const char* dependency)
diff --git a/src/openvswitch.h b/src/openvswitch.h
index 69bd6ee..7abea4d 100644
--- a/src/openvswitch.h
+++ b/src/openvswitch.h
@@ -17,8 +17,13 @@
#pragma once
-#include "parse.h"
+#include "netplan.h"
-void write_ovs_conf(const NetplanNetDefinition* def, const char* rootdir);
-void write_ovs_conf_finish(const char* rootdir);
-void cleanup_ovs_conf(const char* rootdir);
+NETPLAN_INTERNAL void
+write_ovs_conf(const NetplanNetDefinition* def, const char* rootdir);
+
+NETPLAN_INTERNAL void
+write_ovs_conf_finish(const char* rootdir);
+
+NETPLAN_INTERNAL void
+cleanup_ovs_conf(const char* rootdir);
diff --git a/src/parse-globals.h b/src/parse-globals.h
new file mode 100644
index 0000000..a7b2be8
--- /dev/null
+++ b/src/parse-globals.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2021 Canonical, Ltd.
+ * Author: Simon Chopin <simon.chopin@canonical.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 3.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <glib.h>
+#include "types.h"
+
+/* file that is currently being processed, for useful error messages */
+extern const char*
+current_file;
+
+/* List of "seen" ids not found in netdefs yet by the parser.
+ * These are removed when it exists in this list and we reach the point of
+ * creating a netdef for that id; so by the time we're done parsing the yaml
+ * document it should be empty. */
+extern GHashTable*
+missing_id;
+
+extern int
+missing_ids_found;
+
+/* Written/updated by parse_yaml(): char* id → net_definition.
+ *
+ * Since both netdefs and netdefs_ordered store pointers to the same elements,
+ * we consider that only netdefs_ordered is owner of this data. One should not
+ * free() objects obtained from netdefs, and proper care should be taken to remove
+ * any reference of an object in netdefs when destroying it from netdefs_ordered.
+ */
+extern GHashTable*
+netdefs;
+
+extern GList*
+netdefs_ordered;
+
+extern NetplanOVSSettings
+ovs_settings_global;
diff --git a/src/parse-nm.c b/src/parse-nm.c
index bb83bf5..9d5d9cf 100644
--- a/src/parse-nm.c
+++ b/src/parse-nm.c
@@ -23,6 +23,7 @@
#include "parse-nm.h"
#include "parse.h"
#include "util.h"
+#include "util-internal.h"
/**
* NetworkManager writes the alias for '802-3-ethernet' (ethernet),
diff --git a/src/parse.c b/src/parse.c
index f62deef..55ead8f 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -28,11 +28,14 @@
#include <yaml.h>
#include "parse.h"
-#include "util.h"
#include "names.h"
+#include "util-internal.h"
#include "error.h"
#include "validation.h"
+#define NETPLAN_VERSION_MIN 2
+#define NETPLAN_VERSION_MAX 3
+
/* convenience macro to put the offset of a NetplanNetDefinition field into "void* data" */
#define access_point_offset(field) GUINT_TO_POINTER(offsetof(NetplanWifiAccessPoint, field))
#define addr_option_offset(field) GUINT_TO_POINTER(offsetof(NetplanAddressOptions, field))
@@ -73,12 +76,15 @@ const char* cur_filename;
static NetplanBackend backend_global, backend_cur_type;
/* global OpenVSwitch settings */
+NETPLAN_INTERNAL
NetplanOVSSettings ovs_settings_global;
/* Global ID → NetplanNetDefinition* map for all parsed config files */
+NETPLAN_INTERNAL
GHashTable* netdefs;
/* Contains the same objects as 'netdefs' but ordered by dependency */
+NETPLAN_INTERNAL
GList* netdefs_ordered;
/* Set of IDs in currently parsed YAML file, for being able to detect
@@ -900,7 +906,8 @@ handle_match(yaml_document_t* doc, yaml_node_t* node, const void* _, GError** er
return process_mapping(doc, node, match_handlers, NULL, error);
}
-struct NetplanWifiWowlanType NETPLAN_WIFI_WOWLAN_TYPES[] = {
+NETPLAN_ABI struct NetplanWifiWowlanType
+NETPLAN_WIFI_WOWLAN_TYPES[] = {
{"default", NETPLAN_WIFI_WOWLAN_DEFAULT},
{"any", NETPLAN_WIFI_WOWLAN_ANY},
{"disconnect", NETPLAN_WIFI_WOWLAN_DISCONNECT},
@@ -1311,7 +1318,8 @@ handle_link_local(yaml_document_t* doc, yaml_node_t* node, const void* _, GError
return TRUE;
}
-struct NetplanOptionalAddressType NETPLAN_OPTIONAL_ADDRESS_TYPES[] = {
+NETPLAN_ABI struct NetplanOptionalAddressType
+NETPLAN_OPTIONAL_ADDRESS_TYPES[] = {
{"ipv4-ll", NETPLAN_OPTIONAL_IPV4_LL},
{"ipv6-ra", NETPLAN_OPTIONAL_IPV6_RA},
{"dhcp4", NETPLAN_OPTIONAL_DHCP4},
diff --git a/src/sriov.c b/src/sriov.c
index 60f9800..906aac2 100644
--- a/src/sriov.c
+++ b/src/sriov.c
@@ -21,7 +21,8 @@
#include <glib/gstdio.h>
#include <glib-object.h>
-#include "util.h"
+#include "util-internal.h"
+#include "sriov.h"
void
write_sriov_conf_finish(const char* rootdir)
diff --git a/src/sriov.h b/src/sriov.h
index 7cd5896..e71f573 100644
--- a/src/sriov.h
+++ b/src/sriov.h
@@ -16,6 +16,10 @@
*/
#pragma once
+#include "netplan.h"
-void write_sriov_conf_finish(const char* rootdir);
-void cleanup_sriov_conf(const char* rootdir);
+NETPLAN_INTERNAL void
+write_sriov_conf_finish(const char* rootdir);
+
+NETPLAN_INTERNAL void
+cleanup_sriov_conf(const char* rootdir);
diff --git a/src/netdef.c b/src/types.c
index 36464f6..b723f15 100644
--- a/src/netdef.c
+++ b/src/types.c
@@ -15,12 +15,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-/* This module contains functions to deal with the NetplanNetDefinition objects.
- * Notably, accessors and destructors.
+/* This module contains functions to deal with the Netplan objects,
+ * notably, accessors and destructors. Note that types specific to parsing
+ * are implemented separately.
*/
#include <glib.h>
-#include "parse.h"
+#include "types.h"
#define FREE_AND_NULLIFY(ptr) { g_free(ptr); ptr = NULL; }
diff --git a/src/parse.h b/src/types.h
index 28050f2..48c7ad2 100644
--- a/src/parse.h
+++ b/src/types.h
@@ -1,6 +1,6 @@
/*
- * Copyright (C) 2016 Canonical, Ltd.
- * Author: Martin Pitt <martin.pitt@ubuntu.com>
+ * Copyright (C) 2021 Canonical, Ltd.
+ * Author: Simon Chopin <simon.chopin@canonical.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,52 +17,10 @@
#pragma once
-#include <uuid.h>
+#include "parse.h"
+#include <glib.h>
#include <yaml.h>
-
-#define NETPLAN_VERSION_MIN 2
-#define NETPLAN_VERSION_MAX 3
-
-
-/* file that is currently being processed, for useful error messages */
-extern const char* current_file;
-
-/* List of "seen" ids not found in netdefs yet by the parser.
- * These are removed when it exists in this list and we reach the point of
- * creating a netdef for that id; so by the time we're done parsing the yaml
- * document it should be empty. */
-extern GHashTable *missing_id;
-extern int missing_ids_found;
-
-/****************************************************
- * Parsed definitions
- ****************************************************/
-
-typedef enum {
- NETPLAN_DEF_TYPE_NONE,
- /* physical devices */
- NETPLAN_DEF_TYPE_ETHERNET,
- NETPLAN_DEF_TYPE_WIFI,
- NETPLAN_DEF_TYPE_MODEM,
- /* virtual devices */
- NETPLAN_DEF_TYPE_VIRTUAL,
- NETPLAN_DEF_TYPE_BRIDGE = NETPLAN_DEF_TYPE_VIRTUAL,
- NETPLAN_DEF_TYPE_BOND,
- NETPLAN_DEF_TYPE_VLAN,
- NETPLAN_DEF_TYPE_TUNNEL,
- NETPLAN_DEF_TYPE_PORT,
- /* Type fallback/passthrough */
- NETPLAN_DEF_TYPE_NM,
- NETPLAN_DEF_TYPE_MAX_
-} NetplanDefType;
-
-typedef enum {
- NETPLAN_BACKEND_NONE,
- NETPLAN_BACKEND_NETWORKD,
- NETPLAN_BACKEND_NM,
- NETPLAN_BACKEND_OVS,
- NETPLAN_BACKEND_MAX_,
-} NetplanBackend;
+#include <uuid.h>
typedef enum {
NETPLAN_RA_MODE_KERNEL,
@@ -90,6 +48,7 @@ struct NetplanOptionalAddressType {
NetplanOptionalAddressFlag flag;
};
+// Not strictly speaking a type, but seems fair to keep it around.
extern struct NetplanOptionalAddressType NETPLAN_OPTIONAL_ADDRESS_TYPES[];
/* Tunnel mode enum; sync with NetworkManager's DBUS API */
@@ -116,24 +75,6 @@ typedef enum {
NETPLAN_TUNNEL_MODE_MAX_,
} NetplanTunnelMode;
-static const char* const
-netplan_tunnel_mode_table[NETPLAN_TUNNEL_MODE_MAX_] = {
- [NETPLAN_TUNNEL_MODE_UNKNOWN] = "unknown",
- [NETPLAN_TUNNEL_MODE_IPIP] = "ipip",
- [NETPLAN_TUNNEL_MODE_GRE] = "gre",
- [NETPLAN_TUNNEL_MODE_SIT] = "sit",
- [NETPLAN_TUNNEL_MODE_ISATAP] = "isatap",
- [NETPLAN_TUNNEL_MODE_VTI] = "vti",
- [NETPLAN_TUNNEL_MODE_IP6IP6] = "ip6ip6",
- [NETPLAN_TUNNEL_MODE_IPIP6] = "ipip6",
- [NETPLAN_TUNNEL_MODE_IP6GRE] = "ip6gre",
- [NETPLAN_TUNNEL_MODE_VTI6] = "vti6",
-
- [NETPLAN_TUNNEL_MODE_GRETAP] = "gretap",
- [NETPLAN_TUNNEL_MODE_IP6GRETAP] = "ip6gretap",
- [NETPLAN_TUNNEL_MODE_WIREGUARD] = "wireguard",
-};
-
typedef enum {
NETPLAN_WIFI_WOWLAN_DEFAULT = 1<<0,
NETPLAN_WIFI_WOWLAN_ANY = 1<<1,
@@ -230,14 +171,6 @@ typedef union {
} networkd;
} NetplanBackendSettings;
-/**
- * Represent a configuration stanza
- */
-
-struct net_definition;
-
-typedef struct net_definition NetplanNetDefinition;
-
struct net_definition {
NetplanDefType type;
NetplanBackend backend;
@@ -450,15 +383,6 @@ typedef struct {
NetplanBackendSettings backend_settings;
} NetplanWifiAccessPoint;
-#define NETPLAN_ADVERTISED_RECEIVE_WINDOW_UNSPEC 0
-#define NETPLAN_CONGESTION_WINDOW_UNSPEC 0
-#define NETPLAN_MTU_UNSPEC 0
-#define NETPLAN_METRIC_UNSPEC G_MAXUINT
-#define NETPLAN_ROUTE_TABLE_UNSPEC 0
-#define NETPLAN_IP_RULE_PRIO_UNSPEC G_MAXUINT
-#define NETPLAN_IP_RULE_FW_MARK_UNSPEC 0
-#define NETPLAN_IP_RULE_TOS_UNSPEC G_MAXUINT
-
typedef struct {
guint family;
char* type;
@@ -495,27 +419,13 @@ typedef struct {
guint tos;
} NetplanIPRule;
-/* Written/updated by parse_yaml(): char* id → net_definition.
- *
- * Since both netdefs and netdefs_ordered store pointers to the same elements,
- * we consider that only netdefs_ordered is owner of this data. One should not
- * free() objects obtained from netdefs, and proper care should be taken to remove
- * any reference of an object in netdefs when destroying it from netdefs_ordered.
- */
-extern GHashTable* netdefs;
-extern GList* netdefs_ordered;
-extern NetplanOVSSettings ovs_settings_global;
-
-/****************************************************
- * Functions
- ****************************************************/
-
-gboolean netplan_parse_yaml(const char* filename, GError** error);
-GHashTable* netplan_finish_parse(GError** error);
-guint netplan_clear_netdefs();
-NetplanBackend netplan_get_global_backend();
-NetplanNetDefinition* netplan_netdef_new(const char* id, NetplanDefType type, NetplanBackend renderer);
-void reset_netdef(NetplanNetDefinition *netdef, NetplanDefType type, NetplanBackend renderer);
+#define NETPLAN_ADVERTISED_RECEIVE_WINDOW_UNSPEC 0
+#define NETPLAN_CONGESTION_WINDOW_UNSPEC 0
+#define NETPLAN_MTU_UNSPEC 0
+#define NETPLAN_METRIC_UNSPEC G_MAXUINT
+#define NETPLAN_ROUTE_TABLE_UNSPEC 0
+#define NETPLAN_IP_RULE_PRIO_UNSPEC G_MAXUINT
+#define NETPLAN_IP_RULE_FW_MARK_UNSPEC 0
+#define NETPLAN_IP_RULE_TOS_UNSPEC G_MAXUINT
-void process_input_file(const char* f);
-gboolean process_yaml_hierarchy(const char* rootdir);
+void reset_netdef(NetplanNetDefinition *netdef, NetplanDefType type, NetplanBackend renderer);
diff --git a/src/util-internal.h b/src/util-internal.h
new file mode 100644
index 0000000..fe792d4
--- /dev/null
+++ b/src/util-internal.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2016 Canonical, Ltd.
+ * Author: Martin Pitt <martin.pitt@ubuntu.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 3.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#define __USE_MISC
+#include <glob.h>
+#include <glib.h>
+#include "types.h"
+
+#include <glib.h>
+#include "netplan.h"
+
+extern GHashTable*
+wifi_frequency_24;
+
+extern GHashTable*
+wifi_frequency_5;
+
+NETPLAN_ABI void
+safe_mkdir_p_dir(const char* file_path);
+
+NETPLAN_INTERNAL void
+g_string_free_to_file(GString* s, const char* rootdir, const char* path, const char* suffix);
+
+NETPLAN_INTERNAL void
+unlink_glob(const char* rootdir, const char* _glob);
+
+NETPLAN_INTERNAL int
+find_yaml_glob(const char* rootdir, glob_t* out_glob);
+
+NETPLAN_ABI const char*
+get_global_network(int ip_family);
+
+NETPLAN_ABI int
+wifi_get_freq24(int channel);
+
+NETPLAN_ABI int
+wifi_get_freq5(int channel);
+
+NETPLAN_ABI gchar*
+systemd_escape(char* string);
+
+#define OPENVSWITCH_OVS_VSCTL "/usr/bin/ovs-vsctl"
+
+const char*
+tunnel_mode_to_string(NetplanTunnelMode mode);
+
+NetplanNetDefinition*
+netplan_netdef_new(const char* id, NetplanDefType type, NetplanBackend renderer);
+
+NETPLAN_INTERNAL void
+process_input_file(const char* f);
+
+NETPLAN_INTERNAL gboolean
+process_yaml_hierarchy(const char* rootdir);
diff --git a/src/util.c b/src/util.c
index b43aaf9..c8004cf 100644
--- a/src/util.c
+++ b/src/util.c
@@ -23,11 +23,17 @@
#include <glib/gprintf.h>
#include "util.h"
+#include "util-internal.h"
#include "netplan.h"
+#include "parse.h"
+#include "parse-globals.h"
#include "names.h"
-GHashTable* wifi_frequency_24;
-GHashTable* wifi_frequency_5;
+NETPLAN_ABI GHashTable*
+wifi_frequency_24;
+
+NETPLAN_ABI GHashTable*
+wifi_frequency_5;
/**
* Create the parent directories of given file path. Exit program on failure.
diff --git a/src/util.h b/src/util.h
deleted file mode 100644
index f34c601..0000000
--- a/src/util.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2016 Canonical, Ltd.
- * Author: Martin Pitt <martin.pitt@ubuntu.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 3.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#define __USE_MISC
-#include <glob.h>
-#pragma once
-
-extern GHashTable* wifi_frequency_24;
-extern GHashTable* wifi_frequency_5;
-
-void safe_mkdir_p_dir(const char* file_path);
-void g_string_free_to_file(GString* s, const char* rootdir, const char* path, const char* suffix);
-void unlink_glob(const char* rootdir, const char* _glob);
-int find_yaml_glob(const char* rootdir, glob_t* out_glob);
-
-const char *get_global_network(int ip_family);
-
-int wifi_get_freq24(int channel);
-int wifi_get_freq5(int channel);
-
-gchar* systemd_escape(char* string);
-gboolean netplan_delete_connection(const char* id, const char* rootdir);
-gboolean netplan_generate(const char* rootdir);
-gchar* netplan_get_id_from_nm_filename(const char* filename, const char* ssid);
-gchar* netplan_get_filename_by_id(const char* netdef_id, const char* rootdir);
-
-#define OPENVSWITCH_OVS_VSCTL "/usr/bin/ovs-vsctl"
diff --git a/src/validation.c b/src/validation.c
index 2ac5650..7b80559 100644
--- a/src/validation.c
+++ b/src/validation.c
@@ -24,10 +24,12 @@
#include <yaml.h>
#include "parse.h"
+#include "types.h"
+#include "parse-globals.h"
#include "names.h"
#include "error.h"
-#include "util.h"
-
+#include "util-internal.h"
+#include "validation.h"
/* Check sanity for address types */
diff --git a/src/validation.h b/src/validation.h
index 3f6e527..18e98fe 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -19,14 +19,17 @@
#include "parse.h"
#include <glib.h>
+#include <yaml.h>
gboolean is_ip4_address(const char* address);
gboolean is_ip6_address(const char* address);
gboolean is_hostname(const char* hostname);
-gboolean is_wireguard_key(const char* hostname);
gboolean validate_ovs_target(gboolean host_first, gchar* s);
+NETPLAN_ABI gboolean
+is_wireguard_key(const char* hostname);
+
gboolean
validate_netdef_grammar(NetplanNetDefinition* nd, yaml_node_t* node, GError** error);
diff --git a/src/netplan.h b/src/yaml-helpers.h
index f8c09d1..6408ac1 100644
--- a/src/netplan.h
+++ b/src/yaml-helpers.h
@@ -1,6 +1,6 @@
/*
- * Copyright (C) 2021 Canonical, Ltd.
- * Author: Lukas Märdian <slyon@ubuntu.com>
+ * Copyright (C) 2016 Canonical, Ltd.
+ * Author: Martin Pitt <martin.pitt@ubuntu.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
#pragma once
-#include "parse.h"
+#include <yaml.h>
#define YAML_MAPPING_OPEN(event_ptr, emitter_ptr) \
{ \
@@ -92,5 +92,3 @@
if (!yaml_emitter_emit(emitter_ptr, event_ptr)) goto error; \
yaml_emitter_delete(emitter_ptr); \
}
-
-void write_netplan_conf(const NetplanNetDefinition* def, const char* rootdir);