Thread Simpler Threaded Server: ...und ich find den fehler nicht...
(5 answers)
Opened by
Gast
at 2007-05-18 11:56
User since 2007-05-18
142
Artikel
BenutzerIn
Vielen dank für eure schnelle hilfe! :)
Also.. es läuft nun an, aber tut nich wirklich was ich will :/
Und zwar sollte er eigendlich die verbindung aufrecht erhalten und in die server funktion zurückfallen, stattdessen wird das prog aber komplett gekilled... Ausserdem weiss ich nicht wie ich jetzt weiter machen soll was die kommunikation zwischen server und client angeht, der client sendet zwar raus, aber wie kann ich im serverscript ne variable festlegen in die der input vom client kommt, gespeichert wird (strg+f: $authresponse)? Und die abfrage müsste ich doch dann per while schleife konstruieren oder?
so sieht es im moment aus:
Server.pl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#!/usr/bin/perl
#Simples Server konstrukt by FlorianL;)
use strict; require 5.002; use warnings; use IO::Socket; use Carp;
my $cfgfile = "serverconfig.cfg"; my $keyfile = ".key.file";
my ($port, $username, $choosed, $coderef, $crypted);
sub logmsg {print "$0 $$: @_ at ", scalar localtime, "\n" } sub spawn;
sub genpass { print ("\nGenerating KeyFile\nEnter your Password: "); chomp (my $password = <STDIN>); my @chars = ("A" .. "Z"); my $salt = join("", @chars[ map {rand @chars } (1 .. 2) ]); $crypted = crypt("$password", "$salt"); open(KEYFILE,">$keyfile") or die "Error: Cant write the Keyfile"; print KEYFILE $crypted; close (KEYFILE); print ("\nHash = $crypted\n"); system("chmod 666 $keyfile"); print ("Keyfile saved to $keyfile!\nPermissions set to 666\nUpload it to your Clients now!\n"); }
sub readconfig() { open(CONFIG,$cfgfile) or die "Error: Cant open $cfgfile"; my @config=<CONFIG>; close(CONFIG); chomp($port = $config[0]); if ($port eq "") { die ("Error: No Port specified"); } chomp($username = $config[1]); if ($username eq "") { die ("Error: No Username specified"); } }
sub writeconfig() { print ("Config\n------\n"); print ("\nPort: "); my $port = <STDIN>; if ($port =~ m/[a-z]+/) { die ("Port must be a number!"); } print ("\nUsername: "); my $username = <STDIN>; print ("Config written to $cfgfile\n"); my @config = ($port, $username,); open(CONFIG,">$cfgfile"); print CONFIG (@config); close (CONFIG); open(KEYFILE,">$keyfile"); if (-z "$keyfile") { print ("No KeyFile present, well we generate one now...\n"); genpass(); } else { print ("KeyFile allready present!\n"); } close(KEYFILE); }
sub server() { readconfig(); my $proto = getprotobyname('tcp'); socket(SERVER, PF_INET, SOCK_STREAM, $proto) || die "FAILED: socket: $!"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, pack ("l", 1)) || die "FAILED: setsockopt: $!"; bind(SERVER, sockaddr_in($port, INADDR_ANY)) || die "FAILED: bind: $!"; listen(SERVER, SOMAXCONN) || die "FAILED: listen: $!";
logmsg "Server started on Port $port";
my $waitedpid = 0; my $paddr;
sub KILLER { our $waitedpid = wait; $SIG{CHLD} = \&KILLER; logmsg "Killed $waitedpid" . ($? ? " with exit $?" : ''); } $SIG{CHLD} = \&KILLER; for ($waitedpid = 0; ($paddr = accept(CLIENT,SERVER)) || $waitedpid; $waitedpid = 0, close CLIENT) { next if $waitedpid and not $paddr; my ($port,$iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr,AF_INET);
logmsg "Connection from",inet_ntoa($iaddr);
spawn sub { print "CONNECT\n"; open(KEYFILE,$keyfile); my $crypt = <KEYFILE>; close(KEYFILE); my $authresponse = ''; if ($authresponse eq $crypt) { print "AUTHED\n"; } else { print "DENIED\n"; } } }
sub spawn { my $coderef = shift; unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') { confess "usage: spawn CODEREF"; } my $pid; if (!defined($pid = fork)) { logmsg "cannot fork: $!"; return; } elsif ($pid) { logmsg "forked $pid"; return; } open (STDIN, "<&CLIENT") || die "cant dup client to stdin"; open (STDOUT, ">&CLIENT") || die "cant dup client ti stdout"; exit &$coderef(); } }
sub help() { print ("Valid commandline Options are:\n"); print ("-config -Initiates Configuration\n"); print ("-printcfg -Print Config\n"); print ("-server -Starts the Server\n"); print ("-genpass -Generates a new keyfile \(1st-timers: Use -config instead!\)\n"); }
main { $choosed = $ARGV[0]; if ($choosed eq '-config') { writeconfig(); exit 0; } elsif ($choosed eq '-printcfg') { readconfig(); print ("Port: $port"); print ("Username: $username"); exit 0; } elsif ($choosed eq '-genpass') { genpass(); exit 0; } elsif ($choosed eq '-server') { server(); exit 0; } else { help(); exit 0; } }
client.pl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#!/usr/bin/perl
use strict; use warnings; use IO::Socket;
my ($server, $port, $keyfile, $choosed);
sub readconfig() { open(CONFIG,"config.cfg") or die "Error: Cant open config.cfg"; my @config=<CONFIG>; close(CONFIG); chomp($server = $config[0]); if ($server eq "") { die ("Error: No Server specified"); } chomp($port = $config[1]); if ($port eq "") { die ("Error: No Port specified"); } chomp($keyfile = $config[2]); if ($keyfile eq "") { die ("Error: No Keyfile specified"); } }
sub writeconfig() { print ("Config\n------\n"); print ("Server: "); my $server = <STDIN>; print ("\nPort: "); my $port = <STDIN>; if ($port =~ m/[a-z]+/) { die ("Port must be a number!"); } print ("\nKeyfile: "); my $keyfile = <STDIN>; print ("Config written to config.cfg\n"); my @config = ($server, $port, $keyfile); open(CONFIG,">config.cfg"); print CONFIG (@config); close (CONFIG); }
sub connection() { readconfig(); my $remote = IO::Socket::INET->new ( Proto => 'tcp', PeerAddr => $server, PeerPort => $port, Reuse => 1, ) or die "$!n"; print ("Connected to ", $remote->peerhost, " on port ",$remote->peerport, "\n\n"); $remote->autoflush(1); while ($remote) { my $line = <$remote>; open(KEYFILE,$keyfile); my $crypt = <KEYFILE>; close(KEYFILE); if ($remote eq 'AUTH') { print $remote "$crypt"; } elsif ($remote eq 'AUTHED') { print $remote "TEST"; } else { print "Communication failed, exiting...\n"; exit; } } close $remote; }
sub help() { print ("Valid commandline Options are:\n"); print ("-config -Initiates Configuration\n"); print ("-printcfg -Print Config\n"); print ("-connect -Connects to the Server\n"); }
$choosed = $ARGV[0]; if ($choosed eq '-config') { writeconfig(); exit 0; } elsif ($choosed eq '-printcfg') { readconfig(); print ("Server: $server"); print ("Port: $port"); print ("Keyfile: $keyfile"); exit 0; } elsif ($choosed eq '-connect') { connection(); exit 0; } else { help(); exit 0; }
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Server:
[root@doorgunner scripts]# perl server.pl -server server.pl 18904: Server started on Port 1337 at Fri May 18 11:32:44 2007 server.pl 18904: Connection from 127.0.0.1 at Fri May 18 11:32:47 2007 server.pl 18904: forked 18906 at Fri May 18 11:32:47 2007 server.pl 18904: Killed 18906 with exit 256 at Fri May 18 11:32:47 2007 [root@doorgunner scripts]#
Client:
[root@doorgunner scripts]# perl client.pl -connect Connected to 127.0.0.1 on port 1337
Communication failed, exiting... [root@doorgunner scripts]#
Netcat:
[root@doorgunner scripts]# nc localhost 1337 CONNECT DENIED [root@doorgunner scripts]#
View full thread Simpler Threaded Server: ...und ich find den fehler nicht...
|