1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long qw(:config posix_default bundling);
use Net::SIP ':all';
##############################################################
#
# Implements 3pcc according to RFC 3725,4.1 'Flow I'
#
##############################################################
# Usage
# -------------------------------------------------------------
sub usage {
print STDERR "ERROR: @_\n" if @_;
print STDERR <<EOS;
Implements 3rd Party control according to RFC 3625,4.1 'Flow I'
Usage: $0 [ options ] laddr from to
Options:
-d|--debug [level] Enable debugging
-h|--help Help (this info)
Example:
$0 -d 192.168.178.3:5090 \
sip:me\@192.168.178.3:5070 \
sip:me\@192.168.178.3:5080
EOS
exit( @_ ? 1:0 );
}
# get options
# -------------------------------------------------------------
my $debug;
GetOptions(
'd|debug:i' => \$debug,
'h|help' => sub { usage() },
) || usage( 'bad options' );
Debug->level($debug || 1) if defined $debug;
my ($laddr,$from,$to) = @ARGV;
$to || usage( "no TO given" );
# create Dispatcher
# -------------------------------------------------------------
my $loop = Dispatcher_Eventloop->new;
my $leg = Leg->new( addr => $laddr );
my $disp = Dispatcher->new(
[ $leg ],
$loop,
do_retransmits => 0
) || die;
$disp->set_receiver( \&receive );
my $me = ($disp->get_legs())[0]->{contact};
# create initial invite without SDP with
# To: $to, From: $from, Contact: $me
# put these info in call-id to be stateless
# -------------------------------------------------------------
# assume no '|' is in $from and $to
my $callid = "$from|$to|0|". sprintf( "%08x",rand(2**16));
my $invite = Request->new( "INVITE",$from, {
from => $to,
to => $from,
contact => $me,
'call-id' => $callid,
cseq => '1 INVITE',
});
$disp->deliver( $invite, do_retransmits => 1 );
# and loop
# -------------------------------------------------------------
my $stop_loop;
$loop->loop( undef, \$stop_loop );
$loop->loop(1) if $stop_loop; # some time to forward remaining stuff
###############################################################
#
# callback for incoming packets:
#
# - there are two calls which slightly different call-id, with
# a simple way one can get the other call-id from one call-id.
# - responses are for me if there is only one via header, and
# that's me -> handle to make requests (INVITE,ACK) from it
# - all other responses get forwarded. If last via has a cseq
# parameter they get forwarded after changing the cseq
# - requests are for me if the URI is the contact of the local leg
# -> forward to other call, but add "cseq" parameter to last
# via so that the cseq of the calling uac gets saved for
# responses
# - all requests I get should be for me, because a contact header
# is explicitly added
#
###############################################################
sub receive {
my ($packet,$leg,$from_addr) = @_;
# extract info from call-id
my $callid = $packet->callid() or do {
DEBUG( 1,"no callid in packet. DROP" );
return;
};
my ($from,$to,$dir,$random) = split( qr{\|}, $callid );
my $new_callid = join( '|',$from,$to, $dir?0:1, $random );
my ( $request,$response ) = $packet->is_response
? ( undef,$packet )
: ( $packet, undef );
if ( $response ) {
# ------------------------------------------------------------------
# Handle Responses:
# - if it has only one via (and this is myself) it is a response
# to a request which originated locally. In this case make
# the appropriate request from it and forward it to the other side
# - if it has more than one via just forward it to the other side
# ------------------------------------------------------------------
# top via must be me
my @via = $response->get_header( 'via' );
$leg->check_via($response) or do {
DEBUG( 5, "top via isn't me: $via[0]" );
return;
};
# exactly one via ?
my $cseq = $response->cseq;
my ($num,$method) = split( ' ',$cseq );
if ( @via == 1 ) {
# cancel retransmits
$disp->cancel_delivery( $response->tid );
if ( $method eq 'INVITE' && $dir == 0 ) {
# ---------------------------------------------------------
# response to initial INVITE ME->FROM
# on success create INVITE ME->TO with SDP from response
# ---------------------------------------------------------
my $code = $response->code;
if ( $code < 200 ) {
# preliminary response, ignore and don't reply
DEBUG( 10,"ignoring preliminary reply to initial invite" );
return;
} elsif ( $code >= 300 ) {
# non successful response (we don't care about redirects)
# send ACK and ignore
$disp->deliver( Request->new( 'ACK',$from, {
'call-id' => $callid,
cseq => "$num ACK",
to => scalar($response->get_header('from')),
from => scalar($response->get_header('to')),
contact => $me,
}));
} else {
# success: extract SDP and forward in INVITE to
# other party
DEBUG( 10,"got success to initial INVITE" );
my $sdp = $response->sdp_body or do {
DEBUG( 1,"no SDP in response to INVITE from $from" );
return;
};
$disp->deliver( Request->new( 'INVITE', $to,
{
from => scalar($response->get_header( 'to' )),
to => scalar($response->get_header( 'from' )),
'call-id' => $new_callid,
contact => $me,
cseq => "$num INVITE",
},
$sdp,
));
}
} elsif ( $method eq 'INVITE' && $dir == 1 ) {
# ---------------------------------------------------------
# response from $to to the initial INVITE
# on success create ACK
# ---------------------------------------------------------
my $code = $response->code;
if ( $code < 200 ) {
# preliminary response, ignore and don't reply
DEBUG( 10,"ignoring preliminary reply from TO to initial invite" );
return;
}
# create ACK to TO
$disp->deliver( Request->new( 'ACK', $to, {
from => scalar($response->get_header( 'from' )),
to => scalar($response->get_header( 'to' )),
'call-id' => $callid,
contact => $me,
cseq => "$num ACK",
}));
if ( $code >= 300 ) {
# non successful response (we don't care about redirects)
# cancel initial call [ME,FROM]
DEBUG( 10,"got code $code on INVITE 'TO'" );
$disp->deliver( Request->new( 'CANCEL',$from, {
'call-id' => $new_callid,
cseq => "$num INVITE",
from => scalar($response->get_header( 'to' )),
to => scalar($response->get_header( 'from' )),
contact => $me,
}));
} else {
DEBUG( 10,"got success on INVITE 'TO'" );
# success: extract SDP and forward in ACK to FROM
my $sdp = $response->sdp_body or do {
DEBUG( 1,"no SDP in response to INVITE from $to" );
return;
};
$disp->deliver( Request->new( 'ACK', $from,
{
from => scalar($response->get_header( 'to' )),
to => scalar($response->get_header( 'from' )),
'call-id' => $new_callid,
contact => $me,
cseq => "$num ACK",
},
$sdp,
));
}
}
} else {
# ---------------------------------------------------------
# response for forwarded request
# change call-id and forward
# ---------------------------------------------------------
# get addr from next via
my ($data) = sip_hdrval2parts( via => $via[1] );
my ($addr,$port) = $data =~m{([\w\-\.]+)(?::(\d+))?\s*$};
$port ||= 5060; # FIXME: not for sips!
$response->set_header( contact => $me );
$leg->forward_incoming( $response );
$response->set_header( 'call-id' => $new_callid );
# check if the last via header had a cseq attribute.
# in this case forward the response with the given cseq
my ($via) = $response->get_header( 'via' );
my (undef,$param) = sip_hdrval2parts( via => $via );
if ( defined( my $num = $param->{cseq} )) {
my $cseq = $response->cseq;
$cseq =~s{^(\d+)}{$num};
$response->set_header( cseq => $cseq );
}
# if this was response to BYE end this program
$stop_loop = 1 if $method eq 'BYE';
$leg->forward_outgoing( $response,$leg );
$disp->deliver( $response, leg => $leg, dst_addr => "$addr:$port" );
}
} else {
# ------------------------------------------------------------------
# Handle requests from one of the parties
# change call-id and cseq (because I have to use one of my cseqs)
# and forward
# ------------------------------------------------------------------
if ( $request->uri eq $leg->{contact} ) {
# this is for me
# could be CANCEL or BYE
my $m = $request->method;
if ( $m ne 'BYE' and $m ne 'CANCEL' ) {
DEBUG( 10,"will not forward request to me with method $m" );
return;
}
# set URI to other party
# if we were stateful we could store Contact infos from
# older packets and use them here instead.
$request->set_uri( $dir ? $from : $to );
}
my ($num,$method) = split( ' ',$request->cseq );
# we just add 20 to the cseq we got from the uac
# this is higher then every other locally generated cseq on
# this side (we only used "1" until now for the first INVITE)
$request->set_header( cseq => ( $num + 20 ).' '.$method );
$request->set_header( contact => $me );
$leg->forward_incoming( $request );
$request->set_header( 'call-id' => $new_callid );
# add cseq param to last via header because both calls maintain
# different cseq spaces and we must know with which cseq we
# need to forward the response
if ( my @via = $request->get_header( 'via' ) ) {
my ($data,$param) = sip_hdrval2parts( via => $via[0] );
$param->{cseq} = $num;
$via[0] = sip_parts2hdrval( 'via',$data,$param );
$request->set_header( via => \@via );
}
$leg->forward_outgoing( $request,$leg );
$disp->deliver( $request )
}
}
|