FreeSWITCH给指定地址发送SIP Message

SIP Message发到注册用户是很容易的,但是要发到指定的地址还是有点难度的。我查了半天mod_sofia的源码终于解决了。不多说了,直接上lua版本的示例代码:

function debug(s)
    freeswitch.consoleLog("ERR", s .. "\n")
end

local domain = "xxx.com.cn"
-- domain = freeswitch.expand("$${domain}") -- 这样似乎更好
local from = "1001"
local to = "1002"
local x_head = "1003" -- 扩展头
local body = "hello fs\r\n"
local sip_ip = "192.168.1.100"
local sip_port = "5060"
local event = freeswitch.Event("CUSTOM", "SMS::SEND_MESSAGE")

event:addHeader("proto", "sip")
event:addHeader("dest_proto", "sip")
event:addHeader("from",  from .. "@" .. domain)
event:addHeader("from_full", "sip:" .. from .. "@" .. domain)
event:addHeader("sip_h_X-head", x_head)
event:addHeader("to",  to .."@" .. domain)
event:addHeader("sip_profile", "internal") -- 改成自己的sip_profile
event:addHeader("profile", "internal")
event:addHeader("type", "text/plain")
event:addHeader("to_sip_ip", sip_ip)
event:addHeader("to_sip_port", sip_port)
event:addBody(body)
debug(event:serialize())
event:fire()

默认用udp发送,如果想用tcp发送,修改sip profile这个参数即可:

<param name="bind-params" value="transport=tcp"/>

另外,记得加载mod_sms。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
FreeSWITCH中,要将SIP中的INFO参照INVITE发送,可以通过以下步骤实现: 1. 在FreeSWITCH中创建一个SIP会话,即建立一个SIP通道。 2. 在SIP通道中发送一个INVITE请求。 3. 在SIP通道中等待对方的响应,如果对方接受了INVITE请求,则会发送一个200 OK响应。如果对方拒绝了INVITE请求,则会发送一个4xx或5xx的响应。 4. 在SIP通道中发送一个INFO请求,该请求需要参照之前发送的INVITE请求,即要使用相同的Call-ID、From、To等头部信息,并在INFO请求的Content-Type头部中指定应用程序类型和版本号。 5. 在SIP通道中等待对方对INFO请求的响应,如果对方接受了INFO请求,则会发送一个200 OK响应。如果对方拒绝了INFO请求,则会发送一个4xx或5xx的响应。 在FreeSWITCH中,可以使用mod_sofia模块来实现SIP通道的创建和INVITE请求的发送。可以使用SIP API来发送INFO请求,具体实现步骤如下: 1. 创建一个SIP会话: ``` session = sofia_glue_session_create(switch_core_session_get_pool(session), "sofia/external/username@sip.provider.com", NULL, NULL, NULL); ``` 2. 发送一个INVITE请求: ``` switch_channel_set_variable(session->channel, "sip_auto_answer", "true"); switch_channel_set_variable(session->channel, "sip_force_contact", "sip:username@sip.provider.com"); switch_channel_set_variable(session->channel, "sip_force_expires", "1800"); switch_channel_set_variable(session->channel, "sip_callee_id_number", "1000"); switch_channel_set_variable(session->channel, "sip_callee_id_name", "Alice"); switch_channel_set_variable(session->channel, "sip_caller_id_number", "2000"); switch_channel_set_variable(session->channel, "sip_caller_id_name", "Bob"); switch_channel_set_variable(session->channel, "sip_auth_username", "username"); switch_channel_set_variable(session->channel, "sip_auth_password", "password"); switch_channel_set_variable(session->channel, "sip_profile", "external"); switch_channel_set_variable(session->channel, "sip_invite_stamp", switch_channel_get_var(session->channel, "last_sent_cseq")); switch_channel_set_variable(session->channel, "sip_call_id", switch_channel_get_variable(session->channel, "sip_call_id")); switch_channel_set_variable(session->channel, "sip_full_from", switch_channel_get_variable(session->channel, "sip_full_from")); switch_channel_set_variable(session->channel, "sip_full_to", switch_channel_get_variable(session->channel, "sip_full_to")); switch_channel_set_variable(session->channel, "sip_req_uri", "sip:1000@sip.provider.com"); switch_channel_set_variable(session->channel, "sip_req_user", "1000"); switch_channel_set_variable(session->channel, "sip_req_host", "sip.provider.com"); switch_channel_set_variable(session->channel, "sip_req_port", "5060"); switch_channel_set_variable(session->channel, "sip_req_method", "INVITE"); switch_channel_set_variable(session->channel, "sip_req_user_agent", "FreeSWITCH"); switch_channel_set_variable(session->channel, "sip_req_contact", "<sip:[email protected]:5080;transport=udp>"); switch_channel_set_variable(session->channel, "sip_req_accept", "application/sdp"); switch_channel_set_variable(session->channel, "sip_req_content_type", "application/sdp"); switch_channel_set_variable(session->channel, "sip_req_content_length", "0"); switch_channel_set_variable(session->channel, "sip_req_via_protocol", "udp"); switch_channel_set_variable(session->channel, "sip_req_via_port", "5060"); switch_channel_set_variable(session->channel, "sip_req_via_host", "127.0.0.1"); switch_channel_set_variable(session->channel, "sip_req_via_branch", "z9hG4bK-1234-5678"); switch_channel_set_variable(session->channel, "sip_req_via_rport", "5060"); switch_channel_set_variable(session->channel, "sip_req_via_received", "127.0.0.1"); switch_channel_set_variable(session->channel, "sip_req_via_protocol_id", "1"); switch_channel_set_variable(session->channel, "sip_req_via_secure", "false"); switch_channel_set_variable(session->channel, "sip_req_via_transport", "udp"); switch_channel_set_variable(session->channel, "sip_req_user_info", "<sip:[email protected]>"); switch_channel_set_variable(session->channel, "sip_req_uri_user", "1000"); switch_channel_set_variable(session->channel, "sip_req_uri_host", "sip.provider.com"); switch_core_session_receive_message(session, "mod_sofia", "sip", "INVITE"); ``` 3. 等待对方的响应: ``` while (switch_channel_up(session->channel) && !switch_channel_test_flag(session->channel, CF_BRIDGE)) { switch_yield(1000000); } ``` 4. 发送一个INFO请求: ``` switch_channel_set_variable(session->channel, "sip_req_method", "INFO"); switch_channel_set_variable(session->channel, "sip_req_content_type", "application/vnd.3gpp.sms"); switch_channel_set_variable(session->channel, "sip_req_content_length", "10"); switch_channel_set_variable(session->channel, "sip_req_accept", "application/sdp"); switch_channel_set_variable(session->channel, "sip_req_contact", "<sip:[email protected]:5080;transport=udp>"); switch_channel_set_variable(session->channel, "sip_req_uri", "sip:1000@sip.provider.com"); switch_channel_set_variable(session->channel, "sip_req_user", "1000"); switch_channel_set_variable(session->channel, "sip_req_host", "sip.provider.com"); switch_channel_set_variable(session->channel, "sip_req_port", "5060"); switch_channel_set_variable(session->channel, "sip_req_user_info", "<sip:[email protected]>"); switch_core_session_receive_message(session, "mod_sofia", "sip", "INFO"); ``` 5. 等待对方对INFO请求的响应: ``` while (switch_channel_up(session->channel) && !switch_channel_test_flag(session->channel, CF_BRIDGE)) { switch_yield(1000000); } ``` 通过以上步骤,就可以在FreeSWITCH中将SIP中的INFO参照INVITE发送。需要注意的是,在发送INFO请求时,需要参照之前发送的INVITE请求,并且在Content-Type头部中指定应用程序类型和版本号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值