#!/usr/bin/perl -w # # $Id: program,v 1.11 2002/03/09 02:19:09 jj Exp $ # # Author: Shannon -jj Behrens # Date: Tue Sep 11 13:52:16 PDT 2001 # use strict; # Read "man getaddrinfo", "perldoc Socket6", and "perldoc perlipc". use Socket; use Socket6; # This is the port we should listen to for incoming bell # requests. $::BELL_PORT = 1029; # # A lot of other code has been snipped. # # This is a stub function that will do something with a # host and two ports. sub ring_bell { my ($pip, $vic_port, $rat_port) = @_; print "pip: $pip vic_port: $vic_port " . "rat_port: $rat_port\n"; } # Open up a socket on $::BELL_PORT. If a connection is # received, accept the Vic and Rat port values, call # ring_bell, and disconnect. This function must # be called from a forked process before Tk is used. This # function does not return. sub bell_daemon { my @addrinfo = getaddrinfo("", $::BELL_PORT, PF_INET6, SOCK_STREAM, 0, AI_PASSIVE); if (scalar(@addrinfo) < 5) { die "Error while trying to bind to port. " . "Reason: $1"; } my ($family, $socktype, $proto, $saddr) = @addrinfo; socket SERVER, $family, $socktype, $proto || die "Couldn't create socket. Reason: $1"; setsockopt SERVER, SOL_SOCKET, SO_REUSEADDR, pack("l", 1) || die "Couldn't set socket options. Reason: $1"; bind SERVER, $saddr || die "Couldn't bind to socket. Reason: $1"; listen SERVER, SOMAXCONN || die "Couldn't call listen on socket. Reason: $1"; for ( ; my $paddr = accept CLIENT, SERVER; close CLIENT) { my ($pip, $pport) = getnameinfo($paddr, NI_NUMERICHOST | NI_NUMERICSERV); my $line; if (!defined($line = )) { warn "connecting socket didn't send " . "port values"; next; } my @port_values = split / /, $line; if (scalar(@port_values) != 2) { warn "connecting socket didn't send exactly " . "two port values"; next; } my ($vic_port, $rat_port) = @port_values; ring_bell $pip, $vic_port, $rat_port; } close SERVER; } bell_daemon;