first commit
This commit is contained in:
155
GB28181Device/sip/sip_msg.cpp
Normal file
155
GB28181Device/sip/sip_msg.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
/***************************************************************************************
|
||||
*
|
||||
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
*
|
||||
* By downloading, copying, installing or using the software you agree to this license.
|
||||
* If you do not agree to this license, do not download, install,
|
||||
* copy or use the software.
|
||||
*
|
||||
* Copyright (C) 2014-2022, Happytimesoft Corporation, all rights reserved.
|
||||
*
|
||||
* Redistribution and use in binary forms, with or without modification, are permitted.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the License.
|
||||
*
|
||||
****************************************************************************************/
|
||||
|
||||
#include "sys_inc.h"
|
||||
#include "hsip.h"
|
||||
#include "sua.h"
|
||||
#include "sip_parse.h"
|
||||
#include "sip_rx.h"
|
||||
#include "sip_tx.h"
|
||||
#include "util.h"
|
||||
#include "sip_trans.h"
|
||||
#include "sip_msg.h"
|
||||
|
||||
/***************************************************************************************/
|
||||
extern HSIP_CLASS hsip;
|
||||
extern HSIP_USER g_user;
|
||||
|
||||
/***************************************************************************************/
|
||||
BOOL sip_msg_tx_internal(STRANSN * p_trans, const char * p_ctx, const char * p_msg)
|
||||
{
|
||||
int len = strlen(p_msg);
|
||||
HSIP_USER * p_user = &g_user;
|
||||
HSIP_MSG * tx_msg = sip_get_msg_large_buf(len + 1024);
|
||||
if (NULL == tx_msg)
|
||||
{
|
||||
log_print(HT_LOG_ERR, "%s, get message buffer failed\r\n", __FUNCTION__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
tx_msg->msg_type = 0;
|
||||
tx_msg->msg_sub_type = SIP_MT_MSG;
|
||||
tx_msg->msg_crpty_mode = p_user->user_crpty_mode;
|
||||
tx_msg->local_port = p_user->sip_port;
|
||||
|
||||
sip_add_tx_msg_fline(tx_msg, "MESSAGE", "%s SIP/2.0", p_user->server_sip_addr);
|
||||
|
||||
if (p_user->usrf_tcp_sip == 1)
|
||||
{
|
||||
sip_add_tx_msg_via(tx_msg, "SIP/2.0/TCP %s:%u", p_user->user_ip, p_user->user_port);
|
||||
}
|
||||
else
|
||||
{
|
||||
sip_add_tx_msg_via(tx_msg, "SIP/2.0/UDP %s:%u", p_user->user_ip, p_user->user_port);
|
||||
}
|
||||
|
||||
sip_add_tx_msg_line(tx_msg, "From", "<%s>;tag=%08x", p_user->user_sip_addr, rand());
|
||||
sip_add_tx_msg_line(tx_msg, "To", "<%s>", p_user->server_sip_addr);
|
||||
sip_add_tx_msg_line(tx_msg, "Call-ID", "%08X%08X@%s", rand(), rand(), p_user->user_ip);
|
||||
sip_add_tx_msg_line(tx_msg, "CSeq", "%d MESSAGE", ++p_user->message_cseq);
|
||||
sip_add_tx_msg_line(tx_msg, "Max-Forwards", "70");
|
||||
sip_add_tx_msg_line(tx_msg, "User-Agent", p_user->user_agent_desc);
|
||||
sip_add_tx_msg_line(tx_msg, "Content-Type", "%s", p_ctx);
|
||||
sip_add_tx_msg_line(tx_msg, "Content-Length", "%d", len+2);
|
||||
|
||||
sip_add_tx_msg_sdp_line(tx_msg, "", "%s", p_msg);
|
||||
|
||||
tx_msg->remote_ip = inet_addr(p_user->server_ipstr);
|
||||
tx_msg->remote_port = htons(p_user->server_port);
|
||||
|
||||
// add to trans lists, wait for response
|
||||
sip_trans_set_msg(p_trans, tx_msg);
|
||||
sip_trans_hash_add(&(hsip.sip_trans), p_trans);
|
||||
|
||||
user_tx_free_msg(p_user, tx_msg);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/***************************************************************************************
|
||||
*
|
||||
* return the response status code, 200,400..., 0 means timeout, -1 means error
|
||||
*
|
||||
****************************************************************************************/
|
||||
int sip_message_tx(const char * p_ctx, const char * p_msg, int timeout)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
// sip trans
|
||||
STRANSN * p_trans = sip_trans_get_idle(&(hsip.sip_trans), TRUE);
|
||||
if (p_trans == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sip_msg_tx_internal(p_trans, p_ctx, p_msg))
|
||||
{
|
||||
if (sip_trans_wait(p_trans, timeout) == 0)
|
||||
{
|
||||
ret = p_trans->rly_status;
|
||||
}
|
||||
}
|
||||
|
||||
sip_trans_free_used(&(hsip.sip_trans), p_trans);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
BOOL sip_message_rly_rx(HSIP_MSG * rx_msg)
|
||||
{
|
||||
char from[32], to[32], callid[128], cseq[16];
|
||||
|
||||
if (sip_get_user_name(rx_msg, "From", from, sizeof(from)) == FALSE)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (sip_get_user_name(rx_msg, "To", to, sizeof(to)) == FALSE)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (sip_get_msg_cseq(rx_msg, cseq, sizeof(cseq)) == FALSE)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (sip_get_msg_call_id(rx_msg, callid, sizeof(callid)) == FALSE)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
STRANSN * p_trans = sip_trans_rly_find(&(hsip.sip_trans), callid, from, to, atoi(cseq));
|
||||
if (p_trans)
|
||||
{
|
||||
p_trans->rly_status = rx_msg->msg_sub_type;
|
||||
|
||||
if (p_trans->p_wait_sig)
|
||||
{
|
||||
sys_os_sig_sign(p_trans->p_wait_sig);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user