#!/usr/bin/perl -w # # syntax: send-tcp-message.pl [] if ( @ARGV < 2 ) { print "ERROR, need at least 2 parameters. Syntax is:\n"; print " send-tcp-message.pl []\n"; print " host:port: where to send the request\n"; print " size: total size of the request, including headers and body\n"; print " id: optional, parameter used in from-tag, call-id and Via-branch\n"; print " exit the programm with CTRL-C\n"; print "\n"; print "Examples:\n"; print " send-tcp-message.pl 127.0.0.1:5060 1200\n"; print " send-tcp-message.pl 127.0.0.1:5060 100k (100*1000)\n"; print " send-tcp-message.pl 127.0.0.1:5060 10m (10*1000*1000)\n"; print " send-tcp-message.pl 127.0.0.1:5060 100K (100*1024)\n"; print " send-tcp-message.pl 127.0.0.1:5060 10M (10*1024*1024)\n"; exit(1); } # URI to be used in the SIP message (RURI, From, To) my $uri = "sip:test\@example.com"; # connect to destination use IO::Socket; my $server = $ARGV[0]; $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "$server", ); if ( !$remote) { print "ERROR: Cannot connect to $server\n"; exit(1); } # create random id for from-tag, call-id and Via-branch my $id; if(!defined $ARGV[2]) { $id = int(rand(10000000000))+1; } else { $id = $ARGV[2]; } # convert message size my $size = $ARGV[1]; if ( substr($ARGV[1],-1,1) eq "k") { $size = substr($ARGV[1],0,length($ARGV[1])-1) * 1000; } if ( substr($ARGV[1],-1,1) eq "m") { $size = substr($ARGV[1],0,length($ARGV[1])-1) * 1000*1000; } if ( substr($ARGV[1],-1,1) eq "K") { $size = substr($ARGV[1],0,length($ARGV[1])-1) * 1024; } if ( substr($ARGV[1],-1,1) eq "M") { $size = substr($ARGV[1],0,length($ARGV[1])-1) * 1024*1024; } $headers = <;tag=$id\r To: <$uri>\r Call-ID: $id\r CSeq: 1 OPTIONS\r Contact: sip:foobar\@127.0.0.1\r Content-Type: text/plain\r Content-Length: 0000000000\r Expires: 30\r Event: presence\r Accept: application/pidf+xml, application/xpidf+xml\r Supported: eventlist\r Accept: application/rlmi+xml\r Accept: multipart/related\r \r END my $headerslen = length($headers); my $bodylen = $size - $headerslen; if ($bodylen < 0) { print "ERROR: The size of the message headers ($headerslen) is already bigger then the specified message size ($size)!\n"; exit(1); } my $body = 'a' x $bodylen; my $bodylenstring = sprintf("%010d", $bodylen); $headers=~ s/0000000000/\Q$bodylenstring/; print "Sending a SIP message with $size bytes to $server\n"; print "Message headers are:\n"; print $headers; print $remote $headers; print $remote $body; print "Finished sending a SIP message with $size bytes to $server\n"; print "Waiting for a response. Press CTRL-C to exit.\n\n"; while ( <$remote> ) { print }