summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c90f3da)
raw | patch | inline | side by side (parent: c90f3da)
author | janw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8> | |
Mon, 14 Jan 2008 12:54:15 +0000 (12:54 +0000) | ||
committer | janw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8> | |
Mon, 14 Jan 2008 12:54:15 +0000 (12:54 +0000) |
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8322 594d385d-05f5-0310-b6e9-bd551577e9d8
gosa-si/gosa-si-bus | patch | blob | history |
diff --git a/gosa-si/gosa-si-bus b/gosa-si/gosa-si-bus
index 30808a668744523ae276583c712b183d31fd2ecf..330d205a94cab6caa121329a979eec9896b7e960 100755 (executable)
--- a/gosa-si/gosa-si-bus
+++ b/gosa-si/gosa-si-bus
use GOSA::DBsqlite;
my ($cfg_file, $default_cfg_file, %cfg_defaults, $foreground, $verbose);
-my ($bus_activ, $bus_passwd, $bus_ip, $bus_port, $bus_address, $bus, $bus_mac_address);
+my ($bus_activ, $bus_passwd, $bus_ip, $bus_port, $bus_address, $bus, $bus_mac_address, $network_interface);
my ($pid_file, $procid, $pid, $log_file, $my_own_address);
my (%free_child, %busy_child, $child_max, $child_min, %child_alive_time, $child_timeout);
my ($bus_known_server_db, $bus_known_server_file_name);
"bus" =>
{"bus_activ" => [\$bus_activ, "on"],
"bus_passwd" => [\$bus_passwd, ""],
+ "bus_ip" => [\$bus_ip, "0.0.0.0"],
"bus_port" => [\$bus_port, "20080"],
}
);
print STDERR "cannot open $log_file: $!";
return }
chomp($msg);
- if($level <= $verbose){
+ if($level && $verbose && $level <= $verbose){
print LOG_HANDLE $msg."\n";
if(defined $foreground) { print $msg."\n" }
}
}
$SIG{INT} = \&sig_int_handler;
+#=== FUNCTION ================================================================
+# NAME: get_interface_for_ip
+# PARAMETERS: ip address (i.e. 192.168.0.1)
+# RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
+# DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
+#===============================================================================
+sub get_interface_for_ip {
+ my $result;
+ my $ip= shift;
+ if ($ip && length($ip) > 0) {
+ my @ifs= &get_interfaces();
+ if($ip eq "0.0.0.0") {
+ $result = "all";
+ } else {
+ foreach (@ifs) {
+ my $if=$_;
+ if(get_ip($if) eq $ip) {
+ $result = $if;
+ }
+ }
+ }
+ }
+ return $result;
+}
#=== FUNCTION ================================================================
-# NAME: get_ip_and_mac
-# PARAMETERS: nothing
-# RETURNS: (ip, mac)
-# DESCRIPTION: executes /sbin/ifconfig and parses the output, the first occurence
-# of a inet address is returned as well as the mac address in the line
-# above the inet address
+# NAME: get_interfaces
+# PARAMETERS: none
+# RETURNS: (list of interfaces)
+# DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
#===============================================================================
-sub get_ip_and_mac {
- my $ip = "0.0.0.0.0"; # Defualt-IP
- my $mac_address = "00:00:00:00:00:00"; # Default-MAC
- my @ifconfig = qx(/sbin/ifconfig);
- foreach(@ifconfig) {
- if (/Hardware Adresse (\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/) {
- $mac_address = "$1:$2:$3:$4:$5:$6";
- next;
+sub get_interfaces {
+ my @result;
+ my $PROC_NET_DEV= ('/proc/net/dev');
+
+ open(PROC_NET_DEV, "<$PROC_NET_DEV")
+ or die "Could not open $PROC_NET_DEV";
+
+ my @ifs = <PROC_NET_DEV>;
+
+ close(PROC_NET_DEV);
+
+ # Eat first two line
+ shift @ifs;
+ shift @ifs;
+
+ chomp @ifs;
+ foreach my $line(@ifs) {
+ my $if= (split /:/, $line)[0];
+ $if =~ s/^\s+//;
+ push @result, $if;
}
- if (/inet Adresse:(\d+).(\d+).(\d+).(\d+)/) {
- $ip = "$1.$2.$3.$4";
- last;
+
+ return @result;
+}
+
+#=== FUNCTION ================================================================
+# NAME: get_mac
+# PARAMETERS: interface name (i.e. eth0)
+# RETURNS: (mac address)
+# DESCRIPTION: Uses ioctl to get mac address directly from system.
+#===============================================================================
+sub get_mac {
+ my $ifreq= shift;
+ my $result;
+ if ($ifreq && length($ifreq) > 0) {
+ if($ifreq eq "all") {
+ $result = "00:00:00:00:00:00";
+ } else {
+ my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
+
+ # A configured MAC Address should always override a guessed value
+ if ($bus_mac_address and length($bus_mac_address) > 0) {
+ return $bus_mac_address;
+ }
+
+ socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
+ or die "socket: $!";
+
+ if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
+ my ($if, $mac)= unpack 'h36 H12', $ifreq;
+
+ if (length($mac) > 0) {
+ $mac=~ m/^([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/;
+ $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
+ $result = $mac;
+ }
+ }
+ }
}
- }
- return ($ip, $mac_address);
+ return $result;
}
+#=== FUNCTION ================================================================
+# NAME: get_ip
+# PARAMETERS: interface name (i.e. eth0)
+# RETURNS: (ip address)
+# DESCRIPTION: Uses ioctl to get ip address directly from system.
+#===============================================================================
+sub get_ip {
+ my $ifreq= shift;
+ my $result= "";
+ my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
+ my $proto= getprotobyname('ip');
+
+ socket SOCKET, PF_INET, SOCK_DGRAM, $proto
+ or die "socket: $!";
+
+ if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
+ my ($if, $sin) = unpack 'a16 a16', $ifreq;
+ my ($port, $addr) = sockaddr_in $sin;
+ my $ip = inet_ntoa $addr;
+
+ if ($ip && length($ip) > 0) {
+ $result = $ip;
+ }
+ }
+ return $result;
+}
#=== FUNCTION ================================================================
# NAME: activating_child
# detect own ip and mac address
-($bus_ip, $bus_mac_address) = &get_ip_and_mac();
-if (not defined $bus_ip) {
- die "EXIT: ip address of $0 could not be detected";
-}
+$network_interface= &get_interface_for_ip($bus_ip);
+$bus_mac_address= &get_mac($network_interface);
+
daemon_log("bus ip address detected: $bus_ip", 1);
daemon_log("bus mac address detected: $bus_mac_address", 1);