SpSecureClient.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. // 使用集中配置项连接
  296. bool SpSecureClient::ConnectFromCentralSetting()
  297. {
  298. #ifdef RVC_OS_WIN
  299. SetthreadGroup(GetCurrentThreadId(), m_pEntity->GetEntityName());
  300. #endif // RVC_OS_WIN
  301. if (IsConnectionOK())
  302. return true;
  303. const char* pEntityName = m_pEntity->GetEntityName();
  304. ENTITY_CONNECT_INFO connectInfo;
  305. if (!readConnectInfoFromCentralSetting(pEntityName, &connectInfo))
  306. {
  307. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Read %s Info Failed", pEntityName);
  308. DbgWithLink_DualActive("Read %s Info Failed", pEntityName);
  309. return false;
  310. }
  311. // 判断当前是否已经准入,已经准入则强制使用安全连接
  312. int nOption = 3;
  313. CSimpleStringA strVal;
  314. if (Error_Succeed == m_pEntity->GetFunction()->GetSysVar("EntryPermit", strVal)) {
  315. if (strVal == "L" && stricmp(pEntityName, "AccessAuthorization") != 0 && stricmp(pEntityName, "Initializer") != 0)
  316. nOption = 2; // 优先使用缓存密钥
  317. }
  318. if (1 == connectInfo.m_DualActive)
  319. return Connect_Dual(&connectInfo, nOption);
  320. else
  321. return Connect_Single(&connectInfo, nOption);
  322. }
  323. int SpSecureClient::getCurrentLink()
  324. {
  325. if (!IsConnectionOK())
  326. return -1;
  327. const char* pEntityName = m_pEntity->GetEntityName();
  328. ENTITY_CONNECT_INFO connectInfo;
  329. if (!readConnectInfoFromCentralSetting(pEntityName, &connectInfo))
  330. {
  331. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Read %s Info Failed", pEntityName);
  332. return -1;
  333. }
  334. return connectInfo.m_currentLink;
  335. }
  336. bool SpSecureClient::Connect_Single(ENTITY_CONNECT_INFO *connectInfo, int option)
  337. {
  338. if (NULL == connectInfo || 0 == strlen(connectInfo->m_EntityName))
  339. return false;
  340. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("mod %s using option %d", connectInfo->m_EntityName, option);
  341. DbgWithLink_DualActive("mod %s using option %d", connectInfo->m_EntityName, option);
  342. if (Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option))
  343. connectInfo->m_currentLink = 0;
  344. else if (0 != strlen(connectInfo->m_Server_BackupIP) && strcmp(connectInfo->m_ServerIP, connectInfo->m_Server_BackupIP)
  345. && Connect(connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, option))
  346. connectInfo->m_currentLink = 1;
  347. else
  348. connectInfo->m_currentLink = -1;
  349. setConnectInfo(connectInfo);
  350. DbgWithLink_DualActive("Single Connection: EntityName:%s, server:%s %d, serverBackup:%s %d, DualOpen: %d, currentLink:%d", connectInfo->m_EntityName, connectInfo->m_ServerIP,
  351. connectInfo->m_ServerPort, connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, connectInfo->m_DualActive, connectInfo->m_currentLink);
  352. return -1 != connectInfo->m_currentLink;
  353. }
  354. bool SpSecureClient::Connect_Dual(ENTITY_CONNECT_INFO *connectInfo, int option)
  355. {
  356. if (NULL == connectInfo || 0 == strlen(connectInfo->m_EntityName))
  357. return false;
  358. connectControl *entityConnect = connectControl::getInstance();
  359. bool quickConnect = false;
  360. const int priorLink = entityConnect->getPriorLink(connectInfo->m_lastLink);
  361. if (-1 != priorLink)
  362. {
  363. /*
  364. 根据上次双活成功时的server进行连接
  365. */
  366. if (0 == priorLink && 0 != strlen(connectInfo->m_ServerIP)) {
  367. connectInfo->m_currentLink = (Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option) ? 0 : -1);
  368. }
  369. else if (1 == priorLink && 0 != strlen(connectInfo->m_Server_BackupIP)) {
  370. connectInfo->m_currentLink = (Connect(connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, option) ? 1 : -1);
  371. }
  372. quickConnect = (-1 == connectInfo->m_currentLink ? false : true);
  373. }
  374. if (!IsConnectionOK()) {
  375. /*
  376. 根据终端号进行连接
  377. */
  378. //获取终端号,判断优先连接的IP和端口
  379. auto pFunc = m_pEntity->GetFunction();
  380. CSmartPointer<IConfigInfo> spConfig;
  381. auto rc = pFunc->OpenConfig(Config_Root, spConfig);
  382. if (rc != Error_Succeed) {
  383. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root.ini Fail: %s", SpStrError(rc));
  384. return false;
  385. }
  386. CSimpleStringA strValue;
  387. spConfig->ReadConfigValue("Terminal", "TerminalNo", strValue);
  388. bool isMainServer = true;
  389. if (strValue.GetLength() > 0) {
  390. char str[20] = "";
  391. sprintf(str, "%s", (const char*)strValue);
  392. const int f = (str[strlen(str) - 1] - '0');
  393. isMainServer = (0 == (f % 2));
  394. DbgWithLink_DualActive("TerminalNo: %s, lastByte: %d, MainServer:%d", str, f, isMainServer);
  395. }
  396. DbgWithLink_DualActive("mod %s using option %d", connectInfo->m_EntityName, option);
  397. if (0 == strlen(connectInfo->m_Server_BackupIP) && 0 == strlen(connectInfo->m_ServerIP)) {
  398. connectInfo->m_currentLink = -1;
  399. } else if (0 == strlen(connectInfo->m_Server_BackupIP))
  400. {//serverBackup Ip and port error, link to server
  401. connectInfo->m_currentLink = (Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option) ? 0 : -1);//Link to first one
  402. DbgWithLink_DualActive("%s serverBackup param error", connectInfo->m_EntityName);
  403. }
  404. else
  405. {
  406. if (isMainServer)
  407. {//link to first server
  408. if (Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option))
  409. connectInfo->m_currentLink = 0;
  410. else if (
  411. strcmp(connectInfo->m_ServerIP, connectInfo->m_Server_BackupIP)
  412. && Connect(connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, option)
  413. ) {
  414. connectInfo->m_currentLink = 1;
  415. }
  416. else {
  417. connectInfo->m_currentLink = -1;
  418. }
  419. }
  420. else
  421. {//link to second server
  422. DbgWithLink_DualActive("BackUp");
  423. if (Connect(connectInfo->m_Server_BackupIP, connectInfo->m_Server_BackupPort, option))
  424. connectInfo->m_currentLink = 1;
  425. else if (strcmp(connectInfo->m_ServerIP, connectInfo->m_Server_BackupIP)
  426. && Connect(connectInfo->m_ServerIP, connectInfo->m_ServerPort, option))
  427. connectInfo->m_currentLink = 0;
  428. else
  429. connectInfo->m_currentLink = -1;
  430. }
  431. }
  432. }
  433. /*
  434. 连接结束
  435. */
  436. char msg[1024];
  437. sprintf(msg, "%sDual Connection: EntityName:%s, server:%s %d, serverBackup:%s %d, DualOpen: %d, currentLink:%d, curEntityNum:%d", (quickConnect ? "quick " : ""),
  438. connectInfo->m_EntityName, connectInfo->m_ServerIP, connectInfo->m_ServerPort, connectInfo->m_Server_BackupIP,
  439. connectInfo->m_Server_BackupPort, connectInfo->m_DualActive, connectInfo->m_currentLink, entityConnect->getEntityNum());
  440. DbgWithLink_DualActive(msg);
  441. setConnectInfo(connectInfo);
  442. return (-1 != connectInfo->m_currentLink);
  443. }
  444. bool SpSecureClient::IsConnectionOK()
  445. {
  446. return GetFunction()->IsConnectionOK();
  447. }
  448. bool SpSecureClient::IsSecureConn()
  449. {
  450. return GetFunction()->IsSecureConnection();
  451. }
  452. void SpSecureClient::Close()
  453. {
  454. if (!m_bClosed)
  455. {
  456. m_bClosed = true;
  457. GetFunction()->Close();
  458. //clear the connect Info
  459. connectClose(m_pEntity->GetEntityName());
  460. DbgWithLink_DualActive("%s closed", m_pEntity->GetEntityName());
  461. }
  462. }
  463. string SpSecureClient::SendPackage(const CSmartPointer<IPackage>& pSendPkg)
  464. {
  465. return GetFunction()->SendPackage(pSendPkg);
  466. }
  467. CSmartPointer<IPackage> SpSecureClient::ReceivePackage(int nWaitSecond)
  468. {
  469. return GetFunction()->ReceivePackage(nWaitSecond);
  470. }
  471. CSmartPointer<IPackage> SpSecureClient::CreateNewPackage(const char *pServiceCode)
  472. {
  473. return GetFunction()->CreateNewPackage(pServiceCode);
  474. }
  475. CSmartPointer<IPackage> SpSecureClient::CreateReplyPackage(const CSmartPointer<IPackage>& pRecvPkg)
  476. {
  477. return GetFunction()->CreateReplyPackage(pRecvPkg);
  478. }
  479. void SpSecureClient::OnErrorMsg(DWORD dwSysCode, DWORD dwUserCode, const CSimpleStringA &pErrMsg)
  480. {
  481. // 切换到实体线程执行
  482. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("SpSecureClient error: %s", (const char*)pErrMsg);
  483. }
  484. void SpSecureClient::OnDisconnect()
  485. {
  486. DbgWithLink_DualActive("Entity %s OnDisconnect", m_pEntity->GetEntityName());
  487. }
  488. void SpSecureClient::OnReConnect()
  489. {
  490. if (!IsConnectionOK())
  491. {//可能OnDisconnect被重载,已重新连接
  492. DbgWithLink_DualActive("Entity %s disconnect, try to reconnect", m_pEntity->GetEntityName());
  493. ConnectFromCentralSetting();
  494. }
  495. }
  496. struct OnPackageTask : public ITaskSp
  497. {
  498. OnPackageTask(SpSecureClient *pClient, IPackage* pkg)
  499. :m_pSecureClient(pClient), m_pPackage(pkg)
  500. {}
  501. SpSecureClient *m_pSecureClient;
  502. IPackage * m_pPackage;
  503. virtual void Process()
  504. {
  505. TOOLKIT_ASSERT(m_pPackage != NULL);
  506. CSmartPointer<IPackage> ptr;
  507. ptr.Attach(m_pPackage);
  508. m_pSecureClient->OnPkgAnswer(ptr);
  509. m_pSecureClient->DecRefCount();
  510. }
  511. };
  512. struct OnDisconnectTask : public ITaskSp
  513. {
  514. OnDisconnectTask(SpSecureClient *pClient)
  515. :m_pSecureClient(pClient)
  516. {}
  517. SpSecureClient *m_pSecureClient;
  518. virtual void Process()
  519. {
  520. m_pSecureClient->Close();
  521. m_pSecureClient->OnDisconnect();
  522. m_pSecureClient->DecRefCount();
  523. m_pSecureClient->OnReConnect();
  524. }
  525. };
  526. struct OnErrorTask : public ITaskSp
  527. {
  528. OnErrorTask(SpSecureClient *pClient, DWORD dwSysCode, DWORD dwUserCode, const char *pMsg)
  529. :m_pSecureClient(pClient), m_dwSysCode(dwSysCode), m_dwUserCode(dwUserCode), m_strErrMsg(pMsg)
  530. {}
  531. SpSecureClient *m_pSecureClient;
  532. DWORD m_dwSysCode;
  533. DWORD m_dwUserCode;
  534. CSimpleStringA m_strErrMsg;
  535. virtual void Process()
  536. {
  537. m_pSecureClient->OnErrorMsg(m_dwSysCode, m_dwUserCode, m_strErrMsg);
  538. m_pSecureClient->DecRefCount();
  539. }
  540. };
  541. void SpSecureClient::OnReceivePackage(CSmartPointer<IPackage> &pRecvPkg)
  542. {
  543. // 切换到实体线程执行
  544. OnPackageTask *pTask = new OnPackageTask(this, pRecvPkg.Detach());
  545. IncRefCount();
  546. m_pEntity->GetFunction()->PostEntityTaskFIFO(pTask);
  547. }
  548. void SpSecureClient::OnClose()
  549. {
  550. // 切换到实体线程执行
  551. OnDisconnectTask *pTask = new OnDisconnectTask(this);
  552. IncRefCount();
  553. m_pEntity->GetFunction()->PostEntityTaskFIFO(pTask);
  554. }
  555. void SpSecureClient::OnError(DWORD dwSysCode, DWORD dwUserCode, const char *pErrMsg)
  556. {
  557. // 切换到实体线程执行
  558. OnErrorTask *pTask = new OnErrorTask(this, dwSysCode, dwUserCode, pErrMsg);
  559. IncRefCount();
  560. m_pEntity->GetFunction()->PostEntityTaskFIFO(pTask);
  561. }
  562. void SpSecureClient::OnAuthPass()
  563. {
  564. }
  565. bool SpSecureClient::OnAuthRequest(CConnAuthReq *pReq)
  566. {
  567. ErrorCodeEnum Error = Error_Unexpect;
  568. // prepare verify info
  569. CSystemStaticInfo si;
  570. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  571. CVerifyInfo info;
  572. memset(&info, 0, sizeof(info));
  573. strncpy(info.m_arrTerminalNo, (LPCSTR)si.strTerminalID, sizeof(info.m_arrTerminalNo)-1);
  574. strcpy(info.m_arrServiceType, "CMBRVC");
  575. {
  576. #ifdef RVC_OS_WIN
  577. hostent *ent = _gethostbyname(NULL);
  578. if (ent && ent->h_addr_list[0] != NULL)
  579. {
  580. int i = 0;
  581. for (; ent->h_addr_list[i] != NULL; ++i) {
  582. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  583. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  584. break;
  585. }
  586. if (ent->h_addr_list[i] == NULL)
  587. i = 0;
  588. auto in = (struct in_addr*)ent->h_addr_list[i];
  589. info.m_arrIP[0] = in->S_un.S_un_b.s_b1;
  590. info.m_arrIP[1] = in->S_un.S_un_b.s_b2;
  591. info.m_arrIP[2] = in->S_un.S_un_b.s_b3;
  592. info.m_arrIP[3] = in->S_un.S_un_b.s_b4;
  593. }
  594. #else
  595. char buf[512];
  596. toolkit_interface_address_t* addr;
  597. int count, i, aim = -1, backup = -1, third = -1;
  598. toolkit_interface_addresses(&addr, &count);
  599. i = count;
  600. WLog_DBG(TAG, "Number of interfaces: %d", count);
  601. while (i--) {
  602. toolkit_interface_address_t interface = addr[i];
  603. WLog_DBG(TAG, "Name: %s", interface.name);
  604. WLog_DBG(TAG, "Internal? %s", interface.is_internal ? "Yes" : "No");
  605. if (interface.address.address4.sin_family == AF_INET) {
  606. third = i;
  607. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  608. WLog_DBG(TAG, "IPv4 address: %s", buf);
  609. CSimpleStringA s(buf);
  610. if (s.IsStartWith("99.") || s.IsStartWith("10.")) {
  611. aim = i;
  612. break;
  613. }
  614. if (!interface.is_internal && backup != -1) {
  615. backup = i;
  616. }
  617. } else if (interface.address.address4.sin_family == AF_INET6) {
  618. toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
  619. WLog_DBG(TAG, "IPv6 address: %s", buf);
  620. }
  621. }
  622. if (aim == -1 && backup != -1) {
  623. aim = backup;
  624. }
  625. if (aim == -1 && third != -1)
  626. aim = third;
  627. memset(info.m_arrIP, 0, sizeof(info.m_arrIP));
  628. if (aim != -1) {
  629. char ipStr[32];
  630. int b1, b2, b3, b4;
  631. b1 = b2 = b3 = b4 = 0;
  632. toolkit_ip4_name(&addr[aim].address.address4, ipStr, sizeof(ipStr));
  633. WLog_DBG(TAG, "specified local ip: %s", ipStr);
  634. sscanf(ipStr, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
  635. info.m_arrIP[0] = (BYTE)b1;
  636. info.m_arrIP[1] = (BYTE)b2;
  637. info.m_arrIP[2] = (BYTE)b3;
  638. info.m_arrIP[3] = (BYTE)b4;
  639. }
  640. toolkit_free_interface_addresses(addr, count);
  641. #endif
  642. }
  643. if (pReq != NULL)
  644. {
  645. // call token service to encrypt verify info
  646. int nDestLen = sizeof(pReq->m_arrVerifyInfo);
  647. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  648. Error = client->Connect();
  649. if (Error == Error_Succeed) {
  650. TokenService_EncryptWithPubKey_Req Req;
  651. TokenService_EncryptWithPubKey_Ans Ans;
  652. Req.raw_data.m_pData = (char*)&info;
  653. Req.raw_data.m_iLength = sizeof(info); //28
  654. Error = client->EncryptWithPubKey(Req, Ans, -1);
  655. if (Error != Error_Succeed) {
  656. LOG_TRACE("EncryptWithPriKey for VerifyInfo failed! Error = %s", SpStrError(Error));
  657. } else {
  658. memcpy_s(pReq->m_arrVerifyInfo, sizeof(pReq->m_arrVerifyInfo), Ans.enc_data.m_pData, Ans.enc_data.m_iLength);
  659. // get token
  660. TokenService_GetToken_Req Req1;
  661. TokenService_GetToken_Ans Ans1;
  662. Error = client-> GetToken(Req1, Ans1, -1);
  663. if (Error != Error_Succeed) {
  664. LOG_TRACE("GetToken for VerifyInfo failed! Error = %s", SpStrError(Error));
  665. } else {
  666. memcpy_s(pReq->m_arrVerifyToken, sizeof(pReq->m_arrVerifyToken), Ans1.token.m_pData, Ans1.token.m_iLength);
  667. }
  668. }
  669. client->GetFunction()->CloseSession();
  670. } else {
  671. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  672. client->SafeDelete();
  673. }
  674. }
  675. return Error == Error_Succeed;
  676. }
  677. bool SpSecureClient::OnAuthRequestSM(CConnAuthSMReq* pReq)
  678. {
  679. ErrorCodeEnum Error = Error_Unexpect;
  680. CSystemStaticInfo si;
  681. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  682. CVerifyInfo info;
  683. memset(&info, 0, sizeof(info));
  684. strncpy(info.m_arrTerminalNo, (LPCSTR)si.strTerminalID, sizeof(info.m_arrTerminalNo) - 1);
  685. strcpy(info.m_arrServiceType, "CMBRVC");
  686. {
  687. #ifdef RVC_OS_LINUX
  688. char buf[512];
  689. toolkit_interface_address_t* addr;
  690. int count, i, aim = -1, backup = -1, third=-1;
  691. toolkit_interface_addresses(&addr, &count);
  692. i = count;
  693. WLog_DBG(TAG, "Number of interfaces: %d", count);
  694. while (i--) {
  695. toolkit_interface_address_t interface = addr[i];
  696. WLog_DBG(TAG, "Name: %s", interface.name);
  697. WLog_DBG(TAG, "Internal? %s", interface.is_internal ? "Yes" : "No");
  698. if (interface.address.address4.sin_family == AF_INET) {
  699. third = i;
  700. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  701. WLog_DBG(TAG, "IPv4 address: %s", buf);
  702. CSimpleStringA s(buf);
  703. if (s.IsStartWith("99.") || s.IsStartWith("10.")) {
  704. aim = i;
  705. break;
  706. }
  707. if (!interface.is_internal && backup != -1) {
  708. backup = i;
  709. }
  710. } else if (interface.address.address4.sin_family == AF_INET6) {
  711. toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
  712. WLog_DBG(TAG, "IPv6 address: %s", buf);
  713. }
  714. }
  715. if (aim == -1 && backup != -1) {
  716. aim = backup;
  717. }
  718. if (aim == -1 && third != -1)
  719. aim = third;
  720. memset(info.m_arrIP, 0, sizeof(info.m_arrIP));
  721. if (aim != -1) {
  722. char ipStr[32];
  723. int b1, b2, b3, b4;
  724. b1 = b2 = b3 = b4 = 0;
  725. toolkit_ip4_name(&addr[aim].address.address4, ipStr, sizeof(ipStr));
  726. WLog_DBG(TAG, "specified local ip: %s", ipStr);
  727. sscanf(ipStr, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
  728. info.m_arrIP[0] = (BYTE)b1;
  729. info.m_arrIP[1] = (BYTE)b2;
  730. info.m_arrIP[2] = (BYTE)b3;
  731. info.m_arrIP[3] = (BYTE)b4;
  732. }
  733. toolkit_free_interface_addresses(addr, count);
  734. #else
  735. hostent* ent = _gethostbyname(NULL);
  736. WLog_DBG(TAG, "_gethostbyname finished ");
  737. if (ent && ent->h_addr_list[0] != NULL) {
  738. int i = 0;
  739. WLog_DBG(TAG, "office hostname: %s", ent->h_name);
  740. for (; ent->h_addr_list[i] != NULL; ++i) {
  741. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  742. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  743. break;
  744. }
  745. if (ent->h_addr_list[i] == NULL)
  746. i = 0;
  747. auto in = (struct in_addr*)ent->h_addr_list[i];
  748. info.m_arrIP[0] = in->S_un.S_un_b.s_b1;
  749. info.m_arrIP[1] = in->S_un.S_un_b.s_b2;
  750. info.m_arrIP[2] = in->S_un.S_un_b.s_b3;
  751. info.m_arrIP[3] = in->S_un.S_un_b.s_b4;
  752. }
  753. #endif //RVC_OS_LINUX
  754. }
  755. if(pReq != NULL)
  756. {
  757. // call token service to encrypt verify info
  758. int nDestLen = sizeof(pReq->m_arrVerifyInfo);
  759. TokenService_ClientBase* client = new TokenService_ClientBase(m_pEntity);
  760. Error = client->Connect();
  761. if (Error == Error_Succeed)
  762. {
  763. TokenService_EncryptWithPubKey_Req Req;
  764. TokenService_EncryptWithPubKey_Ans Ans;
  765. Req.raw_data.m_pData = (char*)&info;
  766. Req.raw_data.m_iLength = sizeof(info); //28
  767. Error = client->EncryptWithPubKey(Req, Ans, -1);
  768. if (Error != Error_Succeed) {
  769. LOG_TRACE("EncryptWithPriKey for VerifyInfo failed! Error = %s", SpStrError(Error));
  770. } else {
  771. memcpy_s(pReq->m_arrVerifyInfo, sizeof(pReq->m_arrVerifyInfo), Ans.enc_data.m_pData, Ans.enc_data.m_iLength);
  772. // get token
  773. TokenService_GetToken_Req Req1;
  774. TokenService_GetToken_Ans Ans1;
  775. Error = client->GetToken(Req1, Ans1, -1);
  776. if (Error != Error_Succeed) {
  777. LOG_TRACE("GetToken for VerifyInfo failed! Error = %s", SpStrError(Error));
  778. }
  779. else {
  780. memcpy_s(pReq->m_arrVerifyToken, sizeof(pReq->m_arrVerifyToken), Ans1.token.m_pData, Ans1.token.m_iLength);
  781. }
  782. }
  783. client->GetFunction()->CloseSession();
  784. }
  785. else {
  786. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  787. client->SafeDelete();
  788. }
  789. }
  790. return Error == Error_Succeed;
  791. }
  792. bool SpSecureClient::OnSessionKeyRet(CConnAuthRet *pRet, BYTE *pBuf, int *pBufLen)
  793. {
  794. memset(pBuf, 0, *pBufLen);
  795. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  796. ErrorCodeEnum Error = client->Connect();
  797. if (Error == Error_Succeed)
  798. {
  799. TokenService_DecryptWithPriKey_Req Req;
  800. TokenService_DecryptWithPriKey_Ans Ans;
  801. Req.enc_data.m_pData = (char*)&pRet->m_arrEncSessionKey[0];
  802. Req.enc_data.m_iLength = sizeof(pRet->m_arrEncSessionKey);
  803. Error = client->DecryptWithPriKey(Req, Ans, -1);
  804. if (Error == Error_Succeed)
  805. {
  806. memcpy_s(pBuf, *pBufLen, Ans.raw_data.m_pData, Ans.raw_data.m_iLength);
  807. *pBufLen = Ans.raw_data.m_iLength;
  808. } else {
  809. *pBufLen = 0;
  810. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  811. }
  812. client->GetFunction()->CloseSession();
  813. } else {
  814. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  815. client->SafeDelete();
  816. }
  817. return Error == Error_Succeed;
  818. }
  819. bool SpSecureClient::OnSessionKeySMRet(CConnAuthSMRet* pRet, BYTE* pBuf, int* pBufLen)
  820. {
  821. memset(pBuf, 0, *pBufLen);
  822. TokenService_ClientBase* client = new TokenService_ClientBase(m_pEntity);
  823. ErrorCodeEnum Error = client->Connect();
  824. if (Error == Error_Succeed)
  825. {
  826. TokenService_DecryptWithPriKey_Req Req;
  827. TokenService_DecryptWithPriKey_Ans Ans;
  828. Req.enc_data.m_pData = (char*)&pRet->m_arrEncSessionKey[0];
  829. Req.enc_data.m_iLength = sizeof(pRet->m_arrEncSessionKey);
  830. Error = client->DecryptWithPriKey(Req, Ans, -1);
  831. if (Error == Error_Succeed)
  832. {
  833. memcpy_s(pBuf, *pBufLen, Ans.raw_data.m_pData, Ans.raw_data.m_iLength);
  834. *pBufLen = Ans.raw_data.m_iLength;
  835. }
  836. else {
  837. *pBufLen = 0;
  838. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  839. }
  840. client->GetFunction()->CloseSession();
  841. } else {
  842. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  843. client->SafeDelete();
  844. }
  845. return Error == Error_Succeed;
  846. }
  847. bool SpSecureClient::OnGetSharedSK(char *pTerminalNo, int *pTerminalNoLen, BYTE *pBuf, int *pBufLen)
  848. {
  849. #ifdef _MSC_VER
  850. ErrorCodeEnum Error = Error_Unexpect;
  851. CSystemStaticInfo si;
  852. {
  853. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  854. }
  855. TOOLKIT_ASSERT(si.strTerminalID.GetLength() <= *pTerminalNoLen);
  856. strcpy(pTerminalNo, (LPCSTR)si.strTerminalID);
  857. *pTerminalNoLen = strlen(pTerminalNo);
  858. {
  859. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  860. Error = client->Connect();
  861. if (Error == Error_Succeed) {
  862. TokenService_GetSharedSK_Req Req;
  863. TokenService_GetSharedSK_Ans Ans;
  864. Error = client->GetSharedSK(Req, Ans, 10000);
  865. if (Error == Error_Succeed) {
  866. memcpy_s(pBuf, *pBufLen, Ans.ssk.m_pData, Ans.ssk.m_iLength);
  867. *pBufLen = Ans.ssk.m_iLength;
  868. } else {
  869. *pBufLen = 0;
  870. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  871. }
  872. client->GetFunction()->CloseSession();
  873. } else {
  874. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  875. client->SafeDelete();
  876. }
  877. }
  878. return Error == Error_Succeed;
  879. #else
  880. return false;
  881. #endif //_MSC_VER
  882. }
  883. ErrorCodeEnum SpSecureClient::ParseIPAddress(const char *str, CSimpleStringA &ip, int &port)
  884. {
  885. ErrorCodeEnum Error = Error_Unexpect;
  886. if (str) {
  887. char tmp1[32] = {};
  888. char tmp2[16] = {};
  889. sscanf(str, "%s %s", tmp1, tmp2);
  890. if (tmp1[0] != 0 && tmp2[0] != 0)
  891. {
  892. ip = tmp1;
  893. port = atoi(tmp2);
  894. Error = Error_Succeed;
  895. }
  896. else
  897. return Error;
  898. }
  899. return Error;
  900. }
  901. DWORD SpSecureClient::GetEntityUserCode(DWORD code)
  902. {
  903. TOOLKIT_ASSERT(m_pEntity != NULL);
  904. auto pEnv = sp_get_env();
  905. TOOLKIT_ASSERT(pEnv != NULL);
  906. auto pCfg = pEnv->cfg;
  907. TOOLKIT_ASSERT(pCfg != NULL);
  908. auto pEntity = sp_cfg_get_entity_by_name(pCfg, m_pEntity->GetEntityName());
  909. TOOLKIT_ASSERT(pEntity != NULL);
  910. DWORD dwDevelopID = pEntity->devel_id;
  911. return (dwDevelopID << 20 | code);
  912. }