summaryrefslogtreecommitdiffstats
path: root/common/IFCFG.pm
blob: a8953487305a75cfb0c4253625e38cf086f67458 (plain)
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
#!/usr/bin/perl

# Interfaces Conf Parser

# Copyright (C) 2002,2003 Mandrakesoft 
#
# Author: Arnaud Desmons <adesmons@mandrakesoft.com>
#
# 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; either version 2, or (at your option)
# any later version.
#
# 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.

package MDK::Wizard::IFCFG;
use strict;
use lib qw(/usr/lib/libDrakX);
use MDK::Common;
use network::network;
use detect_devices;
require Net::Route::Table;

#my	$file = "/etc/sysconfig/network-scripts/drakconnect_conf";
#!-f $file and die "no such $file";
sub new {
    my @network_masks = (
    "128.0.0.0",
    "192.0.0.0",
    "224.0.0.0",
    "240.0.0.0",
    "248.0.0.0",
    "252.0.0.0",
    "254.0.0.0",
    "255.0.0.0",
    "255.128.0.0",
    "255.192.0.0",
    "255.224.0.0",
    "255.240.0.0",
    "255.248.0.0",
    "255.252.0.0",
    "255.254.0.0",
    "255.255.0.0",
    "255.255.128.0",
    "255.255.192.0",
    "255.255.224.0",
    "255.255.240.0",
    "255.255.248.0",
    "255.255.252.0",
    "255.225.254.0",
    "255.255.255.0",
    "255.255.255.128",
    "255.255.255.192",
    "255.255.255.224",
    "255.255.255.240",
    "255.255.255.248",
    "255.255.255.252",
    "255.255.255.254",
    "255.255.255.255");
    my	$self = {};
    my @devices = detect_devices::get_all_net_devices();
    foreach my $device (@devices) {
        if (detect_devices::is_lan_interface($device)) {
            local $_ = `/sbin/ip -4 address show dev $device |grep inet`;
            my ($ip, $netmask, $bcast,) = m!inet\s([0-9\.]*)/([0-9]*)\sbrd\s([0-9\.]*)!;
            if (defined $ip && defined $bcast && defined $netmask) {
                $self->{itf}{$device} = { IPADDR => $ip, BROADCAST => $bcast, NETMASK => @network_masks[$netmask - 1] };
                my %conf = getVarsFromSh("/etc/sysconfig/network-scripts/ifcfg-$device");
                $self->{itf}{$device}{$_} = $conf{$_} foreach 'BOOTPROTO';
            }
        }
    }
    %{$self->{network}} = getVarsFromSh("/etc/sysconfig/network");
    my $r = network::network::read_resolv_conf();
    foreach my $k (keys %$r) {
      $self->{network}{$k} ||= $r->{$k};
    } 
    $self->{network}{HOSTNAME} ||= `/bin/hostname` and chomp $self->{network}{HOSTNAME};
    ($self->{network}{DOMAINNAME}) = $self->{network}{HOSTNAME} =~ /\.(.*)/;
    $self->{network}{DOMAINNAME} ||= `/bin/dnsdomainname` and chomp $self->{network}{DOMAINNAME};
    bless $self;
}

sub is_dhcp {
    my ($self, $o_itf) = @_;

    $o_itf ||= default_itf();
    $self->{itf}{$o_itf}{BOOTPROTO} eq 'dhcp';
}

sub default_itf {
    return eval { Net::Route::Table->from_system->default_route->interface };
}

sub itf_get {
    my ($self, $key, $o_itf) = @_;

    $o_itf ||= default_itf();
    exists $self->{itf}{$o_itf}{$key} or print "ERROR: no $key field in $o_itf hash\n";
    $self->{itf}{$o_itf}{$key};
}

sub network_get {
    my ($self, $key) = @_;

    exists $self->{network}{$key} or print "ERROR: no $key field in network hash\n";
    $self->{network}{$key};
}

10;