Net::SIP::Packet(3pm) | User Contributed Perl Documentation | Net::SIP::Packet(3pm) |
Net::SIP::Packet - handling of SIP packets
use Net::SIP::Packet; use Net::SIP::Request; use Net::SIP::Response; my $pkt = eval { Net::SIP::Packet->new( $sip_string ) } or die "invalid SIP packet"; $pkt->get_header( 'call-id' ) || die "no call-id"; $pkt->set_header( via => \@via ); print $pkt->as_string;
This module implements the parsing, manipulation and creation of SIP packets according to RFC3261.
NET::SIP::Packet's objects can be created by parsing a string containing the SIP packet or by constructing it from parts, e.g. header keys and values, body, method+URI (requests) or code+text (responses).
All parts can be manipulated and finally the string representation of the manipulated packet can be (re)created.
For dealing with requests and responses directly usually the subclasses Net::SIP::Request or Net::SIP::Response will be used instead.
# create packet from string my $invite = Net::SIP::Packet->new( <<'EOS' ); INVITE sip:you@example.com SIP/2.0 From: <sip:me@example.com> To: <sip:you@example.com> ... EOS # show and manipulate some header print "callid=".$invite->get_header( 'call-id' )."\n"; print "route=".join( ",", $invite->get_header( 'route' ))."\n"; $invite->set_header( 'via' => [ $via1,$via2,.. ] ); # get resulting string representation print $invite->as_string; # create packet from parts my $resp = Net::SIP::Packet->new( 200, 'Ok', { to => '<sip:you@example.com>', from => '<sip:me@example.com>',.. } Net::SIP::SDP->new(...) ); # and get the packet as string print $resp->as_string;
Header data can be given as a hash %HEADER or array @HEADER reference. In case of a hash the key is the SIP field name and the value as either a string or a \@list of strings. The fields on the resulting SIP packet will be sorted by name of the fields and fields with multiple values will be created as seperat lines.
If the header is given as an array the elements of the array are "[ key => value ]" pairs where the keys are the field names and the values are strings or \@list of strings. Each pair will result in a single line in the SIP header. If the value was a list reference the values in the list will be concatened by ','. The order of the fields in the resulting SIP packet will be the same as in the array.
The BODY is optional and can be given either as a string or as an reference to an object which has a method as_string, like Net::SIP::SDP. If the BODY is an object which has a method content_type it will set the "content-type" header of the SIP object based on the result of "BODY->content_type" unless a "content-type" header was explicitly given.
If no NAME is given it will return a reference to a hash which contains all fields and has the format described in new_from_parts.
If body contains invalid SDP it raises an exception (e.g. die()).
If the optional SDP argument is given, it is expected to be a Net::SIP::SDP object. This will be converted into a string and will replace an existing body (also inside multipart/mixed, leaving the rest untouched) or set a new body if none existed before.
2023-02-04 | perl v5.36.0 |