SpSecureClient.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. #include "SpSecureClient.h"
  2. #include "sp_cfg.h"
  3. #include "sp_env.h"
  4. #include "spShareMemoryBase.h"
  5. #include "toolkit.h"
  6. #include "dbgutil.h"
  7. #ifdef RVC_OS_WIN
  8. #include "IHttpFunc.h"
  9. #include "sp_checkEntity.h"
  10. #include "SpEntity.h"
  11. #endif
  12. #include <winpr/winsock.h>
  13. #include "TokenKeeper_client_g.h"
  14. using namespace TokenKeeper;
  15. #define TAG RVCCOMM_TAG("SecureClient")
  16. CEntityBase* SpSecureClient::m_pEntity = NULL;
  17. void getHttpToken(std::string &channelId, std::string &token)
  18. {
  19. CSimpleString t_channelId, t_token;
  20. if (SpSecureClient::m_pEntity == NULL || SpSecureClient::m_pEntity->GetFunction()->GetToken(t_channelId, t_token) != ErrorCodeEnum::Error_Succeed)
  21. return;
  22. if(t_channelId.GetLength() != 0 && t_token.GetLength() != 0)
  23. {
  24. channelId = t_channelId.GetData();
  25. token = t_token.GetData();
  26. }
  27. }
  28. enum CommEventEnum
  29. {
  30. //连接主用地址 事件 1 0xNNN08001 连接配置文件定义的主用地址,需要记录地址信息。如果为短连接,记录首次连接事件。
  31. CE_ConnectMasterServer = 0x00008001,
  32. //连接备用地址 事件 1 0xNNN08002 如果没有定义备用地址,就不会有这条信息
  33. CE_ConnectSlaveServer = 0x00008002,
  34. //主动断开连接 事件 1 0xNNN08011 如果为短连接,无需记录此事件
  35. CE_ActiveClose = 0x00008011,
  36. //读取配置失败 错误 3 0xNNN08021 不能读到需要配置项,代码或配置有误,可以切换备份区域,或需要人干预
  37. CE_ReadCfgFail = 0x00008021,
  38. //连接地址无效 错误 2 0xNNN88031 对端不存在,配置错误,或当前服务器故障
  39. CE_InvalidAddr = 0x00088031,
  40. //对端拒绝连接 错误 2 0xNNN88032 对端存在,但可能故障,导致服务器拒绝建立连接
  41. CE_PeerReject = 0x00088032,
  42. //连接超时 错误 2 0xNNN88033 等待连接超时
  43. CE_ConnectTimeout = 0x00088033,
  44. //容量拒绝 错误 2 0xNNN88034 由于达到后台的最大容量,连接拒绝,需要重试
  45. CE_ReachMaxNum = 0x00088034,
  46. //服务暂停 错误 2 0xNNN88035 后台暂停服务,实体可以切换主备服务或抛出连接失败,切换备份区域
  47. CE_OutOfService = 0x00088035,
  48. //失去连接 错误 2 0xNNN88036 由于对端或网络原因连接断开
  49. CE_LostConnection = 0x00088036,
  50. //无效请求包 错误 3 0xNNN88041 实体或后台代码错误,需要进行人工修复。在描述中应该说明错误的原因。
  51. CE_InvalidReqPack = 0x00088041,
  52. //鉴权不可识别 错误 3 0xNNN88051 由于服务准入服务证书不符,不能解开请求包,错误需要干预,不能自动修复。
  53. CE_AuthFailed = 0x00088051,
  54. //鉴权信息不匹配 错误 2 0xNNN88052 请求时鉴权信息不匹配,需要重新进行准入。
  55. CE_HashMismatch = 0x00088052,
  56. //当前区域连接失败 事件 2 0xNNN880F1 由于多次尝试失败,或发生不可尝试错误,实体放弃了当前区域连接尝试,用于驱动状态切换。记录区域代码。
  57. CE_CurRegionDisabled = 0x000880F1,
  58. };
  59. void* SpSecureClient::logProducer(NULL);
  60. void SpSecureClient::DbgWithLink_DualActive(const char* formatStr, ...) {
  61. if (formatStr == nullptr)
  62. return;
  63. va_list arg;
  64. va_start(arg, formatStr);
  65. //不这么写?貌似到DbgWithLink内后,arg会被析构?
  66. int n = _vscprintf(formatStr, arg) + 1;
  67. std::vector<char> buf(n + 1, '\0');
  68. vsnprintf(&buf[0], n, formatStr, arg);
  69. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).withLogProducer(logProducer).withExtendLog(false)(&buf[0]);
  70. if(EntityResource::getSaveFile())
  71. m_log.LOGERROR(&buf[0]);
  72. va_end(arg);
  73. }
  74. // 不应该直接delete,而用DecRef()
  75. SpSecureClient::~SpSecureClient()
  76. {
  77. //this would lost the process at UOS plat.
  78. //#ifndef _WIN32
  79. // if (logProducer != NULL) {
  80. // destroy_log_producer_storage(logProducer);
  81. // logProducer = NULL;
  82. // }
  83. //#endif
  84. }
  85. SpSecureClient::SpSecureClient(CEntityBase *pEntityBase)
  86. : m_bClosed(true)
  87. {
  88. m_pEntity = pEntityBase;
  89. if(logProducer == NULL) {
  90. logProducer = create_log_producer_storage("DualActive", "0", "");
  91. }
  92. }
  93. std::pair<bool, std::string> FromAddressToIP(std::string host) {
  94. struct hostent* ht = NULL;
  95. ht = gethostbyname(host.c_str());
  96. if (ht) {
  97. for (int i = 0; ; i++) {
  98. if (ht->h_addr_list[i] != NULL)
  99. return std::make_pair(true, inet_ntoa(*((struct in_addr*)ht->h_addr_list[i])));
  100. else
  101. break;
  102. }
  103. for (int j = 0; ; j++) {
  104. if (ht->h_aliases[j] != NULL)
  105. return std::make_pair(true, ht->h_aliases[j]);
  106. else
  107. break;
  108. }
  109. }
  110. return std::make_pair(false, "");
  111. }
  112. bool SpSecureClient::Connect(const char *pServerAddr, int nPort, int nOption) // wait until all authentication operation has been finished
  113. {
  114. TOOLKIT_ASSERT(m_pEntity != NULL);
  115. auto ipRet = FromAddressToIP(pServerAddr);
  116. std::string dstIp = ipRet.first ? ipRet.second : pServerAddr;
  117. bool result = false;
  118. if (GetFunction()->Connect(dstIp.c_str(), nPort, nOption))
  119. {
  120. m_bClosed = false;
  121. result = true;
  122. }
  123. else {
  124. DbgWithLink_DualActive("connect to %s:%d, option=%d, result:%s", dstIp.c_str(), nPort, nOption, result ? "SUCCESS" : "FAIL");
  125. result = false;
  126. }
  127. return result;
  128. }
  129. // 使用配置项连接, 连接参数从实体配置文件中读取 [Main]/Server和[Main]/Server_Backup项
  130. // @option:1、重新鉴权新建会话密钥;2、通过握手使用缓存会话密钥;
  131. // 3、不使用会话密钥,即非安全通道; 4、不协商,直接使用共享会话密钥
  132. bool SpSecureClient::ConnectFromConfig(int nOption)
  133. {
  134. DbgWithLink_DualActive("ConnectFromConfig has been deprecated.");
  135. return false;
  136. if (IsConnectionOK())
  137. return true;
  138. const char* pEntityName = m_pEntity->GetEntityName();
  139. ENTITY_CONNECT_INFO connectInfo;
  140. if (!readConnectInfoFromConfig(pEntityName, &connectInfo))
  141. {
  142. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Read %s Info Failed", pEntityName);
  143. return false;
  144. }
  145. // 判断当前是否已经准入,已准入时强制使用加密通道
  146. int nActOption = nOption;
  147. CSimpleStringA strVal;
  148. const ErrorCodeEnum result = m_pEntity->GetFunction()->GetSysVar("EntryPermit", strVal);
  149. if (Error_Succeed == result) {
  150. if (strVal == "L" && stricmp(pEntityName, "AccessAuthorization") != 0 && stricmp(pEntityName, "Initializer") != 0)
  151. nActOption = 2; // 优先使用缓存密钥
  152. }
  153. if (1 == connectInfo.m_DualActive)
  154. return Connect_Dual(&connectInfo, nOption);
  155. else
  156. return Connect_Single(&connectInfo, nOption);
  157. }
  158. bool SpSecureClient::readConnectInfoFromConfig(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo)
  159. {
  160. if (NULL == pEntityName || NULL == connectInfo)
  161. return false;
  162. connectControl *entityConnect = connectControl::getInstance();
  163. bool readSuccess = entityConnect->getEntityInfo(pEntityName, connectInfo);
  164. auto pFunc = m_pEntity->GetFunction();
  165. CSmartPointer<IConfigInfo> spConfig;
  166. if (Error_Succeed != pFunc->OpenConfig(Config_CenterSetting, spConfig))
  167. {
  168. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("open center setting config fail");
  169. return false;
  170. }
  171. CSimpleStringA str;
  172. int readEach = 0;
  173. if (Error_Succeed == spConfig->ReadConfigValue("SpBase", "ReadEach", str) && !str.IsNullOrEmpty())
  174. readEach = atoi(str);
  175. if (0 == readEach && readSuccess)
  176. return true;//不是每次都读取新的Entity的IP和Port,只在实体启动时读取一次
  177. int dualActive = -1;
  178. if (Error_Succeed == spConfig->ReadConfigValue("SpBase", "DualActive", str) && !str.IsNullOrEmpty())
  179. dualActive = atoi(str);
  180. if (Error_Succeed != pFunc->OpenConfig(Config_Software, spConfig))
  181. {
  182. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("open entity config fail");
  183. return false;
  184. }
  185. if (Error_Succeed != spConfig->ReadConfigValue("Main", "Server", str) || str.IsNullOrEmpty())
  186. {
  187. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Main::Server config fail");
  188. return false;
  189. }
  190. CSimpleStringA strServer1;
  191. int nServer1Port;
  192. if (Error_Succeed != ParseIPAddress(str, strServer1, nServer1Port))
  193. {
  194. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("parse ip addr fail: %s", (const char*)str);
  195. return false;
  196. }
  197. CSimpleStringA strServer2;
  198. int nServer2Port = 0;
  199. if (Error_Succeed == spConfig->ReadConfigValue("Main", "Server_Backup", str) && !str.IsNullOrEmpty())
  200. ParseIPAddress(str, strServer2, nServer2Port);
  201. if (readSuccess)
  202. connectInfo->setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, dualActive, connectInfo->m_currentLink);
  203. else
  204. {
  205. connectInfo->clear();
  206. connectInfo->setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, dualActive);
  207. }
  208. entityConnect->setEntityInfo(connectInfo);
  209. return true;
  210. }
  211. void SpSecureClient::connectClose(const char *pEntityName)
  212. {
  213. if (NULL == pEntityName)
  214. return;
  215. ENTITY_CONNECT_INFO connectInfo;
  216. if (!findConnectInfo(pEntityName, &connectInfo))
  217. {
  218. DbgWithLink_DualActive("can not find %s in connection list.", pEntityName);
  219. return;
  220. }
  221. connectInfo.m_currentLink = -1;
  222. }
  223. bool SpSecureClient::findConnectInfo(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo)
  224. {
  225. if (NULL == pEntityName || NULL == connectInfo)
  226. return false;
  227. connectControl *entityConnect = connectControl::getInstance();
  228. if (entityConnect->getEntityInfo(pEntityName, connectInfo))
  229. return true;
  230. return false;
  231. }
  232. bool SpSecureClient::readConnectInfoFromCentralSetting(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo)
  233. {
  234. if (NULL == pEntityName || NULL == connectInfo)
  235. return false;
  236. connectControl *entityConnect = connectControl::getInstance();
  237. bool readSuccess = entityConnect->getEntityInfo(pEntityName, connectInfo);
  238. //获得集中配置信息
  239. auto pFunc = m_pEntity->GetFunction();
  240. CSmartPointer<IConfigInfo> spConfig;
  241. if (Error_Succeed != pFunc->OpenConfig(Config_CenterSetting, spConfig))
  242. {
  243. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("open center setting config fail");
  244. return false;
  245. }
  246. CSimpleStringA str;
  247. int readEach = 1;
  248. if (Error_Succeed == spConfig->ReadConfigValue("SpBase", "ReadEach", str) && !str.IsNullOrEmpty())
  249. readEach = atoi(str);
  250. if (0 == readEach && readSuccess)
  251. return true;//不是每次都读取新的Entity的IP和Port,只在实体启动时读取一次
  252. ErrorCodeEnum configRc = Error_Bug;
  253. if (Error_Succeed != (configRc = spConfig->ReadConfigValue(pEntityName, "Server", str)) || str.IsNullOrEmpty())
  254. {
  255. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Main::Server config fail, %d, %s", configRc, pEntityName);
  256. return false;
  257. }
  258. CSimpleStringA strServer1;
  259. int nServer1Port;
  260. if (Error_Succeed != (configRc = ParseIPAddress(str, strServer1, nServer1Port)))
  261. {
  262. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("parse ip addr fail: %s, %d", (const char*)str, configRc);
  263. return false;
  264. }
  265. CSimpleStringA strServer2 = "";
  266. int nServer2Port = 0;
  267. if (Error_Succeed == spConfig->ReadConfigValue(pEntityName, "Server_Backup", str) && !str.IsNullOrEmpty())
  268. ParseIPAddress(str, strServer2, nServer2Port);
  269. int dualActive = -1;
  270. if (Error_Succeed == spConfig->ReadConfigValue("SpBase", "DualActive", str) && !str.IsNullOrEmpty())
  271. dualActive = atoi(str);
  272. if (readSuccess)
  273. connectInfo->setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, dualActive, connectInfo->m_currentLink);
  274. else
  275. {
  276. //DbgWithLink_DualActive("call clear");
  277. connectInfo->clear();
  278. connectInfo->setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, dualActive);
  279. }
  280. entityConnect->setEntityInfo(connectInfo);
  281. return true;
  282. }
  283. bool SpSecureClient::setConnectInfo(const ENTITY_CONNECT_INFO *connectInfo)
  284. {
  285. if (NULL == connectInfo)
  286. return false;
  287. connectControl *entityConnect = connectControl::getInstance();
  288. entityConnect->setLastLink(connectInfo->m_currentLink);
  289. return entityConnect->setEntityInfo(connectInfo);
  290. }
  291. // 使用集中配置项连接
  292. bool SpSecureClient::ConnectFromCentralSetting()
  293. {
  294. #ifdef RVC_OS_WIN
  295. SetthreadGroup(GetCurrentThreadId(), m_pEntity->GetEntityName());
  296. #endif // RVC_OS_WIN
  297. if (IsConnectionOK())
  298. return true;
  299. const char* pEntityName = m_pEntity->GetEntityName();
  300. ENTITY_CONNECT_INFO connectInfo;
  301. if (!readConnectInfoFromCentralSetting(pEntityName, &connectInfo))
  302. {
  303. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Read %s Info Failed", pEntityName);
  304. DbgWithLink_DualActive("Read %s Info Failed", pEntityName);
  305. return false;
  306. }
  307. // 判断当前是否已经准入,已经准入则强制使用安全连接
  308. int nOption = 3;
  309. CSimpleStringA strVal;
  310. if (Error_Succeed == m_pEntity->GetFunction()->GetSysVar("EntryPermit", strVal)) {
  311. if (strVal == "L" && stricmp(pEntityName, "AccessAuthorization") != 0 && stricmp(pEntityName, "Initializer") != 0)
  312. nOption = 2; // 优先使用缓存密钥
  313. }
  314. if (1 == connectInfo.m_DualActive)
  315. return Connect_Dual(&connectInfo, nOption);
  316. else
  317. return Connect_Single(&connectInfo, nOption);
  318. }
  319. int SpSecureClient::getCurrentLink()
  320. {
  321. if (!IsConnectionOK())
  322. return -1;
  323. const char* pEntityName = m_pEntity->GetEntityName();
  324. ENTITY_CONNECT_INFO connectInfo;
  325. if (!readConnectInfoFromCentralSetting(pEntityName, &connectInfo))
  326. {
  327. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Read %s Info Failed", pEntityName);
  328. return -1;
  329. }
  330. return connectInfo.m_currentLink;
  331. }
  332. bool SpSecureClient::Connect_Single(ENTITY_CONNECT_INFO *connectInfo, int option)
  333. {
  334. if (NULL == connectInfo || 0 == strlen(connectInfo->m_EntityName))
  335. return false;
  336. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("mod %s using option %d", connectInfo->m_EntityName, option);
  337. DbgWithLink_DualActive("mod %s using option %d", connectInfo->m_EntityName, option);
  338. if (Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option))
  339. connectInfo->m_currentLink = 0;
  340. else if (0 != strlen(connectInfo->m_Server_BackupIP) && strcmp(connectInfo->m_ServerIP, connectInfo->m_Server_BackupIP)
  341. && Connect(connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, option))
  342. connectInfo->m_currentLink = 1;
  343. else
  344. connectInfo->m_currentLink = -1;
  345. setConnectInfo(connectInfo);
  346. DbgWithLink_DualActive("Single Connection: EntityName:%s, server:%s %d, serverBackup:%s %d, DualOpen: %d, currentLink:%d", connectInfo->m_EntityName, connectInfo->m_ServerIP,
  347. connectInfo->m_ServerPort, connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, connectInfo->m_DualActive, connectInfo->m_currentLink);
  348. return -1 != connectInfo->m_currentLink;
  349. }
  350. bool SpSecureClient::Connect_Dual(ENTITY_CONNECT_INFO *connectInfo, int option)
  351. {
  352. if (NULL == connectInfo || 0 == strlen(connectInfo->m_EntityName))
  353. return false;
  354. connectControl *entityConnect = connectControl::getInstance();
  355. bool quickConnect = false;
  356. const int priorLink = entityConnect->getPriorLink(connectInfo->m_lastLink);
  357. if (-1 != priorLink)
  358. {
  359. /*
  360. 根据上次双活成功时的server进行连接
  361. */
  362. if (0 == priorLink && 0 != strlen(connectInfo->m_ServerIP)) {
  363. connectInfo->m_currentLink = (Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option) ? 0 : -1);
  364. }
  365. else if (1 == priorLink && 0 != strlen(connectInfo->m_Server_BackupIP)) {
  366. connectInfo->m_currentLink = (Connect(connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, option) ? 1 : -1);
  367. }
  368. quickConnect = (-1 == connectInfo->m_currentLink ? false : true);
  369. }
  370. if (!IsConnectionOK()) {
  371. /*
  372. 根据终端号进行连接
  373. */
  374. //获取终端号,判断优先连接的IP和端口
  375. auto pFunc = m_pEntity->GetFunction();
  376. CSmartPointer<IConfigInfo> spConfig;
  377. auto rc = pFunc->OpenConfig(Config_Root, spConfig);
  378. if (rc != Error_Succeed) {
  379. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root.ini Fail: %s", SpStrError(rc));
  380. return false;
  381. }
  382. CSimpleStringA strValue;
  383. spConfig->ReadConfigValue("Terminal", "TerminalNo", strValue);
  384. bool isMainServer = true;
  385. if (strValue.GetLength() > 0) {
  386. char str[20] = "";
  387. sprintf(str, "%s", (const char*)strValue);
  388. const int f = (str[strlen(str) - 1] - '0');
  389. isMainServer = (0 == (f % 2));
  390. DbgWithLink_DualActive("TerminalNo: %s, lastByte: %d, MainServer:%d", str, f, isMainServer);
  391. }
  392. DbgWithLink_DualActive("mod %s using option %d", connectInfo->m_EntityName, option);
  393. if (0 == strlen(connectInfo->m_Server_BackupIP) && 0 == strlen(connectInfo->m_ServerIP)) {
  394. connectInfo->m_currentLink = -1;
  395. } else if (0 == strlen(connectInfo->m_Server_BackupIP))
  396. {//serverBackup Ip and port error, link to server
  397. connectInfo->m_currentLink = (Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option) ? 0 : -1);//Link to first one
  398. DbgWithLink_DualActive("%s serverBackup param error", connectInfo->m_EntityName);
  399. }
  400. else
  401. {
  402. if (isMainServer)
  403. {//link to first server
  404. if (Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option))
  405. connectInfo->m_currentLink = 0;
  406. else if (
  407. strcmp(connectInfo->m_ServerIP, connectInfo->m_Server_BackupIP)
  408. && Connect(connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, option)
  409. ) {
  410. connectInfo->m_currentLink = 1;
  411. }
  412. else {
  413. connectInfo->m_currentLink = -1;
  414. }
  415. }
  416. else
  417. {//link to second server
  418. DbgWithLink_DualActive("BackUp");
  419. if (Connect(connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, option))
  420. connectInfo->m_currentLink = 1;
  421. else if (strcmp(connectInfo->m_ServerIP, connectInfo->m_Server_BackupIP)
  422. && Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option))
  423. connectInfo->m_currentLink = 0;
  424. else
  425. connectInfo->m_currentLink = -1;
  426. }
  427. }
  428. }
  429. /*
  430. 连接结束
  431. */
  432. char msg[1024];
  433. sprintf(msg, "%sDual Connection: EntityName:%s, server:%s %d, serverBackup:%s %d, DualOpen: %d, currentLink:%d, curEntityNum:%d", (quickConnect ? "quick " : ""),
  434. connectInfo->m_EntityName, connectInfo->m_ServerIP, connectInfo->m_ServerPort, connectInfo->m_Server_BackupIP,
  435. connectInfo->m_Server_BackupPort, connectInfo->m_DualActive, connectInfo->m_currentLink, entityConnect->getEntityNum());
  436. DbgWithLink_DualActive(msg);
  437. setConnectInfo(connectInfo);
  438. return (-1 != connectInfo->m_currentLink);
  439. }
  440. bool SpSecureClient::IsConnectionOK()
  441. {
  442. return GetFunction()->IsConnectionOK();
  443. }
  444. bool SpSecureClient::IsSecureConn()
  445. {
  446. return GetFunction()->IsSecureConnection();
  447. }
  448. void SpSecureClient::Close()
  449. {
  450. if (!m_bClosed)
  451. {
  452. m_bClosed = true;
  453. GetFunction()->Close();
  454. //clear the connect Info
  455. connectClose(m_pEntity->GetEntityName());
  456. DbgWithLink_DualActive("%s closed", m_pEntity->GetEntityName());
  457. }
  458. }
  459. string SpSecureClient::SendPackage(const CSmartPointer<IPackage>& pSendPkg)
  460. {
  461. return GetFunction()->SendPackage(pSendPkg);
  462. }
  463. CSmartPointer<IPackage> SpSecureClient::ReceivePackage(int nWaitSecond)
  464. {
  465. return GetFunction()->ReceivePackage(nWaitSecond);
  466. }
  467. CSmartPointer<IPackage> SpSecureClient::CreateNewPackage(const char *pServiceCode)
  468. {
  469. return GetFunction()->CreateNewPackage(pServiceCode);
  470. }
  471. CSmartPointer<IPackage> SpSecureClient::CreateReplyPackage(const CSmartPointer<IPackage>& pRecvPkg)
  472. {
  473. return GetFunction()->CreateReplyPackage(pRecvPkg);
  474. }
  475. void SpSecureClient::OnErrorMsg(DWORD dwSysCode, DWORD dwUserCode, const CSimpleStringA &pErrMsg)
  476. {
  477. // 切换到实体线程执行
  478. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("SpSecureClient error: %s", (const char*)pErrMsg);
  479. }
  480. void SpSecureClient::OnDisconnect()
  481. {
  482. DbgWithLink_DualActive("Entity %s OnDisconnect", m_pEntity->GetEntityName());
  483. }
  484. void SpSecureClient::OnReConnect()
  485. {
  486. if (!IsConnectionOK())
  487. {//可能OnDisconnect被重载,已重新连接
  488. DbgWithLink_DualActive("Entity %s disconnect, try to reconnect", m_pEntity->GetEntityName());
  489. ConnectFromCentralSetting();
  490. }
  491. }
  492. struct OnPackageTask : public ITaskSp
  493. {
  494. OnPackageTask(SpSecureClient *pClient, IPackage* pkg)
  495. :m_pSecureClient(pClient), m_pPackage(pkg)
  496. {}
  497. SpSecureClient *m_pSecureClient;
  498. IPackage * m_pPackage;
  499. virtual void Process()
  500. {
  501. TOOLKIT_ASSERT(m_pPackage != NULL);
  502. CSmartPointer<IPackage> ptr;
  503. ptr.Attach(m_pPackage);
  504. m_pSecureClient->OnPkgAnswer(ptr);
  505. m_pSecureClient->DecRefCount();
  506. }
  507. };
  508. struct OnDisconnectTask : public ITaskSp
  509. {
  510. OnDisconnectTask(SpSecureClient *pClient)
  511. :m_pSecureClient(pClient)
  512. {}
  513. SpSecureClient *m_pSecureClient;
  514. virtual void Process()
  515. {
  516. m_pSecureClient->Close();
  517. m_pSecureClient->OnDisconnect();
  518. m_pSecureClient->DecRefCount();
  519. m_pSecureClient->OnReConnect();
  520. }
  521. };
  522. struct OnErrorTask : public ITaskSp
  523. {
  524. OnErrorTask(SpSecureClient *pClient, DWORD dwSysCode, DWORD dwUserCode, const char *pMsg)
  525. :m_pSecureClient(pClient), m_dwSysCode(dwSysCode), m_dwUserCode(dwUserCode), m_strErrMsg(pMsg)
  526. {}
  527. SpSecureClient *m_pSecureClient;
  528. DWORD m_dwSysCode;
  529. DWORD m_dwUserCode;
  530. CSimpleStringA m_strErrMsg;
  531. virtual void Process()
  532. {
  533. m_pSecureClient->OnErrorMsg(m_dwSysCode, m_dwUserCode, m_strErrMsg);
  534. m_pSecureClient->DecRefCount();
  535. }
  536. };
  537. void SpSecureClient::OnReceivePackage(CSmartPointer<IPackage> &pRecvPkg)
  538. {
  539. // 切换到实体线程执行
  540. OnPackageTask *pTask = new OnPackageTask(this, pRecvPkg.Detach());
  541. IncRefCount();
  542. m_pEntity->GetFunction()->PostEntityTaskFIFO(pTask);
  543. }
  544. void SpSecureClient::OnClose()
  545. {
  546. // 切换到实体线程执行
  547. OnDisconnectTask *pTask = new OnDisconnectTask(this);
  548. IncRefCount();
  549. m_pEntity->GetFunction()->PostEntityTaskFIFO(pTask);
  550. }
  551. void SpSecureClient::OnError(DWORD dwSysCode, DWORD dwUserCode, const char *pErrMsg)
  552. {
  553. // 切换到实体线程执行
  554. OnErrorTask *pTask = new OnErrorTask(this, dwSysCode, dwUserCode, pErrMsg);
  555. IncRefCount();
  556. m_pEntity->GetFunction()->PostEntityTaskFIFO(pTask);
  557. }
  558. void SpSecureClient::OnAuthPass()
  559. {
  560. }
  561. bool SpSecureClient::OnAuthRequest(CConnAuthReq *pReq)
  562. {
  563. ErrorCodeEnum Error = Error_Unexpect;
  564. // prepare verify info
  565. CSystemStaticInfo si;
  566. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  567. CVerifyInfo info;
  568. memset(&info, 0, sizeof(info));
  569. strncpy(info.m_arrTerminalNo, (LPCSTR)si.strTerminalID, sizeof(info.m_arrTerminalNo)-1);
  570. strcpy(info.m_arrServiceType, "CMBRVC");
  571. {
  572. #ifdef RVC_OS_WIN
  573. hostent *ent = _gethostbyname(NULL);
  574. if (ent && ent->h_addr_list[0] != NULL)
  575. {
  576. int i = 0;
  577. for (; ent->h_addr_list[i] != NULL; ++i) {
  578. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  579. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  580. break;
  581. }
  582. if (ent->h_addr_list[i] == NULL)
  583. i = 0;
  584. auto in = (struct in_addr*)ent->h_addr_list[i];
  585. info.m_arrIP[0] = in->S_un.S_un_b.s_b1;
  586. info.m_arrIP[1] = in->S_un.S_un_b.s_b2;
  587. info.m_arrIP[2] = in->S_un.S_un_b.s_b3;
  588. info.m_arrIP[3] = in->S_un.S_un_b.s_b4;
  589. }
  590. #else
  591. char buf[512];
  592. toolkit_interface_address_t* addr;
  593. int count, i, aim = -1, backup = -1, third = -1;
  594. toolkit_interface_addresses(&addr, &count);
  595. i = count;
  596. WLog_DBG(TAG, "Number of interfaces: %d", count);
  597. while (i--) {
  598. toolkit_interface_address_t interface = addr[i];
  599. WLog_DBG(TAG, "Name: %s", interface.name);
  600. WLog_DBG(TAG, "Internal? %s", interface.is_internal ? "Yes" : "No");
  601. if (interface.address.address4.sin_family == AF_INET) {
  602. third = i;
  603. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  604. WLog_DBG(TAG, "IPv4 address: %s", buf);
  605. CSimpleStringA s(buf);
  606. if (s.IsStartWith("99.") || s.IsStartWith("10.")) {
  607. aim = i;
  608. break;
  609. }
  610. if (!interface.is_internal && backup != -1) {
  611. backup = i;
  612. }
  613. } else if (interface.address.address4.sin_family == AF_INET6) {
  614. toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
  615. WLog_DBG(TAG, "IPv6 address: %s", buf);
  616. }
  617. }
  618. if (aim == -1 && backup != -1) {
  619. aim = backup;
  620. }
  621. if (aim == -1 && third != -1)
  622. aim = third;
  623. memset(info.m_arrIP, 0, sizeof(info.m_arrIP));
  624. if (aim != -1) {
  625. char ipStr[32];
  626. int b1, b2, b3, b4;
  627. b1 = b2 = b3 = b4 = 0;
  628. toolkit_ip4_name(&addr[aim].address.address4, ipStr, sizeof(ipStr));
  629. WLog_DBG(TAG, "specified local ip: %s", ipStr);
  630. sscanf(ipStr, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
  631. info.m_arrIP[0] = (BYTE)b1;
  632. info.m_arrIP[1] = (BYTE)b2;
  633. info.m_arrIP[2] = (BYTE)b3;
  634. info.m_arrIP[3] = (BYTE)b4;
  635. }
  636. toolkit_free_interface_addresses(addr, count);
  637. #endif
  638. }
  639. if (pReq != NULL)
  640. {
  641. // call token service to encrypt verify info
  642. int nDestLen = sizeof(pReq->m_arrVerifyInfo);
  643. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  644. Error = client->Connect();
  645. if (Error == Error_Succeed) {
  646. TokenService_EncryptWithPubKey_Req Req;
  647. TokenService_EncryptWithPubKey_Ans Ans;
  648. Req.raw_data.m_pData = (char*)&info;
  649. Req.raw_data.m_iLength = sizeof(info); //28
  650. Error = client->EncryptWithPubKey(Req, Ans, -1);
  651. if (Error != Error_Succeed) {
  652. LOG_TRACE("EncryptWithPriKey for VerifyInfo failed! Error = %s", SpStrError(Error));
  653. } else {
  654. memcpy_s(pReq->m_arrVerifyInfo, sizeof(pReq->m_arrVerifyInfo), Ans.enc_data.m_pData, Ans.enc_data.m_iLength);
  655. // get token
  656. TokenService_GetToken_Req Req1;
  657. TokenService_GetToken_Ans Ans1;
  658. Error = client-> GetToken(Req1, Ans1, -1);
  659. if (Error != Error_Succeed) {
  660. LOG_TRACE("GetToken for VerifyInfo failed! Error = %s", SpStrError(Error));
  661. } else {
  662. memcpy_s(pReq->m_arrVerifyToken, sizeof(pReq->m_arrVerifyToken), Ans1.token.m_pData, Ans1.token.m_iLength);
  663. }
  664. }
  665. client->GetFunction()->CloseSession();
  666. } else {
  667. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  668. client->SafeDelete();
  669. }
  670. }
  671. return Error == Error_Succeed;
  672. }
  673. bool SpSecureClient::OnAuthRequestSM(CConnAuthSMReq* pReq)
  674. {
  675. ErrorCodeEnum Error = Error_Unexpect;
  676. CSystemStaticInfo si;
  677. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  678. CVerifyInfo info;
  679. memset(&info, 0, sizeof(info));
  680. strncpy(info.m_arrTerminalNo, (LPCSTR)si.strTerminalID, sizeof(info.m_arrTerminalNo) - 1);
  681. strcpy(info.m_arrServiceType, "CMBRVC");
  682. {
  683. #ifdef RVC_OS_LINUX
  684. char buf[512];
  685. toolkit_interface_address_t* addr;
  686. int count, i, aim = -1, backup = -1, third=-1;
  687. toolkit_interface_addresses(&addr, &count);
  688. i = count;
  689. WLog_DBG(TAG, "Number of interfaces: %d", count);
  690. while (i--) {
  691. toolkit_interface_address_t interface = addr[i];
  692. WLog_DBG(TAG, "Name: %s", interface.name);
  693. WLog_DBG(TAG, "Internal? %s", interface.is_internal ? "Yes" : "No");
  694. if (interface.address.address4.sin_family == AF_INET) {
  695. third = i;
  696. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  697. WLog_DBG(TAG, "IPv4 address: %s", buf);
  698. CSimpleStringA s(buf);
  699. if (s.IsStartWith("99.") || s.IsStartWith("10.")) {
  700. aim = i;
  701. break;
  702. }
  703. if (!interface.is_internal && backup != -1) {
  704. backup = i;
  705. }
  706. } else if (interface.address.address4.sin_family == AF_INET6) {
  707. toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
  708. WLog_DBG(TAG, "IPv6 address: %s", buf);
  709. }
  710. }
  711. if (aim == -1 && backup != -1) {
  712. aim = backup;
  713. }
  714. if (aim == -1 && third != -1)
  715. aim = third;
  716. memset(info.m_arrIP, 0, sizeof(info.m_arrIP));
  717. if (aim != -1) {
  718. char ipStr[32];
  719. int b1, b2, b3, b4;
  720. b1 = b2 = b3 = b4 = 0;
  721. toolkit_ip4_name(&addr[aim].address.address4, ipStr, sizeof(ipStr));
  722. WLog_DBG(TAG, "specified local ip: %s", ipStr);
  723. sscanf(ipStr, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
  724. info.m_arrIP[0] = (BYTE)b1;
  725. info.m_arrIP[1] = (BYTE)b2;
  726. info.m_arrIP[2] = (BYTE)b3;
  727. info.m_arrIP[3] = (BYTE)b4;
  728. }
  729. toolkit_free_interface_addresses(addr, count);
  730. #else
  731. hostent* ent = _gethostbyname(NULL);
  732. WLog_DBG(TAG, "_gethostbyname finished ");
  733. if (ent && ent->h_addr_list[0] != NULL) {
  734. int i = 0;
  735. WLog_DBG(TAG, "office hostname: %s", ent->h_name);
  736. for (; ent->h_addr_list[i] != NULL; ++i) {
  737. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  738. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  739. break;
  740. }
  741. if (ent->h_addr_list[i] == NULL)
  742. i = 0;
  743. auto in = (struct in_addr*)ent->h_addr_list[i];
  744. info.m_arrIP[0] = in->S_un.S_un_b.s_b1;
  745. info.m_arrIP[1] = in->S_un.S_un_b.s_b2;
  746. info.m_arrIP[2] = in->S_un.S_un_b.s_b3;
  747. info.m_arrIP[3] = in->S_un.S_un_b.s_b4;
  748. }
  749. #endif //RVC_OS_LINUX
  750. }
  751. if(pReq != NULL)
  752. {
  753. // call token service to encrypt verify info
  754. int nDestLen = sizeof(pReq->m_arrVerifyInfo);
  755. TokenService_ClientBase* client = new TokenService_ClientBase(m_pEntity);
  756. Error = client->Connect();
  757. if (Error == Error_Succeed)
  758. {
  759. TokenService_EncryptWithPubKey_Req Req;
  760. TokenService_EncryptWithPubKey_Ans Ans;
  761. Req.raw_data.m_pData = (char*)&info;
  762. Req.raw_data.m_iLength = sizeof(info); //28
  763. Error = client->EncryptWithPubKey(Req, Ans, -1);
  764. if (Error != Error_Succeed) {
  765. LOG_TRACE("EncryptWithPriKey for VerifyInfo failed! Error = %s", SpStrError(Error));
  766. } else {
  767. memcpy_s(pReq->m_arrVerifyInfo, sizeof(pReq->m_arrVerifyInfo), Ans.enc_data.m_pData, Ans.enc_data.m_iLength);
  768. // get token
  769. TokenService_GetToken_Req Req1;
  770. TokenService_GetToken_Ans Ans1;
  771. Error = client->GetToken(Req1, Ans1, -1);
  772. if (Error != Error_Succeed) {
  773. LOG_TRACE("GetToken for VerifyInfo failed! Error = %s", SpStrError(Error));
  774. }
  775. else {
  776. memcpy_s(pReq->m_arrVerifyToken, sizeof(pReq->m_arrVerifyToken), Ans1.token.m_pData, Ans1.token.m_iLength);
  777. }
  778. }
  779. client->GetFunction()->CloseSession();
  780. }
  781. else {
  782. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  783. client->SafeDelete();
  784. }
  785. }
  786. return Error == Error_Succeed;
  787. }
  788. bool SpSecureClient::OnSessionKeyRet(CConnAuthRet *pRet, BYTE *pBuf, int *pBufLen)
  789. {
  790. memset(pBuf, 0, *pBufLen);
  791. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  792. ErrorCodeEnum Error = client->Connect();
  793. if (Error == Error_Succeed)
  794. {
  795. TokenService_DecryptWithPriKey_Req Req;
  796. TokenService_DecryptWithPriKey_Ans Ans;
  797. Req.enc_data.m_pData = (char*)&pRet->m_arrEncSessionKey[0];
  798. Req.enc_data.m_iLength = sizeof(pRet->m_arrEncSessionKey);
  799. Error = client->DecryptWithPriKey(Req, Ans, -1);
  800. if (Error == Error_Succeed)
  801. {
  802. memcpy_s(pBuf, *pBufLen, Ans.raw_data.m_pData, Ans.raw_data.m_iLength);
  803. *pBufLen = Ans.raw_data.m_iLength;
  804. } else {
  805. *pBufLen = 0;
  806. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  807. }
  808. client->GetFunction()->CloseSession();
  809. } else {
  810. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  811. client->SafeDelete();
  812. }
  813. return Error == Error_Succeed;
  814. }
  815. bool SpSecureClient::OnSessionKeySMRet(CConnAuthSMRet* pRet, BYTE* pBuf, int* pBufLen)
  816. {
  817. memset(pBuf, 0, *pBufLen);
  818. TokenService_ClientBase* client = new TokenService_ClientBase(m_pEntity);
  819. ErrorCodeEnum Error = client->Connect();
  820. if (Error == Error_Succeed)
  821. {
  822. TokenService_DecryptWithPriKey_Req Req;
  823. TokenService_DecryptWithPriKey_Ans Ans;
  824. Req.enc_data.m_pData = (char*)&pRet->m_arrEncSessionKey[0];
  825. Req.enc_data.m_iLength = sizeof(pRet->m_arrEncSessionKey);
  826. Error = client->DecryptWithPriKey(Req, Ans, -1);
  827. if (Error == Error_Succeed)
  828. {
  829. memcpy_s(pBuf, *pBufLen, Ans.raw_data.m_pData, Ans.raw_data.m_iLength);
  830. *pBufLen = Ans.raw_data.m_iLength;
  831. }
  832. else {
  833. *pBufLen = 0;
  834. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  835. }
  836. client->GetFunction()->CloseSession();
  837. } else {
  838. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  839. client->SafeDelete();
  840. }
  841. return Error == Error_Succeed;
  842. }
  843. bool SpSecureClient::OnGetSharedSK(char *pTerminalNo, int *pTerminalNoLen, BYTE *pBuf, int *pBufLen)
  844. {
  845. #ifdef _MSC_VER
  846. ErrorCodeEnum Error = Error_Unexpect;
  847. CSystemStaticInfo si;
  848. {
  849. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  850. }
  851. TOOLKIT_ASSERT(si.strTerminalID.GetLength() <= *pTerminalNoLen);
  852. strcpy(pTerminalNo, (LPCSTR)si.strTerminalID);
  853. *pTerminalNoLen = strlen(pTerminalNo);
  854. {
  855. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  856. Error = client->Connect();
  857. if (Error == Error_Succeed) {
  858. TokenService_GetSharedSK_Req Req;
  859. TokenService_GetSharedSK_Ans Ans;
  860. Error = client->GetSharedSK(Req, Ans, 10000);
  861. if (Error == Error_Succeed) {
  862. memcpy_s(pBuf, *pBufLen, Ans.ssk.m_pData, Ans.ssk.m_iLength);
  863. *pBufLen = Ans.ssk.m_iLength;
  864. } else {
  865. *pBufLen = 0;
  866. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  867. }
  868. client->GetFunction()->CloseSession();
  869. } else {
  870. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  871. client->SafeDelete();
  872. }
  873. }
  874. return Error == Error_Succeed;
  875. #else
  876. return false;
  877. #endif //_MSC_VER
  878. }
  879. ErrorCodeEnum SpSecureClient::ParseIPAddress(const char *str, CSimpleStringA &ip, int &port)
  880. {
  881. ErrorCodeEnum Error = Error_Unexpect;
  882. if (str) {
  883. char tmp1[32] = {};
  884. char tmp2[16] = {};
  885. sscanf(str, "%s %s", tmp1, tmp2);
  886. if (tmp1[0] != 0 && tmp2[0] != 0)
  887. {
  888. ip = tmp1;
  889. port = atoi(tmp2);
  890. Error = Error_Succeed;
  891. }
  892. else
  893. return Error;
  894. }
  895. return Error;
  896. }
  897. DWORD SpSecureClient::GetEntityUserCode(DWORD code)
  898. {
  899. TOOLKIT_ASSERT(m_pEntity != NULL);
  900. auto pEnv = sp_get_env();
  901. TOOLKIT_ASSERT(pEnv != NULL);
  902. auto pCfg = pEnv->cfg;
  903. TOOLKIT_ASSERT(pCfg != NULL);
  904. auto pEntity = sp_cfg_get_entity_by_name(pCfg, m_pEntity->GetEntityName());
  905. TOOLKIT_ASSERT(pEntity != NULL);
  906. DWORD dwDevelopID = pEntity->devel_id;
  907. return (dwDevelopID << 20 | code);
  908. }