// VH_OPTION void InVhOption(VH_OPTION *t, PACK *p) { // Validate arguments if (t == NULL || p == NULL) { return; } Zero(t, sizeof(VH_OPTION)); PackGetData2(p, "MacAddress", t->MacAddress, 6); PackGetIp(p, "Ip", &t->Ip); PackGetIp(p, "Mask", &t->Mask); t->UseNat = PackGetBool(p, "UseNat"); t->Mtu = PackGetInt(p, "Mtu"); t->NatTcpTimeout = PackGetInt(p, "NatTcpTimeout"); t->NatUdpTimeout = PackGetInt(p, "NatUdpTimeout"); t->UseDhcp = PackGetBool(p, "UseDhcp"); PackGetIp(p, "DhcpLeaseIPStart", &t->DhcpLeaseIPStart); PackGetIp(p, "DhcpLeaseIPEnd", &t->DhcpLeaseIPEnd); PackGetIp(p, "DhcpSubnetMask", &t->DhcpSubnetMask); t->DhcpExpireTimeSpan = PackGetInt(p, "DhcpExpireTimeSpan"); PackGetIp(p, "DhcpGatewayAddress", &t->DhcpGatewayAddress); PackGetIp(p, "DhcpDnsServerAddress", &t->DhcpDnsServerAddress); PackGetIp(p, "DhcpDnsServerAddress2", &t->DhcpDnsServerAddress2); PackGetStr(p, "DhcpDomainName", t->DhcpDomainName, sizeof(t->DhcpDomainName)); t->SaveLog = PackGetBool(p, "SaveLog"); PackGetStr(p, "RpcHubName", t->HubName, sizeof(t->HubName)); t->ApplyDhcpPushRoutes = PackGetBool(p, "ApplyDhcpPushRoutes"); PackGetStr(p, "DhcpPushRoutes", t->DhcpPushRoutes, sizeof(t->DhcpPushRoutes)); }
// RPC_NAT_STATUS void InRpcNatStatus(RPC_NAT_STATUS *t, PACK *p) { // 引数チェック if (t == NULL || p == NULL) { return; } Zero(t, sizeof(RPC_NAT_STATUS)); t->NumTcpSessions = PackGetInt(p, "NumTcpSessions"); t->NumUdpSessions = PackGetInt(p, "NumUdpSessions"); t->NumDhcpClients = PackGetInt(p, "NumDhcpClients"); PackGetStr(p, "HubName", t->HubName, sizeof(t->HubName)); }
// RPC_ENUM_NAT void InRpcEnumNat(RPC_ENUM_NAT *t, PACK *p) { UINT i; // Validate arguments if (t == NULL || p == NULL) { return; } Zero(t, sizeof(RPC_ENUM_NAT)); t->NumItem = PackGetInt(p, "NumItem"); PackGetStr(p, "HubName", t->HubName, sizeof(t->HubName)); t->Items = ZeroMalloc(sizeof(RPC_ENUM_NAT_ITEM) * t->NumItem); for (i = 0;i < t->NumItem;i++) { RPC_ENUM_NAT_ITEM *e = &t->Items[i]; e->Id = PackGetIntEx(p, "Id", i); e->Protocol = PackGetIntEx(p, "Protocol", i); e->SrcIp = PackGetIntEx(p, "SrcIp", i); PackGetStrEx(p, "SrcHost", e->SrcHost, sizeof(e->SrcHost), i); e->SrcPort = PackGetIntEx(p, "SrcPort", i); e->DestIp = PackGetIntEx(p, "DestIp", i); PackGetStrEx(p, "DestHost", e->DestHost, sizeof(e->DestHost), i); e->DestPort = PackGetIntEx(p, "DestPort", i); e->CreatedTime = PackGetInt64Ex(p, "CreatedTime", i); e->LastCommTime = PackGetInt64Ex(p, "LastCommTime", i); e->SendSize = PackGetInt64Ex(p, "SendSize", i); e->RecvSize = PackGetInt64Ex(p, "RecvSize", i); e->TcpStatus = PackGetIntEx(p, "TcpStatus", i); } }
// RPC_ENUM_DHCP void InRpcEnumDhcp(RPC_ENUM_DHCP *t, PACK *p) { UINT i; // Validate arguments if (t == NULL || p == NULL) { return; } Zero(t, sizeof(RPC_ENUM_DHCP)); t->NumItem = PackGetInt(p, "NumItem"); t->Items = ZeroMalloc(sizeof(RPC_ENUM_DHCP_ITEM) * t->NumItem); PackGetStr(p, "HubName", t->HubName, sizeof(t->HubName)); for (i = 0;i < t->NumItem;i++) { RPC_ENUM_DHCP_ITEM *e = &t->Items[i]; e->Id = PackGetIntEx(p, "Id", i); e->LeasedTime = PackGetInt64Ex(p, "LeasedTime", i); e->ExpireTime = PackGetInt64Ex(p, "ExpireTime", i); PackGetDataEx2(p, "MacAddress", e->MacAddress, 6, i); e->IpAddress = PackGetIp32Ex(p, "IpAddress", i); e->Mask = PackGetIntEx(p, "Mask", i); PackGetStrEx(p, "Hostname", e->Hostname, sizeof(e->Hostname), i); } }
// RPC_NAT_STATUS void InRpcNatStatus(RPC_NAT_STATUS *t, PACK *p) { // Validate arguments if (t == NULL || p == NULL) { return; } Zero(t, sizeof(RPC_NAT_STATUS)); t->NumTcpSessions = PackGetInt(p, "NumTcpSessions"); t->NumUdpSessions = PackGetInt(p, "NumUdpSessions"); t->NumIcmpSessions = PackGetInt(p, "NumIcmpSessions"); t->NumDnsSessions = PackGetInt(p, "NumDnsSessions"); t->NumDhcpClients = PackGetInt(p, "NumDhcpClients"); t->IsKernelMode = PackGetBool(p, "IsKernelMode"); PackGetStr(p, "HubName", t->HubName, sizeof(t->HubName)); }
// RPC_NAT_INFO void InRpcNatInfo(RPC_NAT_INFO *t, PACK *p) { // Validate arguments if (t == NULL || p == NULL) { return; } Zero(t, sizeof(RPC_NAT_INFO)); PackGetStr(p, "NatProductName", t->NatProductName, sizeof(t->NatProductName)); PackGetStr(p, "NatVersionString", t->NatVersionString, sizeof(t->NatVersionString)); PackGetStr(p, "NatBuildInfoString", t->NatBuildInfoString, sizeof(t->NatBuildInfoString)); t->NatVerInt = PackGetInt(p, "NatVerInt"); t->NatBuildInt = PackGetInt(p, "NatBuildInt"); PackGetStr(p, "NatHostName", t->NatHostName, sizeof(t->NatHostName)); InRpcOsInfo(&t->OsInfo, p); InRpcMemInfo(&t->MemInfo, p); }
void InRpcAddDevice(RPC_ADD_DEVICE *t, PACK *p) { UINT i; // Validate arguments if (t == NULL || p == NULL) { return; } Zero(t, sizeof(RPC_ADD_DEVICE)); PackGetStr(p, "DeviceName", t->DeviceName, sizeof(t->DeviceName)); t->NoPromiscus = PackGetInt(p, "NoPromiscus"); t->LogSetting.PacketLogSwitchType = PackGetInt(p, "PacketLogSwitchType"); for (i = 0;i < NUM_PACKET_LOG;i++) { t->LogSetting.PacketLogConfig[i] = PackGetIntEx(p, "PacketLogConfig", i); } }
// Get error UINT RpcGetError(PACK *p) { // Validate arguments if (p == NULL) { return ERR_DISCONNECTED; } return PackGetInt(p, "error_code"); }
// エラーの取得 UINT RpcGetError(PACK *p) { // 引数チェック if (p == NULL) { return ERR_DISCONNECTED; } return PackGetInt(p, "error_code"); }
// RPC_DUMMY void InRpcDummy(RPC_DUMMY *t, PACK *p) { // Validate arguments if (t == NULL || p == NULL) { return; } Zero(t, sizeof(RPC_DUMMY)); t->DummyValue = PackGetInt(p, "DummyValue"); }
// Error checking bool RpcIsOk(PACK *p) { // Validate arguments if (p == NULL) { return false; } if (PackGetInt(p, "error") == 0) { return true; } else { return false; } }
// エラーチェック bool RpcIsOk(PACK *p) { // 引数チェック if (p == NULL) { return false; } if (PackGetInt(p, "error") == 0) { return true; } else { return false; } }
void InRpcEnumDevice(RPC_ENUM_DEVICE *t, PACK *p) { UINT i; // Validate arguments if (t == NULL || p == NULL) { return; } Zero(t, sizeof(RPC_ENUM_DEVICE)); t->NumItem = PackGetInt(p, "NumItem"); t->Items = ZeroMalloc(sizeof(RPC_ENUM_DEVICE_ITEM) * t->NumItem); for (i = 0;i < t->NumItem;i++) { RPC_ENUM_DEVICE_ITEM *d = &t->Items[i]; PackGetStrEx(p, "DeviceName", d->DeviceName, sizeof(d->DeviceName), i); d->Active = PackGetBoolEx(p, "Active", i); } t->IsLicenseSupported = PackGetBool(p, "IsLicenseSupported"); }
// VPN Azure client main thread void AcMainThread(THREAD *thread, void *param) { AZURE_CLIENT *ac = (AZURE_CLIENT *)param; UINT last_ip_revision = INFINITE; UINT64 last_reconnect_tick = 0; UINT64 next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL; UINT num_reconnect_retry = 0; UINT64 next_ddns_retry_tick = 0; bool last_connect_ok = false; // Validate arguments if (ac == NULL || thread == NULL) { return; } while (ac->Halt == false) { UINT64 now = Tick64(); bool connect_was_ok = false; // Wait for enabling VPN Azure function if (ac->IsEnabled) { // VPN Azure is enabled DDNS_CLIENT_STATUS st; bool connect_now = false; bool azure_ip_changed = false; Lock(ac->Lock); { Copy(&st, &ac->DDnsStatus, sizeof(DDNS_CLIENT_STATUS)); if (StrCmpi(st.CurrentAzureIp, ac->DDnsStatusCopy.CurrentAzureIp) != 0) { if (IsEmptyStr(st.CurrentAzureIp) == false) { // Destination IP address is changed connect_now = true; num_reconnect_retry = 0; } } if (StrCmpi(st.CurrentHostName, ac->DDnsStatusCopy.CurrentHostName) != 0) { // DDNS host name is changed connect_now = true; num_reconnect_retry = 0; } Copy(&ac->DDnsStatusCopy, &st, sizeof(DDNS_CLIENT_STATUS)); } Unlock(ac->Lock); if (last_ip_revision != ac->IpStatusRevision) { last_ip_revision = ac->IpStatusRevision; connect_now = true; num_reconnect_retry = 0; } if (last_reconnect_tick == 0 || (now >= (last_reconnect_tick + next_reconnect_interval))) { UINT r; last_reconnect_tick = now; num_reconnect_retry++; next_reconnect_interval = (UINT64)num_reconnect_retry * AZURE_CONNECT_INITIAL_RETRY_INTERVAL; next_reconnect_interval = MIN(next_reconnect_interval, AZURE_CONNECT_MAX_RETRY_INTERVAL); r = (UINT)next_reconnect_interval; r = GenRandInterval(r / 2, r); next_reconnect_interval = r; connect_now = true; } if (IsEmptyStr(st.CurrentAzureIp) == false && IsEmptyStr(st.CurrentHostName) == false) { if (connect_now) { SOCK *s; char *host = NULL; UINT port = AZURE_SERVER_PORT; Debug("VPN Azure: Connecting to %s...\n", st.CurrentAzureIp); if (ParseHostPort(st.CurrentAzureIp, &host, &port, AZURE_SERVER_PORT)) { if (st.InternetSetting.ProxyType == PROXY_DIRECT) { s = ConnectEx2(host, port, 0, (bool *)&ac->Halt); } else { s = WpcSockConnect2(host, port, &st.InternetSetting, NULL, AZURE_VIA_PROXY_TIMEOUT); } if (s != NULL) { PACK *p; UINT64 established_tick = 0; Debug("VPN Azure: Connected.\n"); SetTimeout(s, AZURE_PROTOCOL_CONTROL_TIMEOUT_DEFAULT); Lock(ac->Lock); { ac->CurrentSock = s; ac->IsConnected = true; StrCpy(ac->ConnectingAzureIp, sizeof(ac->ConnectingAzureIp), st.CurrentAzureIp); } Unlock(ac->Lock); SendAll(s, AZURE_PROTOCOL_CONTROL_SIGNATURE, StrLen(AZURE_PROTOCOL_CONTROL_SIGNATURE), false); // Receive parameter p = RecvPackWithHash(s); if (p != NULL) { UCHAR c; AZURE_PARAM param; bool hostname_changed = false; Zero(¶m, sizeof(param)); param.ControlKeepAlive = PackGetInt(p, "ControlKeepAlive"); param.ControlTimeout = PackGetInt(p, "ControlTimeout"); param.DataTimeout = PackGetInt(p, "DataTimeout"); param.SslTimeout = PackGetInt(p, "SslTimeout"); FreePack(p); param.ControlKeepAlive = MAKESURE(param.ControlKeepAlive, 1000, AZURE_SERVER_MAX_KEEPALIVE); param.ControlTimeout = MAKESURE(param.ControlTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT); param.DataTimeout = MAKESURE(param.DataTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT); param.SslTimeout = MAKESURE(param.SslTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT); Lock(ac->Lock); { Copy(&ac->AzureParam, ¶m, sizeof(AZURE_PARAM)); } Unlock(ac->Lock); SetTimeout(s, param.ControlTimeout); // Send parameter p = NewPack(); PackAddStr(p, "CurrentHostName", st.CurrentHostName); PackAddStr(p, "CurrentAzureIp", st.CurrentAzureIp); PackAddInt64(p, "CurrentAzureTimestamp", st.CurrentAzureTimestamp); PackAddStr(p, "CurrentAzureSignature", st.CurrentAzureSignature); Lock(ac->Lock); { if (StrCmpi(st.CurrentHostName, ac->DDnsStatus.CurrentHostName) != 0) { hostname_changed = true; } } Unlock(ac->Lock); if (hostname_changed == false) { if (SendPackWithHash(s, p)) { // Receive result if (RecvAll(s, &c, 1, false)) { if (c && ac->Halt == false) { connect_was_ok = true; established_tick = Tick64(); AcWaitForRequest(ac, s, ¶m); } } } } FreePack(p); } else { WHERE; } Debug("VPN Azure: Disconnected.\n"); Lock(ac->Lock); { ac->IsConnected = false; ac->CurrentSock = NULL; ClearStr(ac->ConnectingAzureIp, sizeof(ac->ConnectingAzureIp)); } Unlock(ac->Lock); if (established_tick != 0) { if ((established_tick + (UINT64)AZURE_CONNECT_MAX_RETRY_INTERVAL) <= Tick64()) { // If the connected time exceeds the AZURE_CONNECT_MAX_RETRY_INTERVAL, reset the retry counter. last_reconnect_tick = 0; num_reconnect_retry = 0; next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL; } } Disconnect(s); ReleaseSock(s); } else { Debug("VPN Azure: Error: Connect Failed.\n"); } Free(host); } } } } else { last_reconnect_tick = 0; num_reconnect_retry = 0; next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL; } if (ac->Halt) { break; } if (connect_was_ok) { // If connection goes out after connected, increment connection success count to urge DDNS client query 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); } if ((next_ddns_retry_tick != 0) && (Tick64() >= next_ddns_retry_tick)) { next_ddns_retry_tick = 0; ac->DDnsTriggerInt++; } Wait(ac->Event, rand() % 1000); } }
// Wait for connection request void AcWaitForRequest(AZURE_CLIENT *ac, SOCK *s, AZURE_PARAM *param) { // Validate arguments if (ac == NULL || s == NULL || param == NULL) { return; } while (ac->Halt == false) { UCHAR uc; // Receive 1 byte if (RecvAll(s, &uc, 1, false) == 0) { break; } if (uc != 0) { // Receive a Pack PACK *p = RecvPackWithHash(s); if (p == NULL) { break; } else { // Verify contents of Pack char opcode[MAX_SIZE]; char cipher_name[MAX_SIZE]; char hostname[MAX_SIZE]; PackGetStr(p, "opcode", opcode, sizeof(opcode)); PackGetStr(p, "cipher_name", cipher_name, sizeof(cipher_name)); PackGetStr(p, "hostname", hostname, sizeof(hostname)); if (StrCmpi(opcode, "relay") == 0) { IP client_ip, server_ip; UINT client_port; UINT server_port; UCHAR session_id[SHA1_SIZE]; if (PackGetIp(p, "client_ip", &client_ip) && PackGetIp(p, "server_ip", &server_ip) && PackGetData2(p, "session_id", session_id, sizeof(session_id))) { client_port = PackGetInt(p, "client_port"); server_port = PackGetInt(p, "server_port"); if (client_port != 0 && server_port != 0) { SOCK *ns; Debug("Connect Request from %r:%u\n", &client_ip, client_port); // Create new socket and connect VPN Azure Server if (ac->DDnsStatusCopy.InternetSetting.ProxyType == PROXY_DIRECT) { ns = ConnectEx2(ac->DDnsStatusCopy.CurrentAzureIp, AZURE_SERVER_PORT, 0, (bool *)&ac->Halt); } else { ns = WpcSockConnect2(ac->DDnsStatusCopy.CurrentAzureIp, AZURE_SERVER_PORT, &ac->DDnsStatusCopy.InternetSetting, NULL, AZURE_VIA_PROXY_TIMEOUT); } if (ns == NULL) { Debug("Connect Error.\n"); } else { Debug("Connected to the relay server.\n"); SetTimeout(ns, param->DataTimeout); if (StartSSLEx(ns, NULL, NULL, true, 0, NULL)) { // Check certification char server_cert_hash_str[MAX_SIZE]; UCHAR server_cert_hash[SHA1_SIZE]; Zero(server_cert_hash, sizeof(server_cert_hash)); GetXDigest(ns->RemoteX, server_cert_hash, true); BinToStr(server_cert_hash_str, sizeof(server_cert_hash_str), server_cert_hash, SHA1_SIZE); if (IsEmptyStr(ac->DDnsStatusCopy.AzureCertHash) || StrCmpi(server_cert_hash_str, ac->DDnsStatusCopy.AzureCertHash) == 0) { if (SendAll(ns, AZURE_PROTOCOL_DATA_SIANGTURE, 24, true)) { PACK *p2 = NewPack(); PackAddStr(p2, "hostname", hostname); PackAddData(p2, "session_id", session_id, sizeof(session_id)); if (SendPackWithHash(ns, p2)) { UCHAR uc; if (RecvAll(ns, &uc, 1, true) != false) { if (uc != 0) { SOCK *accept_sock = GetReverseListeningSock(ac->Cedar); if (accept_sock != NULL) { AddRef(ns->ref); SetTimeout(ns, INFINITE); Copy(&ns->Reverse_MyServerGlobalIp, &server_ip, sizeof(IP)); ns->Reverse_MyServerPort = server_port; InjectNewReverseSocketToAccept(accept_sock, ns, &client_ip, client_port); ReleaseSock(accept_sock); } } } } FreePack(p2); } } } ReleaseSock(ns); } } } } FreePack(p); } } // Send 1 byte uc = 0; if (SendAll(s, &uc, 1, false) == 0) { break; } } }