0x1949 Team - FAZEMRX - MANAGER
Edit File: Net::SIP::Util.3pm
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Net::SIP::Util 3pm" .TH Net::SIP::Util 3pm "2021-05-04" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Net::SIP::Util \- utility functions used by all of Net::SIP .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Net::SIP::Util qw( create_rtp_sockets ); \& my ($port,@socks) = create_rtp_sockets( \*(Aq192.168.0.10\*(Aq ) or die; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This package implements various utility function used within various Net::SIP packages and partly usable for the user of Net::SIP too. .PP Each of this functions is exportable, but none is exported per default. All functions can be exported at once with the import flag \f(CW\*(C`:all\*(C'\fR. .SH "SUBROUTINES" .IX Header "SUBROUTINES" .ie n .IP "invoke_callback ( \s-1CALLBACK,\s0 @ARGS )" 4 .el .IP "invoke_callback ( \s-1CALLBACK,\s0 \f(CW@ARGS\fR )" 4 .IX Item "invoke_callback ( CALLBACK, @ARGS )" Invokes callback \s-1CALLBACK\s0 with additional args \f(CW@ARGS\fR. \&\s-1CALLBACK\s0 can be: .RS 4 .IP "A code reference" 8 .IX Item "A code reference" In this case it will be called as \f(CW\*(C`$CALLBACK\->(@ARGS)\*(C'\fR and return the return value of this call. .IP "A reference to a scalar" 8 .IX Item "A reference to a scalar" In this case the scalar will be set to \f(CW$ARGS[0]\fR and the rest of \f(CW@ARGS\fR will be ignored. If no \f(CW@ARGS\fR are given the scalar will be set to \s-1TRUE.\s0 It will return with the value of the scalar. .IP "An object which has a method \fBrun\fR" 8 .IX Item "An object which has a method run" In this case it will call \f(CW\*(C`$CALLBACK\->run(@ARGS)\*(C'\fR and return with the return value of this call. .IP "A reference to an array" 8 .IX Item "A reference to an array" The first element of the array will be interpreted as code reference, while the rest as args, e.g. it will do: .Sp .Vb 2 \& my ($coderef,@cb_args) = @$CALLBACK; \& return $coderef\->( @cb_args, @ARGS ); .Ve .IP "A regular expression" 8 .IX Item "A regular expression" In this case it will try to match all \f(CW@ARGS\fR against the regex. If anything matches it will return \s-1TRUE,\s0 else \s-1FALSE.\s0 .RE .RS 4 .RE .IP "laddr4dst(\s-1DST\s0) \-> \s-1SRC\s0" 4 .IX Item "laddr4dst(DST) -> SRC" This will try to find out which local \s-1IP\s0 address \fB\s-1SRC\s0\fR is used for connections to target \fB\s-1DST\s0\fR. This will be done by creating a connected \s-1UDP\s0 socket to the target and using \fBgetsockname\fR to get the local \s-1IP\s0 address of this socket. .IP "create_socket_to ( \s-1ADDR,\s0 [ \s-1PROTO\s0 ] )" 4 .IX Item "create_socket_to ( ADDR, [ PROTO ] )" Creates socket with protocol \s-1PROTO\s0 (default 'udp'). It will use \f(CW\*(C`laddr4dst\*(C'\fR to find the appropriate local source \s-1IP\s0 address. .Sp It will try to bind the socket to port 5060 (default \s-1SIP\s0 port). If this fails it will try port 5062..5100 and if it cannot bind to any of these ports it will just use any port which gets assigned by the \s-1OS.\s0 .Sp For multihomed hosts where several addresses are bound to the same interface it will just use one of these addresses. If you need more control about the address the socket is bound to (and which will be used as the local \s-1IP\s0 in outgoing packets) you need to create the socket yourself. .Sp In scalar context it just returns the newly created socket. In array context it will return the socket and the \f(CW"ip:port"\fR the created socket is bound to. If the creation of the socket fails it will return \f(CW\*(C`()\*(C'\fR and set \f(CW$!\fR. .Sp Example: .Sp .Vb 2 \& my ($sock,$ip_port) = create_socket_to ( \*(Aq192.168.0.1\*(Aq ) \& or die $!; .Ve .IP "create_rtp_sockets ( \s-1LADDR,\s0 [ \s-1RANGE, MINPORT, MAXPORT, TRIES\s0 ] )" 4 .IX Item "create_rtp_sockets ( LADDR, [ RANGE, MINPORT, MAXPORT, TRIES ] )" This tries to allocate sockets for \s-1RTP. RTP\s0 consists usually of a data socket on an even port number and a control socket (\s-1RTCP\s0) and the following port. It will try to create these sockets. \s-1MINPORT\s0 is the minimal port number to use (default 2000), \s-1MAXPORT\s0 the highest port (default \&\s-1MINPORT+10000\s0), \s-1TRIES\s0 is the number of attempts it makes to create such socket pairs and defaults to 1000. .Sp \&\s-1RANGE\s0 is the number of consecutive ports it needs to allocate and defaults to 2 (e.g. data and control socket). .Sp Allocation will be done by choosing a random even number between \s-1MINPORT\s0 and \s-1MAXPORT\s0 and then trying to allocate all the sockets on this and the following port numbers. .Sp If the allocation fails after \s-1TRIES\s0 attempts were made it will return \f(CW\*(C`()\*(C'\fR, otherwise it will return an array with at first the starting port number followed by all the allocated sockets. .Sp Example: .Sp .Vb 2 \& my ($port,$rtp_sock,$rtcp_sock) = create_rtp_sockets( \*(Aq192.168.0.10\*(Aq ) \& or die "allocation failed"; .Ve .IP "sip_hdrval2parts ( \s-1KEY, VALUE\s0 )" 4 .IX Item "sip_hdrval2parts ( KEY, VALUE )" Interprets \s-1VALUE\s0 as a value for the \s-1SIP\s0 header field \s-1KEY\s0 and splits it into the parts (prefix, parameter). Because for most keys the delimiter is \f(CW\*(C`;\*(C'\fR, but for some keys \f(CW\*(C`,\*(C'\fR the field name \s-1KEY\s0 need to be known. .Sp \&\s-1KEY\s0 needs to be normalized already (lower case, no abbreviation). .Sp Returns array with initial data (up to first delimiter) and the parameters as hash. .Sp Example for key 'to': .Sp .Vb 2 \& \*(Aq"Silver; John" <silver@example.com>; tag=...; protocol=TCP\*(Aq \& \-> ( \*(Aq"Silver; John" <silver@example.com>\*(Aq, { tag => ..., protocol => \*(AqTCP\*(Aq } ) .Ve .Sp Example for key 'www\-authenticate': .Sp .Vb 2 \& \*(AqDigest method="md5", qop="auth"\*(Aq \& \-> ( \*(AqDigest\*(Aq, { method => \*(Aqmd5\*(Aq, qop => \*(Aqauth\*(Aq } ) .Ve .IP "sip_parts2hdrval ( \s-1KEY, PREFIX,\s0 \e%PARAMETER )" 4 .IX Item "sip_parts2hdrval ( KEY, PREFIX, %PARAMETER )" Inverse function to \fBsip_hdrval2parts\fR, e.g constructs header value for \s-1KEY\s0 from \&\s-1PREFIX\s0 and \f(CW%PARAMETER\fR and returns value. .IP "sip_uri2parts(\s-1URI\s0) \-> (\s-1DOMAIN, USER, PROTO, PARAM, DATA\s0) | \s-1DOMAIN\s0" 4 .IX Item "sip_uri2parts(URI) -> (DOMAIN, USER, PROTO, PARAM, DATA) | DOMAIN" Returns parts from \s-1URI.\s0 If called in scalar context it returns only the domain part. In array context it returns an array with the following values: .RS 4 .IP "\s-1DOMAIN\s0" 4 .IX Item "DOMAIN" The lower cased domain part as given in the \s-1URI,\s0 i.e. \f(CW\*(C`host\*(C'\fR, \f(CW\*(C`host:port\*(C'\fR, \&\f(CW\*(C`[ipv6]:port\*(C'\fR etc. .IP "\s-1USER\s0" 4 .IX Item "USER" The optional user part of the \s-1SIP\s0 address. .IP "\s-1PROTO\s0" 4 .IX Item "PROTO" The protocol, e.g. \f(CW\*(C`sip\*(C'\fR or \f(CW\*(C`sips\*(C'\fR. If not explicitly given it will default to \f(CW\*(C`sip\*(C'\fR. .IP "\s-1PARAM\s0" 4 .IX Item "PARAM" A hash reference to any parameters following \f(CW\*(C`data\*(C'\fR as returned by \&\fBsip_hdrval2parts\fR. .IP "\s-1DATA\s0" 4 .IX Item "DATA" The original full part before any parameter, i.e. the part containing the domain, optional user and optional proto. .RE .RS 4 .RE .IP "sip_parts2uri(\s-1DOMAIN, USER, PROTO, PARAM\s0) \-> \s-1URI\s0" 4 .IX Item "sip_parts2uri(DOMAIN, USER, PROTO, PARAM) -> URI" Returns \s-1URI\s0 from parts, i.e. the reverse to \fBsip_uri2parts\fR. For the meaning of the parameters see there. .IP "sip_uri2sockinfo(\s-1URI\s0; \s-1OPAQUE\s0) \-> (\s-1PROTO, HOST, PORT, FAMILY\s0)" 4 .IX Item "sip_uri2sockinfo(URI; OPAQUE) -> (PROTO, HOST, PORT, FAMILY)" This extracts information from \fB\s-1URI\s0\fR which are needed for creating the socket: \&\fB\s-1HOST\s0\fR is the \s-1IP\s0 address or host name, \fB\s-1PORT\s0\fR the port (undef if not given) and \fB\s-1FAMILY\s0\fR the family, i.e. \s-1AF_INET, AF_INET6\s0 or undef if \fB\s-1HOST\s0\fR is not an \&\s-1IP\s0 address. \&\fB\s-1PROTO\s0\fR will be set based on the \s-1SIP\s0 protocol and parameters, i.e. \f(CW\*(C`tls\*(C'\fR for \&\f(CW\*(C`sips\*(C'\fR \s-1URI,\s0 \f(CW\*(C`tcp\*(C'\fR or \f(CW\*(C`udp\*(C'\fR for \f(CW\*(C`sip\*(C'\fR \s-1URI\s0 with explicit \f(CW\*(C`transport\*(C'\fR parameter and \f(CW\*(C`undef\*(C'\fR otherwise. .Sp If \fB\s-1OPAQUE\s0\fR the host name is not required to be a valid name. See \fB\s-1OPAQUE\s0\fR in \f(CW\*(C`ip_string2parts\*(C'\fR for more information. .IP "sip_sockinfo2uri(\s-1PROTO, HOST, PORT, FAMILY\s0) \-> \s-1URI\s0" 4 .IX Item "sip_sockinfo2uri(PROTO, HOST, PORT, FAMILY) -> URI" This is the reverse to \fBsip_uri2sockinfo\fR. See there for the meaning of the parameters. .Sp If the first argument is a hash ref it will expected to provided the arguments in the keys \f(CW\*(C`proto\*(C'\fR, \f(CW\*(C`host\*(C'\fR, \f(CW\*(C`port\*(C'\fR and \f(CW\*(C`family\*(C'\fR. .IP "sip_uri_eq ( \s-1URI1, URI2\s0 )" 4 .IX Item "sip_uri_eq ( URI1, URI2 )" Returns true if both URIs point to the same \s-1SIP\s0 address. This compares user part case sensitive, domain part case insensitive (does no \s-1DNS\s0 resolution) protocol and ports in domain (assumes default ports for protocol if no port is given). .IP "ip_string2parts(\s-1STR\s0;OPAQUE) \-> (\s-1HOST, PORT, FAMILY\s0) | \e%HASH" 4 .IX Item "ip_string2parts(STR;OPAQUE) -> (HOST, PORT, FAMILY) | %HASH" This will parse the given string \s-1STR\s0 and split it into the parts as follows: .Sp .Vb 6 \& IPv4, [IPv4] \-> (IPv4, undef, AF_INET) \& IPv4:port, [IPv4]:port \-> (IPv4, port, AF_INET) \& IPv6, [IPv6] \-> (IPv6, undef, AF_INET6) \& [IPv6]:port \-> (IPv6, port, AF_INET6) \& host, [host] \-> (host, undef, undef) \& host:port, [host]:port \-> (host, port, undef) .Ve .Sp The \s-1IP\s0 address and host will be returned in a canonicalized way. .Sp If this function is used to parse strings where the host part is not a real hostname but some identifier with more allowed characters than a hostname then \&\s-1OPAQUE\s0 should be set and in this way no strict checking and no canonicalization is done. .Sp If the function is called with a scalar context it will return the result as a hash ref with the keys \f(CW\*(C`host\*(C'\fR, \f(CW\*(C`addr\*(C'\fR, \f(CW\*(C`port\*(C'\fR, \f(CW\*(C`family\*(C'\fR where \f(CW\*(C`addr\*(C'\fR is only set if it is \s-1IP\s0 address. .IP "ip_parts2string(\s-1HOST,\s0 [ \s-1PORT, FAMILY,\s0 IPv6_BRCKT]) \-> \s-1STR\s0" 4 .IX Item "ip_parts2string(HOST, [ PORT, FAMILY, IPv6_BRCKT]) -> STR" This is the reverse to ip_string2parts. If family is not given it will be determined by checking if ip_or_host contains ':' (i.e IPv6 address): .Sp .Vb 4 \& (ip_or_host) \-> ip_or_host \& (ipv4_or_host,port) \-> ipv4_or_host:port \& (ipv6,port) \-> [ipv6]:port \& (ipv6,undef,*,true) \-> [ipv6] .Ve .Sp If the first argument is a hash ref it will be treated as a hash ref as returned by \fBip_string2parts\fR. In this case also \f(CW\*(C`use_host\*(C'\fR can be given which prefers \&\f(CW\*(C`host\*(C'\fR to \f(CW\*(C`addr\*(C'\fR for stringification. Also, \f(CW\*(C`default_port\*(C'\fR can be used to give a port number which will be treated as default and omitted from string. .IP "ip_sockaddr2parts(\s-1SOCKADDR,\s0 [\s-1FAMILY\s0]) \-> (\s-1IP, PORT, FAMILY\s0)" 4 .IX Item "ip_sockaddr2parts(SOCKADDR, [FAMILY]) -> (IP, PORT, FAMILY)" This will return the \s-1IP, PORT\s0 and \s-1FAMILY\s0 from a sockaddr_in or sockaddr_in6. If \s-1FAMILY\s0 is not given it will be determined based on the size of \s-1SOCKADDR.\s0 .Sp If the function is called with a scalar context it will return the result as a hash ref as described for \fBip_string2parts\fR. .IP "ip_parts2sockaddr(\s-1IP, PORT,\s0 [\s-1FAMILY\s0]) \-> \s-1SOCKADDR\s0" 4 .IX Item "ip_parts2sockaddr(IP, PORT, [FAMILY]) -> SOCKADDR" This will create a sockaddr_in or sockaddr_in6 from \s-1IP\s0 and \s-1PORT. FAMILY\s0 will be determined by checking the \s-1IP\s0 if not given. .Sp If the first argument is a hash ref it will be treated as a hash ref as returned by \fBip_sockaddr2parts\fR. .IP "ip_sockaddr2string(\s-1SOCKADDR,\s0 [\s-1FAMILY\s0]) \-> \s-1STR\s0" 4 .IX Item "ip_sockaddr2string(SOCKADDR, [FAMILY]) -> STR" This will return the \s-1STR\s0 from a sockaddr_in or sockaddr_in6, i.e. like combining \&\f(CW\*(C`ip_sockaddr2parts\*(C'\fR with \f(CW\*(C`ip_parts2string\*(C'\fR. If \s-1FAMILY\s0 is not given it will be determined based on the size of \s-1SOCKADDR.\s0 .IP "ip_ptr(\s-1IP,\s0 [\s-1FAMILY\s0]) \-> \s-1PTR_NAME\s0" 4 .IX Item "ip_ptr(IP, [FAMILY]) -> PTR_NAME" This will create the hostname used in reverse lookup of an \s-1IP\s0 address, i.e. \&\f(CW\*(C`*.in\-addr.arpa\*(C'\fR or \f(CW\*(C`*.ip6.arpa\*(C'\fR. If \s-1FAMILY\s0 is not given it will be determined based on the syntax of \s-1IP.\s0 .IP "ip_canonical(\s-1IP,\s0 [\s-1FAMILY\s0]) \-> \s-1IP\s0" 4 .IX Item "ip_canonical(IP, [FAMILY]) -> IP" This will convert the given \s-1IP\s0 address into a canonical form suitable for comparison. .ie n .IP "hostname2ip(host, [\s-1FAMILY\s0]) \-> @IP" 4 .el .IP "hostname2ip(host, [\s-1FAMILY\s0]) \-> \f(CW@IP\fR" 4 .IX Item "hostname2ip(host, [FAMILY]) -> @IP" This will lookup the given name using getaddrinfo and return the \s-1IP\s0 addresses. If \s-1FAMILY\s0 is given only addresses matching the family will be returned. .IP "ip_is_v4(\s-1IP\s0) \-> true|false" 4 .IX Item "ip_is_v4(IP) -> true|false" This checks if the given \s-1IP\s0 address is a valid IPv4 address. .IP "ip_is_v6(\s-1IP\s0) \-> true|false" 4 .IX Item "ip_is_v6(IP) -> true|false" This checks if the given \s-1IP\s0 address is a valid IPv6 address. .IP "ip_is_v46(\s-1IP\s0) \-> AF_INET|AF_INET6|undef" 4 .IX Item "ip_is_v46(IP) -> AF_INET|AF_INET6|undef" This checks if the given \s-1IP\s0 address is a valid IPv4 or IPv6 address and returns the address family if this is an \s-1IP\s0 address.