void CFindHostsDlg::OnLoadlist() { CFileDialog f( TRUE, "*.txt", m_filename, OFN_HIDEREADONLY | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, "Text (*.txt)|*.txt|All files (*.*)|*.*||" ); if (f.DoModal() == IDOK) { POSITION p; QVS_Container qvs; CStdioFile fin; CString line; p = f.GetStartPosition(); m_filename = f.GetNextPathName(p); if (fin.Open(m_filename, CFile::modeRead)) { while (fin.ReadString(line)) { qvs.decode_string((char*)(LPCTSTR)line); } fin.Close(); char host[100]; if (qvs.first(host, 100)) { InsertHost(host); while (qvs.next(host, 100)) { InsertHost(host); } } qvs.output_encoded_string(m_encoded_hosts.GetBuffer(8192), 8192); m_encoded_hosts.ReleaseBuffer(); UpdateData(FALSE); } else { MessageBox(m_filename, "Unable to open file"); } } }
void CFindHostsDlg::Refresh() { DWORD num_read=0, total=0, size; int index; SERVER_INFO_100 *pBuf = NULL; char tBuf[100], tLocalHost[100]; DWORD ret_val; UpdateData(); HCURSOR hOldCursor = SetCursor( LoadCursor(NULL, IDC_WAIT) ); if (m_domain == "") { ret_val = NetServerEnum( NULL, 100, (LPBYTE*)&pBuf, MAX_PREFERRED_LENGTH, &num_read, &total, SV_TYPE_NT, NULL, 0); } else { WCHAR wDomain[100]; mbstowcs(wDomain, m_domain, 100); ret_val = NetServerEnum( NULL, 100, (LPBYTE*)&pBuf, MAX_PREFERRED_LENGTH, &num_read, &total, SV_TYPE_NT, (LMCSTR)wDomain, 0); } if (ret_val == NERR_Success) { size = 100; GetComputerName(tLocalHost, &size); m_list.DeleteAllItems(); if (num_read == 0) { //m_list.InsertItem(0, tLocalHost, 0); //m_list.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED); InsertHost(tLocalHost); SelectHost(tLocalHost); } else { index = -1; for (unsigned int i=0; i<num_read; i++) { wcstombs(tBuf, (WCHAR*)pBuf[i].sv100_name, wcslen((WCHAR*)pBuf[i].sv100_name)+1); InsertHost(tBuf); /* index = m_list.InsertItem(0, tBuf, 0); if (stricmp(tBuf, tLocalHost) == 0) index = ret_val; else index = -1; */ } /* if (index != -1) { m_list.SetItemState(index, LVIS_SELECTED, LVIS_SELECTED); } */ SelectHost(tLocalHost); } NetApiBufferFree(pBuf); } else { sprintf(tBuf, "error: %d", ret_val); MessageBox(tBuf, "Unable to retrieve network host names"); } SetCursor(hOldCursor); }
// Generic hostname and IP processor // Resolves the hostname or IP if one is missing, inserts or updates the Hosts table // Inserts or updates the HostsToMeasure table if requested int ProcessHost(string & hostName, string & ipStr, bool updateForPolling, int readinessState, unsigned int * hostID){ int ret; bool hostNameExists = false; bool ipStrExists = false; bool matches = false; list<string> ipStrings; char * ipStrFromDB; unsigned int netorderIp, id, hostToMeasureID; // this call will try to populate hostName and ipStr based on which exists ret = CheckHostIPValues(ipStr, hostName, &ipStrExists, &hostNameExists); if(ret == GENERIC_ERROR){ printf("ProcessHost(): CheckHostIPValues() failed\n"); return GENERIC_ERROR; } // both should hopefully now exist if(hostNameExists == true && ipStrExists == true){ // check if this hostName -> IP mapping exists in hosts ret = SelectHostIPsByHostName((char *)hostName.c_str(), &ipStrings); if(ret == GENERIC_ERROR){ printf("ProcessHost(): SelectHostIPByHostName() failed\n"); return GENERIC_ERROR; } while(ipStrings.size() > 0){ ipStrFromDB = new char[ipStrings.front().size()+1]; ipStrFromDB[ipStrings.front().size()]=0; memcpy(ipStrFromDB,ipStrings.front().c_str(),ipStrings.front().size()); ipStrings.pop_front(); // try to find the ipStr if(strcmp((char *)ipStr.c_str(),ipStrFromDB) == 0){ // flag that we found the entry for later matches = true; // get the hostID associated with this ipStr from the hosts table ret = SelectHostIDByHostNameAndIP((char *)hostName.c_str(), (char *)ipStr.c_str(), hostID); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): SelectHostIDByIP() failed\n"); return GENERIC_ERROR; } // then check if this hostname is in the HostsToMeasure table ret = SelectHostIDFromHostsToMeasureByHostNameAndIP((char *)hostName.c_str(),(char *)ipStr.c_str(), &id); if(ret == GENERIC_ERROR){ printf("ProcessHost(): SelectIDFromHostsToMeasureByHostID() failed\n"); return GENERIC_ERROR; } // if the host exists in HostToMeasure, update it if(id > 0 && ret != NO_RESULTS_FOUND){ // update HostsToMeasure to use this latest mapping ret = UpdateHostToMeasureHostID(id, *hostID); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): UpdateHostToMeasureHostID() failed\n"); return GENERIC_ERROR; } // update its readiness as well since its already in hosts to measure ret = UpdateHostToMeasure(0, READY, id); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): UpdateHostToMeasure() failed\n"); return GENERIC_ERROR; } } else if(updateForPolling == true) { // insert it if requested ret = InsertHostToMeasure(*hostID, READY, &hostToMeasureID); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): InsertHostToMeasure() failed\n"); return GENERIC_ERROR; } } } } // if the hostname -> ipStr mapping was not found in hosts, make a new entry in Hosts if(matches == false){ netorderIp = inet_addr((char *)ipStr.c_str()); ret = InsertHost((char *)hostName.c_str(), (char *)ipStr.c_str(), netorderIp, hostID); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): InsertHost() failed\n"); return GENERIC_ERROR; } id = 0; ret = SelectHostIDFromHostsToMeasureByHostName((char *)hostName.c_str(), &id); if(ret == GENERIC_ERROR){ printf("ProcessHost(): SelectIDFromHostsToMeasureByHostID() failed\n"); return GENERIC_ERROR; } // if the host exists in HostToMeasure, update it if(id > 0 && ret != NO_RESULTS_FOUND){ // update HostsToMeasure to use this latest mapping ret = UpdateHostToMeasureHostID(id, *hostID); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): UpdateHostToMeasureHostID() failed\n"); return GENERIC_ERROR; } // update its readiness as well since its already in hosts to measure ret = UpdateHostToMeasure(0, READY, id); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): UpdateHostToMeasure() failed\n"); return GENERIC_ERROR; } } // If not found, make an entry in HostsToMeasure if requested else if(updateForPolling == true){ ret = InsertHostToMeasure(*hostID, readinessState, &hostToMeasureID); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): InsertHostToMeasure() failed\n"); return GENERIC_ERROR; } } } } // or if we at least have the IP address else if(hostNameExists == false && ipStrExists == true){ // try to get the host id for this ip ret = SelectHostIDByIP((char *)ipStr.c_str(), hostID); if(ret == GENERIC_ERROR){ printf("ProcessHost(): SelectHostIDByIP() failed\n"); return GENERIC_ERROR; } // if it didn't exist, insert it if(*hostID < 1){ netorderIp = inet_addr((char *)ipStr.c_str()); ret = InsertHost((char *)hostName.c_str(), (char *)ipStr.c_str(), netorderIp, hostID); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): InsertHost() failed\n"); return GENERIC_ERROR; } // Make an entry in HostsToMeasure if requested if(updateForPolling == true){ ret = InsertHostToMeasure(*hostID, readinessState, &hostToMeasureID); if(ret != GENERIC_SUCCESS){ printf("ProcessHost(): InsertHostToMeasure() failed\n"); return GENERIC_ERROR; } } } } // if neither, error out else{ printf("ProcessHost(): Resolving host name and IP failed failed\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }