minor optimizations
[util-vserver.git] / util-vserver / vserver-start / configuration.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
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.
8 //  
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.
13 //  
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.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "configuration.h"
24 #include "interface.h"
25
26 #include <lib_internal/util.h>
27 #include <ensc_vector/vector.h>
28 #include <lib/internal.h>
29
30 #include <dirent.h>
31 #include <string.h>
32
33 static inline bool
34 getSingleInterface(struct Interface *res,
35                    struct Interface const *tmpl,
36                    PathInfo const *basedir, char const *d_entry)
37 {
38   PathInfo      ent  = { .d = d_entry, .l = strlen(d_entry) };
39   PathInfo      path = *basedir;
40   char          path_buf[ENSC_PI_APPSZ(path, ent)];
41
42   PathInfo_append(&path, &ent, path_buf);
43   if (!utilvserver_isDirectory(path.d, true))
44     return true;        // skip non-directories
45
46   return Iface_read(res, &path, tmpl);
47 }
48
49 static inline bool
50 getInterfaces(struct Configuration *c, PathInfo const *cfgdir)
51 {
52   ENSC_PI_DECLARE(iface_subdir, "interfaces");
53   PathInfo              ifacepath = *cfgdir;
54   char                  path_buf[ENSC_PI_APPSZ(ifacepath, iface_subdir)];
55   struct Interface      iface_default;
56   DIR                   *dir;
57   bool                  rc = true;
58
59   PathInfo_append(&ifacepath, &iface_subdir, path_buf);
60
61   if (!utilvserver_isDirectory(ifacepath.d, true))
62     return true;        // no interface configuration -> ok
63   
64   Iface_init(&iface_default);
65   if (!Iface_read(&iface_default, &ifacepath, 0))
66     return false;
67
68     // iterate through dir-entries...
69   dir = opendir(ifacepath.d);
70   while (dir!=0) {
71     struct dirent       *ent = readdir(dir);
72     struct Interface    iface;
73     
74     if (ent==0)                 break;
75     if (isDotfile(ent->d_name)) continue;       // skip dot-files
76
77     Iface_init(&iface);
78     if (!getSingleInterface(&iface, &iface_default, &ifacepath, ent->d_name))
79       rc = false;
80     else if (iface.addr.ipv4.ip!=0) {   // HACK: non-directory entries would return true also
81       struct Interface  *new_iface = Vector_pushback(&c->interfaces);
82       *new_iface = iface;
83     }
84   }
85
86   if (dir!=0)
87     closedir(dir);
88
89   return rc;
90 }
91
92 bool
93 getConfiguration(struct Configuration *c, PathInfo const *cfgdir)
94 {
95   return getInterfaces(c, cfgdir);
96 }