AzureClient.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // SoftEther VPN Source Code
  2. // Cedar Communication Module
  3. //
  4. // SoftEther VPN Server, Client and Bridge are free software under GPLv2.
  5. //
  6. // Copyright (c) 2012-2014 Daiyuu Nobori.
  7. // Copyright (c) 2012-2014 SoftEther VPN Project, University of Tsukuba, Japan.
  8. // Copyright (c) 2012-2014 SoftEther Corporation.
  9. //
  10. // All Rights Reserved.
  11. //
  12. // http://www.softether.org/
  13. //
  14. // Author: Daiyuu Nobori
  15. // Comments: Tetsuo Sugiyama, Ph.D.
  16. //
  17. // This program is free software; you can redistribute it and/or
  18. // modify it under the terms of the GNU General Public License
  19. // version 2 as published by the Free Software Foundation.
  20. //
  21. // This program is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. // GNU General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU General Public License version 2
  27. // along with this program; if not, write to the Free Software
  28. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  33. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  34. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  35. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  36. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  39. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  40. //
  41. //
  42. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  43. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  44. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  45. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  46. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  47. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  48. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  49. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  50. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  51. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  52. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  53. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  54. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  55. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  56. //
  57. // USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS
  58. // YOU HAVE A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY
  59. // CRIMINAL LAWS OR CIVIL RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS
  60. // SOFTWARE IN OTHER COUNTRIES IS COMPLETELY AT YOUR OWN RISK. THE
  61. // SOFTETHER VPN PROJECT HAS DEVELOPED AND DISTRIBUTED THIS SOFTWARE TO
  62. // COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING CIVIL RIGHTS INCLUDING
  63. // PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER COUNTRIES' LAWS OR
  64. // CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES. WE HAVE
  65. // NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
  66. // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+
  67. // COUNTRIES AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE
  68. // WORLD, WITH DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY
  69. // COUNTRIES' LAWS, REGULATIONS AND CIVIL RIGHTS TO MAKE THE SOFTWARE
  70. // COMPLY WITH ALL COUNTRIES' LAWS BY THE PROJECT. EVEN IF YOU WILL BE
  71. // SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A PUBLIC SERVANT IN YOUR
  72. // COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE LIABLE TO
  73. // RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
  74. // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT
  75. // JUST A STATEMENT FOR WARNING AND DISCLAIMER.
  76. //
  77. //
  78. // SOURCE CODE CONTRIBUTION
  79. // ------------------------
  80. //
  81. // Your contribution to SoftEther VPN Project is much appreciated.
  82. // Please send patches to us through GitHub.
  83. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  84. // http://www.softether.org/5-download/src/9.patch
  85. //
  86. //
  87. // DEAR SECURITY EXPERTS
  88. // ---------------------
  89. //
  90. // If you find a bug or a security vulnerability please kindly inform us
  91. // about the problem immediately so that we can fix the security problem
  92. // to protect a lot of users around the world as soon as possible.
  93. //
  94. // Our e-mail address for security reports is:
  95. // softether-vpn-security [at] softether.org
  96. //
  97. // Please note that the above e-mail address is not a technical support
  98. // inquiry address. If you need technical assistance, please visit
  99. // http://www.softether.org/ and ask your question on the users forum.
  100. //
  101. // Thank you for your cooperation.
  102. //
  103. //
  104. // NO MEMORY OR RESOURCE LEAKS
  105. // ---------------------------
  106. //
  107. // The memory-leaks and resource-leaks verification under the stress
  108. // test has been passed before release this source code.
  109. // AzureClient.c
  110. // VPN Azure Client
  111. #include "CedarPch.h"
  112. // Wait for connection request
  113. void AcWaitForRequest(AZURE_CLIENT *ac, SOCK *s, AZURE_PARAM *param)
  114. {
  115. // Validate arguments
  116. if (ac == NULL || s == NULL || param == NULL)
  117. {
  118. return;
  119. }
  120. while (ac->Halt == false)
  121. {
  122. UCHAR uc;
  123. // Receive 1 byte
  124. if (RecvAll(s, &uc, 1, false) == 0)
  125. {
  126. break;
  127. }
  128. if (uc != 0)
  129. {
  130. // Receive a Pack
  131. PACK *p = RecvPackWithHash(s);
  132. if (p == NULL)
  133. {
  134. break;
  135. }
  136. else
  137. {
  138. // Verify contents of Pack
  139. char opcode[MAX_SIZE];
  140. char cipher_name[MAX_SIZE];
  141. char hostname[MAX_SIZE];
  142. PackGetStr(p, "opcode", opcode, sizeof(opcode));
  143. PackGetStr(p, "cipher_name", cipher_name, sizeof(cipher_name));
  144. PackGetStr(p, "hostname", hostname, sizeof(hostname));
  145. if (StrCmpi(opcode, "relay") == 0)
  146. {
  147. IP client_ip, server_ip;
  148. UINT client_port;
  149. UINT server_port;
  150. UCHAR session_id[SHA1_SIZE];
  151. if (PackGetIp(p, "client_ip", &client_ip) &&
  152. PackGetIp(p, "server_ip", &server_ip) &&
  153. PackGetData2(p, "session_id", session_id, sizeof(session_id)))
  154. {
  155. client_port = PackGetInt(p, "client_port");
  156. server_port = PackGetInt(p, "server_port");
  157. if (client_port != 0 && server_port != 0)
  158. {
  159. SOCK *ns;
  160. Debug("Connect Request from %r:%u\n", &client_ip, client_port);
  161. // Create new socket and connect VPN Azure Server
  162. if (ac->DDnsStatusCopy.InternetSetting.ProxyType == PROXY_DIRECT)
  163. {
  164. ns = ConnectEx2(ac->DDnsStatusCopy.CurrentAzureIp, AZURE_SERVER_PORT,
  165. 0, (bool *)&ac->Halt);
  166. }
  167. else
  168. {
  169. ns = WpcSockConnect2(ac->DDnsStatusCopy.CurrentAzureIp, AZURE_SERVER_PORT,
  170. &ac->DDnsStatusCopy.InternetSetting, NULL, AZURE_VIA_PROXY_TIMEOUT);
  171. }
  172. if (ns == NULL)
  173. {
  174. Debug("Connect Error.\n");
  175. }
  176. else
  177. {
  178. Debug("Connected to the relay server.\n");
  179. SetTimeout(ns, param->DataTimeout);
  180. if (StartSSLEx(ns, NULL, NULL, true, 0, NULL))
  181. {
  182. // Check certification
  183. char server_cert_hash_str[MAX_SIZE];
  184. UCHAR server_cert_hash[SHA1_SIZE];
  185. Zero(server_cert_hash, sizeof(server_cert_hash));
  186. GetXDigest(ns->RemoteX, server_cert_hash, true);
  187. BinToStr(server_cert_hash_str, sizeof(server_cert_hash_str),
  188. server_cert_hash, SHA1_SIZE);
  189. if (IsEmptyStr(ac->DDnsStatusCopy.AzureCertHash) || StrCmpi(server_cert_hash_str, ac->DDnsStatusCopy.AzureCertHash) == 0)
  190. {
  191. if (SendAll(ns, AZURE_PROTOCOL_DATA_SIANGTURE, 24, true))
  192. {
  193. PACK *p2 = NewPack();
  194. PackAddStr(p2, "hostname", hostname);
  195. PackAddData(p2, "session_id", session_id, sizeof(session_id));
  196. if (SendPackWithHash(ns, p2))
  197. {
  198. UCHAR uc;
  199. if (RecvAll(ns, &uc, 1, true) != false)
  200. {
  201. if (uc != 0)
  202. {
  203. SOCK *accept_sock = GetReverseListeningSock(ac->Cedar);
  204. if (accept_sock != NULL)
  205. {
  206. AddRef(ns->ref);
  207. SetTimeout(ns, INFINITE);
  208. Copy(&ns->Reverse_MyServerGlobalIp, &server_ip, sizeof(IP));
  209. ns->Reverse_MyServerPort = server_port;
  210. InjectNewReverseSocketToAccept(accept_sock, ns,
  211. &client_ip, client_port);
  212. ReleaseSock(accept_sock);
  213. }
  214. }
  215. }
  216. }
  217. FreePack(p2);
  218. }
  219. }
  220. }
  221. ReleaseSock(ns);
  222. }
  223. }
  224. }
  225. }
  226. FreePack(p);
  227. }
  228. }
  229. // Send 1 byte
  230. uc = 0;
  231. if (SendAll(s, &uc, 1, false) == 0)
  232. {
  233. break;
  234. }
  235. }
  236. }
  237. // VPN Azure client main thread
  238. void AcMainThread(THREAD *thread, void *param)
  239. {
  240. AZURE_CLIENT *ac = (AZURE_CLIENT *)param;
  241. UINT last_ip_revision = INFINITE;
  242. UINT64 last_reconnect_tick = 0;
  243. UINT64 next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL;
  244. UINT num_reconnect_retry = 0;
  245. UINT64 next_ddns_retry_tick = 0;
  246. bool last_connect_ok = false;
  247. // Validate arguments
  248. if (ac == NULL || thread == NULL)
  249. {
  250. return;
  251. }
  252. while (ac->Halt == false)
  253. {
  254. UINT64 now = Tick64();
  255. bool connect_was_ok = false;
  256. // Wait for enabling VPN Azure function
  257. if (ac->IsEnabled)
  258. {
  259. // VPN Azure is enabled
  260. DDNS_CLIENT_STATUS st;
  261. bool connect_now = false;
  262. bool azure_ip_changed = false;
  263. Lock(ac->Lock);
  264. {
  265. Copy(&st, &ac->DDnsStatus, sizeof(DDNS_CLIENT_STATUS));
  266. if (StrCmpi(st.CurrentAzureIp, ac->DDnsStatusCopy.CurrentAzureIp) != 0)
  267. {
  268. if (IsEmptyStr(st.CurrentAzureIp) == false)
  269. {
  270. // Destination IP address is changed
  271. connect_now = true;
  272. num_reconnect_retry = 0;
  273. }
  274. }
  275. if (StrCmpi(st.CurrentHostName, ac->DDnsStatusCopy.CurrentHostName) != 0)
  276. {
  277. // DDNS host name is changed
  278. connect_now = true;
  279. num_reconnect_retry = 0;
  280. }
  281. Copy(&ac->DDnsStatusCopy, &st, sizeof(DDNS_CLIENT_STATUS));
  282. }
  283. Unlock(ac->Lock);
  284. if (last_ip_revision != ac->IpStatusRevision)
  285. {
  286. last_ip_revision = ac->IpStatusRevision;
  287. connect_now = true;
  288. num_reconnect_retry = 0;
  289. }
  290. if (last_reconnect_tick == 0 || (now >= (last_reconnect_tick + next_reconnect_interval)))
  291. {
  292. UINT r;
  293. last_reconnect_tick = now;
  294. num_reconnect_retry++;
  295. next_reconnect_interval = (UINT64)num_reconnect_retry * AZURE_CONNECT_INITIAL_RETRY_INTERVAL;
  296. next_reconnect_interval = MIN(next_reconnect_interval, AZURE_CONNECT_MAX_RETRY_INTERVAL);
  297. r = (UINT)next_reconnect_interval;
  298. r = GenRandInterval(r / 2, r);
  299. next_reconnect_interval = r;
  300. connect_now = true;
  301. }
  302. if (IsEmptyStr(st.CurrentAzureIp) == false && IsEmptyStr(st.CurrentHostName) == false)
  303. {
  304. if (connect_now)
  305. {
  306. SOCK *s;
  307. char *host = NULL;
  308. UINT port = AZURE_SERVER_PORT;
  309. Debug("VPN Azure: Connecting to %s...\n", st.CurrentAzureIp);
  310. if (ParseHostPort(st.CurrentAzureIp, &host, &port, AZURE_SERVER_PORT))
  311. {
  312. if (st.InternetSetting.ProxyType == PROXY_DIRECT)
  313. {
  314. s = ConnectEx2(host, port, 0, (bool *)&ac->Halt);
  315. }
  316. else
  317. {
  318. s = WpcSockConnect2(host, port, &st.InternetSetting, NULL, AZURE_VIA_PROXY_TIMEOUT);
  319. }
  320. if (s != NULL)
  321. {
  322. PACK *p;
  323. UINT64 established_tick = 0;
  324. Debug("VPN Azure: Connected.\n");
  325. SetTimeout(s, AZURE_PROTOCOL_CONTROL_TIMEOUT_DEFAULT);
  326. Lock(ac->Lock);
  327. {
  328. ac->CurrentSock = s;
  329. ac->IsConnected = true;
  330. StrCpy(ac->ConnectingAzureIp, sizeof(ac->ConnectingAzureIp), st.CurrentAzureIp);
  331. }
  332. Unlock(ac->Lock);
  333. SendAll(s, AZURE_PROTOCOL_CONTROL_SIGNATURE, StrLen(AZURE_PROTOCOL_CONTROL_SIGNATURE), false);
  334. // Receive parameter
  335. p = RecvPackWithHash(s);
  336. if (p != NULL)
  337. {
  338. UCHAR c;
  339. AZURE_PARAM param;
  340. bool hostname_changed = false;
  341. Zero(&param, sizeof(param));
  342. param.ControlKeepAlive = PackGetInt(p, "ControlKeepAlive");
  343. param.ControlTimeout = PackGetInt(p, "ControlTimeout");
  344. param.DataTimeout = PackGetInt(p, "DataTimeout");
  345. param.SslTimeout = PackGetInt(p, "SslTimeout");
  346. FreePack(p);
  347. param.ControlKeepAlive = MAKESURE(param.ControlKeepAlive, 1000, AZURE_SERVER_MAX_KEEPALIVE);
  348. param.ControlTimeout = MAKESURE(param.ControlTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT);
  349. param.DataTimeout = MAKESURE(param.DataTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT);
  350. param.SslTimeout = MAKESURE(param.SslTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT);
  351. Lock(ac->Lock);
  352. {
  353. Copy(&ac->AzureParam, &param, sizeof(AZURE_PARAM));
  354. }
  355. Unlock(ac->Lock);
  356. SetTimeout(s, param.ControlTimeout);
  357. // Send parameter
  358. p = NewPack();
  359. PackAddStr(p, "CurrentHostName", st.CurrentHostName);
  360. PackAddStr(p, "CurrentAzureIp", st.CurrentAzureIp);
  361. PackAddInt64(p, "CurrentAzureTimestamp", st.CurrentAzureTimestamp);
  362. PackAddStr(p, "CurrentAzureSignature", st.CurrentAzureSignature);
  363. Lock(ac->Lock);
  364. {
  365. if (StrCmpi(st.CurrentHostName, ac->DDnsStatus.CurrentHostName) != 0)
  366. {
  367. hostname_changed = true;
  368. }
  369. }
  370. Unlock(ac->Lock);
  371. if (hostname_changed == false)
  372. {
  373. if (SendPackWithHash(s, p))
  374. {
  375. // Receive result
  376. if (RecvAll(s, &c, 1, false))
  377. {
  378. if (c && ac->Halt == false)
  379. {
  380. connect_was_ok = true;
  381. established_tick = Tick64();
  382. AcWaitForRequest(ac, s, &param);
  383. }
  384. }
  385. }
  386. }
  387. FreePack(p);
  388. }
  389. else
  390. {
  391. WHERE;
  392. }
  393. Debug("VPN Azure: Disconnected.\n");
  394. Lock(ac->Lock);
  395. {
  396. ac->IsConnected = false;
  397. ac->CurrentSock = NULL;
  398. ClearStr(ac->ConnectingAzureIp, sizeof(ac->ConnectingAzureIp));
  399. }
  400. Unlock(ac->Lock);
  401. if (established_tick != 0)
  402. {
  403. if ((established_tick + (UINT64)AZURE_CONNECT_MAX_RETRY_INTERVAL) <= Tick64())
  404. {
  405. // If the connected time exceeds the AZURE_CONNECT_MAX_RETRY_INTERVAL, reset the retry counter.
  406. last_reconnect_tick = 0;
  407. num_reconnect_retry = 0;
  408. next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL;
  409. }
  410. }
  411. Disconnect(s);
  412. ReleaseSock(s);
  413. }
  414. else
  415. {
  416. Debug("VPN Azure: Error: Connect Failed.\n");
  417. }
  418. Free(host);
  419. }
  420. }
  421. }
  422. }
  423. else
  424. {
  425. last_reconnect_tick = 0;
  426. num_reconnect_retry = 0;
  427. next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL;
  428. }
  429. if (ac->Halt)
  430. {
  431. break;
  432. }
  433. if (connect_was_ok)
  434. {
  435. // If connection goes out after connected, increment connection success count to urge DDNS client query
  436. next_ddns_retry_tick = Tick64() + MIN((UINT64)DDNS_VPN_AZURE_CONNECT_ERROR_DDNS_RETRY_TIME_DIFF * (UINT64)(num_reconnect_retry + 1), (UINT64)DDNS_VPN_AZURE_CONNECT_ERROR_DDNS_RETRY_TIME_DIFF_MAX);
  437. }
  438. if ((next_ddns_retry_tick != 0) && (Tick64() >= next_ddns_retry_tick))
  439. {
  440. next_ddns_retry_tick = 0;
  441. ac->DDnsTriggerInt++;
  442. }
  443. Wait(ac->Event, rand() % 1000);
  444. }
  445. }
  446. // Get enabled or disabled VPN Azure client
  447. bool AcGetEnable(AZURE_CLIENT *ac)
  448. {
  449. // Validate arguments
  450. if (ac == NULL)
  451. {
  452. return false;
  453. }
  454. return ac->IsEnabled;
  455. }
  456. // Enable or disable VPN Azure client
  457. void AcSetEnable(AZURE_CLIENT *ac, bool enabled)
  458. {
  459. bool old_status;
  460. // Validate arguments
  461. if (ac == NULL)
  462. {
  463. return;
  464. }
  465. old_status = ac->IsEnabled;
  466. ac->IsEnabled = enabled;
  467. if (ac->IsEnabled && (ac->IsEnabled != old_status))
  468. {
  469. ac->DDnsTriggerInt++;
  470. }
  471. AcApplyCurrentConfig(ac, NULL);
  472. }
  473. // Set current configuration to VPN Azure client
  474. void AcApplyCurrentConfig(AZURE_CLIENT *ac, DDNS_CLIENT_STATUS *ddns_status)
  475. {
  476. bool disconnect_now = false;
  477. SOCK *disconnect_sock = NULL;
  478. // Validate arguments
  479. if (ac == NULL)
  480. {
  481. return;
  482. }
  483. // Get current DDNS configuration
  484. Lock(ac->Lock);
  485. {
  486. if (ddns_status != NULL)
  487. {
  488. if (StrCmpi(ac->DDnsStatus.CurrentHostName, ddns_status->CurrentHostName) != 0)
  489. {
  490. // If host name is changed, disconnect current data connection
  491. disconnect_now = true;
  492. }
  493. if (Cmp(&ac->DDnsStatus.InternetSetting, &ddns_status->InternetSetting, sizeof(INTERNET_SETTING)) != 0)
  494. {
  495. // If proxy setting is changed, disconnect current data connection
  496. disconnect_now = true;
  497. }
  498. Copy(&ac->DDnsStatus, ddns_status, sizeof(DDNS_CLIENT_STATUS));
  499. }
  500. if (ac->IsEnabled == false)
  501. {
  502. // If VPN Azure client is disabled, disconnect current data connection
  503. disconnect_now = true;
  504. }
  505. if (disconnect_now)
  506. {
  507. if (ac->CurrentSock != NULL)
  508. {
  509. disconnect_sock = ac->CurrentSock;
  510. AddRef(disconnect_sock->ref);
  511. }
  512. }
  513. }
  514. Unlock(ac->Lock);
  515. if (disconnect_sock != NULL)
  516. {
  517. Disconnect(disconnect_sock);
  518. ReleaseSock(disconnect_sock);
  519. }
  520. Set(ac->Event);
  521. }
  522. // Free VPN Azure client
  523. void FreeAzureClient(AZURE_CLIENT *ac)
  524. {
  525. SOCK *disconnect_sock = NULL;
  526. // Validate arguments
  527. if (ac == NULL)
  528. {
  529. return;
  530. }
  531. ac->Halt = true;
  532. Lock(ac->Lock);
  533. {
  534. if (ac->CurrentSock != NULL)
  535. {
  536. disconnect_sock = ac->CurrentSock;
  537. AddRef(disconnect_sock->ref);
  538. }
  539. }
  540. Unlock(ac->Lock);
  541. if (disconnect_sock != NULL)
  542. {
  543. Disconnect(disconnect_sock);
  544. ReleaseSock(disconnect_sock);
  545. }
  546. Set(ac->Event);
  547. // Stop main thread
  548. WaitThread(ac->MainThread, INFINITE);
  549. ReleaseThread(ac->MainThread);
  550. ReleaseEvent(ac->Event);
  551. DeleteLock(ac->Lock);
  552. Free(ac);
  553. }
  554. // Create new VPN Azure client
  555. AZURE_CLIENT *NewAzureClient(CEDAR *cedar, SERVER *server)
  556. {
  557. AZURE_CLIENT *ac;
  558. // Validate arguments
  559. if (cedar == NULL || server == NULL)
  560. {
  561. return NULL;
  562. }
  563. ac = ZeroMalloc(sizeof(AZURE_CLIENT));
  564. ac->Cedar = cedar;
  565. ac->Server = server;
  566. ac->Lock = NewLock();
  567. ac->IsEnabled = false;
  568. ac->Event = NewEvent();
  569. // Start main thread
  570. ac->MainThread = NewThread(AcMainThread, ac);
  571. return ac;
  572. }
  573. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  574. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  575. // Join us: http://www.tsukuba.ac.jp/english/admission/