-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSIP.php
More file actions
173 lines (155 loc) · 4.55 KB
/
SIP.php
File metadata and controls
173 lines (155 loc) · 4.55 KB
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
<?php
include_once(dirname(__FILE__) . '/net/UdpLink.php');
include_once(dirname(__FILE__) . '/net/SipLink.php');
include_once(dirname(__FILE__) . '/net/SipMessage.php');
include_once(dirname(__FILE__) . '/net/RtpLink.php');
include_once(dirname(__FILE__) . '/net/RtpMessage.php');
include_once(dirname(__FILE__) . '/net/RtpExchange.php');
include_once(dirname(__FILE__) . '/SipEngine.php');
include_once(dirname(__FILE__) . '/SipUri.php');
include_once(dirname(__FILE__) . '/SipContact.php');
include_once(dirname(__FILE__) . '/SipModule.php');
include_once(dirname(__FILE__) . '/SipSession.php');
include_once(dirname(__FILE__) . '/SipRouter.php');
include_once(dirname(__FILE__) . '/session/SipTransaction.php');
include_once(dirname(__FILE__) . '/session/RegisterSession.php');
include_once(dirname(__FILE__) . '/session/RegistrarSession.php');
include_once(dirname(__FILE__) . '/session/BaseCallSession.php');
include_once(dirname(__FILE__) . '/session/CalleeSession.php');
include_once(dirname(__FILE__) . '/session/CallerSession.php');
include_once(dirname(__FILE__) . '/session/LocalCaller.php');
include_once(dirname(__FILE__) . '/session/LocalCallee.php');
include_once(dirname(__FILE__) . '/session/NoopCallerSession.php');
include_once(dirname(__FILE__) . '/module/SipDialog.php');
include_once(dirname(__FILE__) . '/module/SipMixer.php');
include_once(dirname(__FILE__) . '/module/SipChannel.php');
include_once(dirname(__FILE__) . '/module/SipRegistrar.php');
include_once(dirname(__FILE__) . '/module/SipRobotModule.php');
class SIP
{
// role
const NOOP = 0;
const REGISTER = 1;
const REGISTRAR = 2;
const CALLER = 3;
const CALLEE = 4;
const CLOSER = 5;
// state
const NONE = 0;
const CLOSED = -1;
const CLOSING = 1;
const TRYING = 100;
const RINGING = 180;
const COMPLETING = 199;
const COMPLETED = 200;
const AUTHING = 401;
private static $call_id_prefix = 'c';
private static $tag_prefix = 't';
private static $branch_prefix = 'z9hG4bK_';
static function state_text($state){
if($state == self::NONE){
return 'NONE';
}else if($state == self::CLOSING){
return 'CLOSING';
}else if($state == self::CLOSED){
return 'CLOSED';
}else if($state == self::TRYING){
return 'TRYING';
}else if($state == self::RINGING){
return 'RINGING';
}else if($state == self::COMPLETING){
return 'COMPLETING';
}else if($state == self::COMPLETED){
return 'COMPLETED';
}else if($state == self::AUTHING){
return 'AUTHING';
}else{
return 'UNKNOWN';
}
}
static function token(){
static $seq = -1;
if($seq == -1){
$seq = mt_rand();
}
$seq = ($seq + 1) % 1000;
$num = sprintf('%03d', $seq);
$time = substr(sprintf('%.3f', microtime(1)), 2);
return $time.'_'.$num;
}
static function long_token(){
return md5(mt_rand() . microtime(1));
}
static function new_call_id(){
return self::$call_id_prefix . self::token();
}
static function new_branch(){
return self::$branch_prefix . self::token();
}
static function new_tag(){
return self::$tag_prefix . self::token();
}
static function new_cseq(){
return mt_rand(100, 1000);
}
static function encode_www_auth($auth){
$scheme = $auth['scheme'];
$arr = array();
foreach($auth as $k=>$v){
if($k != 'scheme'){
$arr[] = "$k=\"$v\"";
}
}
return "$scheme " . join(', ', $arr);
}
static function decode_www_auth($str){
$ret = array(
'scheme' => '',
);
$kvs = explode(',', $str);
foreach($kvs as $n=>$kv){
$kv = trim($kv);
if($n == 0){
$ps = explode(' ', $kv, 2);
$ret['scheme'] = $ps[0];
$kv = $ps[1];
}
list($k, $v) = explode('=', $kv);
$v = trim($v, '"');
$ret[$k] = $v;
}
return $ret;
}
static function www_auth($username, $password, $uri, $method, $auth){
$scheme = $auth['scheme'];
$realm = $auth['realm'];
$nonce = $auth['nonce'];
if($scheme == 'Digest'){
$ha1 = md5($username .':'. $realm .':'. $password);
$ha2 = md5($method .':'. $uri);
if(isset($auth['qpop']) && $auth['qpop'] == 'auth'){
//MD5(HA1:nonce:nonceCount:cnonce:qop:HA2)
}else{
$res = md5($ha1 .':'. $nonce .':'. $ha2);
}
$auth['uri'] = $uri;
$auth['username'] = $username;
$auth['response'] = $res;
return $auth;
}
return array();
}
static function guess_local_ip($remote_ip){
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if(!@socket_connect($sock, $remote_ip, 8888)){
socket_close($sock);
return false;
}
if(!socket_getsockname($sock, $ip, $port)){
socket_close($sock);
return false;
}
socket_close($sock);
return $ip;
}
}