[dev][ipsec] strongswan plugin的配置文件的添加方法与管理架构解析

前言

我们知道,strongswan是基于插件式管理的。不同的插件有不同的配置文件,在这下面,

我们以netlink的插件为例:etc/strongswan.d/charon/kernel-netlink.conf

在这个文件里,提供了不同的针对插件的配置项。接下来我们将讲解,如何开发这些配置项。

配置文件结构

在开始之前,先了解一下strongswan的配置文件组织结构,

strongswan的所有配置项都是层级结构组织的,如:charon.plugins.kernel-netlink.force_receive_buffer_size

树状结构,它们拥有共同的父节点,charon。

在配置文件里面,可以include其他的配置文件。

strongswan的所有配置文件,也都是树型引用的,它们拥有同一个共同的父节点,也就是所有配置文件的入口:etc/strongswan.conf

所有的plugin的配置文件,都在目录etc/strongswan.d/charon/下,并以插件的名字命令,形如kernel-netlink.conf

扫描二维码关注公众号,回复: 6357875 查看本文章

配置文件的读取和使用

做了简单的代码分析,如 [dev][ipsec] strongswan plugin加载优先级原理 中提到的,

charon程序的一开始,便进行了所有配置文件的加载。在library_init()函数里。

配置文件的生成

作为开发者,我们将通过netlink plugin,来学习如何正确的为plugin设置配置文件。

在strongswan的代码里,所有的配置文件管理,都在这个目录下

strongswan.git/conf

plugin在它的子目录里,strongswan.git/conf/plugins

配置文件是自动生成的,通过模块,和脚本的辅助。

两个模版文件:

default.conf, default.opt

配置文件的生成过程,在Makefile中:Makefile.am

大该的过程如下:

1. 读取configure阶段开启的所以plugin列表。

2. 在plugins目前下读取每一个plugin的自定义配置项,如kernel-netlink.opt

3. 如果没有默认配置项的,将使用默认的配置项配置文件:default.opt

4. 将自定义配置项中的配置,通过配置文件魔板整合生成对应plugin的配置文件 如:kernel-netlink.conf

这个配置文件在代码仓库中是没有的,而是在编译安装过程中生成的。

5. 为其他plugin重复以上步骤。

几段关键的代码:

1. 指定所有配置文件作为target的宏

# we only install snippets for enabled plugins
plugins_install_tmp = $(charon_plugins:%=plugins/%.tmp)
plugins_install_src = $(charon_plugins:%=plugins/%.conf)

2。整合魔板与定制内容的target

.opt.conf:
    $(AM_V_GEN) \
    case "$<" in \
    *plugins/*) \
        sed \
        -e "s:\@PLUGIN_NAME\@:`basename $< .opt`:" \
        $(srcdir)/default.opt | cat - $< | \
        $(PYTHON) $(srcdir)/format-options.py -f conf -r charon.plugins > $(srcdir)/$@ \
        ;; \
    *) \
        $(PYTHON) $(srcdir)/format-options.py -f conf -r charon.plugins $< > $(srcdir)/$@ \
        ;; \
    esac

# we need another implicit rule to generate files from the generic template only
# if the rules above did not catch it.  this requires an intermediate step that
# generates a copy of the generic config template.
$(plugins_install_tmp):
    @mkdir -p $(builddir)/plugins
    @cp $(srcdir)/default.conf $(builddir)/$@

.tmp.conf:
    $(AM_V_GEN) \
    sed \
    -e "s:\@PLUGIN_NAME\@:`basename $< .tmp`:" \
    $(builddir)/$< > $(builddir)/$@

举例:

假设我们现在要自开发一个plugin,classic-tong并为其指定以下配置项,只需要在目录

plugins下,添加一个文件,classic-tong.opt

并编辑内容:

charon.plugins.classic-tong.timeout = 0

就可以了。

猜你喜欢

转载自www.cnblogs.com/hugetong/p/10981346.html
今日推荐