SpSecureClient.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. return;
  23. if (SpSecureClient::m_pEntity->GetFunction()->GetToken(t_channelId, t_token) != ErrorCodeEnum::Error_Succeed ||
  24. SpSecureClient::m_pEntity->GetFunction()->GetSystemStaticInfo(info) != ErrorCodeEnum::Error_Succeed)
  25. return;
  26. if(t_channelId.GetLength() != 0 && t_token.GetLength() != 0 && info.strTerminalID.GetLength() != 0)
  27. {
  28. channelId = t_channelId.GetData();
  29. token = t_token.GetData();
  30. terminalNo = info.strTerminalID.GetData();
  31. }
  32. }
  33. enum CommEventEnum
  34. {
  35. //连接主用地址 事件 1 0xNNN08001 连接配置文件定义的主用地址,需要记录地址信息。如果为短连接,记录首次连接事件。
  36. CE_ConnectMasterServer = 0x00008001,
  37. //连接备用地址 事件 1 0xNNN08002 如果没有定义备用地址,就不会有这条信息
  38. CE_ConnectSlaveServer = 0x00008002,
  39. //主动断开连接 事件 1 0xNNN08011 如果为短连接,无需记录此事件
  40. CE_ActiveClose = 0x00008011,
  41. //读取配置失败 错误 3 0xNNN08021 不能读到需要配置项,代码或配置有误,可以切换备份区域,或需要人干预
  42. CE_ReadCfgFail = 0x00008021,
  43. //连接地址无效 错误 2 0xNNN88031 对端不存在,配置错误,或当前服务器故障
  44. CE_InvalidAddr = 0x00088031,
  45. //对端拒绝连接 错误 2 0xNNN88032 对端存在,但可能故障,导致服务器拒绝建立连接
  46. CE_PeerReject = 0x00088032,
  47. //连接超时 错误 2 0xNNN88033 等待连接超时
  48. CE_ConnectTimeout = 0x00088033,
  49. //容量拒绝 错误 2 0xNNN88034 由于达到后台的最大容量,连接拒绝,需要重试
  50. CE_ReachMaxNum = 0x00088034,
  51. //服务暂停 错误 2 0xNNN88035 后台暂停服务,实体可以切换主备服务或抛出连接失败,切换备份区域
  52. CE_OutOfService = 0x00088035,
  53. //失去连接 错误 2 0xNNN88036 由于对端或网络原因连接断开
  54. CE_LostConnection = 0x00088036,
  55. //无效请求包 错误 3 0xNNN88041 实体或后台代码错误,需要进行人工修复。在描述中应该说明错误的原因。
  56. CE_InvalidReqPack = 0x00088041,
  57. //鉴权不可识别 错误 3 0xNNN88051 由于服务准入服务证书不符,不能解开请求包,错误需要干预,不能自动修复。
  58. CE_AuthFailed = 0x00088051,
  59. //鉴权信息不匹配 错误 2 0xNNN88052 请求时鉴权信息不匹配,需要重新进行准入。
  60. CE_HashMismatch = 0x00088052,
  61. //当前区域连接失败 事件 2 0xNNN880F1 由于多次尝试失败,或发生不可尝试错误,实体放弃了当前区域连接尝试,用于驱动状态切换。记录区域代码。
  62. CE_CurRegionDisabled = 0x000880F1,
  63. };
  64. void* SpSecureClient::logProducer(NULL);
  65. void SpSecureClient::DbgWithLink_DualActive(const char* formatStr, ...) {
  66. if (formatStr == nullptr)
  67. return;
  68. va_list arg;
  69. va_start(arg, formatStr);
  70. //不这么写?貌似到DbgWithLink内后,arg会被析构?
  71. int n = _vscprintf(formatStr, arg) + 1;
  72. std::vector<char> buf(n + 1, '\0');
  73. vsnprintf(&buf[0], n, formatStr, arg);
  74. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).withLogProducer(logProducer).withExtendLog(false)(&buf[0]);
  75. if(EntityResource::getSaveFile())
  76. m_log.LOGERROR(&buf[0]);
  77. va_end(arg);
  78. }
  79. // 不应该直接delete,而用DecRef()
  80. SpSecureClient::~SpSecureClient()
  81. {
  82. //this would lost the process at UOS plat.
  83. //#ifndef _WIN32
  84. // if (logProducer != NULL) {
  85. // destroy_log_producer_storage(logProducer);
  86. // logProducer = NULL;
  87. // }
  88. //#endif
  89. }
  90. SpSecureClient::SpSecureClient(CEntityBase *pEntityBase)
  91. : m_bClosed(true)
  92. {
  93. m_pEntity = pEntityBase;
  94. if(logProducer == NULL) {
  95. logProducer = create_log_producer_storage("DualActive", "0", "", "", "");
  96. }
  97. }
  98. std::pair<bool, std::string> FromAddressToIP(std::string host) {
  99. struct hostent* ht = NULL;
  100. ht = gethostbyname(host.c_str());
  101. if (ht) {
  102. for (int i = 0; ; i++) {
  103. if (ht->h_addr_list[i] != NULL)
  104. return std::make_pair(true, inet_ntoa(*((struct in_addr*)ht->h_addr_list[i])));
  105. else
  106. break;
  107. }
  108. for (int j = 0; ; j++) {
  109. if (ht->h_aliases[j] != NULL)
  110. return std::make_pair(true, ht->h_aliases[j]);
  111. else
  112. break;
  113. }
  114. }
  115. return std::make_pair(false, "");
  116. }
  117. bool SpSecureClient::Connect(const char *pServerAddr, int nPort, int nOption) // wait until all authentication operation has been finished
  118. {
  119. TOOLKIT_ASSERT(m_pEntity != NULL);
  120. auto ipRet = FromAddressToIP(pServerAddr);
  121. std::string dstIp = ipRet.first ? ipRet.second : pServerAddr;
  122. bool result = false;
  123. if (GetFunction()->Connect(dstIp.c_str(), nPort, nOption))
  124. {
  125. m_bClosed = false;
  126. result = true;
  127. }
  128. else {
  129. DbgWithLink_DualActive("connect to %s:%d, option=%d, result:%s", dstIp.c_str(), nPort, nOption, result ? "SUCCESS" : "FAIL");
  130. result = false;
  131. }
  132. return result;
  133. }
  134. // 使用配置项连接, 连接参数从实体配置文件中读取 [Main]/Server和[Main]/Server_Backup项
  135. // @option:1、重新鉴权新建会话密钥;2、通过握手使用缓存会话密钥;
  136. // 3、不使用会话密钥,即非安全通道; 4、不协商,直接使用共享会话密钥
  137. bool SpSecureClient::ConnectFromConfig(int nOption)
  138. {
  139. DbgWithLink_DualActive("ConnectFromConfig has been deprecated.");
  140. return false;
  141. if (IsConnectionOK())
  142. return true;
  143. const char* pEntityName = m_pEntity->GetEntityName();
  144. ENTITY_CONNECT_INFO connectInfo;
  145. if (!readConnectInfoFromConfig(pEntityName, &connectInfo))
  146. {
  147. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Read %s Info Failed", pEntityName);
  148. return false;
  149. }
  150. // 判断当前是否已经准入,已准入时强制使用加密通道
  151. int nActOption = nOption;
  152. CSimpleStringA strVal;
  153. const ErrorCodeEnum result = m_pEntity->GetFunction()->GetSysVar("EntryPermit", strVal);
  154. if (Error_Succeed == result) {
  155. if (strVal == "L" && stricmp(pEntityName, "AccessAuthorization") != 0 && stricmp(pEntityName, "Initializer") != 0)
  156. nActOption = 2; // 优先使用缓存密钥
  157. }
  158. if (1 == connectInfo.m_DualActive)
  159. return Connect_Dual(&connectInfo, nOption);
  160. else
  161. return Connect_Single(&connectInfo, nOption);
  162. }
  163. bool SpSecureClient::readConnectInfoFromConfig(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo)
  164. {
  165. if (NULL == pEntityName || NULL == connectInfo)
  166. return false;
  167. connectControl *entityConnect = connectControl::getInstance();
  168. bool readSuccess = entityConnect->getEntityInfo(pEntityName, connectInfo);
  169. auto pFunc = m_pEntity->GetFunction();
  170. CSmartPointer<IConfigInfo> spConfig;
  171. if (Error_Succeed != pFunc->OpenConfig(Config_CenterSetting, spConfig))
  172. {
  173. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("open center setting config fail");
  174. return false;
  175. }
  176. CSimpleStringA str;
  177. int readEach = 0;
  178. if (Error_Succeed == spConfig->ReadConfigValue("SpBase", "ReadEach", str) && !str.IsNullOrEmpty())
  179. readEach = atoi(str);
  180. if (0 == readEach && readSuccess)
  181. return true;//不是每次都读取新的Entity的IP和Port,只在实体启动时读取一次
  182. int dualActive = -1;
  183. if (Error_Succeed == spConfig->ReadConfigValue("SpBase", "DualActive", str) && !str.IsNullOrEmpty())
  184. dualActive = atoi(str);
  185. if (Error_Succeed != pFunc->OpenConfig(Config_Software, spConfig))
  186. {
  187. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("open entity config fail");
  188. return false;
  189. }
  190. if (Error_Succeed != spConfig->ReadConfigValue("Main", "Server", str) || str.IsNullOrEmpty())
  191. {
  192. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Main::Server config fail");
  193. return false;
  194. }
  195. CSimpleStringA strServer1;
  196. int nServer1Port;
  197. if (Error_Succeed != ParseIPAddress(str, strServer1, nServer1Port))
  198. {
  199. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("parse ip addr fail: %s", (const char*)str);
  200. return false;
  201. }
  202. CSimpleStringA strServer2;
  203. int nServer2Port = 0;
  204. if (Error_Succeed == spConfig->ReadConfigValue("Main", "Server_Backup", str) && !str.IsNullOrEmpty())
  205. ParseIPAddress(str, strServer2, nServer2Port);
  206. if (readSuccess)
  207. connectInfo->setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, dualActive, connectInfo->m_currentLink);
  208. else
  209. {
  210. connectInfo->clear();
  211. connectInfo->setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, dualActive);
  212. }
  213. entityConnect->setEntityInfo(connectInfo);
  214. return true;
  215. }
  216. void SpSecureClient::connectClose(const char *pEntityName)
  217. {
  218. if (NULL == pEntityName)
  219. return;
  220. ENTITY_CONNECT_INFO connectInfo;
  221. if (!findConnectInfo(pEntityName, &connectInfo))
  222. {
  223. DbgWithLink_DualActive("can not find %s in connection list.", pEntityName);
  224. return;
  225. }
  226. connectInfo.m_currentLink = -1;
  227. }
  228. bool SpSecureClient::findConnectInfo(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo)
  229. {
  230. if (NULL == pEntityName || NULL == connectInfo)
  231. return false;
  232. connectControl *entityConnect = connectControl::getInstance();
  233. if (entityConnect->getEntityInfo(pEntityName, connectInfo))
  234. return true;
  235. return false;
  236. }
  237. bool SpSecureClient::readConnectInfoFromCentralSetting(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo)
  238. {
  239. if (NULL == pEntityName || NULL == connectInfo)
  240. return false;
  241. connectControl *entityConnect = connectControl::getInstance();
  242. bool readSuccess = entityConnect->getEntityInfo(pEntityName, connectInfo);
  243. //获得集中配置信息
  244. auto pFunc = m_pEntity->GetFunction();
  245. CSmartPointer<IConfigInfo> spConfig;
  246. if (Error_Succeed != pFunc->OpenConfig(Config_CenterSetting, spConfig))
  247. {
  248. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("open center setting config fail");
  249. return false;
  250. }
  251. CSimpleStringA str;
  252. int readEach = 1;
  253. if (Error_Succeed == spConfig->ReadConfigValue("SpBase", "ReadEach", str) && !str.IsNullOrEmpty())
  254. readEach = atoi(str);
  255. if (0 == readEach && readSuccess)
  256. return true;//不是每次都读取新的Entity的IP和Port,只在实体启动时读取一次
  257. ErrorCodeEnum configRc = Error_Bug;
  258. if (Error_Succeed != (configRc = spConfig->ReadConfigValue(pEntityName, "Server", str)) || str.IsNullOrEmpty())
  259. {
  260. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Main::Server config fail, %d, %s", configRc, pEntityName);
  261. return false;
  262. }
  263. CSimpleStringA strServer1;
  264. int nServer1Port;
  265. if (Error_Succeed != (configRc = ParseIPAddress(str, strServer1, nServer1Port)))
  266. {
  267. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("parse ip addr fail: %s, %d", (const char*)str, configRc);
  268. return false;
  269. }
  270. CSimpleStringA strServer2 = "";
  271. int nServer2Port = 0;
  272. if (Error_Succeed == spConfig->ReadConfigValue(pEntityName, "Server_Backup", str) && !str.IsNullOrEmpty())
  273. ParseIPAddress(str, strServer2, nServer2Port);
  274. int dualActive = -1;
  275. if (Error_Succeed == spConfig->ReadConfigValue("SpBase", "DualActive", str) && !str.IsNullOrEmpty())
  276. dualActive = atoi(str);
  277. if (readSuccess)
  278. connectInfo->setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, dualActive, connectInfo->m_currentLink);
  279. else
  280. {
  281. //DbgWithLink_DualActive("call clear");
  282. connectInfo->clear();
  283. connectInfo->setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, dualActive);
  284. }
  285. entityConnect->setEntityInfo(connectInfo);
  286. return true;
  287. }
  288. bool SpSecureClient::setConnectInfo(const ENTITY_CONNECT_INFO *connectInfo)
  289. {
  290. if (NULL == connectInfo)
  291. return false;
  292. connectControl *entityConnect = connectControl::getInstance();
  293. entityConnect->setLastLink(connectInfo->m_currentLink);
  294. return entityConnect->setEntityInfo(connectInfo);
  295. }
  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. //TODO: CrossPlaform [Gifur@2025729]
  578. #ifdef RVC_OS_WIN
  579. hostent *ent = _gethostbyname(NULL);
  580. if (ent && ent->h_addr_list[0] != NULL)
  581. {
  582. int i = 0;
  583. for (; ent->h_addr_list[i] != NULL; ++i) {
  584. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  585. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  586. break;
  587. }
  588. if (ent->h_addr_list[i] == NULL)
  589. i = 0;
  590. auto in = (struct in_addr*)ent->h_addr_list[i];
  591. info.m_arrIP[0] = in->S_un.S_un_b.s_b1;
  592. info.m_arrIP[1] = in->S_un.S_un_b.s_b2;
  593. info.m_arrIP[2] = in->S_un.S_un_b.s_b3;
  594. info.m_arrIP[3] = in->S_un.S_un_b.s_b4;
  595. }
  596. #else
  597. char buf[512];
  598. toolkit_interface_address_t* addr;
  599. int count, i, aim = -1, backup = -1, third = -1;
  600. toolkit_interface_addresses(&addr, &count);
  601. i = count;
  602. WLog_DBG(TAG, "Number of interfaces: %d", count);
  603. while (i--) {
  604. toolkit_interface_address_t interface = addr[i];
  605. WLog_DBG(TAG, "Name: %s", interface.name);
  606. WLog_DBG(TAG, "Internal? %s", interface.is_internal ? "Yes" : "No");
  607. if (interface.address.address4.sin_family == AF_INET) {
  608. third = i;
  609. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  610. WLog_DBG(TAG, "IPv4 address: %s", buf);
  611. CSimpleStringA s(buf);
  612. if (s.IsStartWith("99.") || s.IsStartWith("10.")) {
  613. aim = i;
  614. break;
  615. }
  616. if (!interface.is_internal && backup != -1) {
  617. backup = i;
  618. }
  619. } else if (interface.address.address4.sin_family == AF_INET6) {
  620. toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
  621. WLog_DBG(TAG, "IPv6 address: %s", buf);
  622. }
  623. }
  624. if (aim == -1 && backup != -1) {
  625. aim = backup;
  626. }
  627. if (aim == -1 && third != -1)
  628. aim = third;
  629. memset(info.m_arrIP, 0, sizeof(info.m_arrIP));
  630. if (aim != -1) {
  631. char ipStr[32];
  632. int b1, b2, b3, b4;
  633. b1 = b2 = b3 = b4 = 0;
  634. toolkit_ip4_name(&addr[aim].address.address4, ipStr, sizeof(ipStr));
  635. WLog_DBG(TAG, "specified local ip: %s", ipStr);
  636. sscanf(ipStr, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
  637. info.m_arrIP[0] = (BYTE)b1;
  638. info.m_arrIP[1] = (BYTE)b2;
  639. info.m_arrIP[2] = (BYTE)b3;
  640. info.m_arrIP[3] = (BYTE)b4;
  641. }
  642. toolkit_free_interface_addresses(addr, count);
  643. #endif
  644. }
  645. if (pReq != NULL)
  646. {
  647. // call token service to encrypt verify info
  648. int nDestLen = sizeof(pReq->m_arrVerifyInfo);
  649. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  650. Error = client->Connect();
  651. if (Error == Error_Succeed) {
  652. TokenService_EncryptWithPubKey_Req Req;
  653. TokenService_EncryptWithPubKey_Ans Ans;
  654. Req.raw_data.m_pData = (char*)&info;
  655. Req.raw_data.m_iLength = sizeof(info); //28
  656. Error = client->EncryptWithPubKey(Req, Ans, -1);
  657. if (Error != Error_Succeed) {
  658. LOG_TRACE("EncryptWithPriKey for VerifyInfo failed! Error = %s", SpStrError(Error));
  659. } else {
  660. memcpy_s(pReq->m_arrVerifyInfo, sizeof(pReq->m_arrVerifyInfo), Ans.enc_data.m_pData, Ans.enc_data.m_iLength);
  661. // get token
  662. TokenService_GetToken_Req Req1;
  663. TokenService_GetToken_Ans Ans1;
  664. Error = client-> GetToken(Req1, Ans1, -1);
  665. if (Error != Error_Succeed) {
  666. LOG_TRACE("GetToken for VerifyInfo failed! Error = %s", SpStrError(Error));
  667. } else {
  668. memcpy_s(pReq->m_arrVerifyToken, sizeof(pReq->m_arrVerifyToken), Ans1.token.m_pData, Ans1.token.m_iLength);
  669. }
  670. }
  671. client->GetFunction()->CloseSession();
  672. } else {
  673. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  674. client->SafeDelete();
  675. }
  676. }
  677. return Error == Error_Succeed;
  678. }
  679. bool SpSecureClient::OnAuthRequestSM(CConnAuthSMReq* pReq)
  680. {
  681. ErrorCodeEnum Error = Error_Unexpect;
  682. CSystemStaticInfo si;
  683. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  684. CVerifyInfo info;
  685. memset(&info, 0, sizeof(info));
  686. strncpy(info.m_arrTerminalNo, (LPCSTR)si.strTerminalID, sizeof(info.m_arrTerminalNo) - 1);
  687. strcpy(info.m_arrServiceType, "CMBRVC");
  688. {
  689. //TODO: CrossPlaform [Gifur@2025730]
  690. #ifdef RVC_OS_LINUX
  691. char buf[512];
  692. toolkit_interface_address_t* addr;
  693. int count, i, aim = -1, backup = -1, third=-1;
  694. toolkit_interface_addresses(&addr, &count);
  695. i = count;
  696. WLog_DBG(TAG, "Number of interfaces: %d", count);
  697. while (i--) {
  698. toolkit_interface_address_t interface = addr[i];
  699. WLog_DBG(TAG, "Name: %s", interface.name);
  700. WLog_DBG(TAG, "Internal? %s", interface.is_internal ? "Yes" : "No");
  701. if (interface.address.address4.sin_family == AF_INET) {
  702. third = i;
  703. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  704. WLog_DBG(TAG, "IPv4 address: %s", buf);
  705. CSimpleStringA s(buf);
  706. if (s.IsStartWith("99.") || s.IsStartWith("10.")) {
  707. aim = i;
  708. break;
  709. }
  710. if (!interface.is_internal && backup != -1) {
  711. backup = i;
  712. }
  713. } else if (interface.address.address4.sin_family == AF_INET6) {
  714. toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
  715. WLog_DBG(TAG, "IPv6 address: %s", buf);
  716. }
  717. }
  718. if (aim == -1 && backup != -1) {
  719. aim = backup;
  720. }
  721. if (aim == -1 && third != -1)
  722. aim = third;
  723. memset(info.m_arrIP, 0, sizeof(info.m_arrIP));
  724. if (aim != -1) {
  725. char ipStr[32];
  726. int b1, b2, b3, b4;
  727. b1 = b2 = b3 = b4 = 0;
  728. toolkit_ip4_name(&addr[aim].address.address4, ipStr, sizeof(ipStr));
  729. WLog_DBG(TAG, "specified local ip: %s", ipStr);
  730. sscanf(ipStr, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
  731. info.m_arrIP[0] = (BYTE)b1;
  732. info.m_arrIP[1] = (BYTE)b2;
  733. info.m_arrIP[2] = (BYTE)b3;
  734. info.m_arrIP[3] = (BYTE)b4;
  735. }
  736. toolkit_free_interface_addresses(addr, count);
  737. #else
  738. hostent* ent = _gethostbyname(NULL);
  739. WLog_DBG(TAG, "_gethostbyname finished ");
  740. if (ent && ent->h_addr_list[0] != NULL) {
  741. int i = 0;
  742. WLog_DBG(TAG, "office hostname: %s", ent->h_name);
  743. for (; ent->h_addr_list[i] != NULL; ++i) {
  744. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  745. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  746. break;
  747. }
  748. if (ent->h_addr_list[i] == NULL)
  749. i = 0;
  750. auto in = (struct in_addr*)ent->h_addr_list[i];
  751. info.m_arrIP[0] = in->S_un.S_un_b.s_b1;
  752. info.m_arrIP[1] = in->S_un.S_un_b.s_b2;
  753. info.m_arrIP[2] = in->S_un.S_un_b.s_b3;
  754. info.m_arrIP[3] = in->S_un.S_un_b.s_b4;
  755. }
  756. #endif //RVC_OS_LINUX
  757. }
  758. if(pReq != NULL)
  759. {
  760. // call token service to encrypt verify info
  761. int nDestLen = sizeof(pReq->m_arrVerifyInfo);
  762. TokenService_ClientBase* client = new TokenService_ClientBase(m_pEntity);
  763. Error = client->Connect();
  764. if (Error == Error_Succeed)
  765. {
  766. TokenService_EncryptWithPubKey_Req Req;
  767. TokenService_EncryptWithPubKey_Ans Ans;
  768. Req.raw_data.m_pData = (char*)&info;
  769. Req.raw_data.m_iLength = sizeof(info); //28
  770. Error = client->EncryptWithPubKey(Req, Ans, -1);
  771. if (Error != Error_Succeed) {
  772. LOG_TRACE("EncryptWithPriKey for VerifyInfo failed! Error = %s", SpStrError(Error));
  773. } else {
  774. memcpy_s(pReq->m_arrVerifyInfo, sizeof(pReq->m_arrVerifyInfo), Ans.enc_data.m_pData, Ans.enc_data.m_iLength);
  775. // get token
  776. TokenService_GetToken_Req Req1;
  777. TokenService_GetToken_Ans Ans1;
  778. Error = client->GetToken(Req1, Ans1, -1);
  779. if (Error != Error_Succeed) {
  780. LOG_TRACE("GetToken for VerifyInfo failed! Error = %s", SpStrError(Error));
  781. }
  782. else {
  783. memcpy_s(pReq->m_arrVerifyToken, sizeof(pReq->m_arrVerifyToken), Ans1.token.m_pData, Ans1.token.m_iLength);
  784. }
  785. }
  786. client->GetFunction()->CloseSession();
  787. }
  788. else {
  789. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  790. client->SafeDelete();
  791. }
  792. }
  793. return Error == Error_Succeed;
  794. }
  795. bool SpSecureClient::OnSessionKeyRet(CConnAuthRet *pRet, BYTE *pBuf, int *pBufLen)
  796. {
  797. memset(pBuf, 0, *pBufLen);
  798. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  799. ErrorCodeEnum Error = client->Connect();
  800. if (Error == Error_Succeed)
  801. {
  802. TokenService_DecryptWithPriKey_Req Req;
  803. TokenService_DecryptWithPriKey_Ans Ans;
  804. Req.enc_data.m_pData = (char*)&pRet->m_arrEncSessionKey[0];
  805. Req.enc_data.m_iLength = sizeof(pRet->m_arrEncSessionKey);
  806. Error = client->DecryptWithPriKey(Req, Ans, -1);
  807. if (Error == Error_Succeed)
  808. {
  809. memcpy_s(pBuf, *pBufLen, Ans.raw_data.m_pData, Ans.raw_data.m_iLength);
  810. *pBufLen = Ans.raw_data.m_iLength;
  811. } else {
  812. *pBufLen = 0;
  813. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  814. }
  815. client->GetFunction()->CloseSession();
  816. } else {
  817. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  818. client->SafeDelete();
  819. }
  820. return Error == Error_Succeed;
  821. }
  822. bool SpSecureClient::OnSessionKeySMRet(CConnAuthSMRet* pRet, BYTE* pBuf, int* pBufLen)
  823. {
  824. memset(pBuf, 0, *pBufLen);
  825. TokenService_ClientBase* client = new TokenService_ClientBase(m_pEntity);
  826. ErrorCodeEnum Error = client->Connect();
  827. if (Error == Error_Succeed)
  828. {
  829. TokenService_DecryptWithPriKey_Req Req;
  830. TokenService_DecryptWithPriKey_Ans Ans;
  831. Req.enc_data.m_pData = (char*)&pRet->m_arrEncSessionKey[0];
  832. Req.enc_data.m_iLength = sizeof(pRet->m_arrEncSessionKey);
  833. Error = client->DecryptWithPriKey(Req, Ans, -1);
  834. if (Error == Error_Succeed)
  835. {
  836. memcpy_s(pBuf, *pBufLen, Ans.raw_data.m_pData, Ans.raw_data.m_iLength);
  837. *pBufLen = Ans.raw_data.m_iLength;
  838. }
  839. else {
  840. *pBufLen = 0;
  841. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  842. }
  843. client->GetFunction()->CloseSession();
  844. } else {
  845. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  846. client->SafeDelete();
  847. }
  848. return Error == Error_Succeed;
  849. }
  850. bool SpSecureClient::OnGetSharedSK(char *pTerminalNo, int *pTerminalNoLen, BYTE *pBuf, int *pBufLen)
  851. {
  852. #ifdef _MSC_VER
  853. ErrorCodeEnum Error = Error_Unexpect;
  854. CSystemStaticInfo si;
  855. {
  856. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  857. }
  858. TOOLKIT_ASSERT(si.strTerminalID.GetLength() <= *pTerminalNoLen);
  859. strcpy(pTerminalNo, (LPCSTR)si.strTerminalID);
  860. *pTerminalNoLen = strlen(pTerminalNo);
  861. {
  862. TokenService_ClientBase *client = new TokenService_ClientBase(m_pEntity);
  863. Error = client->Connect();
  864. if (Error == Error_Succeed) {
  865. TokenService_GetSharedSK_Req Req;
  866. TokenService_GetSharedSK_Ans Ans;
  867. Error = client->GetSharedSK(Req, Ans, 10000);
  868. if (Error == Error_Succeed) {
  869. memcpy_s(pBuf, *pBufLen, Ans.ssk.m_pData, Ans.ssk.m_iLength);
  870. *pBufLen = Ans.ssk.m_iLength;
  871. } else {
  872. *pBufLen = 0;
  873. LOG_TRACE("OnSessionKey decrypt session key failed! Error = %s", SpStrError(Error));
  874. }
  875. client->GetFunction()->CloseSession();
  876. } else {
  877. LOG_TRACE("connect to tokenservice failed! Error = %s", SpStrError(Error));
  878. client->SafeDelete();
  879. }
  880. }
  881. return Error == Error_Succeed;
  882. #else
  883. return false;
  884. #endif //_MSC_VER
  885. }
  886. ErrorCodeEnum SpSecureClient::ParseIPAddress(const char *str, CSimpleStringA &ip, int &port)
  887. {
  888. ErrorCodeEnum Error = Error_Unexpect;
  889. if (str) {
  890. char tmp1[32] = {};
  891. char tmp2[16] = {};
  892. sscanf(str, "%s %s", tmp1, tmp2);
  893. if (tmp1[0] != 0 && tmp2[0] != 0)
  894. {
  895. ip = tmp1;
  896. port = atoi(tmp2);
  897. Error = Error_Succeed;
  898. }
  899. else
  900. return Error;
  901. }
  902. return Error;
  903. }
  904. DWORD SpSecureClient::GetEntityUserCode(DWORD code)
  905. {
  906. TOOLKIT_ASSERT(m_pEntity != NULL);
  907. auto pEnv = sp_get_env();
  908. TOOLKIT_ASSERT(pEnv != NULL);
  909. auto pCfg = pEnv->cfg;
  910. TOOLKIT_ASSERT(pCfg != NULL);
  911. auto pEntity = sp_cfg_get_entity_by_name(pCfg, m_pEntity->GetEntityName());
  912. TOOLKIT_ASSERT(pEntity != NULL);
  913. DWORD dwDevelopID = pEntity->devel_id;
  914. return (dwDevelopID << 20 | code);
  915. }