+
+ eval {
+ local $^W;
+ require IO::Socket::INET6;
+ };
+
+ if ($@) {
+ dbg($@);
+ require IO::Socket;
+ $io_socket = 'IO::Socket::INET';
+ } else {
+ $io_socket = 'IO::Socket::INET6';
+ }
+ $io_socket->import;
+
+ if ($@ || $main::is_win) {
+ $blocking_supported = $io_socket->can('blocking') ? 2 : 0;
+ } else {
+ $blocking_supported = $io_socket->can('blocking') ? 2 : 1;
+ }
+
+
+ # import as many of these errno values as are available
+ eval {
+ local $^W;
+ require Errno; Errno->import(qw(EAGAIN EINPROGRESS EWOULDBLOCK));
+ };
+
+ unless ($^O eq 'MSWin32') {
+ if ($] >= 5.6) {
+ eval {
+ local $^W;
+ require Socket; Socket->import(qw(IPPROTO_TCP TCP_NODELAY));
+ };
+ } else {
+ dbg("IPPROTO_TCP and TCP_NODELAY manually defined");
+ eval 'sub IPPROTO_TCP { 6 };';
+ eval 'sub TCP_NODELAY { 1 };';
+ }
+ }
+ # http://support.microsoft.com/support/kb/articles/Q150/5/37.asp
+ # defines EINPROGRESS as 10035. We provide it here because some
+ # Win32 users report POSIX::EINPROGRESS is not vendor-supported.
+ if ($^O eq 'MSWin32') {
+ eval '*EINPROGRESS = sub { 10036 };' unless defined *EINPROGRESS;
+ eval '*EWOULDBLOCK = *EAGAIN = sub { 10035 };' unless defined *EWOULDBLOCK;
+ eval '*F_GETFL = sub { 0 };' unless defined *F_GETFL;
+ eval '*F_SETFL = sub { 0 };' unless defined *F_SETFL;
+ eval 'sub IPPROTO_TCP { 6 };';
+ eval 'sub TCP_NODELAY { 1 };';
+ $blocking_supported = 0; # it appears that this DOESN'T work :-(
+ }
+}
+
+my $w = $^W;
+$^W = 0;
+my $eagain = eval {EAGAIN()};
+my $einprogress = eval {EINPROGRESS()};
+my $ewouldblock = eval {EWOULDBLOCK()};
+$^W = $w;
+$cnum = 0;
+
+
+#
+#-----------------------------------------------------------------
+# Generalised initializer
+
+sub new
+{
+ my ($pkg, $rproc) = @_;
+ my $obj = ref($pkg);
+ my $class = $obj || $pkg;
+
+ my $conn = {
+ rproc => $rproc,
+ inqueue => [],
+ outqueue => [],
+ state => 0,
+ lineend => "\r\n",
+ csort => 'telnet',
+ timeval => 60,
+ blocking => 0,
+ cnum => (($cnum < 999) ? (++$cnum) : ($cnum = 1)),
+ };
+
+ $noconns++;
+
+ dbg("$class Connection $conn->{cnum} created (total $noconns)") if isdbg('connll');
+ return bless $conn, $class;
+}
+
+sub set_error
+{
+ my $conn = shift;
+ my $callback = shift;
+ $conn->{eproc} = $callback;
+ set_event_handler($conn->{sock}, error => $callback) if exists $conn->{sock};
+}
+
+sub set_rproc
+{
+ my $conn = shift;
+ my $callback = shift;
+ $conn->{rproc} = $callback;
+}
+
+sub blocking
+{
+ return unless $blocking_supported;
+
+ # Make the handle stop blocking, the Windows way.
+ if ($blocking_supported) {
+ $_[0]->blocking($_[1]);
+ } else {
+ my $flags = fcntl ($_[0], F_GETFL, 0);
+ if ($_[1]) {
+ $flags &= ~O_NONBLOCK;
+ } else {
+ $flags |= O_NONBLOCK;
+ }
+ fcntl ($_[0], F_SETFL, $flags);
+ }
+}
+
+# save it
+sub conns
+{
+ my $pkg = shift;
+ my $call = shift;
+ my $ref;
+
+ if (ref $pkg) {
+ $call = $pkg->{call} unless $call;
+ return undef unless $call;
+ dbg((ref $pkg) . " changing $pkg->{call} to $call") if isdbg('connll') && exists $pkg->{call} && $call ne $pkg->{call};
+ delete $conns{$pkg->{call}} if exists $pkg->{call} && exists $conns{$pkg->{call}} && $pkg->{call} ne $call;
+ $pkg->{call} = $call;
+ $ref = $conns{$call} = $pkg;
+ dbg((ref $pkg) . " Connection $pkg->{cnum} $call stored") if isdbg('connll');
+ } else {
+ $ref = $conns{$call};
+ }
+ return $ref;
+}
+
+# this is only called by any dependent processes going away unexpectedly
+sub pid_gone
+{
+ my ($pkg, $pid) = @_;
+
+ my @pid = grep {$_->{pid} == $pid} values %conns;
+ foreach my $p (@pid) {
+ &{$p->{eproc}}($p, "$pid has gorn") if exists $p->{eproc};
+ $p->disconnect;
+ }
+}
+
+sub ax25
+{
+ my $conn = shift;
+ return $conn->{csort} eq 'ax25';
+}
+
+sub peerhost
+{
+ my $conn = shift;
+ $conn->{peerhost} ||= 'ax25' if $conn->ax25;
+ $conn->{peerhost} ||= $conn->{sock}->peerhost if $conn->{sock} && $conn->{sock}->isa('IO::Socket::INET');
+ $conn->{peerhost} ||= 'UNKNOWN';
+ return $conn->{peerhost};