library/src/network/BP-Network.cc
// Connect Socket Creation -
// 	To create a socket to connect to a remote host, pass a 0 to the
// 	function in the setListen field.  This will set the source information
//  as inaddr any.
//
// This function will create a new network handle. If no network handle is
// provided (NULL ptr value) this function will create a new network handle
// and return it.
Connect Socket Creation
To create a socket to connect to a remote host, pass a 0 to the
function in the setListen field.  This will set the source information
as inaddr any.

This function will create a new network handle. If no network handle is
provided (NULL ptr value) this function will create a new network handle
and return it.
Connect Socket Creation To create a socket to connect to a remote host pass a 0 to the function in the setListen field . This will set the source information as inaddr any . This function will create a new network handle . If no network handle is provided ( NULL ptr value ) this function will create a new network handle and return it .
P_BP_NETWORK_HANDLE BP_NetCreateSocket
(
	char              * netAddr,
	short               netPort,
	int                 proto,
	int                 setListen,
	P_BP_NETWORK_HANDLE net_handle_in
)
{
	if(!netAddr || !netPort)
		return NULL;
	P_BP_NETWORK_HANDLE net_handle = NULL;
	size_t local_handle = BP_FALSE;
	if(net_handle_in)
	{
		net_handle = net_handle_in;
	}
	else
	{
		net_handle = BP_CreateNetworkHandle();
		local_handle = BP_TRUE;
	}
	struct hostent *hostPtr = NULL;
	int optionFlag = 1;
	if( (proto == IPPROTO_TCP) || (proto == 0x00) )
	{
		net_handle->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
		if(setsockopt(net_handle->sock, IPPROTO_TCP, TCP_NODELAY, (char *) &optionFlag, sizeof(int) ) == -1)
		{
			BP_DestroyNetworkHandle(net_handle);
			return NULL;
		}
		if(setsockopt(net_handle->sock, SOL_SOCKET, SO_REUSEADDR, (char *) &optionFlag, sizeof(int)) == -1)
		{
			BP_DestroyNetworkHandle(net_handle);
			return NULL;
		}
	}
	else if(proto == IPPROTO_UDP)
	{
		if(net_handle->proxy_chain_established_successfully == BP_TRUE)
		{
		}
		else
		{
			net_handle->sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
		}
	}
	else
	{
		BP_DestroyNetworkHandle(net_handle);
		return NULL;
	}
	hostPtr = gethostbyname(netAddr);
	if(!hostPtr)
	{
		BP_DestroyNetworkHandle(net_handle);
		return NULL;
	}
	if(setListen == 0)
	{
		net_handle->source.sin_family      = AF_INET;
		net_handle->source.sin_addr.s_addr = INADDR_ANY;
		net_handle->source.sin_port		   = 0;
		net_handle->dest.sin_family = hostPtr->h_addrtype;
		memcpy(&net_handle->dest.sin_addr.s_addr, hostPtr->h_addr, hostPtr->h_length);
		net_handle->dest.sin_port = htons(netPort);
	}
	else
	{
		net_handle->source.sin_family	   = AF_INET;
		net_handle->source.sin_port        = htons(netPort);
		net_handle->source.sin_addr.s_addr = INADDR_ANY;
	}
	if(setListen == 0)
	{
		if(connect(net_handle->sock, (struct sockaddr *) &net_handle->dest, sizeof(struct sockaddr)) == -1)
		{
			BP_DestroyNetworkHandle(net_handle);
			return NULL;
		}
		net_handle->last_tcp_connect_succeeded = BP_TRUE;
	}
	if(setListen != 0)
	{
		if( bind(net_handle->sock, (struct sockaddr *) &net_handle->source, sizeof(struct sockaddr)) == -1)
		{
			BP_DestroyNetworkHandle(net_handle);
			return NULL;
		}			
		if(listen(net_handle->sock, DEFAULT_BACKLOG) != 0)
		{
			BP_DestroyNetworkHandle(net_handle);
			return NULL;
		}
		net_handle->last_tcp_listen_succeeded = BP_TRUE;
	}
	return net_handle;
}
P_BP_NETWORK_HANDLE BP_NetCreateSocket ( char * netAddr , short netPort , int proto , int setListen , P_BP_NETWORK_HANDLE net_handle_in ) { if ( ! netAddr || ! netPort ) return NULL ; P_BP_NETWORK_HANDLE net_handle = NULL ; size_t local_handle = BP_FALSE ; if ( net_handle_in ) { net_handle = net_handle_in ; } else { net_handle = BP_CreateNetworkHandle ( ) ; local_handle = BP_TRUE ; } struct hostent * hostPtr = NULL ; int optionFlag = 1 ; if ( ( proto == IPPROTO_TCP ) || ( proto == 0x00 ) ) { net_handle -> sock = socket ( PF_INET , SOCK_STREAM , IPPROTO_TCP ) ; if ( setsockopt ( net_handle -> sock , IPPROTO_TCP , TCP_NODELAY , ( char * ) & optionFlag , sizeof ( int ) ) == -1 ) { BP_DestroyNetworkHandle ( net_handle ) ; return NULL ; } if ( setsockopt ( net_handle -> sock , SOL_SOCKET , SO_REUSEADDR , ( char * ) & optionFlag , sizeof ( int ) ) == -1 ) { BP_DestroyNetworkHandle ( net_handle ) ; return NULL ; } } else if ( proto == IPPROTO_UDP ) { if ( net_handle -> proxy_chain_established_successfully == BP_TRUE ) { } else { net_handle -> sock = socket ( PF_INET , SOCK_DGRAM , IPPROTO_UDP ) ; } } else { BP_DestroyNetworkHandle ( net_handle ) ; return NULL ; } hostPtr = gethostbyname ( netAddr ) ; if ( ! hostPtr ) { BP_DestroyNetworkHandle ( net_handle ) ; return NULL ; } if ( setListen == 0 ) { net_handle -> source . sin_family = AF_INET ; net_handle -> source . sin_addr . s_addr = INADDR_ANY ; net_handle -> source . sin_port = 0 ; net_handle -> dest . sin_family = hostPtr -> h_addrtype ; memcpy ( & net_handle -> dest . sin_addr . s_addr , hostPtr -> h_addr , hostPtr -> h_length ) ; net_handle -> dest . sin_port = htons ( netPort ) ; } else { net_handle -> source . sin_family = AF_INET ; net_handle -> source . sin_port = htons ( netPort ) ; net_handle -> source . sin_addr . s_addr = INADDR_ANY ; } if ( setListen == 0 ) { if ( connect ( net_handle -> sock , ( struct sockaddr * ) & net_handle -> dest , sizeof ( struct sockaddr ) ) == -1 ) { BP_DestroyNetworkHandle ( net_handle ) ; return NULL ; } net_handle -> last_tcp_connect_succeeded = BP_TRUE ; } if ( setListen != 0 ) { if ( bind ( net_handle -> sock , ( struct sockaddr * ) & net_handle -> source , sizeof ( struct sockaddr ) ) == -1 ) { BP_DestroyNetworkHandle ( net_handle ) ; return NULL ; } if ( listen ( net_handle -> sock , DEFAULT_BACKLOG ) != 0 ) { BP_DestroyNetworkHandle ( net_handle ) ; return NULL ; } net_handle -> last_tcp_listen_succeeded = BP_TRUE ; } return net_handle ; }
Connect Socket Creation
To create a socket to connect to a remote host, pass a 0 to the
function in the setListen field.
Connect Socket Creation To create a socket to connect to a remote host pass a 0 to the function in the setListen field .
// ensure we have an address and a port // attempt to create a new network handle // flag indicating whether or not the net_handle was // passed in, or if it was locally allocated. // set the handle from parameter if set // host ent // option flag which will be set via setsockopt // Create socket based around protocol // create the new socket // set options (TCP_NODELAY disables nagle, SO_REUSEADDR allows the address to be reused) // destroy the network handle // return indicating failure // set additional options for reusing addresses (prevents congestion) // destroy the network handle // return indicating failure // check if the user is proxychained or not // create a UDP socket if the dev wants unproxied UDP // destination gethostbyname // if its a not a listening port // source setup for non listening socket // destination setup for non listening socket // source setup for listening socket // if not set to listen, attempt to connect // attempt connect if we're not listening // indicate that the last tcp connect was established OK // if listening is a nonzero value, attempt to listen // attempt to bind // attempt to listen // indicate that the last tcp listen succeded // if we got here we've got a successful connection made