SpSecureClient.cpp 34 KB

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