Net::SIP::Leg - Wrapper around Socket for sending and receiving SIP packets


  my $leg = Net::SIP::Leg->new( addr => '' );
  $leg->deliver( $packet, '' );


A Leg wraps the socket which is used to send and receive packets. It provides ways to strip Via header from incoming packets, to add Via header to outgoing packets and to add Record-Route header while forwarding.

It's usually not used directly, but from Net::SIP::Dispatcher.


new ( %ARGS )

The constructor creates a new object based on the hash %ARGS. The following keys are used from %ARGS:


The socket as IO::Socket object. addr, port, family and proto will be determined from this object and not from %ARGS. This will be used to create the SocketPool object.


The socketpool as Net::SIP::SocketPool compatible object. This argument can not be used together with sock. It will instead determine the master socket by calling master on the given SocketPool object.


The local IP address of the socket. If this is given but no port it will extract port from addr, if it's in the format host:port.


The hostname matching addr. This is used to create default contact information and the Via header. If not given defaults to the IP address. Use of hostname instead of IP address is relevant for TLS where the name is needed in validation of the peers certificate.


The port of the socket. Defaults to 5060.


The family of the socket. Will be determined from addr if omitted.


The connection protocol, e.g. 'udp', 'tcp' or 'tls'. Defaults to 'udp'.


The optional fixed target of the leg as hash with keys host, addr, port and family.


Optional contact information which will be added as Record-route header to outgoing requests and used within Contact header for 200 Responses to INVITE. If not given it will be created based on addr, port and proto.


Optional arguments to be used in creating a TLS connection, as expected by IO::Socket::SSL. These are used for both incoming and outgoing TLS connection. Typically this involves SSL_cert_file, SSL_key_file and SSL_ca_file or similar.

If no socket is given with sock it will be created based on addr, port and proto. If this fails the constructur will die().

The constructor will create a uniq branch tag for this leg.


forward_incoming ( PACKET )

Modifies the Net::SIP::Packet PACKET in-place for forwarding, e.g strips top Via header in responses, adds received parameter to top Via header in requests, handles the difference between forwarding of requests to strict or loose routes and inserts Record-Route header in requests.

forward_outgoing ( PACKET, LEG_IN )

Similar to forward_incoming, but will be called on the outgoing leg. LEG_IN is the Net::SIP::Leg, where the packet came in (and where forward_incoming was called). Will add Record-Route header and remove itself from Route.

deliver ( PACKET, ADDR, [ CALLBACK ] )

Delivers Net::SIP::Packet PACKET through the leg $self to ADDR. ADDR is a hash with the keys host, addr, port and family. Usually this method will be call from within Net::SIP::Dispatcher.

If the packet was received by the other end (which is only possible to say if a reliable protocol, e.g. 'tcp' was used) it will call CALLBACK if provided. See invoke_callback in Net::SIP::Util for the format of callbacks. If the packet could not be delivered CALLBACK will be invoked with the appropriate errno ($!).

While delivering requests it adds a Via header.

receive(PACKET, FROM)

This is called from the dispatcher if the the Net::SIP::Packet PACKET was received from FROM. FROM is given as hash with keys addr, port, family and proto. This function might process the packet further or block it.

It will return (PACKET, FROM) in the normal case or () if blocked.

check_via ( PACKET )

Returns TRUE if the top Via header in the Net::SIP::Packet PACKET contains the branch-tag from $self, otherwise FALSE. Used to check if the response came in through the same leg the response was send.

add_via ( PACKET )

Adds itself to PACKET as Via header.

can_deliver_to ( ADDR|%SPEC )

Returns TRUE if the leg can deliver address specified by ADDR or %SPEC. ADDR is a full or partial SIP URI.

If the caller has 'proto','addr','port' and 'family' already as separate items it can call the method with %SPEC instead.

Right now it has now way to check if the leg can deliver to a specific host because it has no access to the routing information of the underlying OS, so that only proto will be checked.


This checks if the given SPEC matches the leg. This is used from inside the get_legs in Net::SIP::Dispatcher to get all legs matching specific criteria. The SPEC can contain the keys addr, port and proto which match the arguments given during construction of the leg or are obtained from the legs socket. Additionally sub can be used to specify a function which gets called with the leg object and must return true on match only.

The method returns true if there was a full match and false otherwise.


This returns the Net::SIP::SocketPool object associated with the leg.


This will return the local address of the socket, either as address only (LEVEL is 0) or as ip:port (higher levels).


Returns string containing information about the leg. Used for debugging.