Why not adopt me?
NAME
Net::Inet - Internet socket interface module
SYNOPSIS
use Net::Gen; # optional
use Net::Inet;
DESCRIPTION
The Net::Inet module provides basic services for handling socket-based communications for the Internet protocol family. It inherits from Net::Gen, and is a base for Net::TCP and Net::UDP.
Public Methods
- new
-
Usage:
$obj = new Net::Inet; $obj = new Net::Inet $desthost, $destservice; $obj = new Net::Inet \%parameters; $obj = new Net::Inet $desthost, $destservice, \%parameters; $obj = 'Net::Inet'->new(); $obj = 'Net::Inet'->new($desthost, $destservice); $obj = 'Net::Inet'->new(\%parameters); $obj = 'Net::Inet'->new($desthost, $destservice, \%parameters);Returns a newly-initialised object of the given class. If called for a derived class, no validation of the supplied parameters will be performed. (This is so that the derived class can set up the parameter validation it needs in the object before allowing the validation.) Otherwise, it will cause the parameters to be validated by calling its
initmethod. In particular, this means that if both a host and a service are given, then an object will only be returned if a connect() call was successful, or if the object is non-blocking and a connect() call is in progress.The examples above show the indirect object syntax which many prefer, as well as the guaranteed-to-be-safe static method call. There are occasional problems with the indirect object syntax, which tend to be rather obscure when encountered. See http://www.xray.mpe.mpg.de/mailing-lists/perl-porters/1998-01/msg01674.html for details.
- init
-
Usage:
return undef unless $self->init; return undef unless $self->init(\%parameters); return undef unless $self->init($desthost, $destservice); return undef unless $self->init($desthost, $destservice, \%parameters);Verifies that all previous parameter assignments are valid (via
checkparams). Returns the incoming object on success, andundefon failure. Usually called only via a derived class'sinitmethod or its ownnewcall. - bind
-
Usage:
$ok = $obj->bind; $ok = $obj->bind($lclhost, $lclservice); $ok = $obj->bind($lclhost, $lclservice, \%parameters);Sets up the
srcaddrlistobject parameter with the specified $lclhost and $lclservice arguments if supplied (via thethishostandthisportobject parameters), and then returns the value from the inheritedbindmethod. Changing of parameters is also allowed, mainly for setting debug status or timeouts.Example:
$ok = $obj->bind(0, 'echo(7)'); # attach to the local TCP echo port - unbind
-
Usage:
$obj->unbind;Deletes the
thishostandthisportobject parameters, and then (assuming that succeeds, which it should) returns the value from the inheritedunbindmethod. - connect
-
Usage:
$ok = $obj->connect; $ok = $obj->connect($host, $service); $ok = $obj->connect($host, $service, \%parameters);Attempts to establish a connection for the object. If the $host or $service arguments are specified, they will be used to set the
desthostanddestservice/destportobject parameters, with side-effects of setting up thedstaddrlistobject parameter. Then, the result of a call to the inheritedconnectmethod will be returned. Changing of parameters is also allowed, mainly for setting debug status or timeouts. - format_addr
-
Usage:
$string = $obj->format_addr($sockaddr); $string = $obj->format_addr($sockaddr, $numeric_only); $string = format_addr Module $sockaddr; $string = format_addr Module $sockaddr, $numeric_only;Returns a formatted representation of the address. This is a method so that it can be overridden by derived classes. It is used to implement ``pretty-printing'' methods for source and destination addresses. If the $numeric_only argument is true, the address and port number will be used even if they can be resolved to names. Otherwise, the resolved hostname and service name will be used if possible.
- format_local_addr
-
Usage:
$string = $obj->format_local_addr; $string = $obj->format_local_addr($numeric_only);Returns a formatted representation of the local socket address associated with the object. A sugar-coated way of calling the
format_addrmethod for thesrcaddrobject parameter. - format_remote_addr
-
Usage:
$string = $obj->format_remote_addr;Returns a formatted representation of the remote socket address associated with the object. A sugar-coated way of calling the
format_addrmethod for thedstaddrobject parameter. - getsockinfo
-
An augmented form of
Net::Gen::getsockinfo. Aside from updating more object parameters, it behaves the same as that in the base class. The additional object parameters which get set arelcladdr,lclhost,lclport,lclservice,remaddr,remhost,remport, andremservice. (They are described in "Known Object Parameters" below.)
There are also various accessor methods for the object parameters. See "Public Methods" in Net::Gen (where it talks about Accessors) for calling details. See "Known Object Parameters" below for those defined by this class.
Protected Methods
[See the note in "Protected Methods" in Net::Gen about my definition of protected methods in Perl.]
None.
Known Socket Options
These are the socket options known to the Net::Inet module itself:
-
IP_HDRINCLIP_RECVDSTADDRIP_RECVOPTSIP_RECVRETOPTSIP_TOSIP_TTLIP_ADD_MEMBERSHIPIP_DROP_MEMBERSHIPIP_MULTICAST_IFIP_MULTICAST_LOOPIP_MULTICAST_TTLIP_OPTIONSIP_RETOPTS
Known Object Parameters
These are the object parameters registered by the Net::Inet module itself:
- IPproto
-
The name of the Internet protocol in use on the socket associated with the object. Set as a side-effect of setting the
protoobject parameter, and vice versa. - proto
-
Used the same way as with
Net::Gen, but has a handler attached to keep it in sync withIPproto. - thishost
-
The source host name or address to use for the
bindmethod. When used in conjunction with thethisserviceorthisportobject parameter, causes thesrcaddrlistobject parameter to be set, which is how it affects the bind() action. This parameter is validated, and must be either a valid internet address or a hostname for which an address can be found. If a hostname is given, and multiple addresses are found for it, then each address will be entered into thesrcaddrlistarray reference. - desthost
-
The destination host name or address to use for the
connectmethod. When used in conjunction with thedestserviceordestportobject parameter, causes thedstaddrlistobject parameter to be set, which is how it affects the connect() action. This parameter is validated, and must be either a valid internet address or a hostname for which an address can be found. If a hostname is given, and multiple addresses are found for it, then each address will be entered into thedstaddrlistarray reference, in order. This allows theconnectmethod to attempt a connection to each address, as per RFC 1123. - thisservice
-
The source service name (or number) to use for the
bindmethod. An attempt will be made to translate the supplied service name with getservbyname(). If that succeeds, or if it fails but the supplied value was strictly numeric, the port number will be set in thethisportobject parameter. If the supplied value is not numeric and can't be translated, the attempt to set the value will fail. Otherwise, this causes thesrcaddrlistobject parameter to be updated, in preparation for an invocation of thebindmethod (possibly implicitly from theconnectmethod). - thisport
-
The source service number (or name) to use for the
bindmethod. An attempt will be made to translate the supplied service name with getservbyname() if it is not strictly numeric. If that succeeds, the given name will be set in thethisserviceparameter, and the resolved port number will be set in thethisportobject parameter. If the supplied value is strictly numeric, and a call to getservbyport can resolve a name for the service, thethisserviceparameter will be updated appropriately. If the supplied value is not numeric and can't be translated, the attempt to set the value will fail. Otherwise, this causes thesrcaddrlistobject parameter to be updated, in preparation for an invocation of thebindmethod (possibly implicitly from theconnectmethod). - destservice
-
The destination service name (or number) to use for the
connectmethod. An attempt will be made to translate the supplied service name with getservbyname(). If that succeeds, or if it fails but the supplied value was strictly numeric, the port number will be set in thedestportobject parameter. If the supplied value is not numeric and can't be translated, the attempt to set the value will fail. Otherwise, if thedesthostparameter has a defined value, this causes thedstaddrlistobject parameter to be updated, in preparation for an invocation of theconnectmethod. - destport
-
The destination service number (or name) to use for the
connectmethod. An attempt will be made to translate the supplied service name with getservbyname() if it is not strictly numeric. If that succeeds, the given name will be set in thedestserviceparameter, and the resolved port number will be set in thedestportparameter. If the supplied value is strictly numeric, and a call to getservbyport can resolve a name for the service, thedestserviceparameter will be updated appropriately. If the supplied value is not numeric and can't be translated, the attempt to set the value will fail. Otherwise, if thedesthostparameter has a defined value, this causes thedstaddrlistobject parameter to be updated, in preparation for an invocation of theconnectmethod. - lcladdr
-
The local IP address stashed by the
getsockinfomethod after a successful bind() or connect() call. - lclhost
-
The local hostname stashed by the
getsockinfomethod after a successful bind() or connect(), as resolved from thelcladdrobject parameter. - lclport
-
The local port number stashed by the
getsockinfomethod after a successful bind() or connect() call. - lclservice
-
The local service name stashed by the
getsockinfomethod after a successful bind() or connect(), as resolved from thelclportobject parameter. - remaddr
-
The remote IP address stashed by the
getsockinfomethod after a successful connect() call. - remhost
-
The remote hostname stashed by the
getsockinfomethod after a successful connect() call, as resolved from theremaddrobject parameter. - remport
-
The remote port number stashed by the
getsockinfomethod after a successful connect() call. - remservice
-
The remote service name stashed by the
getsockinfomethod after a successful connect() call, as resolved from theremportobject parameter.
Non-Method Subroutines
- inet_aton
-
Usage:
$in_addr = inet_aton('192.0.2.1');Returns the packed
AF_INETaddress in network order, if it is validly formed, orundefon error. This used to be a separate implementation in this package, but is now inherited from theSocketmodule. - inet_addr
-
A synonym for inet_aton() (for old fogeys like me who forget about the new name). (Yes, I know it's different in C, but in Perl there's no need to propagate the old inet_addr() braindamage of being unable to handle "255.255.255.255", so I didn't.)
- inet_ntoa
-
Usage:
$addr_string = inet_ntoa($in_addr);Returns the ASCII representation of the
AF_INETaddress provided (if possible), orundefon error. This used to be a separate implementation in this package, but is now inherited from theSocketmodule. - htonl
- htons
- ntohl
- ntohs
-
About as those who are used to them might expect, I think. However, these versions will return lists in list context, and will complain if given a multi-element list in scalar context.
[For those who don't know what these are, and who don't have documentation on them in their existing system documentation, these functions convert data between 'host' and 'network' byte ordering, for 'short' or 'long' network data. (This should explain the 'h', 'n', 's', and 'l' letters in the names.) Long network data means 32-bit quantities, such as IP addresses, and short network data means 16-bit quantities, such as IP port numbers. You'd only need to use these functions if you're not using the methods from this package to build your packed 'sockaddr' structures or to unpack their data after a connect() or accept().]
- pack_sockaddr_in
-
Usage:
$connect_address = pack_sockaddr_in($family, $port, $in_addr); $connect_address = pack_sockaddr_in($port, $in_addr);Returns the packed
struct sockaddr_incorresponding to the provided $family, $port, and $in_addr arguments. The $family and $port arguments must be numbers, and the $in_addr argument must be a packedstruct in_addrsuch as the trailing elements from perl's gethostent() return list. This differs from the implementation in theSocketmodule in that the$familyargument is available (though optional). The$familyargument defaults toAF_INET. - unpack_sockaddr_in
-
Usage:
($family, $port, $in_addr) = unpack_sockaddr_in($connected_address);Returns the address family, port, and packed
struct in_addrfrom the supplied packedstruct sockaddr_in. This is the inverse of pack_sockaddr_in(). This differs from the implementation in theSocketmodule in that the$familyvalue from the socket address is returned (and might not beAF_INET). - INADDR_UNSPEC_GROUP
- INADDR_ALLHOSTS_GROUP
- INADDR_ALLRTRS_GROUP
- INADDR_MAX_LOCAL_GROUP
-
Constant routines returning the unspecified local, all hosts, all routers, or the maximum possible local IP multicast group address, respectively. These routines return results in the form of a packed
struct inaddrmuch like theINADDR_ANYresult described in "INADDR_ANY" in Socket. - IN_CLASSA
- IN_CLASSB
- IN_CLASSC
- IN_CLASSD
- IN_MULTICAST
- IN_EXPERIMENTAL
- IN_BADCLASS
-
Usage:
$boolean = IN_EXPERIMENTAL(INADDR_ALLHOSTS_GROUP); $boolean = IN_CLASSA(0x7f000001);These routines return the network class information for the supplied IP address. Of these, only IN_BADCLASS() and IN_MULTICAST() are really useful in today's Internet, since the advent of CIDR (classless Internet domain routing). In particular, IN_EXPERIMENTAL() is at the mercy of your vendor's definition. The first example above will be true only on older systems, which almost certainly don't support IP multicast anyway. The argument to any of these functions can be either a packed
struct inaddrsuch as that returned by inet_ntoa() or unpack_sockaddr_in(), or an integer (or integer expression) giving an IP address in host byte order. - IPOPT_CLASS
- IPOPT_COPIED
- IPOPT_NUMBER
-
Usage:
$optnum = IPOPT_NUMBER($option);These routines extract information from IP option numbers, as per the information on IP options in RFC 791.
- ...
-
Other constants which relate to parts of IP or ICMP headers or vendor-defined socket options, as listed in "Exports" below.
Exports
- default
-
INADDR_ALLHOSTS_GROUPINADDR_ALLRTRS_GROUPINADDR_ANYINADDR_BROADCASTINADDR_LOOPBACKINADDR_MAX_LOCAL_GROUPINADDR_NONEINADDR_UNSPEC_GROUPIPPORT_RESERVEDIPPORT_USERRESERVEDIPPORT_DYNAMICIPPROTO_EGPIPPROTO_EONIPPROTO_GGPIPPROTO_HELLOIPPROTO_ICMPIPPROTO_IDPIPPROTO_IGMPIPPROTO_IPIPPROTO_IPIPIPPROTO_MAXIPPROTO_PUPIPPROTO_RAWIPPROTO_RSVPIPPROTO_TCPIPPROTO_TPIPPROTO_UDPhtonlhtonsinet_addrinet_atoninet_ntoantohlntohs - exportable
-
DEFTTLICMP_ADVLENMINICMP_ECHOICMP_ECHOREPLYICMP_INFOTYPEICMP_IREQICMP_IREQREPLYICMP_MASKLENICMP_MASKREPLYICMP_MASKREQICMP_MAXTYPEICMP_MINLENICMP_PARAMPROBICMP_REDIRECTICMP_REDIRECT_HOSTICMP_REDIRECT_NETICMP_REDIRECT_TOSHOSTICMP_REDIRECT_TOSNETICMP_SOURCEQUENCHICMP_TIMXCEEDICMP_TIMXCEED_INTRANSICMP_TIMXCEED_REASSICMP_TSLENICMP_TSTAMPICMP_TSTAMPREPLYICMP_UNREACHICMP_UNREACH_HOSTICMP_UNREACH_NEEDFRAGICMP_UNREACH_NETICMP_UNREACH_PORTICMP_UNREACH_PROTOCOLICMP_UNREACH_SRCFAILIN_BADCLASSIN_CLASSAIN_CLASSA_HOSTIN_CLASSA_MAXIN_CLASSA_NETIN_CLASSA_NSHIFTIN_CLASSA_SUBHOSTIN_CLASSA_SUBNETIN_CLASSA_SUBNSHIFTIN_CLASSBIN_CLASSB_HOSTIN_CLASSB_MAXIN_CLASSB_NETIN_CLASSB_NSHIFTIN_CLASSB_SUBHOSTIN_CLASSB_SUBNETIN_CLASSB_SUBNSHIFTIN_CLASSCIN_CLASSC_HOSTIN_CLASSC_MAXIN_CLASSC_NETIN_CLASSC_NSHIFTIN_CLASSDIN_CLASSD_HOSTIN_CLASSD_NETIN_CLASSD_NSHIFTIN_EXPERIMENTALIN_LOOPBACKNETIN_MULTICASTIPFRAGTTLIPOPT_CIPSOIPOPT_CLASSIPOPT_CONTROLIPOPT_COPIEDIPOPT_DEBMEASIPOPT_EOLIPOPT_LSRRIPOPT_MINOFFIPOPT_NOPIPOPT_NUMBERIPOPT_OFFSETIPOPT_OLENIPOPT_OPTVALIPOPT_RESERVED1IPOPT_RESERVED2IPOPT_RIPSO_AUXIPOPT_RRIPOPT_SATIDIPOPT_SECURITYIPOPT_SECUR_CONFIDIPOPT_SECUR_EFTOIPOPT_SECUR_MMMMIPOPT_SECUR_RESTRIPOPT_SECUR_SECRETIPOPT_SECUR_TOPSECRETIPOPT_SECUR_UNCLASSIPOPT_SSRRIPOPT_TSIPOPT_TS_PRESPECIPOPT_TS_TSANDADDRIPOPT_TS_TSONLYIPPORT_TIMESERVERIPTOS_LOWDELAYIPTOS_PREC_CRITIC_ECPIPTOS_PREC_FLASHIPTOS_PREC_FLASHOVERRIDEIPTOS_PREC_IMMEDIATEIPTOS_PREC_INTERNETCONTROLIPTOS_PREC_NETCONTROLIPTOS_PREC_PRIORITYIPTOS_PREC_ROUTINEIPTOS_RELIABILITYIPTOS_THROUGHPUTIPTTLDECIPVERSIONIP_ADD_MEMBERSHIPIP_DEFAULT_MULTICAST_LOOPIP_DEFAULT_MULTICAST_TTLIP_DFIP_DROP_MEMBERSHIPIP_HDRINCLIP_MAXPACKETIP_MAX_MEMBERSHIPSIP_MFIP_MSSIP_MULTICAST_IFIP_MULTICAST_LOOPIP_MULTICAST_TTLIP_OPTIONSIP_RECVDSTADDRIP_RECVOPTSIP_RECVRETOPTSIP_RETOPTSIP_TOSIP_TTLMAXTTLMAX_IPOPTLENMINTTLSUBNETSHIFTpack_sockaddr_inunpack_sockaddr_in -
The following :tags are in
%EXPORT_TAGS, with the associated exportable values as listed:- :sockopts
-
IP_HDRINCLIP_RECVDSTADDRIP_RECVOPTSIP_RECVRETOPTSIP_TOSIP_TTLIP_ADD_MEMBERSHIPIP_DROP_MEMBERSHIPIP_MULTICAST_IFIP_MULTICAST_LOOPIP_MULTICAST_TTLIP_OPTIONSIP_RETOPTS - :routines
-
pack_sockaddr_inunpack_sockaddr_ininet_ntoainet_atoninet_addrhtonlntohlhtonsntohsICMP_INFOTYPEIN_BADCLASSIN_EXPERIMENTALIN_MULTICASTIPOPT_CLASSIPOPT_COPIEDIPOPT_NUMBER - :icmpvalues
-
ICMP_ADVLENMINICMP_ECHOICMP_ECHOREPLYICMP_IREQICMP_IREQREPLYICMP_MASKLENICMP_MASKREPLYICMP_MASKREQICMP_MAXTYPEICMP_MINLENICMP_PARAMPROBICMP_REDIRECTICMP_REDIRECT_HOSTICMP_REDIRECT_NETICMP_REDIRECT_TOSHOSTICMP_REDIRECT_TOSNETICMP_SOURCEQUENCHICMP_TIMXCEEDICMP_TIMXCEED_INTRANSICMP_TIMXCEED_REASSICMP_TSLENICMP_TSTAMPICMP_TSTAMPREPLYICMP_UNREACHICMP_UNREACH_HOSTICMP_UNREACH_NEEDFRAGICMP_UNREACH_NETICMP_UNREACH_PORTICMP_UNREACH_PROTOCOLICMP_UNREACH_SRCFAIL - :ipoptions
-
IPOPT_CIPSOIPOPT_CONTROLIPOPT_DEBMEASIPOPT_EOLIPOPT_LSRRIPOPT_MINOFFIPOPT_NOPIPOPT_OFFSETIPOPT_OLENIPOPT_OPTVALIPOPT_RESERVED1IPOPT_RESERVED2IPOPT_RIPSO_AUXIPOPT_RRIPOPT_SATIDIPOPT_SECURITYIPOPT_SECUR_CONFIDIPOPT_SECUR_EFTOIPOPT_SECUR_MMMMIPOPT_SECUR_RESTRIPOPT_SECUR_SECRETIPOPT_SECUR_TOPSECRETIPOPT_SECUR_UNCLASSIPOPT_SSRRIPOPT_TSIPOPT_TS_PRESPECIPOPT_TS_TSANDADDRIPOPT_TS_TSONLYMAX_IPOPTLEN - :iptosvalues
-
IPTOS_LOWDELAYIPTOS_PREC_CRITIC_ECPIPTOS_PREC_FLASHIPTOS_PREC_FLASHOVERRIDEIPTOS_PREC_IMMEDIATEIPTOS_PREC_INTERNETCONTROLIPTOS_PREC_NETCONTROLIPTOS_PREC_PRIORITYIPTOS_PREC_ROUTINEIPTOS_RELIABILITYIPTOS_THROUGHPUT - :protocolvalues
-
DEFTTLINADDR_ALLHOSTS_GROUPINADDR_ALLRTRS_GROUPINADDR_ANYINADDR_BROADCASTINADDR_LOOPBACKINADDR_MAX_LOCAL_GROUPINADDR_NONEINADDR_UNSPEC_GROUPIN_LOOPBACKNETIPPORT_RESERVEDIPPORT_USERRESERVEDIPPORT_DYNAMICIPPROTO_EGPIPPROTO_EONIPPROTO_GGPIPPROTO_HELLOIPPROTO_ICMPIPPROTO_IDPIPPROTO_IGMPIPPROTO_IPIPPROTO_IPIPIPPROTO_MAXIPPROTO_PUPIPPROTO_RAWIPPROTO_RSVPIPPROTO_TCPIPPROTO_TPIPPROTO_UDPIPFRAGTTLIPTTLDECIPVERSIONIP_DFIP_MAXPACKETIP_MFIP_MSSMAXTTLMAX_IPOPTLENMINTTL - :ipmulticast
-
IP_ADD_MEMBERSHIPIP_DEFAULT_MULTICAST_LOOPIP_DEFAULT_MULTICAST_TTLIP_DROP_MEMBERSHIPIP_MAX_MEMBERSHIPSIP_MULTICAST_IFIP_MULTICAST_LOOPIP_MULTICAST_TTL - :deprecated
-
IN_CLASSA_HOSTIN_CLASSA_MAXIN_CLASSA_NETIN_CLASSA_NSHIFTIN_CLASSA_SUBHOSTIN_CLASSA_SUBNETIN_CLASSA_SUBNSHIFTIN_CLASSB_HOSTIN_CLASSB_MAXIN_CLASSB_NETIN_CLASSB_NSHIFTIN_CLASSB_SUBHOSTIN_CLASSB_SUBNETIN_CLASSB_SUBNSHIFTIN_CLASSC_HOSTIN_CLASSC_MAXIN_CLASSC_NETIN_CLASSC_NSHIFTIN_CLASSD_HOSTIN_CLASSD_NETIN_CLASSD_NSHIFTIN_CLASSAIN_CLASSBIN_CLASSCIN_CLASSDIPPORT_TIMESERVERSUBNETSHIFT - :ALL
-
All of the above exportable items.
NOTES
Anywhere a service or port argument is used above, the allowed syntax is either a service name, a port number, or a service name with a caller-supplied default port number. Examples are 'echo', 7, and 'echo(7)', respectively. For a service argument, a bare port number must be translatable into a service name with getservbyport() or an error will result. A service name must be translatable into a port with getservbyname() or an error will result. However, a service name with a default port number will succeed (by using the supplied default) even if the translation with getservbyname() fails.
For example:
$obj->setparam('destservice', 'http(80)');
This always succeeds, although if your /etc/services file (or equivalent for non-UNIX systems) maps "http" to something other than port 80, you'll get that other port.
For a contrasting example:
$obj->setparam('destservice', 80);
This will fail, despite the numeric value, if your /etc/services file (or equivalent) is behind the times and has no mapping to a service name for port 80.
THREADING STATUS
This module has been tested with threaded perls, and should be as thread-safe as perl itself. (As of 5.005_03 and 5.005_57, that's not all that safe just yet.) It also works with interpreter-based threads ('ithreads') in more recent perl releases.
SEE ALSO
Net::Gen(3), Net::TCP(3), Net::UDP(3)
AUTHOR
Spider Boardman <spidb@cpan.org>