File: interface-read.c

package info (click to toggle)
util-vserver 0.30.204-5sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,452 kB
  • ctags: 2,353
  • sloc: sh: 28,471; ansic: 15,138; xml: 1,334; cpp: 938; makefile: 334; perl: 85
file content (134 lines) | stat: -rw-r--r-- 3,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// $Id: interface-read.c,v 1.2 2004/08/19 16:06:37 ensc Exp $    --*- c -*--

// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "interface.h"

#include <lib_internal/filecfg.h>
#include <lib_internal/util.h>

#include <netinet/in.h>
#include <arpa/inet.h>

static inline char *
readEntryStr(PathInfo const *cfgdir, char const *file, char const *dflt)
{
  return FileCfg_readEntryStr(cfgdir, file, false, dflt);
}

static inline bool
readEntryFlag(PathInfo const *cfgdir, char const *file, bool dflt)
{
  return FileCfg_readEntryFlag(cfgdir, file, dflt);
}

static int
assumeNonNull(PathInfo const *cfgdir, char const *file, char const *val)
{
  if (val!=0) return 0;

  WRITE_MSG(2, "vserver-start: no value configured for '");
  write(2, cfgdir->d, cfgdir->l);
  WRITE_MSG(2, "/");
  WRITE_STR(2, file);
  WRITE_STR(2, "'\n");
  return 1;
}

bool
Iface_read(struct Interface *res, PathInfo *cfgdir,
	   struct Interface const *dflt)
{
  char const *	extip;
  char const *	ip;
  char const *	mask;
  char const *	prefix;
  char const *	bcast;
  bool		rc = false;

    // skip 'disabled' interfaces
  if (readEntryFlag(cfgdir, "disabled", false)) return true;
    
  ip          =  readEntryStr (cfgdir, "ip",       0);
  mask        =  readEntryStr (cfgdir, "mask",     0);
  prefix      =  readEntryStr (cfgdir, "prefix",   0);
  extip       =  readEntryStr (cfgdir, "extip",    0);
  bcast       =  readEntryStr (cfgdir, "bcast",    0);
  res->mac    =  readEntryStr (cfgdir, "mac",      0);
  res->name   =  readEntryStr (cfgdir, "name",     0);
  res->dev    =  readEntryStr (cfgdir, "dev",      dflt ? dflt->dev   : 0);
  res->scope  =  readEntryStr (cfgdir, "scope",    dflt ? dflt->scope : 0);
  res->nodev  =  readEntryFlag(cfgdir, "nodev",    false);
  res->direct = !readEntryFlag(cfgdir, "indirect", false);
  res->up     = !readEntryFlag(cfgdir, "down",     false);

  if (dflt && (
	assumeNonNull(cfgdir, "ip",  ip) +
	assumeNonNull(cfgdir, "dev", res->dev) +
	(dflt->addr.ipv4.mask>0) ? 0 : (
	  (mask   ? 0 : assumeNonNull(cfgdir, "prefix", prefix)) +
	  (prefix ? 0 : assumeNonNull(cfgdir, "mask",   mask))
	  )))
    goto err;

  if (mask && prefix) {
    WRITE_MSG(2, "vserver-start: both 'prefix' and 'mask' specified in '");
    write(2, cfgdir->d, cfgdir->l);
    WRITE_MSG(2, "'\n");
    goto err;
  }

  if (bcast)
    res->addr.ipv4.bcast = inet_addr(bcast);

  if (ip)
    res->addr.ipv4.ip    = inet_addr(ip);
  
  if (extip)
    res->addr.ipv4.extip = inet_addr(extip);

  if (prefix) {
    int		p = atoi(prefix);
    if (p==0) {
      WRITE_MSG(2, "vserver-start: invalid 'prefix' specified in '");
      write(2, cfgdir->d, cfgdir->l);
      WRITE_MSG(2, "'\n");
      goto err;
    }
      
    res->addr.ipv4.mask = htonl(-1u << (32-p));
  }
  else if (mask)
    res->addr.ipv4.mask = inet_addr(mask);
  else if (dflt)
    res->addr.ipv4.mask = dflt->addr.ipv4.mask;

  rc = true;

  err:
  free(const_cast(void *)(bcast));
  free(const_cast(void *)(extip));
  free(const_cast(void *)(ip));
  free(const_cast(void *)(mask));
  free(const_cast(void *)(prefix));

  return rc;
}