1 # slackware-linux-lib.pl
2 # Networking functions for slackware linux
3 # To support boot-time interfaces, ifconfig commands are added to rc.local so
4 # that additional virtual interfaces can be created
7 %iconfig = &foreign_config("init");
8 $interfaces_file = $iconfig{'local_script'} || $iconfig{'extra_init'};
9 $rc_init = "/etc/rc.d/rc.inet1";
10 $dhcp_init = "/etc/rc.d/rc.dhcpd";
13 # Returns a list of interfaces brought up at boot time
18 # Look in rc.init1 file for master interface
19 local $iface = { 'up' => 1,
27 &open_readfile(INIT, $rc_init);
31 if (/^\s*IPADDR\s*=\s*["']?([0-9\.]+)/) {
32 $iface->{'address'} = $1;
34 elsif (/^\s*DEVICE\s*=\s*["']?([0-9\.]+)/) {
35 $iface->{'name'} = $iface->{'fullname'} = $1;
38 elsif (/^\s*NETMASK\s*=\s*["']?([0-9\.]+)/) {
39 $iface->{'netmask'} = $1;
41 elsif (/^\s*BROADCAST\s*=\s*["']?([0-9\.]+)/) {
42 $iface->{'broadcast'} = $1;
44 elsif (/^\s*DHCP\s*=\s*["']?([0-9\.]+)/) {
45 $iface->{'dhcp'} = ($1 eq "yes");
47 elsif (/^\s*ifconfig\s+(\S+)\s+.*IPADDR.*/ && !$gotdevice) {
48 $iface->{'name'} = $iface->{'fullname'} = $1;
52 local @st1 = stat($rc_init);
53 local @st2 = stat($dhcp_init);
54 if ($st1[7] == $st2[7]) {
55 # Looks like rc.dhcpd script has been copied to rc.inet1 - assume DHCP
58 push(@rv, $iface) if ($iface->{'address'} || $iface->{'dhcp'});
60 # Read extra init script for virtual interfaces
62 &open_readfile(IFACES, $interfaces_file);
65 if (/^(#*)\s*(\S*ifconfig)\s+(\S+)\s+(\S+)(\s+netmask\s+(\S+))?(\s+broadcast\s+(\S+))?(\s+mtu\s+(\d+))?\s+up$/) {
66 # Found a usable interface line
67 local $b = { 'fullname' => $3,
75 'file' => $interfaces_file,
76 'index' => scalar(@rv) };
77 if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
82 $b->{'name'} = $b->{'fullname'};
92 # save_interface(&details)
93 # Create or update a boot-time interface's ifconfig command
96 if ($_[0]->{'index'} == 0 && $_[0]->{'fullname'} eq 'eth0') {
97 # Modifying the primary interface
99 if ($_[0]->{'dhcp'} && -r $dhcp_init) {
100 # Just copy rc.dhcpd to rc.inet1
101 &system_logged("cp $dhcp_init $rc_init");
104 # Is the current file rc.dhcpd?
105 if (!$_[0]->{'dhcp'}) {
106 local @st1 = stat($rc_init);
107 local @st2 = stat($dhcp_init);
108 if ($st1[7] == $st2[7]) {
109 # Yes! Use built-in static IP version
110 &system_logged("cp $module_root_directory/rc.inet1 $rc_init");
114 # Update init script with new settings
115 local $lref = &read_file_lines($rc_init);
116 foreach $l (@$lref) {
117 if ($l =~ /^(\s*)IPADDR\s*=\s*(\S+)(.*)/) {
118 $l = $1."IPADDR=\"".$_[0]->{'address'}."\"".$3;
120 elsif ($l =~ /^(\s*)NETMASK\s*=\s*(\S+)(.*)/) {
121 $l = $1."NETMASK=\"".$_[0]->{'netmask'}."\"".$3;
123 elsif ($l =~ /^(\s*)BROADCAST\s*=\s*(\S+)(.*)/) {
124 $l = $1."BROADCAST=\"".$_[0]->{'broadcast'}."\"".$3;
126 if ($l =~ /^(\s*)DHCP\s*=\s*(\S+)(.*)/) {
127 $l = $1."DHCP=\"".($_[0]->{'dhcp'} ? "yes" : "no")."\"".$3;
132 &unlock_file($rc_init);
135 # Modifying or adding some other interface
136 $_[0]->{'dhcp'} && &error($text{'bifc_edhcpmain'});
137 &lock_file($interfaces_file);
138 local $lref = &read_file_lines($interfaces_file);
139 local $lnum = defined($_[0]->{'line'}) ? $_[0]->{'line'}
140 : &interface_lnum($_[0]);
141 if (defined($lnum)) {
142 $lref->[$lnum] = &interface_line($_[0]);
145 push(@$lref, &interface_line($_[0]));
148 &unlock_file($interfaces_file);
152 # delete_interface(&details)
153 # Delete a boot-time interface's ifconfig command
156 if ($_[0]->{'init'}) {
157 &error("The primary network interface cannot be deleted");
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 splice(@$lref, $lnum, 1);
168 &unlock_file($interfaces_file);
174 local @boot = &boot_interfaces();
175 local ($found) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
176 return $found ? $found->{'line'} : undef;
182 $str .= "# " if (!$_[0]->{'up'});
183 $str .= &has_command("ifconfig");
184 if (!$_[0]->{'fullname'}) {
185 $_[0]->{'fullname'} = $_[0]->{'virtual'} ne "" ?
186 $_[0]->{'name'}.":".$_[0]->{'virtual'} : $_[0]->{'name'};
188 $str .= " $_[0]->{'fullname'} $_[0]->{'address'}";
189 if ($_[0]->{'netmask'}) {
190 $str .= " netmask $_[0]->{'netmask'}";
192 if ($_[0]->{'broadcast'}) {
193 $str .= " broadcast $_[0]->{'broadcast'}";
195 if ($_[0]->{'mtu'}) {
196 $str .= " mtu $_[0]->{'mtu'}";
203 # Can some boot-time interface parameter be edited?
206 return $_[0] ne "bootp";
209 # valid_boot_address(address)
210 # Is some address valid for a bootup interface
211 sub valid_boot_address
213 return &check_ipaddress($_[0]);
219 local $hn = &read_file_contents("/etc/HOSTNAME");
224 return &get_system_hostname(1);
227 # save_hostname(name)
230 &system_logged("hostname $_[0] >/dev/null 2>&1");
231 &open_lock_tempfile(HOST, ">/etc/HOSTNAME");
232 &print_tempfile(HOST, $_[0],"\n");
233 &close_tempfile(HOST);
234 undef(@main::get_system_hostname); # clear cache
237 sub routing_config_files
244 &open_readfile(INIT, $rc_init);
248 if (/^\s*GATEWAY\s*=\s*["']?([0-9\.]+)/) {
253 print &ui_table_row($text{'routes_default'},
254 &ui_opt_textbox("gw", $gw, 20, $text{'routes_none'},
255 $text{'routes_gateway'}));
261 if (!$in{'gw_def'}) {
262 &check_ipaddress($in{'gw'}) ||
263 &error(&text('routes_edefault', $in{'gw'}));
266 &lock_file($rc_init);
267 local $lref = &read_file_lines($rc_init);
268 foreach $l (@$lref) {
269 if ($l =~ /^(\s*)GATEWAY\s*=\s*(\S+)(.*)/) {
270 $l = $1."GATEWAY=\"".$gw."\"".$3;
274 &unlock_file($rc_init);
278 # supports_address6([&iface])
279 # Returns 1 if managing IPv6 interfaces is supported
280 sub supports_address6