2
0

snom-pnpd.pl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/perl
  2. #
  3. # Snom PNP Daemon that can provide URL for provisioning.
  4. #
  5. # Authors: (and wish lists)
  6. #
  7. # Brian West <brian@freeswitch.org> http://www.amazon.com/gp/registry/wishlist/1BWDJUX5LYQE0
  8. # Raymond Chandler <intralanman@freeswitch.org> http://www.amazon.com/gp/registry/wishlist/27XDISBBI4NOU
  9. #
  10. #
  11. use Data::Dumper;
  12. use Net::SIP;
  13. use IO::Socket::Multicast;
  14. use Getopt::Std;
  15. my $count = 0;
  16. getopt("dui");
  17. $| = 1;
  18. if (!$opt_u && !$opt_i) {
  19. print "Usage: $0 -i <ipaddress> -u <url> [-d 1]\n";
  20. exit;
  21. }
  22. my $local_addr = $opt_i;
  23. my $local_port = '8160';
  24. sub reply($;) {
  25. my ($body) = shift;
  26. if($opt_d) {
  27. print Dumper $body;
  28. }
  29. if($body =~ m/^SUBSCRIBE/i) {
  30. my $pkt = Net::SIP::Request->new( $body );
  31. my $resp = $pkt->create_response(200, "OK");
  32. my $contact = $pkt->get_header('contact');
  33. $contact =~ s/<sip:(.*)>/$1/i;
  34. my $leg = Net::SIP::Leg->new(
  35. addr => $local_addr,
  36. port => $local_port,
  37. );
  38. $leg->deliver( $resp, "$contact" );
  39. my $hash = {};
  40. my @version = split(";", $pkt->get_header('event'));
  41. foreach my $blah (@version) {
  42. if($blah =~ /=/) {
  43. my($var,$val) = split(/=/,$blah);
  44. $val =~ s/\"//g;
  45. $hash->{$var} = $val;
  46. }
  47. }
  48. my $prov_url = "$opt_u/{mac}.xml";
  49. print "Sending pnp provisioning URL as $opt_u/{mac}.xml\n";
  50. $notify = Net::SIP::Request->new('NOTIFY', $contact, {});
  51. $notify->set_header('From' => $pkt->get_header('to'));
  52. $notify->set_header('To' => $pkt->get_header('to'));
  53. $notify->set_header('User-Agent' => 'test');
  54. $notify->set_header('Event' => $pkt->get_header('event'));
  55. $notify->set_header('Contact' => "<sip:$local_host:$local_port>");
  56. $notify->set_header('Call-ID' => rand());
  57. $notify->set_header('CSeq' => '1 NOTIFY');
  58. $notify->set_body("$prov_url");
  59. $leg->deliver($notify, $contact);
  60. }
  61. }
  62. my $socket = IO::Socket::Multicast->new(
  63. LocalPort => '5060',
  64. LocalAddr => '224.0.1.75',
  65. Proto => 'udp',
  66. ReuseAddr => 1
  67. );
  68. $socket->mcast_add('224.0.1.75');
  69. while($socket->recv($data,8192)) {
  70. &reply($data);
  71. }