示例#1
0
        void CTcpHandler::ProcessCmd(CCommand *pCmd)
        {
		    NsqLogPrintf(LOG_DEBUG, "CTcpHandler::ProcessCmd ProcessCmd Type = %d", pCmd->GetCmdType());

            switch(pCmd->GetCmdType())
            {
                case TCP_CONNECT_TYPE:
                {
                    TcpConnect(pCmd);
                    break;
                }
                case TCP_ADD_TYPE:
                {
                    TcpAdd(pCmd);
                    break;
                }
                case TCP_SEND_TYPE:
                {
                    TcpSend(pCmd);
                    break;
                }
            }

            delete pCmd;
        }
示例#2
0
static int TcpClientCommServer(char *pStrIp, unsigned short u16Port,
                               char *pSendData, unsigned int u32SendDataSize,
                               unsigned int u32Timeout, bool IsRespond,
                               char *pRecvData, unsigned int u32RecvDataSize)
{
    int ret, sock;

    sock = TcpConnect(pStrIp, u16Port, u32Timeout);
    if (sock < 0)
    {
        LOGERR("TcpConnect error\n");
        return -1;
    }

    ret = TcpSend(sock, pSendData, u32SendDataSize);
    if (ret != (int)u32SendDataSize)
    {
        LOGERR("TcpSend error, ret=%d, u32SendDataSize=%d\n", ret, u32SendDataSize);
        close(sock);
        return -1;
    }

    if (IsRespond == TRUE)
    {
        ret = TcpReceive(sock, pRecvData, u32RecvDataSize);
        if (ret <= 0)
        {
            LOGERR("TcpReceive error\n");
            close(sock);
            return -1;
        }
    }

    return sock;
}
/**
 * @ingroup SnmpStack
 * @brief SNMP trap 과 같은 단방향 SNMP 메시지를 전송한다.
 * @param pclsRequest SNMP 메시지
 * @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
 */
bool CSnmpSession::SendRequest( CSnmpMessage * pclsRequest )
{
	char szSend[SNMP_MAX_PACKET_SIZE];
	int iSendLen;

	if( m_hSocket == INVALID_SOCKET )
	{
		if( Open() == false ) return false;
	}

	if( m_strUserName.empty() == false )
	{
		static uint8_t szEngineId[] = { 0x80, 0x00, 0x00, 0x04, 0x80, 0x00 };

		pclsRequest->m_cVersion = SNMP_VERSION_3;
		pclsRequest->m_iMsgId = ++m_iRequestId;
		pclsRequest->m_iRequestId = pclsRequest->m_iMsgId;
		pclsRequest->m_strMsgAuthEngineId = m_strAuthEngineId;
		pclsRequest->m_strMsgUserName = m_strUserName;
		pclsRequest->m_strUserId = m_strUserName;
		pclsRequest->m_strAuthPassWord = m_strAuthPassWord;
		pclsRequest->m_strPrivPassWord = m_strPrivPassWord;

		pclsRequest->m_strContextEngineId.append( (char *)szEngineId, sizeof(szEngineId) );

		pclsRequest->SetPrivParams( );
		pclsRequest->SetAuthParams( );
	}
	else
	{
		pclsRequest->m_strCommunity = m_strCommunity;
		pclsRequest->m_iRequestId = ++m_iRequestId;
	}

	iSendLen = pclsRequest->MakePacket( szSend, sizeof(szSend) );
	if( iSendLen == -1 )
	{
		CLog::Print( LOG_ERROR, "%s MakePacket error", __FUNCTION__ );
		return false;
	}

	if( m_bTcp )
	{
		if( TcpSend( m_hSocket, szSend, iSendLen ) == false )
		{
			CLog::Print( LOG_ERROR, "%s TcpSend error", __FUNCTION__ );
			return false;
		}
	}
	else
	{
		if( UdpSend( m_hSocket, szSend, iSendLen, m_iIp, m_sPort ) == false )
		{
			CLog::Print( LOG_ERROR, "%s UdpSend error", __FUNCTION__ );
			return false;
		}
	}

	return true;
}
示例#4
0
int SendMsg (SOCKET s, int type, const void *data, int size)
{
int Rc=1;
unsigned short full_size = htons (size + sizeof type);
    if (data==NULL)  size=0;
    // send length of message
    Rc = send (s, (char *) & full_size, sizeof full_size, 0);
    Rc = send (s, (char *) & type, sizeof type, 0);
    if (size>0) Rc = TcpSend (s, data, size, 0);
return Rc;
} // SendMsg
示例#5
0
        void HttpServer::HttpSend403Header()
        {
            LOG(__EVENT, "httpserver", __FUNCTION__);

            stringstream response_stream;
            response_stream << "HTTP 403 Forbidden\r\n";
            response_stream << "Connection: Close\r\n";
            response_stream << "\r\n";
            string response_string = response_stream.str();

            LOG(__EVENT, "httpserver", "HttpServer::HttpServerSendHeader SimpleHeader: \n" << response_string);

            Buffer buffer(response_string.c_str(), response_string.length());
            TcpSend(buffer);
        }
示例#6
0
        void HttpServer::HttpSendKeepAliveHeader(size_t content_length, string content_type)
        {
            LOG(__EVENT, "httpserver", "HttpServer::HttpSendKeepAliveHeader content_length: " << content_length << ", type " << content_type);

            stringstream response_stream;
            response_stream << "HTTP/1.0 200 OK\r\n";
            response_stream << "Content-Type: " << content_type << "\r\n";
            response_stream << "Content-Length: " << content_length << "\r\n";
            response_stream << "Connection: Keep-Alive\r\n";
            response_stream << "\r\n";
            string response_string = response_stream.str();

            LOG(__EVENT, "httpserver", "HttpServer::HttpServerSendHeader SimpleHeader: \n" << response_string);

            Buffer buffer(response_string.c_str(), response_string.length());
            TcpSend(buffer);
        }
static Err SendToServer(ClientManager* _clientManager, char* _dataSend, e_tag _tag)
{
	char msg[MAX_SIZE_OF_MESSAGE] = {0};
	if (! _clientManager || ! _dataSend)
	{
		return ERROR_NOT_INITIALIZED;
	}
	if (ERROR_OK != ConvertToProtocol(_dataSend, _tag, 100,msg ))
	{
		return ERROR_CONVERT_TO_PROTOCOL_FAILED;
	}
	if (ERR_OK != TcpSend(_clientManager->m_socket, msg, 100))
	{
		return ERR_SEND_FAILED;
	}
	return ERROR_OK;
}
示例#8
0
static int SendVmProcStateRsp(int alist_idx, unsigned int systemid)
{
    int             i, TcpMsgLength;
    unsigned int    system_addr;
    common_tcpmsg_t sndmsg;

    system_addr = alist[alist_idx].sysinfo.systemaddr;
    for (i = 0; i < MAX_SYSTEM_NUM; i++) {
        if (hostConf.vmc_system[i].systemaddr == system_addr) {
            hostConf.vmc_system[i].is_check = IS_CHECK_ON;
            hostConf.vmc_system[i].link_status = 0;
            break;
        }
    }
    if (i == MAX_SYSTEM_NUM) {
        xprint(FMS_HST | FMS_ERROR, "%s(%d) : System is Unknown.(= %s)\n", __func__, __LINE__, addr2ntoa(system_addr));
        return -1;
    }

    sndmsg.TpktHdr  = 0x00;
    sndmsg.SystemID = systemid;
    sndmsg.MsgType  = VM_PROC_STATE_RSP;
    sndmsg.MsgSize  = 0;

    TcpMsgLength = DEFAULT_TCP_COMHDR_LENGTH + sndmsg.MsgSize;
    TCP_MSG_HTON((common_tcpmsg_t *)&sndmsg);

    i = alist_idx;
    pthread_mutex_lock(&TcpMutex);
    if (alist[i].AcceptFD < 0) {
        xprint(FMS_HST | FMS_ERROR, "%s(%d) : recv fd lost.\n", __func__, __LINE__);
    } else {
        if (TcpSend(i, alist[i].AcceptFD, (unsigned char *)&sndmsg, TcpMsgLength) == TcpMsgLength) {
            xprint(FMS_HST | FMS_INFO3,
                    "|-------->| VM_PROC_STATE_RSP msg send(len:%d) OK. systemid(%d : %s)\n",
                    TcpMsgLength, systemid, alist[i].sysinfo.systemname);
        } else {
            xprint(FMS_HST | FMS_ERROR,
                    "|---XXX-->| VM_PROC_STATE_RSP msg send Fail. systemid(%d : %s)\n", systemid, alist[i].sysinfo.systemname);
        }
    }
    pthread_mutex_unlock(&TcpMutex);

    return i;
}
void NetworkClient::SetClientType ( int newtype )
{

    // Request this mode from the server

    char message [32];
    UplinkSnprintf ( message, sizeof ( message ), "SETCLIENTTYPE %d#", newtype );

    int result = TcpSend ( socket, message, sizeof(message), false, HFILE_ERROR );

    if ( result == TCP4U_SUCCESS ) {

	clienttype = newtype;
	RunScreen ( clienttype );

    }
    else
	printf ( "NetworkClient::SetClientType, failed to convince server to co-operate\n" );

}
/**
 * @ingroup SnmpStack
 * @brief TCP 기반 SNMP 메시지 수신 쓰레드
 * @param lpParameter CSnmpTcpAgentClientArg 객체의 포인터
 * @returns 0 을 리턴한다.
 */
THREAD_API SnmpTcpAgentClientThread( LPVOID lpParameter )
{
	CSnmpTcpAgentClientArg * pclsArg = (CSnmpTcpAgentClientArg *)lpParameter;
	char szPacket[SNMP_MAX_PACKET_SIZE], szSendPacket[SNMP_MAX_PACKET_SIZE];
	bool bCallCallBack;
	int n, iPacketLen = 0, iWantRecvLen = 0, iSendLen;
	pollfd sttPoll[1];
	CSnmpMessage clsRequest, clsResponse;

	CLog::Print( LOG_INFO, "SnmpTcpAgentClientThread is started - client(%s:%d)", pclsArg->m_strIp.c_str(), pclsArg->m_iPort );

	TcpSetPollIn( sttPoll[0], pclsArg->m_hSocket );

	while( pclsArg->m_pclsAgent->m_bStop == false )
	{
		n = poll( sttPoll, 1, 1000 );
		if( n <= 0 ) continue;

		n = recv( pclsArg->m_hSocket, szPacket + iPacketLen, sizeof(szPacket) - iPacketLen, 0 );
		if( n <= 0 ) break;

		iPacketLen += n;

		if( iWantRecvLen == 0 )
		{
			iWantRecvLen = clsRequest.GetPacketLen( szPacket, iPacketLen );
		}

		if( iWantRecvLen > 0 )
		{
			if( iPacketLen >= iWantRecvLen )
			{
				if( clsRequest.ParsePacket( szPacket, iPacketLen ) == -1 )
				{
					CLog::Print( LOG_ERROR, "%s ParsePacket error", __FUNCTION__ );
					break;
				}
				else
				{
					bCallCallBack = false;

					if( clsRequest.m_cVersion == SNMP_VERSION_3 )
					{
						if( pclsArg->m_pclsAgent->m_strUserName.empty() )
						{
							CLog::Print( LOG_ERROR, "%s SNMPv3 request - no user name", __FUNCTION__ );
						}
						else if( ( clsRequest.m_cMsgFlags & SNMP_MSG_FLAG_ENCRYPT ) && pclsArg->m_pclsAgent->m_strPrivPassWord.empty() )
						{
							CLog::Print( LOG_ERROR, "%s SNMPv3 encrypt request - no priv password", __FUNCTION__ );
						}
						else if( ( clsRequest.m_cMsgFlags & SNMP_MSG_FLAG_AUTH ) && pclsArg->m_pclsAgent->m_strAuthPassWord.empty() )
						{
							CLog::Print( LOG_ERROR, "%s SNMPv3 auth request - no auth password", __FUNCTION__ );
						}
						else if( strcmp( clsRequest.m_strMsgUserName.c_str(), pclsArg->m_pclsAgent->m_strUserName.c_str() ) )
						{
							CLog::Print( LOG_ERROR, "%s userId(%s) != userName(%s)", __FUNCTION__, clsRequest.m_strMsgUserName.c_str(), pclsArg->m_pclsAgent->m_strUserName.c_str() );
						}
						else if( clsRequest.m_iMsgAuthEngineBoots == 0 && clsRequest.m_strMsgAuthParams.empty() )
						{
							CSnmpMessage * pclsResponse = clsRequest.CreateResponse();
							if( pclsResponse )
							{
								time_t iTime;

								time( &iTime );

								static uint8_t szEngineId[] = { 0x80, 0x00, 0x00, 0x04, 0x80, 0x00 };

								pclsResponse->m_cMsgFlags = 0;
								pclsResponse->m_strMsgAuthEngineId.append( (char *)szEngineId, sizeof(szEngineId) );
								pclsResponse->m_strContextEngineId = pclsResponse->m_strMsgAuthEngineId;
								pclsResponse->m_iMsgMaxSize = SNMP_MAX_PACKET_SIZE;
								pclsResponse->m_iMsgAuthEngineBoots = (uint32_t)iTime;
								pclsResponse->m_iMsgAuthEngineTime = pclsResponse->m_iMsgAuthEngineBoots;
								pclsResponse->m_cCommand = SNMP_CMD_REPORT;

								pclsResponse->AddOidValueCounter( "1.3.6.1.6.3.15.1.1.4.0", 10 );

								iSendLen = pclsResponse->MakePacket( szSendPacket, sizeof(szSendPacket) );
								if( iSendLen != -1 )
								{
									TcpSend( pclsArg->m_hSocket, szSendPacket, iSendLen );
								}

								delete pclsResponse;
							}

							continue;
						}
						else
						{
							clsRequest.m_strUserId = pclsArg->m_pclsAgent->m_strUserName;
							clsRequest.m_strAuthPassWord = pclsArg->m_pclsAgent->m_strAuthPassWord;
							clsRequest.m_strPrivPassWord = pclsArg->m_pclsAgent->m_strPrivPassWord;

							if( clsRequest.m_strEncryptedPdu.empty() == false )
							{
								clsRequest.ParseEncryptedPdu( );
							}

							if( clsRequest.CheckAuth() == false )
							{
								CLog::Print( LOG_ERROR, "%s CheckAuth error", __FUNCTION__ );
							}
							else
							{
								bCallCallBack = true;
							}
						}
					}
					else if( strcmp( clsRequest.m_strCommunity.c_str(), pclsArg->m_pclsAgent->m_strCommunity.c_str() ) )
					{
						CLog::Print( LOG_ERROR, "%s community(%s) != setup community(%s)", __FUNCTION__, clsRequest.m_strCommunity.c_str(), pclsArg->m_pclsAgent->m_strCommunity.c_str() );
					}
					else
					{
						bCallCallBack = true;
					}

					if( bCallCallBack && pclsArg->m_pclsAgent->m_pclsCallBack )
					{
						if( clsRequest.m_cCommand == SNMP_CMD_TRAP )
						{
							pclsArg->m_pclsAgent->m_pclsCallBack->RecvTrap( &clsRequest );
						}
						else
						{
							bool bRes = false;
							CSnmpMessage * pclsResponse = clsRequest.CreateResponse();
							if( pclsResponse )
							{
								if( pclsArg->m_pclsAgent->m_pclsCallBack->RecvRequest( &clsRequest, pclsResponse ) )
								{
									if( pclsArg->m_pclsAgent->m_strUserName.empty() == false )
									{
										pclsResponse->m_strMsgUserName = pclsArg->m_pclsAgent->m_strUserName;
										pclsResponse->m_strAuthPassWord = pclsArg->m_pclsAgent->m_strAuthPassWord;
										pclsResponse->m_strPrivPassWord = pclsArg->m_pclsAgent->m_strPrivPassWord;

										if( clsRequest.m_cMsgFlags & SNMP_MSG_FLAG_ENCRYPT )
										{
											pclsResponse->SetPrivParams( );
										}

										if( clsRequest.m_cMsgFlags & SNMP_MSG_FLAG_AUTH )
										{
											pclsResponse->SetAuthParams( );
										}
									}

									iSendLen = pclsResponse->MakePacket( szSendPacket, sizeof(szSendPacket) );
									if( iSendLen == -1 )
									{
										TcpSend( pclsArg->m_hSocket, szSendPacket, iSendLen );
									}

									bRes = true;
								}

								delete pclsResponse;

								if( bRes == false ) break;
							}
						}

						clsRequest.Clear();
					}
				}

				if( iPacketLen > iWantRecvLen )
				{
					memmove( szPacket, szPacket + iWantRecvLen, iPacketLen - iWantRecvLen );
					iPacketLen = iPacketLen - iWantRecvLen;
				}
				else
				{
					iPacketLen = 0;
				}

				iWantRecvLen = 0;
			}
		}
	}

	closesocket( pclsArg->m_hSocket );
	CLog::Print( LOG_INFO, "SnmpTcpAgentClientThread is terminated - client(%s:%d)", pclsArg->m_strIp.c_str(), pclsArg->m_iPort );

	delete pclsArg;

	return 0;
}
/**
 * @ingroup SnmpStack
 * @brief SNMP 요청 메시지를 전송한 후, 이에 대한 응답 메시지를 수신한다.
 * @param pclsRequest		SNMP 요청 메시지
 * @param pclsResponse	SNMP 응답 메시지
 * @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
 */
bool CSnmpSession::SendRecv( CSnmpMessage * pclsRequest, CSnmpMessage * pclsResponse )
{
	int iSendLen, iRecvLen = 0, n;
	bool	bRes = false;

	if( m_hSocket == INVALID_SOCKET )
	{
		if( Open() == false ) return false;
	}

	if( m_bTcp )
	{
		char szSend[SNMP_MAX_PACKET_SIZE], szRecv[SNMP_MAX_PACKET_SIZE];
		int iWantRecvLen = 0;

		iSendLen = pclsRequest->MakePacket( szSend, sizeof(szSend) );
		if( iSendLen == -1 )
		{
			CLog::Print( LOG_ERROR, "%s MakePacket error", __FUNCTION__ );
			return false;
		}

		if( TcpSend( m_hSocket, szSend, iSendLen ) == false )
		{
			CLog::Print( LOG_ERROR, "%s TcpSend error", __FUNCTION__ );
			return false;
		}

		while( 1 )
		{
			n = TcpRecv( m_hSocket, szRecv + iRecvLen, sizeof(szRecv) - iRecvLen, m_iMiliTimeout / 1000 ); 
			if( n <= 0 )
			{
				CLog::Print( LOG_ERROR, "%s recv error(%d)", __FUNCTION__, GetError() );
				return false;
			}

			iRecvLen += n;
			if( iWantRecvLen <= 0 )
			{
				iWantRecvLen = pclsResponse->GetPacketLen( szRecv, iRecvLen );
			}

			if( iWantRecvLen > 0 )
			{
				if( iRecvLen == iWantRecvLen ) break;
			}
		}

		if( pclsResponse->ParsePacket( szRecv, iRecvLen ) > 0 )
		{
			if( m_bDebug )
			{
				LogPacket( szRecv, iRecvLen );
			}

			if( pclsRequest->m_iRequestId == pclsResponse->m_iRequestId ||
					( pclsRequest->m_iMsgId > 0 && pclsRequest->m_iMsgId == pclsResponse->m_iMsgId ) )
			{
				bRes = true;
			}
		}
	}
	else
	{
		char szSend[SNMP_MAX_PACKET_SIZE], szRecv[SNMP_MAX_PACKET_SIZE];
		struct pollfd arrPoll[1];
		uint32_t	iIp;
		uint16_t	sPort;

		iSendLen = pclsRequest->MakePacket( szSend, sizeof(szSend) );
		if( iSendLen == -1 )
		{
			CLog::Print( LOG_ERROR, "%s MakePacket error", __FUNCTION__ );
			return false;
		}

		for( int iSend = 0; iSend <= m_iReSendCount; ++iSend )
		{
			if( UdpSend( m_hSocket, szSend, iSendLen, m_iIp, m_sPort ) == false )
			{
				CLog::Print( LOG_ERROR, "%s UdpSend error", __FUNCTION__ );
				return false;
			}

			TcpSetPollIn( arrPoll[0], m_hSocket );

POLL_START:
			n = poll( arrPoll, 1, m_iMiliTimeout );
			if( n > 0 )
			{
				iRecvLen = sizeof(szRecv);
				if( UdpRecv( m_hSocket, szRecv, &iRecvLen, &iIp, &sPort ) )
				{
					if( pclsResponse->ParsePacket( szRecv, iRecvLen ) > 0 )
					{
						if( m_bDebug )
						{
							LogPacket( szRecv, iRecvLen );
						}

						if( pclsRequest->m_iRequestId == pclsResponse->m_iRequestId ||
								( pclsRequest->m_iMsgId > 0 && pclsRequest->m_iMsgId == pclsResponse->m_iMsgId ) )
						{
							bRes = true;
							break;
						}
					}

					// 원하는 응답이 수신되지 않으면 다시 수신 대기로 진입한다.
					goto POLL_START;
				}
			}
		}
	}

	return bRes;
}
示例#12
0
		void HttpServer::HttpSendBuffer(const Buffer& buffer)
		{
			TcpSend(buffer);
		}
示例#13
0
		void HttpServer::HttpSendHeader(string header_string)
		{
			Buffer buffer(header_string.c_str(), header_string.length());
			TcpSend(buffer);
		}