bool CServerList::IsGoodServerIP(const CServer* pServer) const
{
	if (pServer->HasDynIP())
		return true;
	return IsGoodIPPort(pServer->GetIP(), pServer->GetPort());
}
Beispiel #2
0
void CAddSourceDlg::OnBnClickedButton1()
{
	if (!m_pFile)
		return;

	switch (m_nSourceType)
	{
		case 0:
		{
			CString sip;
			GetDlgItem(IDC_EDIT2)->GetWindowText(sip);
			if (sip.IsEmpty())
				return;

			// if the port is specified with the IP, ignore any possible specified port in the port control
			uint16 port;
			int iColon = sip.Find(_T(':'));
			if (iColon != -1) {
				port = (uint16)_tstoi(sip.Mid(iColon + 1));
				sip = sip.Left(iColon);
			}
			else {
				BOOL bTranslated = FALSE;
				port = (uint16)GetDlgItemInt(IDC_EDIT3, &bTranslated, FALSE);
				if (!bTranslated)
					return;
			}

			uint32 ip;
			if ((ip = inet_addr(CT2CA(sip))) == INADDR_NONE && _tcscmp(sip, _T("255.255.255.255")) != 0)
				ip = 0;
			if (IsGoodIPPort(ip, port))
			{
				CUpDownClient* toadd = new CUpDownClient(m_pFile, port, ntohl(ip), 0, 0);
				toadd->SetSourceFrom(SF_PASSIVE);
				theApp.downloadqueue->CheckAndAddSource(m_pFile, toadd);
			}
			break;
		}
		case 1:
		{
			CString strURL;
			GetDlgItem(IDC_EDIT10)->GetWindowText(strURL);

			if( strURL.Find( _T("peer://|") )!=-1 )
			{				
				int iPos = 0;
				uint32 buddyIP,peerIP;
				uint16 buddyPort,peerKadPort;
				BYTE cBuddyID[16];
				BYTE cUserHash[16];
				memset(cBuddyID,0,16);
				memset(cUserHash,0,16);
				CString sBuddyID,sBuddyIP,sBuddyPort,sUserHash,sPeerIP,sPeerKadPort;

				CString strTok = GetNextString(strURL, _T("|"), iPos);
				if (strTok == _T("peer://"))
				{
					sBuddyID = GetNextString(strURL, _T("|"), iPos);
					sBuddyIP = GetNextString(strURL, _T("|"), iPos);
					sBuddyPort = GetNextString(strURL, _T("|"), iPos);
					sUserHash = GetNextString(strURL, _T("|"), iPos);
					sPeerIP = GetNextString(strURL, _T("|"), iPos);
					sPeerKadPort = GetNextString(strURL, _T("|"), iPos);
				}
				LPCTSTR pszBuddyID = (LPCTSTR)sBuddyID;
				if( sBuddyID!=_T("0") )
				{
					for (int idx = 0; idx < 16; ++idx) 
					{
						cBuddyID[idx] = (BYTE)(FromHexDigit(*pszBuddyID++)*16);
						cBuddyID[idx] = (BYTE)(cBuddyID[idx] + FromHexDigit(*pszBuddyID++));
					}
				}				
				LPCTSTR pszUserHash = (LPCTSTR)sUserHash;
				if(sUserHash!=_T("0"))
				for (int idx = 0; idx < 16; ++idx) 
				{
					cUserHash[idx] = (BYTE)(FromHexDigit(*pszUserHash++)*16);
					cUserHash[idx] = (BYTE)(cUserHash[idx] + FromHexDigit(*pszUserHash++));
				}
				buddyIP = inet_addr( CStringA(sBuddyIP) );
				unsigned long ul;
				ul = _tcstoul( sBuddyPort, 0, 10 );
				buddyPort = static_cast<uint16>(ul);
				peerIP = inet_addr( CStringA(sPeerIP) );
				peerKadPort = static_cast<uint16>(ul);

				CUpDownClient* toadd = new CUpDownClient(m_pFile,-1,1,0,0,false);//tcp(-1) 表示默认支持 Nat Trans										
				toadd->SetSourceFrom(SF_KADEMLIA);				
				toadd->SetBuddyID(cBuddyID);
				toadd->SetBuddyIP(buddyIP);
				toadd->SetBuddyPort(buddyPort);	
				toadd->SetUserHash(cUserHash);
				toadd->SetIP(peerIP);
				toadd->SetKadPort(peerKadPort);	
				theApp.downloadqueue->CheckAndAddSource(m_pFile, toadd);
			}
			else if (!strURL.IsEmpty())
			{
				TCHAR szScheme[INTERNET_MAX_SCHEME_LENGTH];
				TCHAR szHostName[INTERNET_MAX_HOST_NAME_LENGTH];
				TCHAR szUrlPath[INTERNET_MAX_PATH_LENGTH];
				TCHAR szUserName[INTERNET_MAX_USER_NAME_LENGTH];
				TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH];
				TCHAR szExtraInfo[INTERNET_MAX_URL_LENGTH];
				URL_COMPONENTS Url = {0};
				Url.dwStructSize = sizeof(Url);
				Url.lpszScheme = szScheme;
				Url.dwSchemeLength = ARRSIZE(szScheme);
				Url.lpszHostName = szHostName;
				Url.dwHostNameLength = ARRSIZE(szHostName);
				Url.lpszUserName = szUserName;
				Url.dwUserNameLength = ARRSIZE(szUserName);
				Url.lpszPassword = szPassword;
				Url.dwPasswordLength = ARRSIZE(szPassword);
				Url.lpszUrlPath = szUrlPath;
				Url.dwUrlPathLength = ARRSIZE(szUrlPath);
				Url.lpszExtraInfo = szExtraInfo;
				Url.dwExtraInfoLength = ARRSIZE(szExtraInfo);
				if (InternetCrackUrl(strURL, 0, 0, &Url) && Url.dwHostNameLength > 0 && Url.dwHostNameLength < INTERNET_MAX_HOST_NAME_LENGTH)
				{
					SUnresolvedHostname* hostname = new SUnresolvedHostname;
					hostname->strURL = strURL;
					hostname->strHostname = szHostName;
					theApp.downloadqueue->AddToResolved(m_pFile, hostname);
					delete hostname;
				}
			}
			break;
		}
	}
}