]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/networking/tun_device_manager.c
libstrongswan: Registration of TUN device plugins
[thirdparty/strongswan.git] / src / libstrongswan / networking / tun_device_manager.c
1 /*
2 * Copyright (C) 2023 Andreas Steffen
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "tun_device_manager.h"
18
19 #include <utils/debug.h>
20
21 typedef struct private_tun_device_manager_t private_tun_device_manager_t;
22
23 /**
24 * private data of tun_device_manager
25 */
26 struct private_tun_device_manager_t {
27
28 /**
29 * public functions
30 */
31 tun_device_manager_t public;
32
33 /**
34 * constructor function to create tun_device instances
35 */
36 tun_device_constructor_t constructor;
37 };
38
39 METHOD(tun_device_manager_t, add_tun_device, void,
40 private_tun_device_manager_t *this, tun_device_constructor_t constructor)
41 {
42 if (!this->constructor)
43 {
44 this->constructor = constructor;
45 }
46 }
47
48 METHOD(tun_device_manager_t, remove_tun_device, void,
49 private_tun_device_manager_t *this, tun_device_constructor_t constructor)
50 {
51 if (this->constructor == constructor)
52 {
53 this->constructor = NULL;
54 }
55 }
56
57 METHOD(tun_device_manager_t, create, tun_device_t*,
58 private_tun_device_manager_t *this, const char *name_tmpl)
59 {
60 if (this->constructor)
61 {
62 return this->constructor(name_tmpl);
63 }
64 else
65 {
66 return tun_device_create(name_tmpl);
67 }
68 }
69
70 METHOD(tun_device_manager_t, destroy, void,
71 private_tun_device_manager_t *this)
72 {
73 free(this);
74 }
75
76 /*
77 * See header
78 */
79 tun_device_manager_t *tun_device_manager_create()
80 {
81 private_tun_device_manager_t *this;
82
83 INIT(this,
84 .public = {
85 .add_tun_device = _add_tun_device,
86 .remove_tun_device = _remove_tun_device,
87 .create = _create,
88 .destroy = _destroy,
89 },
90 );
91
92 return &this->public;
93 }