Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Automatic Home Connection (LAN, WLAN, VPN)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
ocbMaurice
Tux's lil' helper
Tux's lil' helper


Joined: 14 Feb 2003
Posts: 81
Location: Switzerland

PostPosted: Sat Dec 04, 2010 1:31 am    Post subject: Automatic Home Connection (LAN, WLAN, VPN) Reply with quote

Hi folks,

Sorry for the topic title but couldn't think of any better.

I did a fresh setup on my netbook and want to implement my network in a special way. I have a lan at home with a openvpn server. What I would like to accomplish is a init method which would either connect through lan or wlan to my local network, or connect to lan or wlan to the internet and use openvpn to connect to my local lan. I did write an init script for my old installation but I was not really satisfied with it. I still have this script somewhere in my backups, if anyone would like to have it, but I'm curious if this can be done in pure gentoo.

Basically it should do this:
  • Is a network cable attached to the eth port?
  • -> no: try wlan
  • -> yes: try to get ip via dhcp
  • eth dhcp: if successfull: is ip from lan or internet?
  • -> lan : success, exit
  • -> internet: try wlan
  • try wlan with dhcp
  • wlan dhcp: if successfull: is ip from lan or internet?
  • -> lan: success, exit
  • -> internet: is lan also internet ? use lan : wlan
  • internet available, use openvpn to connect to lan

Is this possible with gentoo board tools or do I need to write my own init script?
Do you think this would be a cool feature to be implemented? I do!

Best wishes
Maurice
Back to top
View user's profile Send private message
ewaller
Apprentice
Apprentice


Joined: 11 Aug 2005
Posts: 263
Location: Pasadena, CA

PostPosted: Sat Dec 04, 2010 1:34 am    Post subject: Reply with quote

wicd should do all of that. In addition, you can configure it such that if you are on the wlan and connect a wired connection, it can establish that connection and automatically drop the wireless. Later, when you unplug the cable, it will reestablish the wlan connection.
_________________
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Back to top
View user's profile Send private message
ocbMaurice
Tux's lil' helper
Tux's lil' helper


Joined: 14 Feb 2003
Posts: 81
Location: Switzerland

PostPosted: Sat Dec 04, 2010 1:42 am    Post subject: Reply with quote

Thanks for the reply. Does wicd also cover the connection over vpn if there is only internet available. What I want is in anycase having a local lan ip on the best possible interface (lan/wlan/openvpn). I did a search on the forum for wicd and vpn which did not return anything.

Maurice
Back to top
View user's profile Send private message
ewaller
Apprentice
Apprentice


Joined: 11 Aug 2005
Posts: 263
Location: Pasadena, CA

PostPosted: Sat Dec 04, 2010 1:54 am    Post subject: Reply with quote

Sorry, I missed the VPN question. I don't think wicd will take care of that. I think NetworkManager will. I bounce back and forth between the two, but I don't have a machine with NetworkManager on it at this time.
_________________
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Back to top
View user's profile Send private message
ocbMaurice
Tux's lil' helper
Tux's lil' helper


Joined: 14 Feb 2003
Posts: 81
Location: Switzerland

PostPosted: Sat Dec 04, 2010 1:56 am    Post subject: Reply with quote

I digged out the scripts is used so far. This was just a first attempt to make it work. And since I like to hack perl I did the logic in perl. This is unfiltered from my config, so don't expect you can copy/paste and use it!

/etc/init.d/network:
#!/sbin/runscript
# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

depend() {
   need localmount
   after bootmisc hostname net.lo dnsmasq
   use isapnp isdn pcmcia usb wlan nfsmount netmount
}

start() {
   ebegin "Starting Network"
   perl /root/network.pl start
   eend $? "Check your logs to see why startup failed"
}

stop() {
   ebegin "Stopping Network"
   perl /root/network.pl stop
   eend $? "Check your logs to see why startup failed"
}


/root/network.pl:
#!/usr/bin/perl
use strict; use warnings;

my $devices = ["eth0", "wlan0"];
# my $network = "192.168.0.0/16";

# close(STDERR);

sub get_ip
{
   no warnings;
   if((`/sbin/ifconfig $_[0] 2>/dev/null`)[1]=~m/inet addr:([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/)
   { return $1; } else { return undef; }
}

sub plugged
{
   my $rv = undef;
   system "ifconfig " . $_[0] . " up"; sleep 1;
   if((`/sbin/ifconfig $_[0]`)[1]=~m/UP.+(RUNNING)/)
   { $rv = 1; } else { $rv = 0; }
   system "ifconfig " . $_[0] . " down";
   return $rv || 2;
}

sub start
{
   my $ip = undef;
   my $started = undef;
   my $success = undef;
   my $connection = undef;

   foreach my $device (@{$devices})
   {
      
      if ($device=~m/^eth[0-9]+$/)
      {
         unless(plugged($device))
         {
            print "$device has no cable plugged in\n";
            next; # continue;
         }
      }
      
      system "/etc/init.d/net." . $device . " start";
      my $timeout = $device=~m/^(?:ath|wlan)[0-9]+$/ ? 20 : 10;
   
      $ip = undef;
      $started = time;
      print "started $device .";
      while ($started + $timeout > time)
      {
         $ip = get_ip($device);
         last if $ip;
         print ".";
         sleep 1;
      }
      if (defined $ip) { print ". $ip\n"; }
      else { print ". timeout\n"; }

      if ($ip && $ip=~m/192\.168\.0\.[0-9]{1,3}/)
      {
         if ($success)
         {
            print "local network cannot be connected twice\n";
            system "/etc/init.d/net." . $device . " stop";
         }
         else
         {
            print "set local network connected through $device\n";
            $connection = [] unless ($connection);
            push(@{$connection}, [$device, $ip]);
            $success = [$device, $ip];

         }
      }
      elsif ($ip)
      {
         print "set connection established\n";
         $connection = [] unless ($connection);
         push(@{$connection}, [$device, $ip]);

      }
      else
      {
         print "device $device did not came up\n";
         # system "/etc/init.d/net." . $device . " stop";
      }
   }

   if ((not defined $success) && (defined $connection))
   {
      print "local network not connected - trying vpn via ".join(", ", map { $_->[0] } @{$connection})." - $connection\n";
      system "/etc/init.d/openvpn start";
   
      $ip = undef;
      $started = time;
      print "started openvpn .";
      while ($started + 20 > time)
      {
         $ip = get_ip("tap0");
         last if $ip;
         print ".";
         sleep 1;
      }
      if (defined $ip) { print ". $ip\n"; }
      else { print ". timeout\n"; }
   
      $connection = ["tap0", $ip] if $ip;
      if ($ip) { print "openvpn started ($ip)\n"; }
      else { print "openvpn could not be started\n"; }
   }

   if ($connection)
   {
      print "mounting nfs drives\n";
      system "/etc/init.d/portmap start";
      system "/bin/mount -t nfs -a";
   }
}

sub stop
{
   foreach my $device (@{$devices})
   {
      system "/etc/init.d/net." . $device . " stop";
   }
}

unless ($ARGV[0]) { print "either stop or start\n"; }
elsif ($ARGV[0] eq "stop") { stop(); }
elsif ($ARGV[0] eq "start") { start(); }


Maurice
Back to top
View user's profile Send private message
ocbMaurice
Tux's lil' helper
Tux's lil' helper


Joined: 14 Feb 2003
Posts: 81
Location: Switzerland

PostPosted: Sat Dec 04, 2010 2:15 am    Post subject: Reply with quote

ewaller wrote:
Sorry, I missed the VPN question. I don't think wicd will take care of that. I think NetworkManager will. I bounce back and forth between the two, but I don't have a machine with NetworkManager on it at this time.

No problem. I may also look into NetworkManager.

I'm just puzzled why I cannot find any solution to this particular problem, as it seems to be such a cool feature. Ok, it only applies to people who actually have a openvpn server. And I see that it needs quite a bit of logic (mostly to determine when to start openvpn, so you have to configure what you consider your local lan). But it should be possible to implement this "use case". It's just that I like the idea of being able to mount my local samba shares, nfs mounts, vnc sessions, etc. whenever I have a internet connection. I can do it manually, so why shouldn't it be possible automatically?

OK, enough for tonight 8)

Maurice
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum