-
Notifications
You must be signed in to change notification settings - Fork 2
/
socketconn.php
195 lines (164 loc) · 5.03 KB
/
socketconn.php
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
<?php
/**
* socketconn.php
*
* Copyright (C) 2008-2023 Null Team
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribution.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
?>
<?php
require_once("debug.php");
/*
* Class used to open a socket, send and receive information from it
* After connecting through the socket the header information send by Yate is ignored.
* The authentication is done only if param $rmanager_pass is set
*/
class SocketConn
{
public $socket;
public $error = "";
public $mode;
/*
* @param $mode String. Possible values:
* -> "write_close" - will write command and close the socket
* -> "write_read" - write a command and read the response - this is the default mode that is set in the constructor
* -> "write_read_close" - write a command, wait for the response and then close the socket
* If multiple_write_read is needed the default mode can be used, by calling the method command() with the commands needed,
* after that be carreful to close the socket.
*/
function __construct($ip = null, $port = null, $mode="write_read")
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");
global $default_ip, $default_port, $rmanager_pass, $socket_timeout;
global $default_tries;
$this->mode = $mode;
$protocol_list = stream_get_transports();
if (!isset($socket_timeout))
$socket_timeout = 5;
$default_tries = $socket_timeout*100;
if (!$ip)
$ip = $default_ip;
if (!$port)
$port = $default_port;
if (substr($ip,0,4)=="ssl:" && !in_array("ssl", $protocol_list))
die("Don't have ssl support.");
$errno = 0;
$socket = fsockopen($ip,$port,$errno,$errstr,$socket_timeout);
if (!$socket) {
$this->error = "Can't connect:[$errno] ".$errstr;
$this->socket = false;
} else {
$this->socket = $socket;
stream_set_blocking($this->socket,false);
stream_set_timeout($this->socket,$socket_timeout);
$line1 = $this->read(); // read and ignore header
if (isset($rmanager_pass) && strlen($rmanager_pass)) {
$res = $this->command("auth $rmanager_pass");
if (substr($res,0,30)!="Authenticated successfully as ") {
fclose($socket);
$this->socket = false;
$this->error = "Can't authenticate: ".$res;
}
}
}
}
/**
* Write command through the socket
* close the socket
*/
private function write_close($command)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");
$this->write($command);
$this->close();
}
/*
* Write commmand through the socket
* Read from the socket the answer
* Close the socket
*/
private function write_read_close($command, $marker_end, $limited_tries)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");
$this->write($command);
$response = $this->read($marker_end,$limited_tries);
$this->close();
return $response;
}
function write($str)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");
fwrite($this->socket, $str."\r\n");
}
/*
* @param $marker_end set to null to not limit the reading
*/
function read($marker_end = "\r\n",$limited_tries=false)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");
global $default_tries;
if (!$limited_tries)
$limited_tries = $default_tries;
$keep_trying = true;
$line = "";
$i = 0;
//print "<br/>limited_tries=$limited_tries: ";
while ($keep_trying) {
$line .= fgets($this->socket,8192);
if ($line === false)
continue;
usleep(15000); // sleep 15 miliseconds
if (substr($line, -strlen($marker_end)) == $marker_end)
$keep_trying = false;
$i++;
if ($limited_tries && $limited_tries<=$i)
$keep_trying = false;
if ($i>1500) { // don't try to read for more than 15 seconds
//print "<br/>------force stop<br/>";
$keep_trying = false;
}
}
if ($marker_end != null)
$line = str_replace($marker_end, "", $line);
return $line;
}
function close()
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");
fclose($this->socket);
}
/**
Commands
status
uptime
reload
restart
stop
.... -> will be mapped into an engine.command
*/
function command($command, $marker_end = "\r\n", $limited_tries=false)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");
if ($this->mode == "write_read") {
// if after sending command to yate,
// the page seems to stall it might be because the generated message has not handled or retval was not set
$this->write($command);
return $this->read($marker_end,$limited_tries);
} elseif ($this->mode == "write_close") {
$this->write_close($command);
} elseif ($this->mode == "write_read_close") {
return $this->write_read_close($command, $marker_end, $limited_tries);
}
}
}
?>