예제 #1
0
void NetworkSocket::reapplyRemoteAddress() {
	if(m_type == NST_UDP || m_type == NST_UDPBROADCAST)
		// TODO: comment this, why we need that in some cases
		setRemoteAddress(remoteAddress());
	else
		errors << "NetworkSocket::reapplyRemoteAddress cannot be done as " << TypeStr(m_type) << endl;
}
예제 #2
0
std::string NetworkSocket::debugString() const {
	if(!isOpen()) return "Closed";
	std::string ret = TypeStr(m_type) + "/" + StateStr(m_state);
	{
		std::string localStr = "INVALIDLOCAL";
		NetworkAddr addr;
		if(nlGetLocalAddr(m_socket->sock, getNLaddr(addr)) != NL_FALSE)
			NetAddrToString(addr, localStr);
		else {
			localStr = "ERRORLOCALADDR(" + GetLastErrorStr() + ")";
			ResetSocketError();
		}
		ret += " " + localStr;
	}
	if(m_state == NSS_CONNECTED) {
		ret += " connected to ";
		std::string remoteStr = "INVALIDREMOTE";
		NetworkAddr addr;
		if(nlGetRemoteAddr(m_socket->sock, getNLaddr(addr)) != NL_FALSE)
			NetAddrToString(remoteAddress(), remoteStr);
		else {
			remoteStr = "ERRORREMOTEADDR(" + GetLastErrorStr() + ")";
			ResetSocketError();
		}
		ret += remoteStr;
	}
	return ret;
}
예제 #3
0
//////////////////////////////////////////////////////////////////////////////
/// @brief Parse the current CRadioMessage and creates a string 
/// @param [in] If true and message encrypted  will display it decrypted
/// @retval The string describing object structure information - detalied
//////////////////////////////////////////////////////////////////////////////
string CRadioMessage::ToString(bool p_bDecrypt /*= false*/)
{
	ostringstream oBuf;
	uint8_t *pu8DecryptedPayload = NULL;
	uint32_t u32DecryptedLength = 0;
	bool bEncryptedWithDefaultKey = false;
	
	oBuf << TypeStr()    << " CRadioMessage [ ID: " << m_u32ID << " ] ";
	oBuf << " Time: " << m_u32ReceivedTime << " ";
	
	if(m_u1HMAC)       oBuf << "<HMAC> ";
	if(m_u1Signed)     oBuf << "<Signed> ";
	if(m_u1Compressed) oBuf << "<Compressed> ";
	if(m_u1Encrypted)  oBuf << "<Encrypted> ";
	
	// All useful information
	
	oBuf << "\nRemote: " << CAdapterUtil::IntToHexStr(m_u32RemoteLANAddress) << " [" << CAdapterUtil::Int64ToHexStr(m_u64RemoteWANAddress) << "]" << ", ";
	oBuf << "Local:  "   << CAdapterUtil::IntToHexStr(m_u32LocalLANAddress)  << " [" << CAdapterUtil::Int64ToHexStr(m_u64LocalWANAddress)  << "]" << "\n";
	
	oBuf << "Priority: " << (int)m_u8Priority  << ", TTL: " << m_u16TimeToLive << ", Hops: " << (int)m_u8MaxHops;
	oBuf << ", Mood: "   << (int)m_u8Mood << ", Receipt: " << (int)m_u8ReturnReceipt;
	
	// Check if it is a small/big resident DCW
	
	if(m_u32RemoteLANAddress == 0xFEFEFE7D)      oBuf << " <Small DCW>";
	else if(m_u32RemoteLANAddress == 0xFEFEFE7E) oBuf << " <Big DCW>";
	
	// Display hex formatted payload
	
	oBuf << ", Payload " << ((p_bDecrypt && m_u1Encrypted) ? "<Decrypted>" : "" ) << ": \n" << "------------------------------------------------------------------------\n";
	
	if(p_bDecrypt && m_u1Encrypted)
	{	
		// Decrypt packet
					
		u32DecryptedLength = m_u32PayloadLength - ENC_HMAC_SIZE_USED + AES_BLOCK_SIZE;
		pu8DecryptedPayload = new uint8_t[u32DecryptedLength];
			
		if(!CEncryption::LGDecrypt(m_pu8Payload, m_u32PayloadLength, m_u32RemoteLANAddress, m_u32ID, m_u8ReturnReceipt, 
				m_u4MessageType, m_u1Encrypted, pu8DecryptedPayload, u32DecryptedLength, bEncryptedWithDefaultKey))
		{
			NLOG_ERR("Cannot decrypt packet to display data!");
			
			oBuf << CAdapterUtil::GetFormattedHex(m_pu8Payload, m_u32PayloadLength);
		}
		else 
		{
			NLOG_INFO("Packet was succesfully decrypted to be displayed.");
			oBuf << CAdapterUtil::GetFormattedHex(pu8DecryptedPayload, u32DecryptedLength);
		}
		
		delete[] pu8DecryptedPayload;
	}
	else oBuf << CAdapterUtil::GetFormattedHex(m_pu8Payload, m_u32PayloadLength);
	
	return oBuf.str();
}
예제 #4
0
//////////////////////////////////////////////////////////////////////////////
/// @brief Parse the current CRadioMessage and creates a string representation
/// @retval The string shortly describing object structure information
//////////////////////////////////////////////////////////////////////////////
string CRadioMessage::ToShortString()
{
	ostringstream oBuf;
	string sPDU = "";
	
	unsigned char ucPDUMessage1 = 0;
	unsigned char ucPDUMessage2 = 0;
	
	// Get info about packet from payload (if if is a PDU message)
	
	if(m_u32PayloadLength >= 7 && (memcmp(m_pu8Payload, s_pcPDUIdentifier, 4) == 0))
	{
		ucPDUMessage1 = *(&m_pu8Payload[4]);
		ucPDUMessage2 = *(&m_pu8Payload[5]);
		
		// Metering message details
		
		if(ucPDUMessage1 == 0x00)
		{
			switch(ucPDUMessage2) 
			{
				case 0x00: sPDU = "Init Poll"; break;
				case 0x11: sPDU = "Snap Read"; break;
				case 0x12: sPDU = "Battery Level Report"; break;
				case 0x22: sPDU = "LP Data"; break;
				case 0x26: sPDU = "Optimized Load Profile Data"; break;
				case 0x27: sPDU = "Init Push"; break;
				case 0x28: sPDU = "Meter Configuration Data"; break;
				case 0x29: sPDU = "Unhandled Exception Error"; break;
				case 0x40: sPDU = "Read Scheduled Commands"; break;
				case 0x41: sPDU = "Write Scheduled Commands"; break;
				case 0x42: sPDU = "Get Command Execution Time"; break;
				case 0x59: sPDU = "Ping"; break;
				case 0x75: sPDU = "ANSI Table Read"; break;
				case 0x79: sPDU = "Correlated Generic ANSI Table Read"; break;
				case 0x91: sPDU = "Send Load Profile History"; break;
				case 0x92: sPDU = "LSU Updates of Concentrators"; break;
				default: sPDU = "Unknown"; break;
			}
		}
		
		// Network management
		
		else if(ucPDUMessage1 == 0x01)
		{
			switch(ucPDUMessage2) 
			{
				case 0x01: sPDU = "Chirp Data"; break;
				case 0x02: sPDU = "Communication with End Device Failure"; break;
				case 0x03: sPDU = "Message Sequecing"; break;
				case 0x04: sPDU = "Get Endpoint Info Response"; break;
				case 0x05: sPDU = "Concentrator Configuration"; break;
				case 0x06: sPDU = "Get WAN Nodes Info"; break;
				case 0x07: sPDU = "Network Statistics"; break;
				case 0x0C: sPDU = "Get Endpoint Info"; break;
				case 0x10: sPDU = "Install Encryption Key"; break;
				case 0x11: sPDU = "Remove Encryption Key"; break;
				case 0x12: sPDU = "Enable Encryption"; break;
				case 0x13: sPDU = "Disable Encryption"; break;
				case 0x14: sPDU = "Go to Wide Channel"; break;
				case 0x15: sPDU = "Go to Narrow Channel"; break;
				case 0x16: sPDU = "Get Security Configuration"; break;
				case 0x17: sPDU = "Get WAN Nodes List"; break;
				case 0x18: sPDU = "Radio Stats"; break;
				default: sPDU = "Unknown"; break;
			}
		}
		
		// Meter programming
		
		else if(ucPDUMessage1 == 0x03 && ucPDUMessage2 == 0x01) sPDU = "Meter Time Syncronization Status";
		else if(ucPDUMessage1 == 0x03 && ucPDUMessage2 == 0x02) sPDU = "Meter Time syncronization Configuration";
		
		// Module programming
		
		else if(ucPDUMessage1 == 0x05)
		{
			switch(ucPDUMessage2)
			{
				case 0x00: sPDU = "Configure Endpoint Module"; break;
				case 0x01: sPDU = "Get Module Configuration"; break;
				case 0x0A: sPDU = "Set WAN Address"; break;
				case 0x0B: sPDU = "Module Timezone"; break;
				case 0x0C: sPDU = "Confirm Registration"; break;
				case 0x0D: sPDU = "Read User Groups"; break;
				case 0x0E: sPDU = "Write User Groups"; break;
				case 0x0F: sPDU = "Module Reboot"; break;
				case 0x10: sPDU = "Installation Mode"; break;
				case 0x12: sPDU = "Reset Nonvolatile Memory"; break;
				default: sPDU = "Unknown"; break;
			}
		}
		
		// Default
		
		else sPDU = "Unknown";
		
		sPDU = ", PDU: " + sPDU;
	}
	
	oBuf << "Packet [" << m_u32ID << "] to " << CAdapterUtil::IntToHexStr(m_u32RemoteLANAddress) << " [" << TypeStr() << "]" << sPDU;
	
	// Message bits
	
	if(m_u1HMAC)       oBuf << " <HMAC>";
	if(m_u1Signed)     oBuf << " <Signed>";
	if(m_u1Compressed) oBuf << " <Compressed>";
	if(m_u1Encrypted)  oBuf << " <Encrypted>";
	
	// DCW flags
	
	if(m_u32LocalLANAddress == 0xFEFEFE7D)      oBuf << " <Small DCW>";
	else if(m_u32LocalLANAddress == 0xFEFEFE7E) oBuf << " <Big DCW>";
	
	return oBuf.str();
}
예제 #5
0
/*---------------------------------------------------------------------------------*
   FUNCTION  : main
   Purpose   : program entry point
   Arguments : argc - argument count
   Globals   : none
   Return    : 0 for successful execution
               none-zero for any error
*---------------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
    CONVERT *cp=NULL;
    WAVEHEADER *whp1=NULL, *whp2=NULL;
    int fd1=0, fd2=0, i;
    long total_bytes_written=0;
    
    
    cp = ConvertCreate(inputBuffer, outputBuffer);  /* intialize the CONVERT structure */

    ParseCommandLine(argc,argv, cp);        /* parse the command line options */
    CheckFiles(cp);                         /* validate the input/output file */

    fd1=_open(cp->inputFilename,_O_RDONLY | _O_BINARY );
    if(fd1==-1)     Error("Opening INPUT FILE \"%s\" \n",cp->inputFilename);
    
    fd2=_open(cp->outputFilename,_O_WRONLY | _O_CREAT | _O_EXCL | _O_BINARY, _S_IREAD | _S_IWRITE);
    
    if( errno == EEXIST )       /* if OVERWRITE option not specified, give prompt */
    {
        char ch;
        if      (cp->overWrite == FALSE) 
        {
            fprintf(stderr, "Target %s exists. Overwrite? ", cp->outputFilename );
            ch = getch();
            if( (ch == 'y') || (ch == 'Y') )
                fd2 = _open( cp->outputFilename, _O_BINARY | _O_WRONLY |
                                _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE );
            else return 0;
            fprintf(stderr, "\n" );
        }
        else if (cp->overWrite == TRUE)
            fd2 = _open( cp->outputFilename, _O_BINARY | _O_WRONLY |
                            _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE );
    }
    else if(fd2==-1)     Error("Opening OUTPUT FILE \"%s\" \n",cp->outputFilename);
    

    /* if output type is specified as WAV 8/16 bit, set the WAV format header */
    if( (cp->outputType == WAV8) || (cp->outputType == WAV16) )
    {
        long nSamplesPerSec=11025;
        int  wBitsPerSample= (cp->outputType==WAV8)? 8: 16;
        
        switch(cp->outputSampleRate) {
            case 11: nSamplesPerSec = 11025; break;
            case 22: nSamplesPerSec = 22050; break;
            case 44: nSamplesPerSec = 44100; break;
        }
        whp2  = CreateWaveHeader( nSamplesPerSec, wBitsPerSample );
        if( _write(fd2, whp2, sizeof(WAVEHEADER)) == -1 ) Error("Writing OUTPUT FILE %s",cp->outputFilename);
    }

    /* this is done again for input type WAVxx, since we'll be checking the input file */
    /* if input type is specified as WAV 8/16 bit, set the WAV format header, check it against actual input file */    
    if( (cp->inputType == WAV8) || (cp->inputType == WAV16) )
    {
        whp1  = CreateWaveHeader( 0L, 0 );
        if( _read(fd1, whp1, sizeof(WAVEHEADER)) == -1 ) Error("Reading INPUT FILE %s",cp->inputFilename);
        CheckWaveHeader(whp1);
        
        cp->inputType = (whp1->wBitsPerSample==8)? WAV8: WAV16;
        cp->inputSampleRate = (int)(whp1->nSamplesPerSec/1000L);
        
        SetRateType(cp);
//        if (cp->convertRateCb) CVC_ConvertDestroy(cp->convertRateCb);
        
        if(cp->rateType != CVT_CONV01TO01)        
            cp->convertRateCb = CVC_ConvertCreate(cp->rateType);        
        
//        if (cp->inputTypeCb)  CVC_WaveDestroy(cp->inputTypeCb);
        if (cp->inputType == WAV8)
            { cp->inputTypeCb = CVC_WaveCreate(CVT_MONO08); cp->linInpRatio = 2.0; }   
        else if (cp->inputType == WAV16)                  
            { cp->inputTypeCb = CVC_WaveCreate(CVT_MONO16); cp->linInpRatio = 1.0; }  

    }          
        /* calculate the maximum size allow per conversion in the loop */
        cp->maxInpSize = min( (unsigned int)floor(INPBUFLEN/cp->linInpRatio),
                            (unsigned int)floor((double)INPBUFLEN/cp->outputSampleRate*cp->inputSampleRate));

    
    for (i=0; i < 24; i++) putchar('\n');
    printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
    printf("º RHETOREX           º          CONVERT UTILITY          º  Version: 1.00     º\n");
    printf("ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n");
    printf(" input  filename     :   %s\n",          cp->inputFilename       );
    printf(" input  type         :   %s\n",          TypeStr(cp->inputType)  );
    printf(" input  sample rate  :   %2d Khz\n",    cp->inputSampleRate     );
    printf("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
    printf(" output filename     :   %s\n",          cp->outputFilename      );
    printf(" output type         :   %s\n",          TypeStr(cp->outputType) );  
    printf(" output sample rate  :   %2d Khz\n",    cp->outputSampleRate    );  
    printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n");
    printf("\n                          CONVERTING PLEASE WAIT \n");  
                    
    /* read each block of data upto maxInpSize, do the conversion & write result to output file */
    while((cp->inpBufSize=_read(fd1, inputBuffer,cp->maxInpSize<<1)) > 0)
    {
        cp->inpBufSize >>=1;
        cp->inpBufPtr=inputBuffer;
        cp->outBufPtr=outputBuffer;
        ConvertToLin(cp);               /* 1st, convert it to LIN */

        cp->inpBufPtr=outputBuffer;     /* switch the input <-> output */
        cp->outBufPtr=inputBuffer;
        cp->inpBufSize = *cp->outBufLen;
        ConvertRate(cp);                /* 2nd, convert the LIN to appropriate rate */

        cp->inpBufPtr=inputBuffer;      /* switch the output <-> input */
        cp->outBufPtr=outputBuffer;
        cp->inpBufSize = *cp->outBufLen;
        ConvertFromLin(cp);             /* finally, convert LIN to destination format */
        
        /* write output buffer to file */
        total_bytes_written += _write(fd2, outputBuffer,sizeof(outputBuffer[0])*(*cp->outBufLen));
        putchar('.');
    }
    
    /* if ouput type is specified as WAV 8/16 bit, must update the  header file size */
    if( (cp->outputType == WAV8) || (cp->outputType == WAV16) )
        UpdateWaveHeaderSize(cp->outputFilename, total_bytes_written);

    printf("\n\n");
    printf("------------------------------------------------------------------------------\n");
    printf(" input  file length  :   %5ld bytes\n", _filelength(fd1) );  
    printf(" output file length  :   %5ld bytes\n", _filelength(fd2) );  
    printf("------------------------------------------------------------------------------\n");

    
    /* close file descriptors used and free all structures */
    _close(fd1); _close(fd2);    
 
    ConvertDestroy(cp);
    if (whp1) DestroyWaveHeader(whp1);
    if (whp2) DestroyWaveHeader(whp2);
    return 0; 
}