minor optimizations
[util-vserver.git] / util-vserver / vserver-start / interface-read.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 "interface.h"
24
25 #include <lib_internal/filecfg.h>
26 #include <lib_internal/util.h>
27
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30
31 static inline char *
32 readEntryStr(PathInfo const *cfgdir, char const *file, char const *dflt)
33 {
34   return FileCfg_readEntryStr(cfgdir, file, false, dflt);
35 }
36
37 static inline bool
38 readEntryFlag(PathInfo const *cfgdir, char const *file, bool dflt)
39 {
40   return FileCfg_readEntryFlag(cfgdir, file, dflt);
41 }
42
43 static int
44 assumeNonNull(PathInfo const *cfgdir, char const *file, char const *val)
45 {
46   if (val!=0) return 0;
47
48   WRITE_MSG(2, "vserver-start: no value configured for '");
49   write(2, cfgdir->d, cfgdir->l);
50   WRITE_MSG(2, "/");
51   WRITE_STR(2, file);
52   WRITE_STR(2, "'\n");
53   return 1;
54 }
55
56 bool
57 Iface_read(struct Interface *res, PathInfo *cfgdir,
58            struct Interface const *dflt)
59 {
60   char const *  extip;
61   char const *  ip;
62   char const *  mask;
63   char const *  prefix;
64   char const *  bcast;
65   bool          rc = false;
66
67   ip          =  readEntryStr (cfgdir, "ip",       0);
68   mask        =  readEntryStr (cfgdir, "mask",     0);
69   prefix      =  readEntryStr (cfgdir, "prefix",   0);
70   extip       =  readEntryStr (cfgdir, "extip",    0);
71   bcast       =  readEntryStr (cfgdir, "bcast",    0);
72   res->mac    =  readEntryStr (cfgdir, "mac",      0);
73   res->name   =  readEntryStr (cfgdir, "name",     0);
74   res->dev    =  readEntryStr (cfgdir, "dev",      dflt ? dflt->dev   : 0);
75   res->scope  =  readEntryStr (cfgdir, "scope",    dflt ? dflt->scope : 0);
76   res->nodev  =  readEntryFlag(cfgdir, "nodev",    false);
77   res->direct = !readEntryFlag(cfgdir, "indirect", false);
78   res->up     = !readEntryFlag(cfgdir, "down",     false);
79
80   if (dflt && (
81         assumeNonNull(cfgdir, "ip",  ip) +
82         assumeNonNull(cfgdir, "dev", res->dev) +
83         (dflt->addr.ipv4.mask>0) ? 0 : (
84           (mask   ? 0 : assumeNonNull(cfgdir, "prefix", prefix)) +
85           (prefix ? 0 : assumeNonNull(cfgdir, "mask",   mask))
86           )))
87     goto err;
88
89   if (mask && prefix) {
90     WRITE_MSG(2, "vserver-start: both 'prefix' and 'mask' specified in '");
91     write(2, cfgdir->d, cfgdir->l);
92     WRITE_MSG(2, "'\n");
93     goto err;
94   }
95
96   if (bcast)
97     res->addr.ipv4.bcast = inet_addr(bcast);
98
99   if (ip)
100     res->addr.ipv4.ip    = inet_addr(ip);
101   
102   if (extip)
103     res->addr.ipv4.extip = inet_addr(extip);
104
105   if (prefix) {
106     int         p = atoi(prefix);
107     if (p==0) {
108       WRITE_MSG(2, "vserver-start: invalid 'prefix' specified in '");
109       write(2, cfgdir->d, cfgdir->l);
110       WRITE_MSG(2, "'\n");
111       goto err;
112     }
113       
114     res->addr.ipv4.mask = htonl(-1u << (32-p));
115   }
116   else if (mask)
117     res->addr.ipv4.mask = inet_addr(mask);
118   else if (dflt)
119     res->addr.ipv4.mask = dflt->addr.ipv4.mask;
120
121   rc = true;
122
123   err:
124   free(const_cast(void *)(bcast));
125   free(const_cast(void *)(extip));
126   free(const_cast(void *)(ip));
127   free(const_cast(void *)(mask));
128   free(const_cast(void *)(prefix));
129
130   return rc;
131 }