I slove my trouble with this PHP script.
It's not perfect, but work
The script rename channel to have unique channel name
<?
// Debug
$debug = false;
// Asterisk server
$asterisk_ip = "192.168.0.20";
$asterisk_port = 5038;
// Listen IP and port
$listen_ip = '0.0.0.0';
$listen_port = '5858';
// Start as daemon
become_daemon();
// Init some value
set_time_limit(0);
ob_implicit_flush();
// Replace channel name
$rename_chan = array( 'DAHDI/2-1' => 'DAHDM/2-1',
'DAHDI/3-1' => 'DAHDM/3-1',
'DAHDI/4-1' =>'DAHDM/4-1',
'DAHDI/5-1' => 'DAHDM/5-1',
'DAHDI/6-1' => 'DAHDM/6-1',
'DAHDI/7-1' => 'DAHDM/7-1',
'DAHDI/8-1' => 'DAHDM/8-1',
'DAHDI/9-1' => 'DAHDM/9-1',
'DAHDI/10-1' => 'DAHDM/10-1',
'DAHDI/11-1' => 'DAHDM/11-1',
'DAHDI/12-1' => 'DAHDM/12-1',
'DAHDI/13-1' => 'DAHDM/13-1',
'IAX2/' => 'IAX2M/',
'Parkinglot: default' => 'Parkinglot: defaultm',
);
// Main loop
$infinite_loop = true;
if ($debug) { $infinite_loop = false; }
do {
AddToLog("Main loop start.\n");
// Start listen
unset ($socket);
unset($conn_client);
$socket = stream_socket_server("tcp://0.0.0.0:" . $listen_port, $errno, $errstr);
if (!$socket) {
die("Unable to bind socket : $errstr ($errno) \n");
} else {
$conn_client = stream_socket_accept($socket);
stream_set_blocking($conn_client, 0);
}
// Connect to Asterisk
unset ($conn_asterisk);
$conn_asterisk = @fsockopen($asterisk_ip, $asterisk_port, $errno, $errstr, 30);
if (!$conn_asterisk) {
die("Unable to connect to Asterisk : $errstr ($errno) \n");
} else {
stream_set_blocking($conn_asterisk, 0);
}
// Start loop
$loop = true;
do {
// Read from Client
$from_Client = @fread($conn_client, 2048);
if (@feof($conn_client) || (!$conn_client)) {
// Client disconnect
$loop = false;
} elseif ( strlen($from_Client) > 0) {
// Send to Asterisk
$from_Client = RenameChannel(1,$from_Client);
fwrite($conn_asterisk, $from_Client);
if ($debug) { echo ">>> (" . strlen($from_Client) . ")" . $from_Client . "\n"; }
AddToLog(">>> (" . strlen($from_Client) . ")" . $from_Client . "\n");
}
// Read from Asterisk
$from_Asterisk = @fread($conn_asterisk, 2048);
if (@feof($conn_asterisk) || (!$conn_asterisk)) {
// Client disconnect
$loop = false;
} elseif ( strlen($from_Asterisk) > 0) {
// Send to client
$from_Asterisk = RenameChannel(0,$from_Asterisk);
fwrite($conn_client, $from_Asterisk);
if ($debug) { echo "<<< (" . strlen($from_Asterisk) . ")" . $from_Asterisk . "\n"; }
AddToLog("<<< (" . strlen($from_Asterisk) . ")" . $from_Asterisk . "\n");
}
// Little break
usleep(100);
} while($loop);
// Close everyone
// Close Asterisk
@fclose($conn_asterisk);
unset($conn_asterisk);
// Close client
@fclose($conn_client);
unset ($socket);
unset($conn_client);
// Big break before restarting
usleep(3000);
} while($infinite_loop);
AddToLog("Bye!\n");
// Rename channel
function RenameChannel($direction, $data) {
GLOBAL $rename_chan;
$r = $data;
foreach ($rename_chan as $key => $value) {
if ($direction == 0) {
$r = str_ireplace($key, $value, $r);
} else {
$r = str_ireplace($value, $key, $r);
}
}
return $r;
}
// Add to log
function AddToLog($data) {
GLOBAL $debug;
if ($debug) {
$i = file_put_contents( "/tmp/php_relay.log",
$data,
FILE_APPEND);
}
}
// Daemon
function become_daemon() {
$pid = pcntl_fork();
if ($pid == -1) {
/* fork failed */
echo "fork failure!\n";
exit();
}elseif ($pid) {
/* close the parent */
exit();
} else {
/* child becomes our daemon */
posix_setsid();
chdir('/');
umask(0);
return posix_getpid();
}
}
?>