#-*- perl -*-
#
#  Copyright (C) 2001 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself. 
#
# $Id$
# $FML$
#

package MailingList::Net::INET4;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK);
use Carp;
use MailingList::Utils;

require Exporter;

@ISA       = qw(Exporter);
@EXPORT    = qw(_connect4);

sub _connect4
{
    my ($self, $args) = @_;
    my $mta = $args->{ _mta };
    my $socket = '';

    # avoid croak() in IO::Socket module;
    eval {
	local($SIG{ALRM}) = sub { Log("Error: timeout to connect $mta");};
	use IO::Socket;
	$socket = new IO::Socket::INET($mta);
    };
    if ($@) {
	Log("Error: cannot make socket for $mta");
	$self->_error_reason("Error: cannot make socket: $@");
	return undef;
    }

    if (defined $socket) {
	Log("(debug) o.k. connected to $mta");
	$self->{'_socket'} = $socket;
	$socket->autoflush(1);
	return $socket;
    }
    else {
	Log("(debug) error. fail to connect $mta");
	$self->_error_reason("Error: cannot open socket: $!");
	return undef;
    }
}


=head1 NAME

MailingList::Net::INET4 - establish tcp connection over IPv4

=head1 SYNOPSIS

   use MailingList::Net::INET4;

   $mta = '127.0.0.1:25';
   $self->_connect4( { _mta => $mta });

=head1 DESCRIPTION

This module tries to create a socket and establish tcp connection over
IPv4. This is a typical socket program.

=head1 METHODS

=item C<_connect4()>

try L<connect(2)>. 
If it succeeds, returned 
$self->{ _socket } has true value.
If not, 
$self->{ _socket } is undef. 

Avaialble arguments follows:

    _connect4( { _mta => $mta });

$mta is a hostname or [raw_ipv4_addr]:port form, for example, 
127.0.0.1:25.

=head1 SEE ALSO

L<MailingList::SMTP>,
L<Socket>,
L<IO::Socket>,
L<MailingList::Utils>

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2001 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself. 

=head1 HISTORY

MailingList::Net::INET4.pm appeared in fml5.

=cut

1;
