merge with 0.30.213
[util-vserver.git] / vserver-start / interface-read.c
1 // $Id: interface-read.c 1980 2005-03-24 12:44:17Z ensc $    --*- 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   Vwrite   (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     // skip 'disabled' interfaces
68   if (readEntryFlag(cfgdir, "disabled", false)) return true;
69     
70   ip          =  readEntryStr (cfgdir, "ip",       0);
71   mask        =  readEntryStr (cfgdir, "mask",     0);
72   prefix      =  readEntryStr (cfgdir, "prefix",   0);
73   extip       =  readEntryStr (cfgdir, "extip",    0);
74   bcast       =  readEntryStr (cfgdir, "bcast",    0);
75   res->mac    =  readEntryStr (cfgdir, "mac",      0);
76   res->name   =  readEntryStr (cfgdir, "name",     0);
77   res->dev    =  readEntryStr (cfgdir, "dev",      dflt ? dflt->dev   : 0);
78   res->scope  =  readEntryStr (cfgdir, "scope",    dflt ? dflt->scope : 0);
79   res->nodev  =  readEntryFlag(cfgdir, "nodev",    false);
80   res->direct = !readEntryFlag(cfgdir, "indirect", false);
81   res->up     = !readEntryFlag(cfgdir, "down",     false);
82
83   if (dflt && (
84         assumeNonNull(cfgdir, "ip",  ip) +
85         assumeNonNull(cfgdir, "dev", res->dev) +
86         (dflt->addr.ipv4.mask>0) ? 0 : (
87           (mask   ? 0 : assumeNonNull(cfgdir, "prefix", prefix)) +
88           (prefix ? 0 : assumeNonNull(cfgdir, "mask",   mask))
89           )))
90     goto err;
91
92   if (mask && prefix) {
93     WRITE_MSG(2, "vserver-start: both 'prefix' and 'mask' specified in '");
94     Vwrite   (2, cfgdir->d, cfgdir->l);
95     WRITE_MSG(2, "'\n");
96     goto err;
97   }
98
99   if (bcast)
100     res->addr.ipv4.bcast = inet_addr(bcast);
101
102   if (ip)
103     res->addr.ipv4.ip    = inet_addr(ip);
104   
105   if (extip)
106     res->addr.ipv4.extip = inet_addr(extip);
107
108   if (prefix) {
109     int         p = atoi(prefix);
110     if (p==0) {
111       WRITE_MSG(2, "vserver-start: invalid 'prefix' specified in '");
112       Vwrite   (2, cfgdir->d, cfgdir->l);
113       WRITE_MSG(2, "'\n");
114       goto err;
115     }
116       
117     res->addr.ipv4.mask = htonl(-1u << (32-p));
118   }
119   else if (mask)
120     res->addr.ipv4.mask = inet_addr(mask);
121   else if (dflt)
122     res->addr.ipv4.mask = dflt->addr.ipv4.mask;
123
124   rc = true;
125
126   err:
127   free(const_cast(void *)(bcast));
128   free(const_cast(void *)(extip));
129   free(const_cast(void *)(ip));
130   free(const_cast(void *)(mask));
131   free(const_cast(void *)(prefix));
132
133   return rc;
134 }