【问题标题】:How send a message using SIP/SDP protocol in php [closed]如何在php中使用SIP/SDP协议发送消息[关闭]
【发布时间】:2013-03-08 05:14:57
【问题描述】:

我要做的是实现一个 SIP 客户端,它监听 SIP 消息。好的,所以我在服务器 192.168.0.246 上运行了 SIP 客户端,并且 SIP 服务器在 192.168.2.40 上运行。现在看看在下面的屏幕截图中。

它是在192.168.0.246 上运行客户端代码的服务器的跟踪文件。如您所见,服务器使用 SIP/SDP 协议从192.168.2.40 接收消息,但是当在 192.168.0.246 上运行的客户端程序使用 UDP 协议将消息发送回 192.168.2.40 时,它显示为 UDP 协议,这是正确的。但没有响应在此之后从 192.168.2.40 开始。 所以我假设它与显示为UDP的协议有关。所以如果我有礼貌,我应该把它交给 SIP/SDP。

所以我的问题是如何将此 UDP 更改为 SIP/SDP。

这是我的 php 代码:

<?php

//Reduce errors
error_reporting(~E_WARNING);

//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";

// Bind the source address
if( !socket_bind($sock, "192.168.0.246" , 5060) )
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not bind socket : [$errorcode] $errormsg \n");
}

echo "Socket bind OK \n";

//Do some communication, this loop can handle multiple clients

function GetBranchValue($message)
{
    $data = "";
    if(preg_match('/branch=.*/i', $message, $output))
        $data = explode("=",$output[0]);
    if(sizeOf($data)>1)
        return $data[1];
    else
        return "None";
}

function GetTag($message)
{
    $data = "";
    if(preg_match('/tag=.*/i',$message, $output))
        $data = explode("=", $output[0]);
    if(sizeOf($data)>1)
        return $data[1];
    else
        return "None";
}

function GetCallId($message)
{
    $data = "";
    if(preg_match('/Call-ID:.*/i', $message, $output))
        $data = explode(":",$output[0]);
    if(sizeOf($data)>1)
        return $data[1];
    else
        return "None";
}

function GetCSeq($message)
{ 
    $data = "";
    if(preg_match('/CSeq:.*/i', $message, $output))
    {
        $data = explode(":", $output[0]);
        $data = explode(" ",$data[1]);
    }
    if(sizeOf($data[1])>0)
        return $data[1];
    else
        return "None";
}

function CreateResponse($message)
{
    $msg = "SIP/2.0 302 Moved temporarily
Via:SIP/2.0/UDP 192.168.2.40:5060;branch=".GetBranchValue($message)."
From: <sip:+12012030008@192.168.2.40:5060>;tag=".GetTag($message)."
To:<sip:+17066458407@192.168.0.246:5060;user=phone>;tag=883069368-1363286882583
Call-ID:".GetCallId($message)."
CSeq:".GetCSeq($message)." INVITE
Contact:<sip:+17066458407@192.168.0.246:5060;user=phone>;q=0.5,<sip:+17066458407@192.168.0.246:5060;user=phone>;q=0.25
Content-Length:0";
    return $msg;
}

function Create300Response($message)
{
    $msg = "SIP/2.0 300 Multiple Choices
Via: SIP/2.0/UDP 192.168.2.40:5060;branch=".GetBranchValue($message)."
From: <sip:+12012030008@192.168.2.40:5060>;tag=".GetTag($message).";isup-oli;isup-oli=00
To:<sip:+17066458407@192.168.0.246:5060;user=phone>;tag=123
Contact: <sip: +17066458407@192.168.0.246:5060;dtg=16>
Contact: <sip: +17066458407@192.168.0.246:5060;dtg=16>
Contact: <sip: +17066458407@192.168.0.246:5060;dtg=16>
Contact: <sip: +17066458407@192.168.0.246:5060;dtg=16>";
    return $msg;
}

while(1)
{
    echo "Waiting for data ... \n";

    //Receive some data
    $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
    echo "$remote_ip : $remote_port -- " . $buf;
    file_put_contents("Log.txt","\n",FILE_APPEND);
    file_put_contents("Log.txt","Received Response------\n",FILE_APPEND);
    file_put_contents("Log.txt",$buf,FILE_APPEND);
    $respMessage = Create300Response($buf);
    //Send back the data to the client
    socket_sendto($sock, "OK " . $respMessage , 100 , 0 , $remote_ip , $remote_port);
    file_put_contents("Log.txt","\n",FILE_APPEND);
    file_put_contents("Log.txt","\n",FILE_APPEND);
    file_put_contents("Log.txt",$respMessage,FILE_APPEND);
}

socket_close($sock);

【问题讨论】:

    标签: php udp sip sdp


    【解决方案1】:
    socket_sendto($sock, "OK " . $respMessage , 100 , 0 , $remote_ip , $remote_port);
    //                    ^^ remove this
    

    您发送的OK 无效,它违反了 SIP 协议,因此 Wireshark 中的内置 SIP 解码器无法将该消息识别为有效的 SIP 数据包。

    你应该只回复$respMessage

    此外,我强烈建议您使用适当的解析器来处理传入消息,并使用适当的面向对象编写器来构造传出消息。 SIP 是一个(有些人可能会说是不必要的)复杂协议,您需要的不仅仅是提取的小块信息来构建一个端点,它可以做任何甚至远程有用的事情。

    This small library 我的This small library 可能会为您的解析器奠定良好的基础,如果您将HTTP 替换为SIP,实际上this one 几乎完全符合您的要求。

    【讨论】:

    • 好吧 thnks dat 成功了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2011-12-28
    • 2022-10-14
    相关资源
    最近更新 更多