2 # Common local networking functions
4 BEGIN { push(@INC, ".."); };
7 %access = &get_module_acl();
8 $access{'ipnodes'} = $access{'hosts'};
10 if (-r "$module_root_directory/$gconfig{'os_type'}-$gconfig{'os_version'}-lib.pl") {
11 do "$gconfig{'os_type'}-$gconfig{'os_version'}-lib.pl";
13 elsif ($gconfig{'os_type'} eq 'suse-linux' &&
14 $gconfig{'os_version'} >= 9.2) {
15 # Special case for SuSE 9.2+
16 do "$gconfig{'os_type'}-9.2-ALL-lib.pl";
18 elsif ($gconfig{'os_type'} eq 'slackware-linux' &&
19 $gconfig{'os_version'} >= 9.1) {
20 # Special case for Slackware 9.1+
21 do "$gconfig{'os_type'}-9.1-ALL-lib.pl";
24 do "$gconfig{'os_type'}-lib.pl";
28 # Parse hosts from /etc/hosts into a data structure
35 &open_readfile(HOSTS, $config{'hosts_file'});
36 while($line=<HOSTS>) {
40 local(@f)=split(/\s+/, $line);
41 local($ipaddr)=shift(@f);
42 if (check_ipaddress_any($ipaddr)) {
43 push(@rv, { 'address' => $ipaddr,
46 'index', scalar(@rv) });
55 # Add a new host to /etc/hosts
58 &open_tempfile(HOSTS, ">>$config{'hosts_file'}");
59 &print_tempfile(HOSTS, $_[0]->{'address'},"\t",join(" ",@{$_[0]->{'hosts'}}),"\n");
60 &close_tempfile(HOSTS);
64 # Update the address and hosts of a line in /etc/hosts
67 &replace_file_line($config{'hosts_file'},
69 $_[0]->{'address'}."\t".join(" ",@{$_[0]->{'hosts'}})."\n");
73 # Delete a host from /etc/hosts
76 &replace_file_line($config{'hosts_file'}, $_[0]->{'line'});
80 # Parse ipnodes from /etc/ipnodes into a data structure
85 &open_readfile(HOSTS, $config{'ipnodes_file'});
90 if (/([0-9a-f:]+|[0-9\.]+)\s+(.*)$/) {
91 push(@rv, { 'address' => $1,
92 'ipnodes' => [ split(/\s+/, $2) ],
94 'index', scalar(@rv) });
102 # create_ipnode(&ipnode)
103 # Add a new ipnode to /etc/ipnodes
106 &open_tempfile(HOSTS, ">>$config{'ipnodes_file'}");
107 &print_tempfile(HOSTS, $_[0]->{'address'},"\t",join(" ",@{$_[0]->{'ipnodes'}}),"\n");
108 &close_tempfile(HOSTS);
111 # modify_ipnode(&ipnode)
112 # Update the address and ipnodes of a line in /etc/ipnodes
115 &replace_file_line($config{'ipnodes_file'},
117 $_[0]->{'address'}."\t".join(" ",@{$_[0]->{'ipnodes'}})."\n");
120 # delete_ipnode(&ipnode)
121 # Delete a ipnode from /etc/ipnodes
124 &replace_file_line($config{'ipnodes_file'}, $_[0]->{'line'});
128 # Convert an address like ff000000 into 255.0.0.0
131 $_[0] =~ /(..)(..)(..)(..)$/;
132 return join(".", (hex($1), hex($2), hex($3), hex($4)));
135 # interfaces_chooser_button(field, multiple, [form])
136 # Returns HTML for a javascript button for choosing an interface or interfaces
137 sub interfaces_chooser_button
139 local $form = @_ > 2 ? $_[2] : 0;
140 local $w = $_[1] ? 500 : 300;
141 return "<input type=button onClick='ifield = document.forms[$form].$_[0]; chooser = window.open(\"$gconfig{'webprefix'}/net/interface_chooser.cgi?multi=$_[1]&interface=\"+escape(ifield.value), \"chooser\", \"toolbar=no,menubar=no,scrollbars=yes,width=$w,height=200\"); chooser.ifield = ifield' value=\"...\">\n";
144 # prefix_to_mask(prefix)
145 # Converts a number like 24 to a mask like 255.255.255.0
148 return $_[0] >= 24 ? "255.255.255.".(256-(2 ** (32-$_[0]))) :
149 $_[0] >= 16 ? "255.255.".(256-(2 ** (24-$_[0]))).".0" :
150 $_[0] >= 8 ? "255.".(256-(2 ** (16-$_[0]))).".0.0" :
151 (256-(2 ** (8-$_[0]))).".0.0.0";
154 # mask_to_prefix(mask)
155 # Converts a mask like 255.255.255.0 to a prefix like 24
158 return $_[0] =~ /^255\.255\.255\.(\d+)$/ ? 32-&log2(256-$1) :
159 $_[0] =~ /^255\.255\.(\d+)\.0$/ ? 24-&log2(256-$1) :
160 $_[0] =~ /^255\.(\d+)\.0\.0$/ ? 16-&log2(256-$1) :
161 $_[0] =~ /^(\d+)\.0\.0\.0$/ ? 8-&log2(256-$1) : 32;
166 return int(log($_[0])/log(2));
169 # module_for_interface(&interface)
170 # Returns a structure containing details of some other module that manages
171 # some active interface
172 sub module_for_interface
174 if (&foreign_check("zones") && $_[0]->{'zone'}) {
175 # Zones virtual interface
176 return { 'module' => 'zones',
177 'desc' => &text('mod_zones', $_[0]->{'zone'}) };
179 if (&foreign_check("virtual-server") && $_[0]->{'virtual'} ne '') {
180 # Check for a Virtualmin interface
181 &foreign_require("virtual-server", "virtual-server-lib.pl");
182 local ($d) = &virtual_server::get_domain_by("ip", $_[0]->{'address'});
184 return { 'module' => 'virtual-server',
185 'desc' => &text('mod_virtualmin', $d->{'dom'}) };
187 if (defined(&virtual_server::list_resellers)) {
188 ($resel) = grep { $_->{'acl'}->{'defip'} eq $_[0]->{'address'} }
189 &virtual_server::list_resellers();
191 return { 'module' => 'virtual-server',
192 'desc' => &text('mod_reseller',
197 return undef if ($_[0]->{'name'} !~ /^ppp/); # only for PPP
198 if (&foreign_check("ppp-client")) {
199 # Dialup PPP connection
200 &foreign_require("ppp-client", "ppp-client-lib.pl");
201 local ($ip, $pid, $sect) = &ppp_client::get_connect_details();
202 if ($ip eq $_[0]->{'address'}) {
203 return { 'module' => 'ppp-client',
204 'desc' => &text('mod_ppp', $sect) };
207 if (&foreign_check("adsl-client")) {
208 # ADSL PPP connection
209 &foreign_require("adsl-client", "adsl-client-lib.pl");
210 local ($dev, $ip) = &adsl_client::get_adsl_ip();
211 if ("ppp$dev" eq $_[0]->{'fullname'}) {
212 return { 'module' => 'adsl-client',
213 'desc' => &text('mod_adsl') };
216 if (&foreign_check("pap")) {
217 # Dialin PPP connection
218 # XXX not handled yet
220 if (&foreign_check("pptp-client")) {
221 # PPTP client connection
222 &foreign_require("pptp-client", "pptp-client-lib.pl");
223 local @tunnels = &pptp_client::list_tunnels();
224 local %tunnels = map { $_->{'name'}, 1 } @tunnels;
225 local @conns = &pptp_client::list_connected();
226 foreach $c (@conns) {
227 if ($c->[2] eq $_[0]->{'fullname'}) {
228 return { 'module' => 'pptp-client',
229 'desc' => &text('mod_pptpc', "<i>$c->[0]</i>") };
233 if (&foreign_check("pptp-server")) {
234 # PPTP server connection
235 &foreign_require("pptp-server", "pptp-server-lib.pl");
236 local @conns = &pptp_server::list_connections();
238 foreach $c (@conns) {
239 if ($c->[3] eq $_[0]->{'fullname'} ||
240 $c->[4] eq $_[0]->{'address'}) {
241 return { 'module' => 'pptp-server',
242 'desc' => &text('mod_pptps', $c->[2]) };
252 local $name = ref($_[0]) && $_[0]->{'fullname'} ? $_[0]->{'fullname'} :
253 ref($_[0]) ? $_[0]->{'name'}.
254 ($_[0]->{'virtual'} ne "" ? ":$_[0]->{'virtual'}" : "") :
256 return 0 if ($access{'ifcs'} == 0 || $access{'ifcs'} == 1);
257 return 1 if ($access{'ifcs'} == 2);
258 local %can = map { $_, 1 } split(/\s+/, $access{'interfaces'});
259 if ($access{'ifcs'} == 3) {
269 return $access{'ifcs'} == 2;
272 # interface_choice(name, value, blankmode-text, [disabled?], [non-virt-only])
273 # Returns HTML for an interface chooser menu
276 my ($name, $value, $blanktext, $disabled, $nonvirt) = @_;
277 my @ifacestrs = grep { $_->{'fullname'} }
278 ( &active_interfaces(), &boot_interfaces() );
280 @ifacestrs = grep { $_->{'virtual'} eq '' } @ifacestrs;
282 my @ifaces = map { $_->{'fullname'} } @ifacestrs;
283 @ifaces = sort { $a cmp $b } &unique(@ifaces);
287 push(@opts, [ '', $blanktext ]);
289 $found++ if ($value eq "");
290 foreach my $i (@ifaces) {
291 push(@opts, [ $i, $i ]);
292 $found++ if ($value eq $i);
294 push(@opts, [ 'other', $text{'chooser_other'} ]);
295 return &ui_select($name, !$found && $value ? 'other' : $value,
296 \@opts, 1, 0, 0, $disabled)." ".
297 &ui_textbox($name."_other", $found ? "" : $value, $disabled);
300 # compute_broadcast(ip, netmask)
301 # Returns a computed broadcast address (ip ^ ~netmask)
302 sub compute_broadcast
304 local $ipnum = &ip_to_integer($_[0]);
305 local $nmnum = &ip_to_integer($_[1]);
306 return &integer_to_ip($ipnum | (~$nmnum));
309 # compute_network(ip, netmask)
310 # Returns a computed network address (ip & netmask)
313 local $ipnum = &ip_to_integer($_[0]);
314 local $nmnum = &ip_to_integer($_[1]);
315 return &integer_to_ip($ipnum & $nmnum);
319 # Given an IP address, returns a 32-bit number
322 local @ip = split(/\./, $_[0]);
323 return ($ip[0]<<24) + ($ip[1]<<16) + ($ip[2]<<8) + ($ip[3]<<0);
326 # integer_to_ip(integer)
327 # Given a 32-bit number, converts it to an IP
330 return sprintf "%d.%d.%d.%d",
338 # Returns a list of all active and boot-time interfaces
342 foreach my $a (&active_interfaces()) {
346 foreach my $a (&boot_interfaces()) {
353 # check_netmask(netmask,ipaddress_associated)
354 # check if some netmask is properly formatted accordingly
355 # the associated address format (IPv4 or IPv6)
358 local($netmask,$address)= @_;
361 # Detect IP address type (V4, V6) and check syntax accordingly
362 if ( &check_ip6address($address) ) {
363 $ret=&check_ip6netmask($netmask);
367 $ret=&check_ipaddress($netmask);
372 # check_ip6netmask(netmask)
373 # check if some netmask has IPv6 format: its value is between 0 and 128.
376 return 0 if ( @_[0] <0 || @_[0] >128 );
380 sub check_ipaddress_any
382 return &check_ipaddress($_[0]) || &check_ip6address($_[0]);
385 # common_order_input(name, value, &opts)
386 # Returns a field for a standard DNS resolution order input
387 sub common_order_input
389 my ($name, $value, $opts) = @_;
390 if ($value =~ /\[/) {
391 # Using a complex resolve list
392 return &ui_textbox($name, $value, 60);
395 # Can select by menus
397 my @o = split(/\s+/, $value);
398 for(my $i = 0; $i<scalar(@o)+2; $i++) {
399 $rv .= &ui_select($name."_".$i, $o[$i],
400 [ [ "", " " ], @$opts ], 1, 0, 1)."<br>";
406 # supports_address6([&iface])
407 # Returns 1 if managing IPv6 interfaces is supported
408 sub supports_address6