News:

We really need your input in this questionnaire

Main Menu

problem with development

Started by ophelie6989, November 17, 2009, 06:52:50 PM

Previous topic - Next topic

ophelie6989

Hi,
i try to develop a monitoring in order to retrieve informations about agents. I have installed netxms agent on a computer and i try to communicate with him with an other. the communication between the two works but i have a problem for sending and receiving informations. I have looked at the code already developed by netxms and i tried to send and receive the same type of informations that the agents understand. i have done two files:  One with the code and the other with the include that i need.

Here my file in c++:
/*--Include--*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "./include/CSCPMessage.h"


int main () {
   
   /*--création de la socket grâce à la fonction socket()--*/
   //socketId est un entier qui correspond à un descripteur de socket
   int socketId = socket(AF_INET,SOCK_STREAM,0);
   
   /*--Définition de la structure utilisée avec TCP/IP afin de pouvoir contacter l'hôte--*/
   struct sockaddr_in serveur;//adresse hôte à contacter
   serveur.sin_family = AF_INET;//Protocole internet
   serveur.sin_port = htons(4700);// Port d'écoute
   serveur.sin_addr.s_addr=inet_addr("192.168.0.142"); // inet_adrr permet de spécifier une IP spécifique à utiliser
   
   
   /*--Etablissement de la connexion avec le serveur--*/
   int taille = sizeof(serveur);
   int connexion = connect(socketId, (struct sockaddr *) &serveur,taille);
   // Gestion de l'erreur de la connexion
   if (connexion ==-1)
   {
      printf("Un probleme est survenu lors de la connexion au serveur");
   }
   
   
   
   /*--Envoi du message // Sending--*/
   CSCPMessage msg;//classe
    int dwRqId;
    int i=1;
    dwRqId = i++;
    msg.SetCode(0x0067);//classe
    msg.SetId(dwRqId);//classe
    CSCP_MESSAGE  pRawMsg;//structure
    pRawMsg = msg.CreateMessage();//createMesage fonction de la classe
   
   int message;
   message=send(socketId,&pRawMsg,sizeof(msg),0);// Ecriture du message msg dans la socket ayant pour descripteur socketId
   //Gestion de l'erreur de l'envoi
   if (message == -1)
   {
      printf("Un probleme est survenu lors de l'envoi du message au serveur");
   }
     
   printf("Message envoye par le manager : %s \n",&pRawMsg);
   printf("Message envoye par le manager : %s \n",&msg);
   
    /*--Reception du message // Reception--*/
   int retour;
   CSCP_MESSAGE msg2;
    CSCPMessage msg3;
     
    retour=recv(socketId,&msg2,sizeof(msg2),0);// Lecture du message renvoyé par le serveur dans la socketayant pour descripteur socketId
   
    //Gestion de l'erreur de la reception
   if (retour == -1)
   {
      printf("Un probleme est survenu lors de la reception du message du serveur");
   } 
   
   //Affichage
    printf("Message envoye par l agent : %s \n",&msg2);
    int r=msg2.wCode;
    printf("affichage du code du message structure: %d \n",&r);
    msg3.SetCode(msg2.wCode);
    printf("Message envoye par l agent : %s \n",&msg3);
    int t=msg3.GetCode()  ;
    printf("affichage du code du message classe : %d \n",&t);
   
   
   
   

    /*--Fermeture de la socket--*/
   close(socketId);

   return 0;
}// fin du main








Here my file in .h

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifndef _CSCPMessage_h_
#define _CSCPMessage_h_

#define VID_RCC                     ((float)28)    /* RCC == Request Completion Code */
#define CMD_REQUEST_COMPLETED       0x001D
#define CMD_KEEPALIVE               0x0003

/*--Structure CSCP_DF--*/
typedef struct
{
   int dwVarId;       // Variable identifier
   char  bType;         // Data type
   char  bPadding;      // Padding
   int wInt16;
   union
   {
      int dwInteger;
      int qwInt64;
      double dFloat;
      struct
      {
         int dwLen;
         int szValue[1];
      } string;
   } data;
} CSCP_DF;


/*--Structure CSCP_MESSAGE--*/
typedef struct
{
   int wCode;       // Message (command) code
   int wFlags;      // Message flags
   int dwSize;     // Message size (including header) in bytes
   float dwId;       // Unique message identifier
   float dwNumVars;  // Number of variables in message
   
} CSCP_MESSAGE;

/*--Classe CSCPMessage--*/
class  CSCPMessage
{
private:
   int m_wCode;
   int m_wFlags;
   float m_dwId;
   float m_dwNumVar;    // Number of variables
   CSCP_DF **m_ppVarList;   // List of variables
   int m_nVersion;      // Protocol version

 
public:

   CSCP_MESSAGE CreateMessage(void){
                CSCP_MESSAGE msg;
                msg.wCode= m_wCode;
                return msg;
               
                };
   int GetCode(void) { return m_wCode; }
   void SetCode(int wCode) { m_wCode = wCode; }

  // DWORD GetId(void) { return m_dwId; }
   void SetId(float dwId) { m_dwId = dwId; }

   float GetVariableLong(float dwVarId);

};



class  MsgWaitQueue
{
private:
 

   void *WaitForMessageInternal(int wIsBinary, int wCode, float dwId, float dwTimeOut);
   

public:
   MsgWaitQueue();
   ~MsgWaitQueue();

   
   CSCPMessage *WaitForMessage(int wCode, int dwId, int dwTimeOut)
   {
      return (CSCPMessage *)WaitForMessageInternal(0, wCode, dwId, dwTimeOut);
   }
   
};

#endif   /* _CSCPMessage_h_ */





When i compile, i have that message:

# ./client3.e
Message envoye par le manager : g
Message envoye par le manager : g
Message envoye par l agent : g
affichage du code du message structure: -809671536
Message envoye par l agent : g
affichage du code du message classe : -809671540


The sending didn t work. can you help me, i am on it for 5 days and i don t know how to do it.

Thank you very much

Victor Kirhenshtein

Hello!

I see the following errors:

1. when you sending message to the agent, you give the wrong message size - instead of


send(socketId,&pRawMsg,sizeof(msg),0)


you should use


send(socketId,&pRawMsg,ntohl(pRawMsg->dwSize),0);


2. You use very small receive buffer - sizeof(CSCP_MESSAGE) will be something like 24 to 32 bytes, but incoming message could be much larger. You should use at least 1024 bytes buffer for receiving messages with single parameter's value and much larger if you wish to get lists from agent. We typically use buffers of 256KB. Other option is to receive message header first, check message size, and allocate buffer for received message dynamically.

3. Protocol messages use binary representation of data, so debugging with printf("%s") will not help much. You should print individual fields.

4. For header - there are many errors on different levels. I suggest you to use same declarations as in nms_cscp.h, especially for CSCP_MESSAGE and CSCP_DF structures, because they represents actual data positions in transmission stream. Don't forget to use #pragma pack or you will get incorrect results on some platforms or compilers.


And why you not using already written communication code in libnetxms and libnxsrv? libnxsrv contains ready to use C++ API for communicating with agents, which handles all communication and protocol things and gives you high level API.

Best regards,
Victor


ophelie6989

Thank you, i am going to look at libnetxms. For the header, i had changed types of variables because i wanted to try so;ething easy in order to see if it works. I am going to try like you said.
Thank you very much.

ophelie6989

Hi,
I have another questions , in order to test just the sending, i have created a file named client3.cpp which enable to connect with the agent.I also added in the fonction that is in the file Agent and which enable to send a message to the agent;
   /*--Sending--*/
   CSCPMessage *pMsg;
   CSCP_MESSAGE *pRawMsg;
   BOOL bResult;

   pRawMsg = pMsg->CreateMessage();
   bResult = (SendEx(m_hSocket, (char *)pRawMsg, ntohl(pRawMsg->dwSize), 0) == (int)ntohl(pRawMsg->dwSize));
   free(pRawMsg);
   return bResult;


In order to test. I create also a header where i have included the class and structure that i needed.



#ifndef _CSCPMessage_h_
#define _CSCPMessage_h_

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "nms_common.h"
//#include "netxms-regex.h"
#define VID_RCC                     ((float)28)    /* RCC == Request Completion Code */
#define CMD_REQUEST_COMPLETED       0x001D
#define CMD_KEEPALIVE               0x0003

typedef struct
{
   DWORD dwBufSize;
   DWORD dwBufPos;
   char szBuffer[10000000];
} CSCP_BUFFER;


/*--Structure CSCP_DF--*/
typedef struct
{
   DWORD dwVarId;       // Variable identifier
   BYTE  bType;         // Data type
   BYTE  bPadding;      // Padding
   WORD wInt16;
   union
   {
      DWORD dwInteger;
      //QWORD qwInt64;
      double dFloat;
      struct
      {
         DWORD dwLen;
         WORD szValue[1];
      } string;
   } data;
} CSCP_DF;


/*--Structure CSCP_MESSAGE--*/
typedef struct
{
   WORD wCode;       // Message (command) code
   WORD wFlags;      // Message flags
   DWORD dwSize;     // Message size (including header) in bytes
   DWORD dwId;       // Unique message identifier
   DWORD dwNumVars;  // Number of variables in message
   CSCP_DF df[1];    // Data fields
} CSCP_MESSAGE;

/*--Classe CSCPMessage--*/
class LIBNETXMS_EXPORTABLE CSCPMessage
{
private:
   WORD m_wCode;
   WORD m_wFlags;
   DWORD m_dwId;
   DWORD m_dwNumVar;    // Number of variables
   CSCP_DF **m_ppVarList;   // List of variables
   int m_nVersion;      // Protocol version

   void *Set(DWORD dwVarId, BYTE bType, const void *pValue, DWORD dwSize = 0);
   void *Get(DWORD dwVarId, BYTE bType);
   DWORD FindVariable(DWORD dwVarId);
public:
   CSCPMessage(int nVersion = NXCP_VERSION);
   CSCPMessage(CSCPMessage *pMsg);
   CSCPMessage(CSCP_MESSAGE *pMsg, int nVersion = NXCP_VERSION);
   CSCPMessage(const char *xml);
   ~CSCPMessage();

   CSCP_MESSAGE *CreateMessage(void);
   char *CreateXML(void);
   void ProcessXMLToken(void *state, const char **attrs);
   void ProcessXMLData(void *state);

   WORD GetCode(void) { return m_wCode; }
   void SetCode(WORD wCode) { m_wCode = wCode; }

   DWORD GetId(void) { return m_dwId; }
   void SetId(DWORD dwId) { m_dwId = dwId; }

   BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
   BOOL IsEndOfSequence(void) { return (m_wFlags & MF_END_OF_SEQUENCE) ? TRUE : FALSE; }
   BOOL IsReverseOrder(void) { return (m_wFlags & MF_REVERSE_ORDER) ? TRUE : FALSE; }

   void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
   void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
   void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
   void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
   void SetVariable(DWORD dwVarId, const TCHAR *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
   void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
   void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
   BOOL SetVariableFromFile(DWORD dwVarId, const TCHAR *pszFileName);

   DWORD GetVariableLong(DWORD dwVarId);
   QWORD GetVariableInt64(DWORD dwVarId);
   WORD GetVariableShort(DWORD dwVarId);
   LONG GetVariableShortAsInt32(DWORD dwVarId);
   double GetVariableDouble(DWORD dwVarId);
   TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = NULL, DWORD dwBufSize = 0);
   DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
   DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);

   void DeleteAllVariables(void);

   void DisableEncryption(void) { m_wFlags |= MF_DONT_ENCRYPT; }
   void SetEndOfSequence(void) { m_wFlags |= MF_END_OF_SEQUENCE; }
   void SetReverseOrderFlag(void) { m_wFlags |= MF_REVERSE_ORDER; }
   
};
//
// Message waiting queue class
//

class LIBNETXMS_EXPORTABLE MsgWaitQueue
{
private:
   MUTEX m_mutexDataAccess;
   CONDITION m_condStop;
   CONDITION m_condNewMsg;
   DWORD m_dwMsgHoldTime;
   DWORD m_dwNumElements;
   WAIT_QUEUE_ELEMENT *m_pElements;
   THREAD m_hHkThread;

   void Lock(void) { MutexLock(m_mutexDataAccess, INFINITE); }
   void Unlock(void) { MutexUnlock(m_mutexDataAccess); }
   void HousekeeperThread(void);
   void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
   
   static THREAD_RESULT THREAD_CALL MWQThreadStarter(void *);

public:
   MsgWaitQueue();
   ~MsgWaitQueue();

   void Put(CSCPMessage *pMsg);
   void Put(CSCP_MESSAGE *pMsg);
   CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
   {
      return (CSCPMessage *)WaitForMessageInternal(0, wCode, dwId, dwTimeOut);
   }
   CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
   {
      return (CSCP_MESSAGE *)WaitForMessageInternal(1, wCode, dwId, dwTimeOut);
   }
   
   void Clear(void);
   void SetHoldTime(DWORD dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
};


#endif   /* _CSCPMessage_h_ */


I include in it the link to "nms_common.h" for the definition of the type DWORD, word ETC.

In my makefile, i have integrated the file message.c
When i compile, it didn t work.



g++ client3.cpp -c -I /usr/local/include -I ./include -o client3.o
In file included from client3.cpp:11:
include/CSCPMessage.h:59: error: syntax error before `{' token
include/CSCPMessage.h:71: error: syntax error before `public'
In file included from client3.cpp:11:
include/CSCPMessage.h:4:1: unterminated #ifndef
*** Error code 1


There is a problem with the class CSCPMessage. Do i have forget a header to include?
I also wanted to know what is LIBNETXMS_EXPORTABLE?
I searched in the header nms util but i dont understand the using of it.

Sorry to disturb you :-\
Thank you

Victor Kirhenshtein

This define is required for porting code to Windows. On Windows, if you want to export function or class from DLL, you should add __declspec(dllexport) to it's declaration. So, on Windows we define LIBNETXMS_EXPORTABLE as __declspec(dllexport) when building DLL, and on other platforms it simply defines to nothing. So, if you are not writing your code for Windows or not writing a Windows DLL you can simply remove this or define to nothing.

Best regards,
Victor

ophelie6989

Thank you, i have changed my code in consequences. I have done a sending and a receiving from the agent. I have again a question. I would like to know how i can test and display the message that i received from the agent by the fonction RecvCSCPMessage?
Thank you very much

Victor Kirhenshtein

Hi!

You should create CSCPMessage object from CSCP_MESSAGE structure filled by RecvNXCPMessage function. After that, you can access to variables inside message via GetVariableXxx methods of class CSCPMessage. If you getting parameter's value from agent, you should be interested in variables VID_RCC - DWORD holding response code, and VID_VALUE - received value as string.

Best regards,
Victor

ophelie6989

#7
Thank you, but i dont understand the difference between RecvCSCPMessage() and RecvNXCPMessage()? I have used the fonction

BOOL AgentConnection::SendMessage(CSCPMessage *pMsg)
{
  CSCP_MESSAGE *pRawMsg;
  BOOL bResult;

  pRawMsg = pMsg->CreateMessage();
  bResult = (SendEx(m_hSocket, (char *)pRawMsg, ntohl(pRawMsg->dwSize), 0) == (int)ntohl(pRawMsg->dwSize));
  free(pRawMsg);
  return bResult;
}

to send a message to the agent and RecvCSCPMessage to receive a message from an agent. I have just seen the fonction Send NXCPMessage.
Which fonction do i have to use to send and receive a message?

Thank you very much

Victor Kirhenshtein

CSCP is an old name for NXCP, and it's partially replaces in the code. In latest versions, there are no RecvCSCPMessage() function, only RecvNXCPMessage() - but it's the same function anyway.

Best regards,
Victor

ophelie6989

ok thank you. i have seen that RecvNXCPMessage take in parameters a CSCP_ENCRYPTION_CONTEXT. That means that the message that i receive from the agent is crypted and that i have to decrypted it?
Thank you

Victor Kirhenshtein

Yes, it could be encrypted, and in that case you should provide correct encryption context. However, if you are not using encryption, you can simple pass NULL.

Best regards,
Victor

ophelie6989

Thank you, i am going to use the fonction SendFileOverNXCP to send a message to the agent and RecvNXCPMessage to receive a message from the agent.

ophelie6989

#12
In fact i am not sure that the fonction SendFileOverNXCP is used to send a message to the agent.


  • How can i do to send a message to the agent and to receive it? I have used RecvNXCPMessage but i dont arrive to see the message sent by the agent.
  • What are the steps for the sending and the reception of message?


Thank you very much

Victor Kirhenshtein

In general, steps are following:

1. create CSCPMessage object
2. fill it with necessary variables and set correct command code
3. create raw message (CSCP_MESSAGE) vith CSCPMessage::CreateMessage method
4. send raw message to agent as usual
5. receive response using RecvNXCPMessage function
6. create CSCPMessage object from received raw message
7. read variables from message object

Best regards,
Victor

ophelie6989

#14
Thank you, it was what i had done. I have send to the Agent the code 0x0097 in order to get the server informations. But which variable did i have to read from the message object for having the information about the server?

Thank you very much