1 # slackware-linux-9.1-lib.pl
2 # Networking functions for slackware linux 9.1 and above. Unlike older releases
3 # of slackware, this one actually has a networking config file!!
6 $inet_conf = "/etc/rc.d/rc.inet1.conf";
7 %iconfig = &foreign_config("init");
8 $interfaces_file = $iconfig{'local_script'} || $iconfig{'extra_init'};
11 # Returns a list of interfaces brought up at boot time
18 push(@rv, { 'up' => 1,
23 'address' => '127.0.0.1',
24 'netmask' => '255.0.0.0' } );
26 # Look in inet1.conf file for master interfaces
28 &open_readfile(CONF, $inet_conf);
30 if (/^\s*IPADDR\[(\d+)\]\s*=\s*"(.*)"/) {
31 push(@rv, { 'up' => 1,
40 'fullname' => 'eth'.$1 });
42 elsif (/^\s*NETMASK\[(\d+)\]\s*=\s*"(.*)"/ && @rv) {
43 $rv[$#rv]->{'netmask'} = $2;
44 if ($2 =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/ &&
45 $rv[$#rv]->{'address'}) {
46 local ($a1, $a2, $a3, $a4) =
47 split(/\./, $rv[$#rv]->{'address'});
48 $rv[$#rv]->{'broadcast'} = sprintf "%d.%d.%d.%d",
49 ($a1 | ~int($1))&0xff,
50 ($a2 | ~int($2))&0xff,
51 ($a3 | ~int($3))&0xff,
52 ($a4 | ~int($4))&0xff;
54 $rv[$#rv]->{'eline'} = $lnum;
56 elsif (/^\s*USE_DHCP\[(\d+)\]\s*=\s*"(.*)"/ && @rv) {
57 $rv[$#rv]->{'dhcp'} = 1 if (lc($2) eq "yes");
58 $rv[$#rv]->{'eline'} = $lnum;
60 elsif (/^\s*\S+\[(\d+)\]\s*=\s*"(.*)"/ && @rv) {
61 # Some other directive in the current section
62 $rv[$#rv]->{'eline'} = $lnum;
68 # Filter out any unset
69 @rv = grep { $_->{'address'} || $_->{'dhcp'} } @rv;
71 for($i=0; $i<@rv; $i++) {
72 $rv[$i]->{'index'} = $i;
75 # Read extra init script for virtual interfaces
77 &open_readfile(IFACES, $interfaces_file);
80 if (/^(#*)\s*(\S*ifconfig)\s+(\S+)\s+(\S+)(\s+netmask\s+(\S+))?(\s+broadcast\s+(\S+))?(\s+mtu\s+(\d+))?\s+up$/) {
81 # Found a usable interface line
82 local $b = { 'fullname' => $3,
90 'index' => scalar(@rv) };
91 if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
96 $b->{'name'} = $b->{'fullname'};
106 # save_interface(&details)
107 # Create or update a boot-time interface's ifconfig command
110 # Find in existing config
111 local @boot = &boot_interfaces();
112 local ($old) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
114 if ($old && $old->{'init'}) {
115 # Modifying in inet1.conf file
116 &lock_file($inet_conf);
117 local $lref = &read_file_lines($inet_conf);
119 for($i=$old->{'line'}; $i<=$old->{'eline'}; $i++) {
120 if ($lref->[$i] =~ /^\s*IPADDR\[(\d+)\]\s*=\s*"(.*)"/) {
121 $lref->[$i] = "IPADDR\[$1\]=\"$_[0]->{'address'}\"";
123 elsif ($lref->[$i] =~ /^\s*NETMASK\[(\d+)\]\s*=\s*"(.*)"/) {
124 $lref->[$i] = "NETMASK\[$1\]=\"$_[0]->{'netmask'}\"";
126 elsif ($lref->[$i] =~ /^\s*USE_DHCP\[(\d+)\]\s*=\s*"(.*)"/) {
127 local $dhcp = $_[0]->{'dhcp'} ? "yes" : "";
128 $lref->[$i] = "USE_DHCP\[$1\]=\"$dhcp\"";
132 &unlock_file($inet_conf);
134 elsif (!$old && $_[0]->{'fullname'} =~ /^eth([0-3])$/) {
135 # Adding to inet1.conf file, in the appropriate empty section
137 &lock_file($inet_conf);
138 local $lref = &read_file_lines($inet_conf);
140 for($i=0; $i<@$lref; $i++) {
141 if ($lref->[$i] =~ /^\s*IPADDR\[(\d+)\]\s*=\s*"(.*)"/ &&
143 $lref->[$i] = "IPADDR\[$1\]=\"$_[0]->{'address'}\"";
145 elsif ($lref->[$i] =~ /^\s*NETMASK\[(\d+)\]\s*=\s*"(.*)"/ &&
147 $lref->[$i] = "NETMASK\[$1\]=\"$_[0]->{'netmask'}\"";
149 elsif ($lref->[$i] =~ /^\s*USE_DHCP\[(\d+)\]\s*=\s*"(.*)"/ &&
151 local $dhcp = $_[0]->{'dhcp'} ? "yes" : "";
152 $lref->[$i] = "USE_DHCP\[$1\]=\"$dhcp\"";
156 &unlock_file($inet_conf);
159 # Modifying or adding some other interface in separate file
160 &lock_file($interfaces_file);
161 local $lref = &read_file_lines($interfaces_file);
162 local $lnum = defined($_[0]->{'line'}) ? $_[0]->{'line'}
163 : &interface_lnum($_[0]);
164 if (defined($lnum)) {
165 $lref->[$lnum] = &interface_line($_[0]);
168 push(@$lref, &interface_line($_[0]));
171 &unlock_file($interfaces_file);
175 # delete_interface(&details)
176 # Delete a boot-time interface's ifconfig command
179 # Find in existing config
180 local @boot = &boot_interfaces();
181 local ($old) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
183 if ($old && $old->{'init'}) {
184 # Deleting from inet1.conf file .. just set to blank
185 &lock_file($inet_conf);
186 local $lref = &read_file_lines($inet_conf);
188 for($i=$old->{'line'}; $i<=$old->{'eline'}; $i++) {
189 if ($lref->[$i] =~ /^\s*(\S+)\[(\d+)\]\s*=\s*"(.*)"/) {
190 $lref->[$i] = "$1\[$2\]=\"\"";
194 &unlock_file($inet_conf);
197 # Deleting from separate file
198 &lock_file($interfaces_file);
199 local $lref = &read_file_lines($interfaces_file);
200 local $lnum = defined($_[0]->{'line'}) ? $_[0]->{'line'}
201 : &interface_lnum($_[0]);
202 if (defined($lnum)) {
203 splice(@$lref, $lnum, 1);
206 &unlock_file($interfaces_file);
212 local @boot = &boot_interfaces();
213 local ($found) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
214 return $found ? $found->{'line'} : undef;
220 $str .= "# " if (!$_[0]->{'up'});
221 $str .= &has_command("ifconfig");
222 if (!$_[0]->{'fullname'}) {
223 $_[0]->{'fullname'} = $_[0]->{'virtual'} ne "" ?
224 $_[0]->{'name'}.":".$_[0]->{'virtual'} : $_[0]->{'name'};
226 $str .= " $_[0]->{'fullname'} $_[0]->{'address'}";
227 if ($_[0]->{'netmask'}) {
228 $str .= " netmask $_[0]->{'netmask'}";
230 if ($_[0]->{'broadcast'}) {
231 $str .= " broadcast $_[0]->{'broadcast'}";
233 if ($_[0]->{'mtu'}) {
234 $str .= " mtu $_[0]->{'mtu'}";
241 # Can some boot-time interface parameter be edited?
244 return $_[0] ne "bootp" && $_[0] ne "mtu";
247 # valid_boot_address(address)
248 # Is some address valid for a bootup interface
249 sub valid_boot_address
251 return &check_ipaddress($_[0]);
257 local $hn = &read_file_contents("/etc/HOSTNAME");
262 return &get_system_hostname(1);
265 # save_hostname(name)
268 &system_logged("hostname $_[0] >/dev/null 2>&1");
269 &open_lock_tempfile(HOST, ">/etc/HOSTNAME");
270 &print_tempfile(HOST, $_[0],"\n");
271 &close_tempfile(HOST);
272 undef(@main::get_system_hostname); # clear cache
275 sub routing_config_files
277 return ( $inet_conf );
282 &open_readfile(INIT, $inet_conf);
286 if (/^\s*GATEWAY\s*=\s*"(.*)"/) {
291 print &ui_table_row($text{'routes_default'},
292 &ui_opt_textbox("gw", $gw, 20, $text{'routes_none'},
293 $text{'routes_gateway'}));
299 if (!$in{'gw_def'}) {
300 &check_ipaddress($in{'gw'}) ||
301 &error(&text('routes_edefault', $in{'gw'}));
304 &lock_file($inet_conf);
305 local $lref = &read_file_lines($inet_conf);
306 foreach $l (@$lref) {
307 if ($l =~ /^(\s*)GATEWAY\s*=\s*"(.*)"(.*)/) {
308 $l = $1."GATEWAY=\"".$gw."\"".$3;
312 &unlock_file($inet_conf);
315 # supports_address6([&iface])
316 # Returns 1 if managing IPv6 interfaces is supported
317 sub supports_address6