Skip to content

Commit 63a3d5c

Browse files
committed
modify:dtls handshakes supported
modify:dtls and stun and srtp packet judgment handle
1 parent 4fc2535 commit 63a3d5c

File tree

6 files changed

+77
-36
lines changed

6 files changed

+77
-36
lines changed

XEngine_Source/XEngine_ModuleConfigure/ModuleConfig_Define.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ typedef struct tag_XEngine_ServiceConfig
9090
}st_PullRtsp;
9191
struct
9292
{
93-
XCHAR tszPrivateKey[MAX_PATH];
94-
XCHAR tszPublicKey[MAX_PATH];
95-
XCHAR tszRequestKey[MAX_PATH];
93+
XCHAR tszCertStr[MAX_PATH];
94+
XCHAR tszKeyStr[MAX_PATH];
95+
XCHAR tszCsrStr[MAX_PATH];
9696
bool bEnable;
9797
}st_PullWebRtc;
9898
}st_XPull;

XEngine_Source/XEngine_ModuleConfigure/ModuleConfigure_Json/ModuleConfigure_Json.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ bool CModuleConfigure_Json::ModuleConfigure_Json_File(LPCXSTR lpszConfigFile, XE
155155
pSt_ServerConfig->st_XPull.st_PullHls.nTime = st_PullHls["nTime"].asInt();
156156
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullHls.tszHLSPath, st_PullHls["tszHLSPath"].asCString());
157157

158-
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszPrivateKey, st_PullWebRtc["RSAPrivateKey"].asCString());
159-
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszPublicKey, st_PullWebRtc["RSAPublicKey"].asCString());
160-
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszRequestKey, st_PullWebRtc["RSARequestKey"].asCString());
158+
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszCertStr, st_PullWebRtc["tszCertStr"].asCString());
159+
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszKeyStr, st_PullWebRtc["tszKeyStr"].asCString());
160+
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszCsrStr, st_PullWebRtc["tszCsrStr"].asCString());
161161
//日志配置
162162
if (st_JsonRoot["XLog"].empty() || (3 != st_JsonRoot["XLog"].size()))
163163
{

XEngine_Source/XEngine_ServiceApp/XEngine_StreamMediaApp/StreamMedia_PullStream/PullStream_ClientWebRtc.cpp

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,83 @@
1010
// Purpose: WEBRTC拉流服务
1111
// History:
1212
*********************************************************************/
13-
bool PullStream_ClientProtocol_Handle(LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, int nMsgLen)
13+
int PullStream_ClientProtocol_Dtls(LPCXSTR lpszMSGBuffer, int nMSGLen)
1414
{
15-
int nAttrCount = 0;
16-
RFCCOMPONENTS_NATATTR** ppSt_ListAttr;
17-
RFCCOMPONENTS_NATSTUN st_NatClient = {};
18-
19-
if (!NatProtocol_StunNat_Parse(lpszMsgBuffer, nMsgLen, &st_NatClient, &ppSt_ListAttr, &nAttrCount))
15+
// DTLS有可能以多种不同的记录层类型开头,这里检查它是否是handshake(0x16)
16+
return nMSGLen >= 13 && lpszMSGBuffer[0] == 0x16;
17+
}
18+
int PullStream_ClientProtocol_Stun(LPCXSTR lpszMSGBuffer, int nMSGLen)
19+
{
20+
// STUN消息的类型字段(前两位为00)以及魔术cookie字段
21+
return nMSGLen >= 20 && (lpszMSGBuffer[0] & 0xC0) == 0x00 && lpszMSGBuffer[4] == 0x21 && lpszMSGBuffer[5] == 0x12 && lpszMSGBuffer[6] == 0xA4 && lpszMSGBuffer[7] == 0x42;
22+
}
23+
bool PullStream_ClientProtocol_Handle(LPCXSTR lpszClientAddr, XSOCKET hSocket, LPCXSTR lpszMsgBuffer, int nMsgLen)
24+
{
25+
if (PullStream_ClientProtocol_Dtls(lpszMsgBuffer, nMsgLen))
2026
{
21-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("STUN客户端:%s,请求的STUN协议不正确,解析失败,错误:%lX"), lpszClientAddr, NatProtocol_GetLastError());
22-
return false;
27+
int nSDLen = 2048;
28+
XCHAR tszSDBuffer[2048] = {};
29+
XBYTE tszSDKey[128] = {};
30+
XBYTE tszRVKey[128] = {};
31+
32+
if (OPenSsl_Server_AcceptMemoryEx(xhRTCSsl, hSocket, lpszClientAddr, tszSDBuffer, &nSDLen, lpszMsgBuffer, nMsgLen))
33+
{
34+
OPenSsl_Server_GetKeyEx(xhRTCSsl, lpszClientAddr, tszSDKey, tszRVKey);
35+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _X("RTC客户端:%s,请求的DTLS握手协议处理成功"), lpszClientAddr);
36+
}
37+
else
38+
{
39+
int nPort = 0;
40+
XCHAR tszIPPort[128] = {};
41+
_tcsxcpy(tszIPPort, lpszClientAddr);
42+
BaseLib_OperatorIPAddr_SegAddr(tszIPPort, &nPort);
43+
NetCore_UDPSelect_Send(xhRTCSocket, tszSDBuffer, nSDLen, tszIPPort, nPort);
44+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _X("RTC客户端:%s,请求的DTLS握手协议,还需要进一步处理,响应大小:%d"), lpszClientAddr, nSDLen);
45+
}
2346
}
24-
XCHAR tszUserStr[128] = {};
25-
for (int i = 0; i < nAttrCount; i++)
47+
else if (PullStream_ClientProtocol_Stun(lpszMsgBuffer, nMsgLen))
2648
{
27-
if (RFCCOMPONENTS_NATCLIENT_PROTOCOL_STUN_ATTR_USERNAME == ppSt_ListAttr[i]->wAttr)
49+
int nAttrCount = 0;
50+
RFCCOMPONENTS_NATATTR** ppSt_ListAttr;
51+
RFCCOMPONENTS_NATSTUN st_NatClient = {};
52+
53+
if (!NatProtocol_StunNat_Parse(lpszMsgBuffer, nMsgLen, &st_NatClient, &ppSt_ListAttr, &nAttrCount))
2854
{
29-
memcpy(tszUserStr, ppSt_ListAttr[i]->tszMsgBuffer, ppSt_ListAttr[i]->wLen);
55+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("STUN客户端:%s,请求的STUN协议不正确,解析失败,错误:%lX"), lpszClientAddr, NatProtocol_GetLastError());
56+
return false;
3057
}
31-
}
32-
int nTMPLen = 0;
33-
int nMSGLen = 0;
34-
int nIPPort = 0;
35-
XCHAR tszTMPBuffer[1024] = {};
36-
XCHAR tszMSGBuffer[1024] = {};
37-
XCHAR tszIPAddr[128] = {};
58+
XCHAR tszUserStr[128] = {};
59+
for (int i = 0; i < nAttrCount; i++)
60+
{
61+
if (RFCCOMPONENTS_NATCLIENT_PROTOCOL_STUN_ATTR_USERNAME == ppSt_ListAttr[i]->wAttr)
62+
{
63+
memcpy(tszUserStr, ppSt_ListAttr[i]->tszMsgBuffer, ppSt_ListAttr[i]->wLen);
64+
}
65+
}
66+
int nTMPLen = 0;
67+
int nMSGLen = 0;
68+
int nIPPort = 0;
69+
XCHAR tszTMPBuffer[1024] = {};
70+
XCHAR tszMSGBuffer[1024] = {};
71+
XCHAR tszIPAddr[128] = {};
3872

39-
_tcsxcpy(tszIPAddr, lpszClientAddr);
73+
_tcsxcpy(tszIPAddr, lpszClientAddr);
4074

41-
BaseLib_OperatorIPAddr_SegAddr(tszIPAddr, &nIPPort);
75+
BaseLib_OperatorIPAddr_SegAddr(tszIPAddr, &nIPPort);
4276

43-
NatProtocol_StunNat_BuildAttr(tszTMPBuffer, &nTMPLen, RFCCOMPONENTS_NATCLIENT_PROTOCOL_STUN_ATTR_USERNAME, tszUserStr, _tcsxlen(tszUserStr));
44-
NatProtocol_StunNat_BuildMapAddress(tszTMPBuffer + nTMPLen, &nTMPLen, tszIPAddr, nIPPort, true);
45-
//NatProtocol_StunNat_BuildMSGIntegrity(tszMSGBuffer, &nMSGLen, tszTMPBuffer, nTMPLen, );
46-
NatProtocol_StunNat_Packet(tszMSGBuffer, &nMSGLen, (LPCXSTR)st_NatClient.byTokenStr, RFCCOMPONENTS_NATCLIENT_PROTOCOL_STUN_CLASS_FLAGS, RFCCOMPONENTS_NATCLIENT_PROTOCOL_STUN_ATTR_MAPPED_ADDRESS);
77+
NatProtocol_StunNat_BuildAttr(tszTMPBuffer, &nTMPLen, RFCCOMPONENTS_NATCLIENT_PROTOCOL_STUN_ATTR_USERNAME, tszUserStr, _tcsxlen(tszUserStr));
78+
NatProtocol_StunNat_BuildMapAddress(tszTMPBuffer + nTMPLen, &nTMPLen, tszIPAddr, nIPPort, true);
79+
//NatProtocol_StunNat_BuildMSGIntegrity(tszMSGBuffer, &nMSGLen, tszTMPBuffer, nTMPLen, );
80+
NatProtocol_StunNat_Packet(tszMSGBuffer, &nMSGLen, (LPCXSTR)st_NatClient.byTokenStr, RFCCOMPONENTS_NATCLIENT_PROTOCOL_STUN_CLASS_FLAGS, RFCCOMPONENTS_NATCLIENT_PROTOCOL_STUN_ATTR_MAPPED_ADDRESS);
4781

48-
BaseLib_OperatorMemory_Free((XPPPMEM)&ppSt_ListAttr, nAttrCount);
82+
BaseLib_OperatorMemory_Free((XPPPMEM)&ppSt_ListAttr, nAttrCount);
83+
}
84+
else
85+
{
86+
87+
}
88+
89+
4990
return true;
5091
}
5192
bool PullStream_ClientWebRtc_Handle(RFCCOMPONENTS_HTTP_REQPARAM* pSt_HTTPParam, LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, int nMsgLen)
@@ -137,7 +178,7 @@ bool PullStream_ClientWebRtc_Handle(RFCCOMPONENTS_HTTP_REQPARAM* pSt_HTTPParam,
137178
XBYTE tszDigestStr[MAX_PATH] = {};
138179
XCHAR tszDigestHex[MAX_PATH] = {};
139180
int nPos = _xstprintf(tszDigestHex, _X("sha-256 "));
140-
OPenSsl_Api_Digest(st_ServiceConfig.st_XPull.st_PullWebRtc.tszRequestKey, tszDigestStr, &nDLen, true, XENGINE_OPENSSL_API_DIGEST_SHA256);
181+
OPenSsl_Api_Digest(st_ServiceConfig.st_XPull.st_PullWebRtc.tszCsrStr, tszDigestStr, &nDLen, true, XENGINE_OPENSSL_API_DIGEST_SHA256);
141182
for (int i = 0; i < nDLen; i++)
142183
{
143184
int nRet = _xstprintf(tszDigestHex + nPos, _X("%02X"), tszDigestStr[i]);

XEngine_Source/XEngine_ServiceApp/XEngine_StreamMediaApp/StreamMedia_PullStream/PullStream_ClientWebRtc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
// Purpose: WEBRTC拉流服务
1111
// History:
1212
*********************************************************************/
13-
bool PullStream_ClientProtocol_Handle(LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, int nMsgLen);
13+
bool PullStream_ClientProtocol_Handle(LPCXSTR lpszClientAddr, XSOCKET hSocket, LPCXSTR lpszMsgBuffer, int nMsgLen);
1414
bool PullStream_ClientWebRtc_Handle(RFCCOMPONENTS_HTTP_REQPARAM* pSt_HTTPParam, LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, int nMsgLen);

XEngine_Source/XEngine_ServiceApp/XEngine_StreamMediaApp/XEngine_Network.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void CALLBACK Network_Callback_AudioRTCPRecv(LPCXSTR lpszClientAddr, XSOCKET hSo
161161
//WEBRTC
162162
void CALLBACK Network_Callback_RTCRecv(LPCXSTR lpszClientAddr, XSOCKET hSocket, LPCXSTR lpszRecvMsg, int nMsgLen, XPVOID lParam)
163163
{
164-
PullStream_ClientProtocol_Handle(lpszClientAddr, lpszRecvMsg, nMsgLen);
164+
PullStream_ClientProtocol_Handle(lpszClientAddr, hSocket, lpszRecvMsg, nMsgLen);
165165
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_DEBUG, _X("STUN客户端:%s,发送数据大小:%d 给服务器"), lpszClientAddr, nMsgLen);
166166
}
167167
//////////////////////////////////////////////////////////////////////////网络IO关闭操作

XEngine_Source/XEngine_ServiceApp/XEngine_StreamMediaApp/XEngine_StreamMediaApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ int main(int argc, char** argv)
503503

504504
if (st_ServiceConfig.st_XPull.st_PullWebRtc.bEnable)
505505
{
506-
xhRTCSsl = OPenSsl_Server_InitEx(st_ServiceConfig.st_XPull.st_PullWebRtc.tszPublicKey, NULL, st_ServiceConfig.st_XPull.st_PullWebRtc.tszPublicKey, false, false, XENGINE_OPENSSL_PROTOCOL_DTL_SERVER);
506+
xhRTCSsl = OPenSsl_Server_InitEx(st_ServiceConfig.st_XPull.st_PullWebRtc.tszCertStr, NULL, st_ServiceConfig.st_XPull.st_PullWebRtc.tszKeyStr, false, false, XENGINE_OPENSSL_PROTOCOL_DTL_SERVER);
507507
if (NULL == xhRTCSsl)
508508
{
509509
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("启动服务中,启动WEBRTC-DTLS安全网络,错误:%lX"), OPenSsl_GetLastError());

0 commit comments

Comments
 (0)