library/src/network/BP-Network.cc
// generate a SOCKS4A Greeting Packet for Proxy
generate a SOCKS4A Greeting Packet for Proxy
generate a SOCKS4A Greeting Packet for Proxy
P_BP_NETWORK_PACKET_DATA BP_NetProxyCreateSOCKS4AGreeting(P_BP_NETWORK_HANDLE net_handle, P_BP_NETWORK_PROXY proxy)
{
	if(!net_handle)
		return NULL;
	if(!proxy)
		return NULL;
	P_BP_NETWORK_PACKET_DATA greeting_packet = (P_BP_NETWORK_PACKET_DATA) bpcalloc(sizeof(BP_NETWORK_PACKET_DATA), 1);
	if(!greeting_packet)
		return NULL;
	P_BP_LINKL socks4_greeting = BP_CreateLinkL("socks4a greeting", BP_TRUE);
	BYTE * version_number     = (BYTE *)     tq_calloc(socks4_greeting, 1, 1, BPLN);
	BYTE * command_code       = (BYTE *)     tq_calloc(socks4_greeting, 1, 1, BPLN);
	uint16_t * dst_port       = (uint16_t *) tq_calloc(socks4_greeting, 2, 1, BPLN);
	uint32_t * dst_ip         = (uint32_t *) tq_calloc(socks4_greeting, 4, 1, BPLN);
	*version_number = (BYTE) 0x04;
	*command_code = (BYTE) 0x01;
	*dst_ip = (uint32_t) 0xff000000;
	if(!proxy->user)
	{
		tq_calloc(socks4_greeting, 1, 1, BPLN);
	}
	else
	{
		tq_strdup(socks4_greeting, proxy->user, BPLN);
	}
	if(proxy->destination_host)
	{
		tq_strdup(socks4_greeting, proxy->destination_host, BPLN);
		*dst_port = (uint16_t) htons(proxy->destination_port);
	}
	else
	{
		size_t n = 0;
		for(; n < net_handle->proxy_info->proxy_chain_n; n++)
			if(proxy == (P_BP_NETWORK_PROXY) &net_handle->proxy_info->proxy_chain[n])
				break;
		if(n < net_handle->proxy_info->proxy_chain_n-1)
		{
			tq_strdup(socks4_greeting, net_handle->proxy_info->proxy_chain[n+1].destination_host, BPLN);
			*dst_port = (uint16_t) htons(net_handle->proxy_info->proxy_chain[n+1].destination_port);
		}
	}
	BP_LinkLFlattenRaw(socks4_greeting);
	greeting_packet->data   = (BYTE *) bpcalloc(socks4_greeting->flat_buff_n, 1);
	memcpy(greeting_packet->data, socks4_greeting->flat_buff, socks4_greeting->flat_buff_n);
	greeting_packet->data_n = socks4_greeting->flat_buff_n;
	BP_DestroyLinkL(socks4_greeting, BP_TRUE);
	return greeting_packet;
}
P_BP_NETWORK_PACKET_DATA BP_NetProxyCreateSOCKS4AGreeting ( P_BP_NETWORK_HANDLE net_handle , P_BP_NETWORK_PROXY proxy ) { if ( ! net_handle ) return NULL ; if ( ! proxy ) return NULL ; P_BP_NETWORK_PACKET_DATA greeting_packet = ( P_BP_NETWORK_PACKET_DATA ) bpcalloc ( sizeof ( BP_NETWORK_PACKET_DATA ) , 1 ) ; if ( ! greeting_packet ) return NULL ; P_BP_LINKL socks4_greeting = BP_CreateLinkL ( " " , BP_TRUE ) ; BYTE * version_number = ( BYTE * ) tq_calloc ( socks4_greeting , 1 , 1 , BPLN ) ; BYTE * command_code = ( BYTE * ) tq_calloc ( socks4_greeting , 1 , 1 , BPLN ) ; uint16_t * dst_port = ( uint16_t * ) tq_calloc ( socks4_greeting , 2 , 1 , BPLN ) ; uint32_t * dst_ip = ( uint32_t * ) tq_calloc ( socks4_greeting , 4 , 1 , BPLN ) ; * version_number = ( BYTE ) 0x04 ; * command_code = ( BYTE ) 0x01 ; * dst_ip = ( uint32_t ) 0xff000000 ; if ( ! proxy -> user ) { tq_calloc ( socks4_greeting , 1 , 1 , BPLN ) ; } else { tq_strdup ( socks4_greeting , proxy -> user , BPLN ) ; } if ( proxy -> destination_host ) { tq_strdup ( socks4_greeting , proxy -> destination_host , BPLN ) ; * dst_port = ( uint16_t ) htons ( proxy -> destination_port ) ; } else { size_t n = 0 ; for ( ; n < net_handle -> proxy_info -> proxy_chain_n ; n ++ ) if ( proxy == ( P_BP_NETWORK_PROXY ) & net_handle -> proxy_info -> proxy_chain [ n ] ) break ; if ( n < net_handle -> proxy_info -> proxy_chain_n - 1 ) { tq_strdup ( socks4_greeting , net_handle -> proxy_info -> proxy_chain [ n + 1 ] . destination_host , BPLN ) ; * dst_port = ( uint16_t ) htons ( net_handle -> proxy_info -> proxy_chain [ n + 1 ] . destination_port ) ; } } BP_LinkLFlattenRaw ( socks4_greeting ) ; greeting_packet -> data = ( BYTE * ) bpcalloc ( socks4_greeting -> flat_buff_n , 1 ) ; memcpy ( greeting_packet -> data , socks4_greeting -> flat_buff , socks4_greeting -> flat_buff_n ) ; greeting_packet -> data_n = socks4_greeting -> flat_buff_n ; BP_DestroyLinkL ( socks4_greeting , BP_TRUE ) ; return greeting_packet ; }
generate a SOCKS4A Greeting Packet for Proxy
generate a SOCKS4A Greeting Packet for Proxy
// ensure we have a handle and a proxy pointer // greeting packet // ensure we created the structure // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Create Packet using LinkL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // LinkL is later flattened to become the packet. // create linkl for generating greeting (ez mode) // allocate one byte for version number // allocate one byte for command code // allocate two bytes for port number // allocate 4 bytes for dst_ip (should always be 0xff000000 (0.0.0.255) // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Perform Data Assignments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set the version number to 4 // set the command to 0x01 (for connect) // set the destination IP (always bad value) // set user id here if the user supplied one, if not, add a null byte (created via calloc) // set the destination host here if its provided in the structure, if not // assume that the destination is the next proxy in the chain // add the destination host // set the destination port // Since the items were null, we assume that the next chain element is // the proxy destination. // first get proxy postion // ensure that the position is correct (must contain a next element) // set destination from next element in chain // set port from next element in chain // Flatten the tail queue to generate socks4a greeting packet // allocate space for returning buffer // copy in data // set the size in the packet // destroy the linkl // return the greeting packet