Exemplo n.º 1
0
int GetSockDetails(int sock, char **LocalAddress, int *LocalPort, char **RemoteAddress, int *RemotePort)
{
    int salen, result;
    struct sockaddr_in sa;

    *LocalPort=0;
    *RemotePort=0;
    *LocalAddress=CopyStr(*LocalAddress,"");
    *RemoteAddress=CopyStr(*RemoteAddress,"");

    salen=sizeof(struct sockaddr_in);
    result=getsockname(sock, (struct sockaddr *) &sa, &salen);

    if (result==0)
    {
        *LocalAddress=CopyStr(*LocalAddress,IPtoStr(sa.sin_addr.s_addr));
        *LocalPort=ntohs(sa.sin_port);

        //Set Address to be the same as control sock, as it might not be INADDR_ANY
        result=getpeername(sock, (struct sockaddr *) &sa, &salen);

        if (result==0)
        {
            *RemoteAddress=CopyStr(*RemoteAddress,IPtoStr(sa.sin_addr.s_addr));
            *RemotePort=sa.sin_port;
        }

        //We've got the local sock, so lets still call it a success
        result=0;
    }

    if (result==0) return(TRUE);
    return(FALSE);
}
Exemplo n.º 2
0
bool CChatControl::SendRequest()
{
	bool Sent = false;

	if(m_autNetwork->GetLocalIP() == StrtoIP("127.0.0.1").S_addr)
		return false;

	if(m_autNetwork->GetLocalPort() == 0)
		return false;

	for(int i = 0; i < m_ServerList.size(); i++)
		if(m_ServerList[i]->m_Status == SOCK_CONNECTED)
		{
			bool CacheFound = false;
			
			for(int j = 0; j < m_ServerList[i]->m_RoomList.size(); j++)
			{
				CString Channel = m_ServerList[i]->m_RoomList[j]->m_Name;

				if(Channel == "#gnutella/cache." + DWrdtoStr(m_ServerList[i]->m_CacheNum) ||
				   Channel == "#gnutella/cache/" + m_pPrefs->m_PrefChannel)
				{
					IP   Host;
					Host.S_addr = m_autNetwork->GetLocalIP();
					UINT Port   = m_autNetwork->GetLocalPort();

					CString Notify = "NOTICE " + Channel + " :open(";
					Notify += IPtoStr(Host) + ":";
					Notify += DWrdtoStr(Port) + ":";

					if(m_autNetwork->ClientMode() == CLIENT_ULTRAPEER)
						Notify += "super";
					else
						Notify += "normal";

					Notify += ":)\r\n";

					m_ServerList[i]->Send(Notify, Notify.GetLength());

					CacheFound  = true;
					Sent		= true;
				}
			}

			if(!CacheFound)
			{
				CString Join = "JOIN #gnutella/cache." + DWrdtoStr(m_ServerList[i]->m_CacheNum) + "\r\n";
				m_ServerList[i]->Send(Join, Join.GetLength());
			}
		}

	return Sent;
}		
Exemplo n.º 3
0
CString CGnucleusDoc::ResolveIP(IP HostIP)
{
	/*hostent* pHostent;
	pHostent = (LPHOSTENT) m_pComm->m_HostEnt;
	pHostent = gethostbyaddr((char*) &HostIP.S_addr, sizeof(HostIP), AF_INET);
 
	if(pHostent)
	{
		CString HostName( pHostent->h_name);
		return HostName;
	}*/
	
	return IPtoStr(HostIP);
}
Exemplo n.º 4
0
void CViewStatistics::ExtendedInfo(int NodeID)
{
	CConnectInfo InfoDlg(NULL, m_pDoc);

	IP NodeIP;
	NodeIP.S_addr = m_autNetwork->GetNodeIP(NodeID);
	
	InfoDlg.m_Host		= IPtoStr(NodeIP);
	InfoDlg.m_Port	    = m_autNetwork->GetNodePort(NodeID);
	InfoDlg.m_Time		= m_autNetwork->GetNodeConnectTime(NodeID);
	InfoDlg.m_Handshake = m_autNetwork->GetNodeHandshake(NodeID);

	InfoDlg.DoModal();
}
Exemplo n.º 5
0
void CSearchInfo::OnRclickListResults(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LPNMHDR Control = (LPNMHDR) pNMHDR;		// stay compatable with IE 3.0!
	
	int nItem = m_lstResults.GetSelectionMark();
	if (0 > nItem || nItem > m_lstResults.GetItemCount() - 1)
		return;

    CMenu menu;
    menu.LoadMenu(IDR_SEARCHEX_RCLICK);
    CMenu *pMenu = menu.GetSubMenu(0);
    ASSERT(pMenu != NULL);

    // Display and track the popup menu
    CPoint point;
    GetCursorPos(&point);

	int res = pMenu->TrackPopupMenu( (TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD),
                 point.x, point.y, this);

	POSITION pos = m_lstResults.GetFirstSelectedItemPosition();
	
	switch(res)
	{
	case ID_LSTSEARCHEX_BLOCK:
		if(pos)
		{
			pos   = m_lstResults.GetFirstSelectedItemPosition();
			nItem = m_lstResults.GetNextSelectedItem(pos);

			ResultHost* p = (ResultHost*) m_lstResults.GetItemData(nItem);


			// Dispaly Preferences, sharing tab
			m_pDoc->m_nLastPref = PREF_SEARCHBLOCK;
			
			CPrefDialog PrefDlg("Preferences");
		
			PrefDlg.m_TabSearchBlock->m_SearchExInit = IPtoStr(p->Host);
			PrefDlg.DoModal();

			m_pView->OnChecksScreen();
		}

		break;
	}
		
	*pResult = 0;
}
Exemplo n.º 6
0
BOOL CTransfersUpEx::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_Handshake.Replace("\n\n", "\r\n\r\n");

	m_stcName.SetWindowText(m_FileName);
	m_stcIndex.SetWindowText( CommaIze(DWrdtoStr(m_FileIndex)));
	
	CString Host = IPtoStr(m_Host);
			Host += ":" + DWrdtoStr(m_Port);

	m_stcHost.SetWindowText(Host);
	m_stcAttempts.SetWindowText( DWrdtoStr(m_Attempts) );
	m_ebHandshake.SetWindowText(m_Handshake);

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
Exemplo n.º 7
0
void SendResponse(ConnectStruct *Con,DNSMessageStruct *Response)
{
struct sockaddr_in Send_sa;
short int sendlen;
int len, salen;
char *Buffer=NULL;
ListNode *Curr;
ResourceRecord *RR;

Buffer=SetStrLen(Buffer,1024);
len=CreateResponsePacket(Buffer,Buffer+1024,Response,&Settings);

if (len==0)
{
  LogToFile(Settings.LogFilePath,"ERROR: Zero length response packet");
	DestroyString(Buffer);
  return;
}

if (Con->Type==TCP_CONNECT)
{
   sendlen=htons(len);
   write(Con->fd, &sendlen, sizeof(short int));
   write(Con->fd, Buffer, len);
}
else if (Con->Type==UDP_CONNECT)
{
   Send_sa.sin_family=AF_INET;
   Send_sa.sin_addr.s_addr=Response->ClientIP;
   Send_sa.sin_port=Response->ClientPort;
   salen=sizeof(struct sockaddr_in);

   sendto(Con->fd,Buffer,len,0,(struct sockaddr *) &Send_sa,salen);
}
else LogToFile(Settings.LogFilePath,"ERROR: Unknown Comms Type %d on send",Con->Type);

if (Settings.LogLevel >= LOG_RESPONSES) 
{
	LogToFile(Settings.LogFilePath,"Sent %d answers to %s for %s query",ListSize(Response->Answers), IPtoStr(Response->ClientIP),Response->Question);

	Curr=ListGetNext(Response->Answers);
	while (Curr)
	{
		RR=(ResourceRecord *) Curr->Item;
		LogToFile(Settings.LogFilePath,"	ANS: %s->%s type=%d ttl=%d",RR->Question,RR->Answer,RR->Type,RR->TTL);
		Curr=ListGetNext(Curr);
	}
}

DestroyString(Buffer);
}
Exemplo n.º 8
0
void CSearchInfo::UpdateInfo()
{	
	// Display data to dialog box
	m_stcName.SetWindowText("Name:  " + m_autSearch->GetResultName(m_SearchID, m_ResultID));
	
	uint64 size = m_pDoc->m_RunningXP ? m_autSearch->GetResultSize2(m_SearchID, m_ResultID) : m_autSearch->GetResultSize(m_SearchID, m_ResultID);
	m_stcSize.SetWindowText("Size:  " + CommaIze(DWrdtoStr( size )) + " Bytes");

	m_stcSha1Hash.SetWindowText("SHA1 Hash:  " + m_autSearch->GetResultHash(m_SearchID, m_ResultID, HASH_SHA1));
	
	m_stcSpeed.SetWindowText("Average Speed: " + CommaIze(DWrdtoStr( m_autSearch->GetResultSpeed(m_SearchID, m_ResultID))) + " KB/s");
	
	int HostCount = m_autSearch->GetResultHostCount(m_SearchID, m_ResultID);
	CString Plural = (HostCount == 1) ? " Host:" : " Hosts";
	
	m_stcHosts.SetWindowText(CommaIze(DWrdtoStr(HostCount)) + Plural);

	// Save selected
	std::vector<ResultHost*> ListSelected;

	POSITION lstPos = m_lstResults.GetFirstSelectedItemPosition();
	while(lstPos)
	{
		int nItem = m_lstResults.GetNextSelectedItem(lstPos);

		//m_lstResults.SetItem(nItem, 0, LVIF_STATE, NULL, 0, 0, LVIS_SELECTED, 0);
		if(nItem < m_lstResults.GetItemCount())
			ListSelected.push_back( (ResultHost*) m_lstResults.GetItemData(nItem));
	}

	// Get new Host ID list
	VARIANT var = m_autSearch->GetHostIDs(m_SearchID, m_ResultID);
	SAFEARRAY* psa = var.parray;

	UINT* nArray;
	SafeArrayAccessData(psa, reinterpret_cast<void**> (&nArray));

	for(int i = 0; i < psa->rgsabound->cElements; i++)
	{
		UINT HostID = nArray[i];

		std::map<UINT, ResultHost*>::iterator itHost = m_HostMap.find(HostID);


		// Add host if not in list yet
		if(itHost == m_HostMap.end())
		{
			ResultHost* pHost = new ResultHost;

			pHost->HostID = HostID;

			pHost->Host.S_addr = m_autSearch->GetHostIP(m_SearchID, m_ResultID, HostID);
			pHost->Port	    = m_autSearch->GetHostPort(m_SearchID, m_ResultID, HostID);
			pHost->Speed	= m_autSearch->GetHostSpeed(m_SearchID, m_ResultID, HostID);
			pHost->Distance = m_autSearch->GetHostDistance(m_SearchID, m_ResultID, HostID);

			pHost->Firewall	= m_autSearch->GetHostFirewall(m_SearchID, m_ResultID, HostID) != 0;
			pHost->Stable	= m_autSearch->GetHostStable(m_SearchID, m_ResultID, HostID) != 0;
			pHost->Busy		= m_autSearch->GetHostBusy(m_SearchID, m_ResultID, HostID) != 0;
			
			pHost->Vendor	= m_autSearch->GetHostVendor(m_SearchID, m_ResultID, HostID);
			pHost->Icon	    = m_pDoc->GetIconIndex(m_autSearch->GetResultName(m_SearchID, m_ResultID));

			// Get extended host info
			VARIANT var2 = m_autSearch->GetHostExtended(m_SearchID, m_ResultID, HostID);
			SAFEARRAY* psa2 = var2.parray;

			BSTR* strArray;
			SafeArrayAccessData(psa2, reinterpret_cast<void**> (&strArray));

			for(int i = 0; i < psa2->rgsabound->cElements; i++)
			{
				if(i > 0)
					pHost->strInfo += " and " + CString(strArray[i]);
				else
					pHost->strInfo = strArray[i];
			}

			SafeArrayUnaccessData(psa2);
			VariantClear(&var2);


			m_HostMap[HostID] = pHost;
			m_HostList.push_back(pHost);
		}
	}

	SafeArrayUnaccessData(psa);
	VariantClear(&var);


	SearchExOrder o(this);
	std::stable_sort(m_HostList.begin(), m_HostList.end(), o);

	int itemCount = m_lstResults.GetItemCount();

	// Insert items into list
	for(int i = 0; i < m_HostList.size(); i++)
	{
		ResultHost* p = m_HostList[i];

		CString Properties = "";

		if(p->Firewall)
			Properties += "Firewall, ";

		if(p->Busy)
			Properties += "Busy, ";
		
		if(p->Stable)
			Properties += "Stable, ";

		if(Properties.ReverseFind(',') != -1)
			Properties = Properties.Mid(0, Properties.GetLength() - 2);


		if (itemCount <= i)
			m_lstResults.InsertItem(itemCount++, IPtoStr(p->Host), p->Icon.Index);
		else
			m_lstResults.SetItem(i, 0, LVIF_TEXT | LVIF_IMAGE | LVIF_STATE, IPtoStr(p->Host), p->Icon.Index, 0, LVIS_SELECTED, NULL);
	
		m_lstResults.SetItemText(i, 1, CommaIze( DWrdtoStr( p->Speed)) + " KB/s");
		m_lstResults.SetItemText(i, 2, Properties);
		m_lstResults.SetItemText(i, 3, GetVendor(p->Vendor));
		m_lstResults.SetItemText(i, 4, DWrdtoStr(p->Distance));
		m_lstResults.SetItemText(i, 5, p->strInfo);
		
		m_lstResults.SetItemData(i, (DWORD) p);

		// Re-select previously selected item
		for (int sel = 0; sel < ListSelected.size(); sel++)
			if(ListSelected[sel] == p)
				m_lstResults.SetItem(i, 0, LVIF_STATE, NULL, 0, LVIS_SELECTED, LVIS_SELECTED, 0);
	}

	for (int j = i + 1; j < itemCount; j++)
		m_lstResults.DeleteItem(i + 1);
}
Exemplo n.º 9
0
int ConnectHopSocks(STREAM *S, int SocksLevel, const char *ProxyURL, const char *Destination)
{
    char *Tempstr=NULL;
    char *Token=NULL, *Host=NULL, *User=NULL, *Pass=NULL;
    uint8_t *ptr;
    uint32_t IP;
    const char *tptr;
    int result, RetVal=FALSE, val;
    uint8_t HostType=HT_IP4;

    ParseConnectDetails(ProxyURL, NULL, &Host, &Token, &User, &Pass, NULL);
    if (! (S->State & SS_INITIAL_CONNECT_DONE))
    {
        val=atoi(Token);
        S->in_fd=TCPConnect(Host, val, 0);
        S->out_fd=S->in_fd;
        if (S->in_fd == -1)
        {
            RaiseError(0, "ConnectHopSocks", "connection to socks proxy at %s failed", ProxyURL);
            return(FALSE);
        }


        if (SocksLevel==CONNECT_HOP_SOCKS5)
        {
            if (! ConnectHopSocks5Auth(S, User, Pass))
            {
                RaiseError(0, "ConnectHopSocks", "authentication to socks proxy at %s failed", ProxyURL);
                return(FALSE);
            }
        }
    }

//Horrid binary protocol.
    Tempstr=SetStrLen(Tempstr, StrLen(User) + 20 + StrLen(Destination));
    ptr=Tempstr;

//version
    if (SocksLevel==CONNECT_HOP_SOCKS5) *ptr=5;
    else *ptr=4; //version number
    ptr++;

//connection type
    *ptr=1; //outward connection (2 binds a port for incoming)
    ptr++;



//Sort out destination now
    tptr=Destination;
    if (strncmp(tptr,"tcp:",4)==0) tptr+=4;
    tptr=GetToken(tptr,":",&Token,0);
    if (IsIP4Address(Token)) HostType=HT_IP4;
    else if (IsIP6Address(Token)) HostType=HT_IP6;
    else HostType=HT_DOMAIN;


    if (SocksLevel==CONNECT_HOP_SOCKS5)
    {
//Socks 5 has a 'reserved' byte after the connection type
        *ptr=0;
        ptr++;

        *ptr=HostType;
        ptr++;
        switch (HostType)
        {
        case HT_IP4:
            *((uint32_t *) ptr) =StrtoIP(Token);
            ptr+=4;
            break;

        case HT_IP6:
            break;

        default:
            val=StrLen(Token);
            *ptr=val;
            ptr++;
            memcpy(ptr, Token, val);
            ptr+=val;
            break;
        }
    }


//destination port. By a weird coincidence this is in the right place
//for either socks4 or 5, despite the fact that it comes after the
//host in socks5, and before the host in socks4
    *((uint16_t *) ptr) =htons(atoi(tptr));
    ptr+=2;

    if (SocksLevel==CONNECT_HOP_SOCKS4)
    {
        //destination host
        switch (HostType)
        {
        case HT_IP4:
            *((uint32_t *) ptr) =StrtoIP(Token);
            ptr+=4;
            val=StrLen(User)+1;
            memcpy(ptr,User,val);
            ptr+=val;
            break;

        default:
            *((uint32_t *) ptr) =StrtoIP("0.0.0.1");
            ptr+=4;
            break;
        }

        val=StrLen(User)+1;
        memcpy(ptr, User, val);
        ptr+=val;

        //+1 to include terminating \0
        val=StrLen(Token) +1;
        memcpy(ptr, Token, val);
        ptr+=val;
    }

    STREAMWriteBytes(S,Tempstr,(char *)ptr-Tempstr);
    STREAMFlush(S);
    Tempstr=SetStrLen(Tempstr, 32);
    result=STREAMReadBytes(S,Tempstr,32);


    if (SocksLevel==CONNECT_HOP_SOCKS5)
    {
        if ((result > 8) && (Tempstr[0]==5) && (Tempstr[1]==0))
        {
            RetVal=TRUE;
        }
    }
    else
    {
        //Positive response will be 0x00 0x5a 0x00 0x00 0x00 0x00 0x00 0x00
        //although only the leading two bytes (0x00 0x5a, or \0Z) matters
        if ((result==8) && (Tempstr[0]=='\0') && (Tempstr[1]=='Z'))
        {
            RetVal=TRUE;

            IP=*(uint32_t *) (Tempstr + 4);
            if (IP != 0) STREAMSetValue(S, "IPAddress", IPtoStr(IP));
        }
    }


    if (! RetVal) RaiseError(0, "ConnectHopSocks", "socks proxy at %s refused connection to %s", ProxyURL, Destination);

    DestroyString(Tempstr);
    DestroyString(Host);
    DestroyString(User);
    DestroyString(Pass);
    DestroyString(Token);

    return(RetVal);
}
Exemplo n.º 10
0
void CViewStatistics::OnSockUpdate()
{
	// Save selected nodes
	std::vector<int> ListSelected;
	POSITION lstPos = m_lstNodes.GetFirstSelectedItemPosition();
	while(lstPos)
	{
		int nItem = m_lstNodes.GetNextSelectedItem(lstPos);

		m_lstNodes.SetItem(nItem, 0, LVIF_STATE, NULL, 0, 0, LVIS_SELECTED, 0);
		if(nItem < m_lstNodes.GetItemCount())
			ListSelected.push_back(m_lstNodes.GetItemData(nItem));
	}

	CString HostPort, NodeType;
	int itemCount = m_lstNodes.GetItemCount();
	int Hosts	  = 0;
	int Children  = 0;
	int pos = 0;


	// Get current node IDs and put them into a vector
	VARIANT var = m_autNetwork->GetNodeIDs();
	SAFEARRAY* psa = var.parray;

	int* nArray;
	SafeArrayAccessData(psa, reinterpret_cast<void**> (&nArray));

	std::vector<int> NodeIDs;
	for(int i = 0; i < psa->rgsabound->cElements; i++)
		 NodeIDs.push_back(nArray[i]);

	SafeArrayUnaccessData(psa);
	VariantClear(&var);


	// Get child IDs and put them into a vector
	var = m_autNetwork->GetChildNodeIDs();
	psa = var.parray;

	SafeArrayAccessData(psa, reinterpret_cast<void**> (&nArray));

	std::vector<int> ChildIDs;
	for(int i = 0; i < psa->rgsabound->cElements; i++)
		 ChildIDs.push_back(nArray[i]);

	SafeArrayUnaccessData(psa);
	VariantClear(&var);

	// Mode 0 - Normal nodes
	// Mode 1 - Child nodes
	for(int Mode = 0; Mode < 2; Mode++)
	{
		std::vector<int>* NodeList = NULL;

		if(Mode == 0)
			NodeList = &NodeIDs;
		else
			NodeList = &ChildIDs;


		for(i = 0; i < NodeList->size(); i++)
		{
			int NodeID = NodeList->at(i);

			if(m_autNetwork->GetNodeState(NodeID) != SOCK_CONNECTED)
				continue;
			
			// Count Hosts
			Hosts++;
			
			if(Mode == 1)
				Children++;
			
			IP NodeIP;
			NodeIP.S_addr = m_autNetwork->GetNodeIP(NodeID);

			HostPort = IPtoStr(NodeIP) + ":" + DWrdtoStr(m_autNetwork->GetNodePort(NodeID));

			if(itemCount <= pos)
				m_lstNodes.InsertItem(pos, HostPort);
			else
				m_lstNodes.SetItemText(pos, 0, HostPort);

			// Type 
			if(Mode == 1)
				NodeType = "Local Child";

			else if(m_autNetwork->GetNodeMode(NodeID) == CLIENT_ULTRAPEER)
				NodeType = "Ultrapeer";

			else if(m_autNetwork->GetNodeMode(NodeID) == CLIENT_LEAF)
				NodeType = "Child Node";

			else
				NodeType = "Normal Node";

			m_lstNodes.SetItemText(pos, 1, NodeType);

			m_lstNodes.SetItemData(pos, (DWORD) NodeID);
			

			// Re-select previously selected item
			for (int sel = 0; sel < ListSelected.size(); sel++)
				if(ListSelected[sel] == NodeID)
					m_lstNodes.SetItem(pos, 0, LVIF_STATE, NULL, 0, LVIS_SELECTED, LVIS_SELECTED, 0);
		
			pos++;
		}

		if(Mode == 1 && Children == 0)
			break;

		// Add space between
		if(itemCount <= pos)
			m_lstNodes.InsertItem(pos, "");
		else
			m_lstNodes.SetItemText(pos, 0, "");
		m_lstNodes.SetItemText(pos, 1, "");
		m_lstNodes.SetItemData(pos, NULL);
		pos++;
		

		if(m_autNetwork->ClientMode() == CLIENT_LEAF)
			break;
	}

	// Delete extra lines
	for (int del = pos; del < itemCount; del++)
		m_lstNodes.DeleteItem(pos);
}