// Routing table tracking main void RouteTrackingMain(SESSION *s) { ROUTE_TRACKING *t; UINT64 now; ROUTE_TABLE *table; ROUTE_ENTRY *rs; bool changed = false; bool check = false; bool any_modified = false; // Validate arguments if (s == NULL) { return; } if (s->ClientModeAndUseVLan == false) { return; } // Get the state t = ((VLAN *)s->PacketAdapter->Param)->RouteState; if (t == NULL) { return; } // Current time PROBE_STR("RouteTrackingMain 1"); now = Tick64(); if (t->RouteChange != NULL) { if (t->NextRouteChangeCheckTime == 0 || t->NextRouteChangeCheckTime <= now) { t->NextRouteChangeCheckTime = now + 1000ULL; check = IsRouteChanged(t->RouteChange); if (check) { Debug("*** Routing Table Changed ***\n"); t->NextTrackingTime = 0; } } } if (t->NextTrackingTime != 0 && t->NextTrackingTime > now) { if (s->UseUdpAcceleration && s->UdpAccel != NULL && s->UdpAccel->NatT_IP_Changed) { // Check always if the IP address of the NAT-T server has changed } else { PROBE_STR("RouteTrackingMain 2"); return; } } PROBE_STR("RouteTrackingMain 3"); if (s->UseUdpAcceleration && s->UdpAccel != NULL) { IP nat_t_ip; s->UdpAccel->NatT_IP_Changed = false; Zero(&nat_t_ip, sizeof(nat_t_ip)); Lock(s->UdpAccel->NatT_Lock); { Copy(&nat_t_ip, &s->UdpAccel->NatT_IP, sizeof(IP)); } Unlock(s->UdpAccel->NatT_Lock); // Add a route to the NAT-T server if (IsZeroIp(&nat_t_ip) == false) { if (t->RouteToNatTServer == NULL) { if (t->RouteToEight != NULL) { ROUTE_ENTRY *e = Clone(t->RouteToEight, sizeof(ROUTE_ENTRY)); char ip_str[64]; char ip_str2[64]; Copy(&e->DestIP, &nat_t_ip, sizeof(IP)); e->Metric = e->OldIfMetric; IPToStr(ip_str, sizeof(ip_str), &e->DestIP); IPToStr(ip_str2, sizeof(ip_str2), &e->GatewayIP); t->RouteToNatTServer = e; if (AddRouteEntry(t->RouteToNatTServer)) { Debug("Adding Static Route to %s via %s metric %u: ok.\n", ip_str, ip_str2, e->Metric); } else { FreeRouteEntry(t->RouteToNatTServer); t->RouteToNatTServer = NULL; } } } } } // Get the current routing table table = GetRouteTable(); rs = t->RouteToServer; if (table != NULL) { UINT i; bool route_to_server_erased = true; bool is_vlan_want_to_be_default_gateway = false; UINT vlan_default_gatewat_metric = 0; UINT other_if_default_gateway_metric_min = INFINITE; // Get whether the routing table have been changed if (t->LastRoutingTableHash != table->HashedValue) { t->LastRoutingTableHash = table->HashedValue; changed = true; } //DebugPrintRouteTable(table); // Scan the routing table for (i = 0;i < table->NumEntry;i++) { ROUTE_ENTRY *e = table->Entry[i]; if (rs != NULL) { if (CmpIpAddr(&e->DestIP, &rs->DestIP) == 0 && CmpIpAddr(&e->DestMask, &rs->DestMask) == 0 // && CmpIpAddr(&e->GatewayIP, &rs->GatewayIP) == 0 // && e->InterfaceID == rs->InterfaceID && // e->LocalRouting == rs->LocalRouting && // e->Metric == rs->Metric ) { // Routing entry to the server that added at the time of connection is found route_to_server_erased = false; } } // Search for the default gateway if (IPToUINT(&e->DestIP) == 0 && IPToUINT(&e->DestMask) == 0) { Debug("e->InterfaceID = %u, t->VLanInterfaceId = %u\n", e->InterfaceID, t->VLanInterfaceId); if (e->InterfaceID == t->VLanInterfaceId) { // The virtual LAN card think that he want to be a default gateway is_vlan_want_to_be_default_gateway = true; vlan_default_gatewat_metric = e->Metric; if (vlan_default_gatewat_metric >= 2 && t->OldDefaultGatewayMetric == (vlan_default_gatewat_metric - 1)) { // Restore because the PPP server rewrites // the routing table selfishly DeleteRouteEntry(e); e->Metric--; AddRouteEntry(e); Debug("** Restore metric destroyed by PPP.\n"); any_modified = true; } // Keep this entry if (t->DefaultGatewayByVLan != NULL) { // Delete if there is one added last time FreeRouteEntry(t->DefaultGatewayByVLan); } t->DefaultGatewayByVLan = ZeroMalloc(sizeof(ROUTE_ENTRY)); Copy(t->DefaultGatewayByVLan, e, sizeof(ROUTE_ENTRY)); t->OldDefaultGatewayMetric = vlan_default_gatewat_metric; } else { // There are default gateway other than the virtual LAN card // Save the metric value of the default gateway if (other_if_default_gateway_metric_min > e->Metric) { // Ignore the metric value of all PPP connection in the case of Windows Vista if (MsIsVista() == false || e->PPPConnection == false) { other_if_default_gateway_metric_min = e->Metric; } else { // a PPP is used to Connect to the network // in using Windows Vista t->VistaAndUsingPPP = true; } } } } } if (t->VistaAndUsingPPP) { if (t->DefaultGatewayByVLan != NULL) { if (is_vlan_want_to_be_default_gateway) { if (t->VistaOldDefaultGatewayByVLan == NULL || Cmp(t->VistaOldDefaultGatewayByVLan, t->DefaultGatewayByVLan, sizeof(ROUTE_ENTRY)) != 0) { ROUTE_ENTRY *e; // Add the route of 0.0.0.0/1 and 128.0.0.0/1 // to the system if the virtual LAN card should be // the default gateway in the case of the connection // using PPP in Windows Vista if (t->VistaOldDefaultGatewayByVLan != NULL) { FreeRouteEntry(t->VistaOldDefaultGatewayByVLan); } if (t->VistaDefaultGateway1 != NULL) { DeleteRouteEntry(t->VistaDefaultGateway1); FreeRouteEntry(t->VistaDefaultGateway1); DeleteRouteEntry(t->VistaDefaultGateway2); FreeRouteEntry(t->VistaDefaultGateway2); } t->VistaOldDefaultGatewayByVLan = Clone(t->DefaultGatewayByVLan, sizeof(ROUTE_ENTRY)); e = Clone(t->DefaultGatewayByVLan, sizeof(ROUTE_ENTRY)); SetIP(&e->DestIP, 0, 0, 0, 0); SetIP(&e->DestMask, 128, 0, 0, 0); t->VistaDefaultGateway1 = e; e = Clone(t->DefaultGatewayByVLan, sizeof(ROUTE_ENTRY)); SetIP(&e->DestIP, 128, 0, 0, 0); SetIP(&e->DestMask, 128, 0, 0, 0); t->VistaDefaultGateway2 = e; AddRouteEntry(t->VistaDefaultGateway1); AddRouteEntry(t->VistaDefaultGateway2); Debug("Vista PPP Fix Route Table Added.\n"); any_modified = true; } } else { if (t->VistaOldDefaultGatewayByVLan != NULL) { FreeRouteEntry(t->VistaOldDefaultGatewayByVLan); t->VistaOldDefaultGatewayByVLan = NULL; } if (t->VistaDefaultGateway1 != NULL) { Debug("Vista PPP Fix Route Table Deleted.\n"); DeleteRouteEntry(t->VistaDefaultGateway1); FreeRouteEntry(t->VistaDefaultGateway1); DeleteRouteEntry(t->VistaDefaultGateway2); FreeRouteEntry(t->VistaDefaultGateway2); any_modified = true; t->VistaDefaultGateway1 = t->VistaDefaultGateway2 = NULL; } } } } // If the virtual LAN card want to be the default gateway and // there is no LAN card with smaller metric of 0.0.0.0/0 than // the virtual LAN card, delete other default gateway entries // to elect the virtual LAN card as the default gateway // Debug("is_vlan_want_to_be_default_gateway = %u, rs = %u, route_to_server_erased = %u, other_if_default_gateway_metric_min = %u, vlan_default_gatewat_metric = %u\n", // is_vlan_want_to_be_default_gateway, rs, route_to_server_erased, other_if_default_gateway_metric_min, vlan_default_gatewat_metric); if (is_vlan_want_to_be_default_gateway && (rs != NULL && route_to_server_erased == false) && other_if_default_gateway_metric_min >= vlan_default_gatewat_metric) { // Scan the routing table again for (i = 0;i < table->NumEntry;i++) { ROUTE_ENTRY *e = table->Entry[i]; if (e->InterfaceID != t->VLanInterfaceId) { if (IPToUINT(&e->DestIP) == 0 && IPToUINT(&e->DestMask) == 0) { char str[64]; // Default gateway is found ROUTE_ENTRY *r = ZeroMalloc(sizeof(ROUTE_ENTRY)); Copy(r, e, sizeof(ROUTE_ENTRY)); // Put in the queue InsertQueue(t->DeletedDefaultGateway, r); // Delete this gateway entry once DeleteRouteEntry(e); IPToStr(str, sizeof(str), &e->GatewayIP); Debug("Default Gateway %s Deleted.\n", str); any_modified = true; } } } } if (rs != NULL && route_to_server_erased) { // Physical entry to the server has disappeared Debug("Route to Server entry ERASED !!!\n"); // Forced disconnection (reconnection enabled) s->RetryFlag = true; s->Halt = true; } // Release the routing table FreeRouteTable(table); } // Set the time to perform the next track if (t->NextTrackingTimeAdd == 0 || changed) { t->NextTrackingTimeAdd = TRACKING_INTERVAL_INITIAL; } else { UINT64 max_value = TRACKING_INTERVAL_MAX; if (t->RouteChange != NULL) { max_value = TRACKING_INTERVAL_MAX_RC; } t->NextTrackingTimeAdd += TRACKING_INTERVAL_ADD; if (t->NextTrackingTimeAdd >= max_value) { t->NextTrackingTimeAdd = max_value; } } //Debug("t->NextTrackingTimeAdd = %I64u\n", t->NextTrackingTimeAdd); t->NextTrackingTime = now + t->NextTrackingTimeAdd; if (any_modified) { // Clear the DNS cache Win32FlushDnsCache(); } }
// DDNS client thread void DCThread(THREAD *thread, void *param) { DDNS_CLIENT *c; INTERRUPT_MANAGER *interrput; UINT last_ip_hash = 0; void *route_change_poller = NULL; bool last_time_ip_changed = false; UINT last_azure_ddns_trigger_int = 0; UINT last_vgs_ddns_trigger_int = 0; UINT n; INTERNET_SETTING last_t; // Validate arguments if (thread == NULL || param == NULL) { return; } c = (DDNS_CLIENT *)param; interrput = NewInterruptManager(); route_change_poller = NewRouteChange(); IsRouteChanged(route_change_poller); Zero(&last_t, sizeof(last_t)); n = 0; while (c->Halt == false) { UINT ip_hash = GetHostIPAddressHash32(); UINT interval; UINT64 now = Tick64(); bool ip_changed = false; bool azure_client_triggered = false; bool internet_setting_changed = false; bool vgs_server_triggered = false; if (c->Cedar->Server != NULL && c->Cedar->Server->AzureClient != NULL) { if (c->Cedar->Server->AzureClient->DDnsTriggerInt != last_azure_ddns_trigger_int) { azure_client_triggered = true; last_azure_ddns_trigger_int = c->Cedar->Server->AzureClient->DDnsTriggerInt; last_time_ip_changed = false; Debug("DDNS Thread Triggered by AzureClient.\n"); } } if (Cmp(&last_t, &c->InternetSetting, sizeof(INTERNET_SETTING)) != 0) { Copy(&last_t, &c->InternetSetting, sizeof(INTERNET_SETTING)); internet_setting_changed = true; last_time_ip_changed = false; } if (ip_hash != last_ip_hash) { last_time_ip_changed = false; Debug("DDNS Thread Triggered by IP Hash Changed.\n"); } if ((ip_hash != last_ip_hash) || (IsRouteChanged(route_change_poller)) || azure_client_triggered || internet_setting_changed || vgs_server_triggered) { if (last_time_ip_changed == false) { // Call all getting functions from the beginning if the routing // table or the IP address of this host has changed c->NextRegisterTick_IPv4 = 0; c->NextRegisterTick_IPv6 = 0; c->NextGetMyIpTick_IPv4 = 0; c->NextGetMyIpTick_IPv6 = 0; last_ip_hash = ip_hash; last_time_ip_changed = true; ip_changed = true; Debug("DDNS Internet Condition Changed.\n"); } } else { last_time_ip_changed = false; } if ((n++) >= 1) { // Self IPv4 address acquisition if (c->NextGetMyIpTick_IPv4 == 0 || now >= c->NextGetMyIpTick_IPv4) { UINT next_interval; char ip[MAX_SIZE]; Zero(ip, sizeof(ip)); c->Err_IPv4_GetMyIp = DCGetMyIp(c, false, ip, sizeof(ip), NULL); if (c->Err_IPv4_GetMyIp == ERR_NO_ERROR) { if (StrCmpi(c->LastMyIPv4, ip) != 0) { ip_changed = true; StrCpy(c->LastMyIPv4, sizeof(c->LastMyIPv4), ip); } next_interval = GenRandInterval(DDNS_GETMYIP_INTERVAL_OK_MIN, DDNS_GETMYIP_INTERVAL_OK_MAX); } else { if (IsEmptyStr(c->LastMyIPv4) == false) { ip_changed = true; } Zero(c->LastMyIPv4, sizeof(c->LastMyIPv4)); next_interval = GenRandInterval(DDNS_GETMYIP_INTERVAL_NG_MIN, DDNS_GETMYIP_INTERVAL_NG_MAX); } c->NextGetMyIpTick_IPv4 = Tick64() + (UINT64)next_interval; AddInterrupt(interrput, c->NextGetMyIpTick_IPv4); } // Self IPv6 address acquisition if (c->NextGetMyIpTick_IPv6 == 0 || now >= c->NextGetMyIpTick_IPv6) { UINT next_interval; char ip[MAX_SIZE]; Zero(ip, sizeof(ip)); c->Err_IPv6_GetMyIp = DCGetMyIp(c, true, ip, sizeof(ip), NULL); if (c->Err_IPv6_GetMyIp == ERR_NO_ERROR) { if (StrCmpi(c->LastMyIPv6, ip) != 0) { ip_changed = true; StrCpy(c->LastMyIPv6, sizeof(c->LastMyIPv6), ip); } next_interval = GenRandInterval(DDNS_GETMYIP_INTERVAL_OK_MIN, DDNS_GETMYIP_INTERVAL_OK_MAX); } else { if (IsEmptyStr(c->LastMyIPv6) == false) { ip_changed = true; } Zero(c->LastMyIPv6, sizeof(c->LastMyIPv6)); next_interval = GenRandInterval(DDNS_GETMYIP_INTERVAL_NG_MIN, DDNS_GETMYIP_INTERVAL_NG_MAX); } c->NextGetMyIpTick_IPv6 = Tick64() + (UINT64)next_interval; AddInterrupt(interrput, c->NextGetMyIpTick_IPv6); } } if (ip_changed) { c->NextRegisterTick_IPv4 = 0; c->NextRegisterTick_IPv6 = 0; } // IPv4 host registration if (c->NextRegisterTick_IPv4 == 0 || now >= c->NextRegisterTick_IPv4) { UINT next_interval; c->Err_IPv4 = DCRegister(c, false, NULL, NULL); if (c->Err_IPv4 == ERR_NO_ERROR) { next_interval = GenRandInterval(DDNS_REGISTER_INTERVAL_OK_MIN, DDNS_REGISTER_INTERVAL_OK_MAX); } else { next_interval = GenRandInterval(DDNS_REGISTER_INTERVAL_NG_MIN, DDNS_REGISTER_INTERVAL_NG_MAX); } //next_interval = 0; c->NextRegisterTick_IPv4 = Tick64() + (UINT64)next_interval; if (true) { DDNS_CLIENT_STATUS st; DCGetStatus(c, &st); SiApplyAzureConfig(c->Cedar->Server, &st); } AddInterrupt(interrput, c->NextRegisterTick_IPv4); } if (c->Halt) { break; } // IPv6 host registration if (c->NextRegisterTick_IPv6 == 0 || now >= c->NextRegisterTick_IPv6) { UINT next_interval; c->Err_IPv6 = DCRegister(c, true, NULL, NULL); if (c->Err_IPv6 == ERR_NO_ERROR) { next_interval = GenRandInterval(DDNS_REGISTER_INTERVAL_OK_MIN, DDNS_REGISTER_INTERVAL_OK_MAX); } else { next_interval = GenRandInterval(DDNS_REGISTER_INTERVAL_NG_MIN, DDNS_REGISTER_INTERVAL_NG_MAX); } c->NextRegisterTick_IPv6 = Tick64() + (UINT64)next_interval; if (true) { DDNS_CLIENT_STATUS st; DCGetStatus(c, &st); SiApplyAzureConfig(c->Cedar->Server, &st); } AddInterrupt(interrput, c->NextRegisterTick_IPv6); } interval = GetNextIntervalForInterrupt(interrput); interval = MIN(interval, 1234); if (n == 1) { interval = MIN(interval, 0); } if (c->Halt) { break; } if (c->KeyChanged) { c->KeyChanged = false; c->NextRegisterTick_IPv4 = c->NextRegisterTick_IPv6 = 0; interval = 0; } if (last_time_ip_changed) { if (c->Cedar->Server != NULL && c->Cedar->Server->AzureClient != NULL) { c->Cedar->Server->AzureClient->IpStatusRevision++; } } Wait(c->Event, interval); } FreeRouteChange(route_change_poller); FreeInterruptManager(interrput); }