From dd6c37c13dbafadd0d00c4c55e8e56e24829fa61 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sun, 25 Feb 2018 21:50:18 +0100 Subject: Added options for name and port --- peerdemo | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/peerdemo b/peerdemo index a80a59b9..3f0d5b19 100755 --- a/peerdemo +++ b/peerdemo @@ -13,30 +13,37 @@ use Getopt::Std; use IO::Socket; use IO::Select; +$DefaultSvdrpPort = 6419; +$DefaultSvdrpName = "peerdemo"; + $Usage = qq{ Usage: $0 options -Options: -v be verbose +Options: -n name use the given VDR name (default: $DefaultSvdrpName) + -p port use the given TCP port (default: $DefaultSvdrpPort) + -v be verbose }; -die $Usage if (!getopts("cv")); +die $Usage if (!getopts("n:p:v")); +$Name = $opt_n || $DefaultSvdrpName; +$Port = $opt_p || $DefaultSvdrpPort; $Verbose = $opt_v || 0; -$SvdrpPort = 6419; -$MyName = "peerdemo"; - # Open TCP and UDP sockets: -$TcpSocket = new IO::Socket::INET(Listen => 5, LocalPort => $SvdrpPort, Proto => "tcp", ReusePort => 1) || die "$!"; -$UdpSocket = new IO::Socket::INET( LocalPort => $SvdrpPort, Proto => "udp", ReusePort => 1) || die "$!"; +$TcpPort = $Port; +$UdpPort = $DefaultSvdrpPort; + +$TcpSocket = new IO::Socket::INET(Listen => 5, LocalPort => $TcpPort, Proto => "tcp", ReusePort => 1) || die "$!"; +$UdpSocket = new IO::Socket::INET( LocalPort => $UdpPort, Proto => "udp", ReusePort => 1) || die "$!"; $SvdrpSelect = new IO::Select($TcpSocket); setsockopt($UdpSocket, SOL_SOCKET, SO_RCVTIMEO, pack('L!L!', 0, 1000)); # 1ms timeout on UDP socket # Send UDP broadcast: -$BcastSocket = new IO::Socket::INET(PeerAddr => '255.255.255.255', PeerPort => $SvdrpPort, Proto => "udp", Broadcast => 1) || die "$!"; -$BcastMsg = "SVDRP:discover name:$MyName port:6419 vdrversion:20309 apiversion:20309 timeout:300"; +$BcastSocket = new IO::Socket::INET(PeerAddr => '255.255.255.255', PeerPort => $UdpPort, Proto => "udp", Broadcast => 1) || die "$!"; +$BcastMsg = "SVDRP:discover name:$Name port:$TcpPort vdrversion:20309 apiversion:20309 timeout:300"; Log('>', $BcastSocket, $BcastMsg); print($BcastSocket $BcastMsg); $BcastSocket->close(); @@ -45,7 +52,7 @@ $BcastSocket->close(); while (1) { if ($UdpSocket->recv($Request, 1024)) { - if (Extract($Request, "name") ne $MyName) { + if (Extract($Request, "name") ne $Name) { Log('<', $UdpSocket, $Request); ReportVDR($Request, $UdpSocket->peerhost()); } @@ -57,7 +64,7 @@ while (1) { my $new = $TcpSocket->accept(); Log('<', $new, "incoming TCP connection"); # send mandatory response to simulate an SVDRP host: - my $Prompt = "220 $MyName SVDRP VideoDiskRecorder 2.3.9; Wed Nov 29 17:00:29 2017; ISO-8859-1"; + my $Prompt = "220 $Name SVDRP VideoDiskRecorder 2.3.9; Wed Nov 29 17:00:29 2017; ISO-8859-1"; Log('>', $new, $Prompt); print($new "$Prompt\n"); # add incoming connection to select: @@ -80,7 +87,7 @@ while (1) { Reply($fh, "250 OK"); } elsif ($Request =~ /^PING/) { - Reply($fh, "250 $MyName is alive"); + Reply($fh, "250 $Name is alive"); } elsif ($Request =~ /^QUIT/) { # close connection: -- cgit v1.2.3