3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "configuration.h"
24 #include "interface.h"
26 #include <lib_internal/util.h>
27 #include <lib_internal/filecfg.h>
28 #include <ensc_vector/vector.h>
29 #include <lib/internal.h>
35 getSingleInterface(struct Interface *res,
36 struct Interface const *tmpl,
37 PathInfo const *basedir, char const *d_entry)
39 PathInfo ent = { .d = d_entry, .l = strlen(d_entry) };
40 PathInfo path = *basedir;
41 char path_buf[ENSC_PI_APPSZ(path, ent)];
43 PathInfo_append(&path, &ent, path_buf);
44 if (!utilvserver_isDirectory(path.d, true))
45 return true; // skip non-directories
47 return Iface_read(res, &path, tmpl);
51 getInterfaces(struct Configuration *cfg)
53 ENSC_PI_DECLARE(iface_subdir, "interfaces");
54 PathInfo ifacepath = cfg->cfgdir;
55 char path_buf[ENSC_PI_APPSZ(ifacepath, iface_subdir)];
56 struct Interface iface_default;
60 PathInfo_append(&ifacepath, &iface_subdir, path_buf);
62 if (!utilvserver_isDirectory(ifacepath.d, true))
63 return true; // no interface configuration -> ok
65 Iface_init(&iface_default);
66 if (!Iface_read(&iface_default, &ifacepath, 0))
69 // iterate through dir-entries...
70 dir = opendir(ifacepath.d);
72 struct dirent *ent = readdir(dir);
73 struct Interface iface;
76 if (isDotfile(ent->d_name)) continue; // skip dot-files
79 if (!getSingleInterface(&iface, &iface_default, &ifacepath, ent->d_name))
81 else if (iface.addr.ipv4.ip!=0) { // HACK: non-directory entries would return true also
82 struct Interface *new_iface = Vector_pushback(&cfg->interfaces);
94 initVdir(char const **vdir, PathInfo const *cfgdir)
96 *vdir = vc_getVserverVdir(cfgdir->d, vcCFG_RECENT_FULL, true);
98 WRITE_MSG(2, "Can not find root-directory of the vserver\n");
106 setFlag(void *flags_v, char const *str, size_t len)
108 struct vc_ctx_flags *flags = flags_v;
109 int rc = vc_list2cflag(str,len, 0,flags);
115 setCCap(void *caps_v, char const *str, size_t len)
117 struct vc_ctx_caps *caps = caps_v;
118 int rc = vc_list2ccap(str,len, 0,caps);
124 setBCap(void *caps_v, char const *str, size_t len)
126 struct vc_ctx_caps *caps = caps_v;
127 int rc = vc_list2bcap(str,len, 0,caps);
133 readSomething(void *dest, PathInfo const *cfgdir, char const *item,
134 FileCfg_MultiLineHandler handler)
136 char const *data = FileCfg_readEntryStr(cfgdir, item, true, 0);
139 if (!data) return true;
140 if (!FileCfg_iterateOverMultiLine(data, handler, dest)) {
141 WRITE_MSG(2, "Failed to parse '");
143 WRITE_MSG(2, "' configuration\n");
149 free(const_cast(char *)(data));
154 getConfiguration(struct Configuration *cfg, PathInfo const *cfgdir)
156 cfg->cfgdir = *cfgdir;
157 cfg->nice = FileCfg_readEntryStr(cfgdir, "nice", false, 0);
159 return (initVdir(&cfg->vdir, cfgdir) &&
160 readSomething(&cfg->ctx_flags, cfgdir, "flags", setFlag) &&
161 readSomething(&cfg->ctx_caps, cfgdir, "ccapabilities", setCCap) &&
162 readSomething(&cfg->ctx_caps, cfgdir, "bcapabilities", setBCap) &&