Search code examples
perltk-toolkitinet

Creating a software router


I want to build a software router.

I have written the code to open a socket, receive the data, and print its received data. The GUI is going to be written using Tk

Below is a simple version of the code, it does basically what i want to do, with the exception of not forking the new_port subroutine. Every time i click the submit button the Tk window is stuck. If someone can please assist with adding a fork to the new_port subroutine so it spawns a new child process. The idea is i can fill in a new port in the form and hit sumbit. The window closes, I then press new again put a new port in and now a 2nd socket is open at the same time as the first. I.E port 1234 and 5678 are being listened to at the same time.

#!/usr/bin/perl -w
 use IO::Socket::INET;
 use Tk;

$myip = `ifconfig | grep -i inet | head -1 | cut -d ":" -f2 | cut -d " " -f1`;

sub new_port {
    my $socket = new IO::Socket::INET ( 
        LocalHost => "$myip",
        LocalPort => "$myport",
        Proto => 'tcp'
        Reuse => 1);
die "Cannot create socket on local host" unless $socket;
print "Server waiting for client connection on port $myport\n";

while(1)
{
 my $client_socket = $socket->accept();
 my $client_address = $client_socket->peerhost();
 my $client_port = $client_socket->peerport();
 my $input_data = "";
 my $received_data = "";
 do
 {
   $client_socket->recv($received_data, 65536);
   $input_data = $input_data . $received_data;
 } while ($received_data ne "");
 print "INPUT----------------------------------\n";
 print "Data from $client_address on port $client_port\n";
 print $input_data;
 shutdown($client_socket, 1);
  }
 }

 sub new_port_window {
   my $sw = MainWindow->new;
   $sw->geometry("200x100");
   $sw->title("port opener");
   $sw->Label(-text "Insert port #")->place(-anchor => 'center', -relx => 0.5, -rely => 0.2);
   $sw->Entry(-bg => 'white', -fg => 'black', -textvariable => \$myport)->place(-anchor => 'center', -relx => 0.5, -rely => 0.4);
   $sw->Button(-text "submit", -command => sub{new_port})->place(width => 100,
   -anchor => "center",
   -relx => 0.5,
   -rely => 0.8);
}

 my $mw = MainWindow->new;
       $mw->geometry("150x100");
       $mw->title("GUI TEST NEW FUNCTION");
       $mw->Label(-text => "click new")->place(-anchor => "center", -relx => 0.5, -rely => 0.3);
       $mw->Button(-text => "NEW", -command =>sub{new_port_window})->place(-width => 50, -anchor => "center", -relx => 0.5, -rely => 0.8);
MainLoop;

Solution

  • So i was able to get this working with lots of experimentation. The trick was to fork the sub routine and store the pid in a file to be used at a later time for killing of the process, which never exits due to being in a infinite loop.

    Sub forker {
         $pid = fork ();
          if ( $pid == 0 ) {
              new_port();
          }
          my $pid_file = 'router.pid';
          open (my $pidlog, '>>', $pid_file) or die "cannot open file";
           print $pidlog "$pid\n";
           close $pidlog;
      }