[ipsec][strongswan] 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

 

配置文件的讀取和使用

做了簡單的代碼分析,如 [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

就可以了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM