2 # Networking functions for OSX
4 $virtual_netmask = "255.255.255.255"; # Netmask for virtual interfaces
6 $iftab_file = "/etc/iftab";
7 $hostconfig_file = "/etc/hostconfig";
10 # Returns a list of currently ifconfig'd interfaces
13 local(@rv, @lines, $l);
14 &open_execute_command(IFC, "ifconfig -a", 1, 1);
17 if (/^\S+:/) { push(@lines, $_); }
18 elsif (@lines) { $lines[$#lines] .= $_; }
24 $ifc{'name'} = $ifc{'fullname'} = $1;
25 if ($l =~ /^(\S+):(\d+):\s/) { $ifc{'virtual'} = $2; }
26 if ($l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)\s+broadcast\s+(\S+)//) {
28 $ifc{'netmask'} = &parse_hex($2);
29 $ifc{'broadcast'} = $3;
31 elsif ($l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)//) {
33 $ifc{'netmask'} = &parse_hex($2);
36 if ($l =~ /ether\s+(\S+)/) { $ifc{'ether'} = $1; }
37 if ($l =~ /mtu\s+(\S+)/) { $ifc{'mtu'} = $1; }
38 $ifc{'up'}++ if ($l =~ /\<UP/);
39 $ifc{'edit'} = &iface_type($ifc{'name'}) =~ /ethernet|loopback/i;
40 $ifc{'index'} = scalar(@rv);
42 $ifc{'ether'} = join(":", map { sprintf "%2.2d", $_ }
43 split(/:/, $ifc{'ether'}));
47 # Add aliases as virtual interfaces
49 while($l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)\s+broadcast\s+(\S+)//) {
51 $vifc{'address'} = $1;
52 $vifc{'netmask'} = &parse_hex($2);
53 $vifc{'broadcast'} = $3;
55 $vifc{'edit'} = $ifc{'edit'};
56 $vifc{'virtual'} = $v++;
57 $vifc{'fullname'} = $vifc{'name'}.':'.$vifc{'virtual'};
58 $vifc{'index'} = scalar(@rv);
65 # activate_interface(&details)
66 # Create or modify an interface
67 sub activate_interface
70 map { $act{$_->{'fullname'}} = $_ } &active_interfaces();
71 local $old = $act{$_[0]->{'fullname'}};
72 $act{$_[0]->{'fullname'}} = $_[0];
73 &interface_sync(\%act, $_[0]->{'name'}, $_[0]->{'fullname'});
76 # deactivate_interface(&details)
77 # Deactive an interface
78 sub deactivate_interface
81 local @act = &active_interfaces();
82 if ($_[0]->{'virtual'} eq '') {
83 @act = grep { $_->{'name'} ne $_[0]->{'name'} } @act;
86 @act = grep { $_->{'fullname'} ne $_[0]->{'fullname'} } @act;
88 map { $act{$_->{'fullname'}} = $_ } @act;
89 &interface_sync(\%act, $_[0]->{'name'}, $_[0]->{'fullname'});
92 # interface_sync(interfaces, name, changee)
95 # Remove all IP addresses except for the primary one (unless it is being edited)
96 local $pri = $_[0]->{$_[1]};
97 local $ifconfig = &has_command("ifconfig");
100 &execute_command("$ifconfig $_[1]", undef, \$out);
101 last if ($out !~ /([\000-\377]*)\s+inet\s+(\d+\.\d+\.\d+\.\d+)/);
102 last if ($2 eq $pri->{'address'} && $_[2] ne $pri->{'fullname'});
103 &system_logged("$ifconfig $_[1] delete $2 >/dev/null 2>&1");
106 # Add them back again, except for the primary unless it is being changed
107 foreach $a (sort { $a->{'fullname'} cmp $b->{'fullname'} }
108 grep { $_->{'name'} eq $_[1] } values(%{$_[0]})) {
109 next if ($a->{'fullname'} eq $pri->{'fullname'} &&
110 $_[2] ne $pri->{'fullname'});
111 local $cmd = "$ifconfig $a->{'name'}";
112 if ($a->{'virtual'} ne '') {
113 $cmd .= " alias $a->{'address'}";
116 $cmd .= " $a->{'address'}";
118 if ($a->{'netmask'}) { $cmd .= " netmask $a->{'netmask'}"; }
119 if ($a->{'broadcast'}) { $cmd .= " broadcast $a->{'broadcast'}"; }
120 if ($a->{'mtu'}) { $cmd .= " mtu $a->{'mtu'}"; }
121 local $out = &backquote_logged("$cmd 2>&1");
122 #if ($? && $out !~ /file exists/i) {
126 if ($a->{'virtual'} eq '') {
127 if ($a->{'up'}) { $out = &backquote_logged("$ifconfig $a->{'name'} up 2>&1"); }
128 else { $out = &backquote_logged("$ifconfig $a->{'name'} down 2>&1"); }
129 &error($out) if ($?);
135 # Returns a list of interfaces brought up at boot time
139 local %virtual_count;
141 &open_readfile(IFTAB, $iftab_file);
145 if (/^(\S+)\s+(inet)\s+(.*)/i) {
146 local $ifc = { 'name' => $1,
147 'index' => scalar(@rv),
150 next if ($opts =~ /^!/);
151 $ifc->{'edit'} = 1 if ($ifc->{'name'} =~ /^[a-z]+[0-9]*$/i);
152 if ($opts eq "-AUTOMATIC-" || $opts eq "-BOOTP-") {
156 elsif ($opts eq "-DHCP-") {
161 # Parse the ifconfig params
162 if ($opts =~ /^([0-9\.]+)/) {
163 $ifc->{'address'} = $1;
165 local @a = split(/\./, $ifc->{'address'});
166 if ($opts =~ /netmask\s+([0-9\.]+)/) {
167 $ifc->{'netmask'} = $1;
170 $ifc{'netmask'} = $a[0] >= 192 ? "255.255.255.0" :
171 $a[0] >= 128 ? "255.255.0.0" :
174 if ($opts =~ /broadcast\s+([0-9\.]+)/) {
175 $ifc->{'broadcast'} = $1;
178 local @n = split(/\./, $ifc->{'netmask'});
179 $ifc->{'broadcast'} = sprintf "%d.%d.%d.%d",
180 ($a[0] | ~int($n[0]))&0xff,
181 ($a[1] | ~int($n[1]))&0xff,
182 ($a[2] | ~int($n[2]))&0xff,
183 ($a[3] | ~int($n[3]))&0xff;
185 if ($opts =~ /mtu\s+([0-9\.]+)/) {
188 if ($opts =~ /\s+up/) {
191 if ($opts =~ /\s+alias/) {
194 int($virtual_count{$ifc->{'name'}}++);
197 $ifc->{'fullname'} = $ifc->{'name'};
198 $ifc->{'fullname'} .= ":$ifc->{'virtual'}"
199 if ($ifc->{'virtual'} ne "");
200 $ifc->{'file'} = $iftab_file;
209 # save_interface(&details)
210 # Create or update a boot-time interface
213 local $str = "$_[0]->{'name'} inet";
214 if ($_[0]->{'dhcp'}) {
217 elsif ($_[0]->{'bootp'}) {
218 $str .= " -AUTOMATIC-";
221 $str .= " $_[0]->{'address'}";
222 $str .= " netmask $_[0]->{'netmask'}" if ($_[0]->{'netmask'});
223 $str .= " broadcast $_[0]->{'broadcast'}" if ($_[0]->{'broadcast'});
224 $str .= " mtu $_[0]->{'mtu'}" if ($_[0]->{'mtu'});
225 if ($_[0]->{'virtual'} eq '') {
232 &lock_file($iftab_file);
233 local $lref = &read_file_lines($iftab_file);
234 local @boot = &boot_interfaces();
235 local ($old) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
237 # Replacing existing interface
238 $lref->[$old->{'line'}] = $str;
241 # Adding new interface
243 if ($_[0]->{'virtual'} ne '') {
244 # Work out new virtual num
245 $_[0]->{'virtual'} = 0;
247 if ($b->{'name'} eq $_[0]->{'name'} &&
248 $b->{'virtual'} ne '' &&
249 $b->{'virtual'} >= $_[0]->{'virtual'}) {
250 $_[0]->{'virtual'} = $b->{'virtual'}+1;
253 $_[0]->{'fullname'} = $_[0]->{'name'}.':'.$_[0]->{'virtual'};
257 &unlock_file($iftab_file);
260 # delete_interface(&details)
261 # Delete a boot-time interface
264 &lock_file($iftab_file);
265 local $lref = &read_file_lines($iftab_file);
266 splice(@$lref, $_[0]->{'line'}, 1);
268 &unlock_file($iftab_file);
272 # Returns a human-readable interface type name
275 return $_[0] =~ /^ppp/ ? "PPP" :
276 $_[0] =~ /^pppoe/ ? "PPPoE" :
277 $_[0] =~ /^lo/ ? "Loopback" :
278 $_[0] eq "*" ? $text{'ifcs_all'} :
279 $_[0] =~ /^en/ ? "Ethernet" : $text{'ifcs_unknown'};
282 # iface_hardware(name)
283 # Does some interface have an editable hardware address
290 # Can some boot-time interface parameter be edited?
293 return $_[0] =~ /netmask|broadcast|dhcp|bootp/;
296 # valid_boot_address(address)
297 # Is some address valid for a bootup interface
298 sub valid_boot_address
300 return &check_ipaddress($_[0]);
304 # Returns a hashtable containing keys nameserver, domain, search & order
308 &open_readfile(RESOLV, "/etc/resolv.conf");
312 if (/nameserver\s+(.*)/) {
313 push(@{$dns->{'nameserver'}}, split(/\s+/, $1));
315 elsif (/domain\s+(\S+)/) {
316 $dns->{'domain'} = [ $1 ];
318 elsif (/search\s+(.*)/) {
319 $dns->{'domain'} = [ split(/\s+/, $1) ];
323 $dns->{'files'} = [ "/etc/resolv.conf" ];
327 # save_dns_config(&config)
328 # Writes out the resolv.conf file
331 &lock_file("/etc/resolv.conf");
332 &open_readfile(RESOLV, "/etc/resolv.conf");
333 local @resolv = <RESOLV>;
335 &open_tempfile(RESOLV, ">/etc/resolv.conf");
336 foreach (@{$_[0]->{'nameserver'}}) {
337 &print_tempfile(RESOLV, "nameserver $_\n");
339 if ($_[0]->{'domain'}) {
340 if ($_[0]->{'domain'}->[1]) {
341 &print_tempfile(RESOLV, "search ",join(" ", @{$_[0]->{'domain'}}),"\n");
344 &print_tempfile(RESOLV, "domain $_[0]->{'domain'}->[0]\n");
348 &print_tempfile(RESOLV, $_) if (!/^\s*(nameserver|domain|search)\s+/);
350 &close_tempfile(RESOLV);
351 &unlock_file("/etc/resolv.conf");
354 $max_dns_servers = 3;
357 # Returns HTML for selecting the name resolution order
364 # Parses the form created by order_input()
373 local $hc = &read_hostconfig();
374 if ($hc->{'HOSTNAME'}) {
375 return $hc->{'HOSTNAME'};
377 return &get_system_hostname();
380 # save_hostname(name)
383 &system_logged("hostname $_[0] >/dev/null 2>&1");
384 &lock_file($hostconfig);
385 &set_hostconfig("HOSTNAME", $_[0]);
386 &unlock_file($hostconfig);
387 undef(@main::get_system_hostname); # clear cache
390 sub routing_config_files
392 return ( $hostconfig_file );
397 local $hc = &read_hostconfig();
398 local $r = $hc->{'ROUTER'};
399 local $mode = $r eq "-AUTOMATIC-" ? 1 : $r ? 2 : 0;
402 print &ui_table_row($text{'routes_default'},
403 &ui_radio("router_mode", $mode,
404 [ [ 0, $text{'routes_none2'} ],
405 [ 1, $text{'routes_auto'} ],
406 [ 2, &ui_textbox("router", $mode == 2 ? $r : "", 20) ] ]));
409 local $f = $hc->{'IPFORWARDING'};
410 print &ui_table_row($text{'routes_forward'},
411 &ui_yesno_radio("forward", $f eq '-YES-'));
417 if ($in{'router_mode'} == 0) {
420 elsif ($in{'router_mode'} == 1) {
425 &check_ipaddress($r) || &error(&text('routes_edefault', $r));
427 &lock_file($hostconfig_file);
428 &set_hostconfig("ROUTER", $r);
429 &set_hostconfig("IPFORWARDING", $in{'forward'} ? "-YES-" : "-NO-");
430 &unlock_file($hostconfig_file);
433 # set_hostconfig(name, value)
434 # Add or update an entry in the hostconfig file
437 local $lref = &read_file_lines($hostconfig_file);
439 for($i=0; $i<@$lref; $i++) {
440 if ($lref->[$i] =~ /^(\S+)\s*=/ && lc($1) eq lc($_[0])) {
441 $lref->[$i] = "$_[0]=$_[1]";
446 push(@$lref, "$_[0]=$_[1]");
452 # Returns a hash of hostconfig file values
456 &open_readfile(HOST, $hostconfig_file);
460 if (/^(\S+)\s*=\s*(.*)/) {
468 sub os_feedback_files
470 return ( "/etc/hostconfig", "/etc/resolv.conf", "/etc/iftab" );
474 # Apply the interface and routing settings
477 #system("killall ipconfigd && ipconfigd </dev/null >/dev/null 2>&1 &");
478 #system("ipconfig waitall >/dev/null 2>&1");
479 #local $hc = &read_hostconfig();
480 #system("killall -HUP netinfod >/dev/null 2>&1");
481 #system("killall -HUP lookupd >/dev/null 2>&1");
484 # supports_address6([&iface])
485 # Returns 1 if managing IPv6 interfaces is supported
486 sub supports_address6