Exemple #1
0
int InitServerSock(char *Address, int Port)
{
    int sock;
    struct sockaddr_in sa;
    int salen;
    int result;

    sock=socket(AF_INET,SOCK_STREAM,0);
    if (sock <0) return(-1);

    result=1;
    salen=sizeof(result);
    setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&result,salen);

    sa.sin_port=htons(Port);
    sa.sin_family=AF_INET;
    if (StrLen(Address) > 0) sa.sin_addr.s_addr=StrtoIP(Address);
    else sa.sin_addr.s_addr=INADDR_ANY;

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

    if (result==0)
    {
        result=listen(sock,10);
    }

    if (result==0) return(sock);
    else
    {
        close(sock);
        return(-1);
    }
}
Exemple #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;
}		
Exemple #3
0
int ConnectHopSocks(STREAM *S, int Type, const char *Host, int Port, const char *User, const char *Pass, const char *Path)
{
char *Tempstr=NULL;
uint8_t *ptr;
char *Token=NULL;
const char *tptr;
int result, RetVal=FALSE;

S->in_fd=ConnectToHost(Host,Port,0); 
S->out_fd=S->in_fd;
if (S->in_fd == -1) return(FALSE);


//Horrid binary protocol. 
Tempstr=SetStrLen(Tempstr, StrLen(User) +20);
ptr=Tempstr;
*ptr=4; //version number
ptr++;
*ptr=1; //outward connection (2 binds a port for incoming)
ptr++;

tptr=Path;
if (strncmp(tptr,"tcp:",4)==0) tptr+=4;
tptr=GetToken(tptr,":",&Token,0);

//destination port
*((uint16_t *) ptr) =htons(atoi(tptr));
ptr+=2;

//destination host
*((uint32_t *) ptr) =StrtoIP(Token);
ptr+=4;

strcpy(ptr,User);
ptr+=StrLen(User);
ptr++;
STREAMWriteBytes(S,Tempstr,(char *)ptr-Tempstr); STREAMFlush(S);
result=STREAMReadBytes(S,Tempstr,8);

//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;


DestroyString(Tempstr);
DestroyString(Token);

return(RetVal);
}
Exemple #4
0
int SendQuery(ConnectStruct *Con, char *Server, DNSMessageStruct *Query)
{
struct sockaddr_in Send_sa;
int len, sendlen;
int salen, result;
char *Buffer=NULL;
int BuffLen=1024;

Buffer=SetStrLen(Buffer,BuffLen);
len=CreateQuestionPacket(Buffer,Buffer+BuffLen,Query->Question,Query->Type);
Con->LastActivity=Now;

if (len==0)
{
  LogToFile(Settings.LogFilePath,"ERROR: Zero length query packet");
  return(-1);
}


if (Settings.LogLevel >=LOG_REMOTE) LogToFile(Settings.LogFilePath,"REMOTE: Querying server %s for %s",Server,Query->Question);
if (Con->Type==TCP_CONNECT)
{
   sendlen=htons(len);
   result=write(Con->fd, &sendlen, sizeof(short int));
   if (result < 1) Con->State=CON_CLOSED;
   else 
   {
	result=write(Con->fd, Buffer, len);
   	if (result < 1) Con->State=CON_CLOSED;
   }
}
else
{
Send_sa.sin_family=AF_INET;
Send_sa.sin_addr.s_addr=StrtoIP(Server);
//Send_sa.sin_port=htons(Server->Port);
Send_sa.sin_port=htons(53);
salen=sizeof(struct sockaddr_in);
result=sendto(Con->fd,Buffer,len,0,(struct sockaddr *) &Send_sa,salen);
}

DestroyString(Buffer);
return(result);
}
Exemple #5
0
void CGnuUpload::OnReceive(int nErrorCode) 
{
	byte pBuff[6000];

	int dwBuffLength = m_pSocket->Receive(pBuff, 4096);

	switch (dwBuffLength)
	{
	case 0:
		m_pShell->m_Error = "Bad Push";
		Close();
		return;
		break;
	case SOCKET_ERROR:
		m_pShell->m_Error = "Bad Push";
		Close();
		return;
		break;
	}

	pBuff[dwBuffLength] = 0;
	CString Header(pBuff);


	// Clear old GetRequest when new one comes in
	if(m_GetRequest.Find("\r\n\r\n") != -1)
	{
		m_GetRequest = "";
		m_pShell->m_Handshake  = "";
	}

	m_pShell->m_Handshake += Header;
	m_GetRequest += Header;


	// New Upload
	if(m_GetRequest.Find("\r\n\r\n") != -1)
	{
		if(m_GetRequest.Left(4) == "GET " || m_GetRequest.Left(5) == "HEAD ")
		{	
			// Get Node info
			CString Host;
			UINT    nPort;
			m_pSocket->GetPeerName(Host, nPort);
			

			// Check if it's a blocked Host 
			if (m_pPrefs->BlockedIP(StrtoIP(Host)) == true) 
			{ 
				m_pShell->m_Error = "Blocked"; 
				Send_ClientBlock(Host); 
				Close(); 
			} 
			else 
			{ 
				// Set Variables 
				m_pShell->m_Host = StrtoIP(Host); 
				m_pShell->m_Port = 0; 
				m_pShell->m_RequsetPending = true;
			}

		}
		else if( m_GetRequest.Left(4) == "AUTH" && !m_pShell->m_Challenge.IsEmpty() )
		{		
			CParsedHeaders ParsedHeaders(m_pShell->m_Handshake);
			
			if(m_pShell->m_ChallengeAnswer.Compare( ParsedHeaders.FindHeader("X-Auth-Response") ) == 0 )
			{
				m_Authorized = true;
				StartUpload();
			}
			else
			{
				m_pShell->m_Error = "Authentication Failed";
				Close();
			}
		}

		else
		{
			m_pShell->m_Error = "Bad Push";
			Close();
		}
	}
}
Exemple #6
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);
}
Exemple #7
0
CGnuDownloadShell* CGnuTransfers::LoadDownloadHosts(CString FilePath)
{
	// Check if file already loaded
	for(int i = 0; i < m_DownloadList.size(); i++)
		if( m_DownloadList[i]->m_BackupPath.CompareNoCase(FilePath) == 0 )
			return NULL;


	CStdioFile BackupFile;

	CString NextLine;
	CString Backup;
	
	if (BackupFile.Open(FilePath, CFile::modeRead))
	{
		while (BackupFile.ReadString(NextLine))
			Backup += NextLine + "\n";

		BackupFile.Abort();
	}

	if(Backup.IsEmpty() || Backup.Find("[Download]") == -1)
		return NULL;

	int CurrentPos = Backup.Find("[Download]");

	CGnuDownloadShell* Download = new CGnuDownloadShell(this);
	
	Download->m_ShellStatus		= (CGnuDownloadShell::Status) atoi(GetBackupString("Status", CurrentPos, Backup));
	Download->m_Name			= GetBackupString("Name", CurrentPos, Backup);
	Download->m_FileLength		= _atoi64(GetBackupString("FileLength", CurrentPos, Backup));
	Download->m_PartSize		= atoi(GetBackupString("PartSize", CurrentPos, Backup));
	Download->m_OverrideName	= GetBackupString("OverrideName", CurrentPos, Backup);
	Download->m_OverridePath	= GetBackupString("OverridePath", CurrentPos, Backup);
	Download->m_PartialPath		= GetBackupString("PartialPath", CurrentPos, Backup);
	Download->m_BackupPath  	= FilePath;
	Download->m_Sha1Hash		= GetBackupString("Sha1Hash", CurrentPos, Backup);
	Download->m_Search			= GetBackupString("Search", CurrentPos, Backup);
	Download->m_AvgSpeed		= atoi(GetBackupString("AvgSpeed", CurrentPos, Backup));
	Download->m_HashComputed	= atoi(GetBackupString("HashComputed", CurrentPos, Backup));
	Download->m_HashVerified	= atoi(GetBackupString("HashVerified", CurrentPos, Backup));
	Download->m_FileMoved		= atoi(GetBackupString("FileMoved", CurrentPos, Backup));
	Download->m_ReasonDead		= GetBackupString("ReasonDead", CurrentPos, Backup);
	Download->m_MetaXml         = GetBackupString("Meta", CurrentPos, Backup);

	Download->m_UseProxy		= atoi(GetBackupString("UseProxy", CurrentPos, Backup));
	Download->m_DefaultProxy	= GetBackupString("DefaultProxy", CurrentPos, Backup);

	Download->m_TigerHash		= GetBackupString("TigerHash", CurrentPos, Backup);
	Download->m_TreeSize		= atoi(GetBackupString("TreeSize", CurrentPos, Backup));
	Download->m_TreeRes			= atoi(GetBackupString("TreeRes", CurrentPos, Backup));

	if(Download->m_TreeSize)
	{
		Download->m_TigerTree = new byte[Download->m_TreeSize];
		memset(Download->m_TigerTree, 0, Download->m_TreeSize);
	}

	if(Download->m_TigerTree)
	{
		CString Value = GetBackupString("TigerTree", CurrentPos, Backup);

		int buffPos = 0;
		int dotPos  = Value.Find(".");

		while(dotPos != -1 && buffPos < Download->m_TreeSize)
		{
			DecodeBase32( Value.Mid(dotPos - 39, 39), 39, Download->m_TigerTree + buffPos, Download->m_TreeSize - buffPos );

			buffPos += 24;
			dotPos = Value.Find(".", dotPos + 1);
		}
	}


	Download->Init(Download->m_Name, Download->m_FileLength, HASH_SHA1, Download->m_Sha1Hash);

	
	// Load Host info
	if( !Download->m_FileMoved )
		for(int i = 0; ; i++)
		{
			CurrentPos = Backup.Find("[Host " + NumtoStr(i) + "]");

			if(CurrentPos == -1)
				break;

			CurrentPos += 5; // Host in header and value conflict

			FileSource nResult;
			nResult.Name = GetBackupString("Name", CurrentPos, Backup);
			nResult.NameLower = nResult.Name;
			nResult.NameLower.MakeLower();

			nResult.Sha1Hash	 = GetBackupString("Sha1Hash", CurrentPos, Backup);
			//nResult.BitprintHash = GetBackupString("BitprintHash", CurrentPos, Backup);

			nResult.FileIndex	= atoi(GetBackupString("FileIndex", CurrentPos, Backup));
			nResult.Size		= _atoi64(GetBackupString("Size", CurrentPos, Backup));

			nResult.Address.Host = StrtoIP(GetBackupString("Host", CurrentPos, Backup));
			nResult.Address.Port = atoi(GetBackupString("Port", CurrentPos, Backup));
			nResult.Network      = atoi(GetBackupString("Network", CurrentPos, Backup));
			nResult.HostStr		 = GetBackupString("HostStr", CurrentPos, Backup);
			nResult.Path		 = GetBackupString("Path", CurrentPos, Backup);
			nResult.Speed		 = atoi(GetBackupString("Speed", CurrentPos, Backup));
			nResult.Vendor		 = GetBackupString("Vendor", CurrentPos, Backup);

			nResult.Firewall	= atoi(GetBackupString("Firewall", CurrentPos, Backup)) != 0;
			nResult.OpenSlots	= atoi(GetBackupString("OpenSlots", CurrentPos, Backup)) != 0;
			nResult.Busy		= atoi(GetBackupString("Busy", CurrentPos, Backup)) != 0;
			nResult.Stable		= atoi(GetBackupString("Stable", CurrentPos, Backup)) != 0;
			nResult.ActualSpeed = atoi(GetBackupString("ActualSpeed", CurrentPos, Backup)) != 0;
			nResult.SupportF2F  = atoi(GetBackupString("SupportF2F", CurrentPos, Backup)) != 0;
			DecodeBase16(GetBackupString("PushID", CurrentPos, Backup), 32, (byte*) &nResult.PushID, 16);

			CString Nodes = GetBackupString("Direct", CurrentPos, Backup);
			while(!Nodes.IsEmpty())
				nResult.DirectHubs.push_back( StrtoIPv4(ParseString(Nodes, ',')) );

			nResult.GnuRouteID = 0;
			nResult.Distance = 7;
			//nResult.Icon     = m_pCore->GetIconIndex(nResult.Name);

			Download->AddHost(nResult);
		}

	//Download->m_DoReQuery = true;


	// Add Download to list
	m_DownloadAccess.Lock();
	m_DownloadList.push_back(Download);
	m_DownloadAccess.Unlock();

	TransferLoadMeta();

	if(Download->m_ShellStatus == CGnuDownloadShell::eActive)
		Download->Start();

	return Download;
}