/*
Sends data to the server connected to the client. Retries automatically if all the bytes were not sent.
Returns and tries to reconnect the server until it succeeds or StopCLITCP is called if there is a
tranfer error.

CLITCP* pCLITCP : (IN,OUT) pointer to the data structure representing the client
char *sendbuf : (IN) data to send
int sendbuflen : (IN) number of bytes to send

Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error
*/
int SendCLITCP(CLITCP* pCLITCP, char* sendbuf, int sendbuflen)	{

	if((pCLITCP == NULL) || (pCLITCP->initialized != 1))	{
		return EXIT_FAILURE;
	}

	if(SendTCP(pCLITCP->ConnectSocket, sendbuf, sendbuflen) != EXIT_SUCCESS)	{
		EnterCriticalSection(&pCLITCP->CSconnected);
		if(pCLITCP->connected == 1)	{
			fprintf(stderr, "Connection lost\n");
			pCLITCP->connected = 0; // Notifies the thread to try to reconnect the server
		}
		LeaveCriticalSection(&pCLITCP->CSconnected);
		return EXIT_FAILURE;
	}

	EnterCriticalSection(&pCLITCP->CSconnected);
	if(pCLITCP->connected != 1)	{
		fprintf(stdout, "Connection successful\n");
		pCLITCP->connected = 1; // Notifies the thread not to try to reconnect the server
	}
	LeaveCriticalSection(&pCLITCP->CSconnected);

	return EXIT_SUCCESS;
}
Exemple #2
0
static void CPROC SendStuff( uintptr_t psv )
{
	INDEX idx;
	PCLIENT pc;
	LIST_FORALL( clients, idx, PCLIENT, pc )
	{
      SendTCP( pc, "TEST", 4 );
	}
Exemple #3
0
void CPROC RequestWinnersButton(uintptr_t psv, PCONTROL pcButton)
{
    PCLIENT pc;
    ConnectToRelay( &pc );
    if( pc )
    {
        SendTCP( pc, "WINNERS?", 8 );
        while( pc )
            Idle();
    }
}
Exemple #4
0
void CPROC UpdateAllButton(uintptr_t psv, PCONTROL pcButton)
{
    PCLIENT pc;
    ConnectToRelay( &pc );
    if( pc )
    {
        SendTCP( pc, "UPDATE  ", 8 );
        while( pc )
            Idle();
    }
}
Exemple #5
0
void CPROC GetMasterStatusButton(uintptr_t psv, PCONTROL pcButton)
{
    PCLIENT pc;
    ConnectToRelay( &pc );
    if( pc )
    {
        SendTCP( pc, "MASTER??", 8 );
        while( pc )
            Idle();
    }
}
Exemple #6
0
void CPROC GetUserButton(uintptr_t psv, PCONTROL pcButton)
{
    PCLIENT pc;
    ConnectToRelay( &pc );
    if( pc )
    {
        SendTCP( pc, "LISTUSER", 8 );
        while( pc )
            Idle();
    }
}
Exemple #7
0
void CPROC ServerReadComplete( PCLIENT pc, POINTER buffer, int len )
{
	if( !buffer )
	{
      buffer = Allocate( 4096 );
	}
	else
	{
      SendTCP( pc, buffer, len );
	}
   ReadTCP( pc, buffer, 4096 );
}
Exemple #8
0
void CPROC KillRelayButton(uintptr_t psv, PCONTROL pcButton)
{
    PCLIENT pc;
    int i = 0;
    ConnectToRelay( &pc );
    if( pc )
    {
        SendTCP( pc, "DIE NOW!", 8 );
        while( pc )
        {
            Sleep(1000);
            SendTCP( pc, "DIE NOW!", 8 );
            i++;
            if( i == 5 )
            {
                BasicMessageBox( WIDE("Kill failed"), WIDE("Relay has not responded by ending connection") );
                RemoveClient( pc );
            }
        }
    }
}
Exemple #9
0
void CPROC ScanAllButton(uintptr_t psv, PCONTROL pcButton)
{
    PCLIENT pc;
    ConnectToRelay( &pc );
    if( pc )
    {
        SendTCP( pc, "DO SCAN!", 8 );
        while( pc )
        {
            ProcessControlMessages();
            Sleep(0);
        }
    }
}
Exemple #10
0
void TCPTest1000Byte( void )
{
	int n;
   uint32_t sec = time(NULL);
	while( (sec+5) > time(NULL) )
	//for( n = 0; n < 10000; n++ )
	{
		l.waiting = 1;

		SetTick( l.start );
		SendTCP( l.pClient, l.data, 1000 );
	// have to wait here until the responce is received.
		while( l.waiting )
         Relinquish();
	}
   DumpStats();
}
Exemple #11
0
void CPROC UpdateUserButton(uintptr_t psv, PCONTROL pcButton)
{
    PCLIENT pc;
    ConnectToRelay( &pc );
    if( pc )
    {
        char msg[256];
        int len;
        len = sprintf( msg, "UPDATE  %s",
                       GetItemData(
                           GetSelectedItem(
                               GetControl( frame, LST_USERS ) ) ) );
        SendTCP( pc, msg, len );
        while( pc )
            Idle();
    }
}
Exemple #12
0
int main( int argc, char** argv )
{
	SOCKADDR *sa;
   int n;
	if( argc < 2 )
	{
		printf( "usage: %s <Telnet IP[:port]>\n", argv[0] );
		return 0;
	}
	SystemLog( "Starting the network" );
	NetworkStart();
	SystemLog( "Started the network" );
   sa = CreateSockAddress( argv[1], 23 );
	//if( argc >= 3 ) port = atoi( argv[2] ); else port = 23;
	pc_user = OpenTCPClientAddrExx( sa, ReadComplete, Closed, NULL, Connected );

	if( !pc_user )
	{
		SystemLog( "Failed to open some port as telnet" );
		printf( "failed to open %s%s\n", argv[1], strchr(argv[1],':')?"":":telnet[23]" );
		return 0;
	}
	for( n = 0; n < 1000; n++ )
	{
		if( (rand() & 3) != 0 )
		{
         WakeableSleep( rand() %1000 );
		}
      lprintf( "..." );
		RemoveClient( pc_user );
		pc_user = OpenTCPClientAddrExx( sa, ReadComplete, Closed, NULL, Connected );
		lprintf( "..." );
		if( (rand() &3) < 3 )
		{
         fflush( stdout );
			while( !connected )
				Relinquish();
		}
		if( connected )
		{
         lprintf( "send." );
			SendTCP( pc_user, "test", 4 );
		}
	}
	return -1;
}
/*
Sends data to a client connected to the server. Retries automatically if all the bytes were not sent.
Fails when a timeout occurs.

SRVTCP* pSRVTCP : (IN,OUT) pointer to the data structure representing the server
char *sendbuf : (IN) data to send
int sendbuflen : (IN) number of bytes to send

Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error
*/
int SendSRVTCP(SRVTCP* pSRVTCP, SOCKET ClientSocket, char* sendbuf, int sendbuflen)	{

	int NbConnections = 0;


	if ((pSRVTCP == NULL) || (pSRVTCP->initialized != 1))	{
		return EXIT_FAILURE;
	}

	EnterCriticalSection(&pSRVTCP->CSNbConnections);
	NbConnections = pSRVTCP->NbConnections;
	LeaveCriticalSection(&pSRVTCP->CSNbConnections);

	if (NbConnections <= 0)	{
		return EXIT_FAILURE;
	}

	return SendTCP(ClientSocket, sendbuf, sendbuflen);
}
/*
Sends data to the server connected to the client. Retries automatically if all the bytes were not sent.
Fails when a timeout occurs.

CLITCP* pCLITCP : (IN,OUT) pointer to the data structure representing the client
char *sendbuf : (IN) data to send
int sendbuflen : (IN) number of bytes to send

Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error
*/
int SendCLITCP(CLITCP* pCLITCP, char* sendbuf, int sendbuflen)	{

	int connected = 0;


	if ((pCLITCP == NULL) || (pCLITCP->initialized != 1))	{
		return EXIT_FAILURE;
	}

	EnterCriticalSection(&pCLITCP->CSconnected);
	connected = pCLITCP->connected;
	LeaveCriticalSection(&pCLITCP->CSconnected);

	if (connected != 1)	{
		return EXIT_FAILURE;
	}

	return SendTCP(pCLITCP->ConnectSocket, sendbuf, sendbuflen);
}
Exemple #15
0
static int handshake( PCLIENT pc ) {
   struct ssl_session *ses = pc->ssl_session;
	if (!SSL_is_init_finished(ses->ssl)) {
		int r;
		lprintf( "doing handshake...." );
		/* NOT INITIALISED */

		r = SSL_do_handshake(ses->ssl);
		lprintf( "handle data posted to SSL? %d", r );
		if( r == 0 ) {
			ERR_print_errors_cb( logerr, (void*)__LINE__ );
			r = SSL_get_error( ses->ssl, r );
			ERR_print_errors_cb( logerr, (void*)__LINE__ );
			lprintf( "SSL_Read failed... %d", r );
			return -1;
		}
		if (r < 0) {

			r = SSL_get_error(ses->ssl, r);
			if( SSL_ERROR_SSL == r ) {
				lprintf( "SSL_Read failed... %d", r );
				ERR_print_errors_cb( logerr, (void*)__LINE__ );
				return -1;
			}
			if (SSL_ERROR_WANT_READ == r) 
			{
				int pending = BIO_ctrl_pending( ses->wbio);
				if (pending > 0) {
					int read;
					if( pending > ses->obuflen ) {
						if( ses->obuffer )
							Deallocate( uint8_t *, ses->obuffer );
						ses->obuffer = NewArray( uint8_t, ses->obuflen = pending*2 );
					}
					read = BIO_read(ses->wbio, ses->obuffer, pending);
					lprintf( "send %d for handshake", read );
					if (read > 0)
						SendTCP( pc, ses->obuffer, read );
				}
Exemple #16
0
long SDMmessage::SendTo(const SDMComponent_ID& destination)
{
	int sock;
	long i;
	long result;
	char buf[LARGE_MSG_BUFSIZE];
	char ack[16];
	long address = destination.getAddress();
	int timeout = 2000;   // 2 sec
	int count = 0;
	short error = 0;

	//fill buffer
	i = Marshal(buf);
	if(i < 0)
		return SDM_MESSAGE_SEND_ERROR;
#ifdef TCP_TRANSMIT_OF_LARGE_XTEDS
	if(i>BUFSIZE)
		return SendTCP(destination.getAddress(),destination.getPort());
#endif
	//send message
	sock = UDPconnect(address,destination.getPort());
  if (sock != IP_SOCK_INVALID) // cleanup issue 26
  {
	  result = UDPsend(sock,buf,i);
	  //Look for SDM_ACK from DM for certain messages
	  if(buf[0] == SDM_xTEDS || buf[0] == SDM_CancelxTEDS)
	  {
		  if(buf[0] == SDM_xTEDS)
		  {
			  timeout = 5000;   // 5 sec
		  }
		  UDPset_recv_timeout(sock, timeout);

		  UDPrecv(sock,&ack,13);
		  while(ack[0]!=SDM_ACK && count < NUMRETRIES)
		  {
			  count++;

			  result = UDPsend(sock,buf,i);
			  UDPrecv(sock,&ack,13);
		  }
		  //remove timeout on sdm_dm_sock
		  UDPset_recv_timeout(sock, 0);

      if(count == NUMRETRIES && ack[0]!=SDM_ACK)
      {
		    result = SDM_UNABLE_TO_REGISTER;
      }
	    else
      {
		    error = GET_SHORT(&ack[11]); 
        if(error < 0)
        {
			    result = error;
        }
      }
    }
	  UDPclose (sock);
#ifdef BUILD_WITH_MESSAGE_LOGGING
	  Logger.MessageSent(*this);
#endif
  }
	return result;
}