Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pacp - Send Data
Forum Updated to NodeBB v4.3 + New Features

Pacp - Send Data

Scheduled Pinned Locked Moved Unsolved General and Desktop
pcapc++ qt
12 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neda
    wrote on 13 Jul 2020, 07:58 last edited by neda
    #1

    Hi,
    I use "pcap".
    I wanna send and receive data.
    But I have an error while sending the packet and "pcap_sendpacket" function return "-1".

    myproject.pro

    INCLUDEPATH += D:/WpdPack/Include
    LIBS += -L"D:/WpdPack/Lib" -lwpcap -lPacket
    code_text
    

    main.cpp

    QThread thread;
        thread.setObjectName("PCAPNetworkThread");
        mySerialPort.moveToThread(&thread);
        QObject::connect(&thread, SIGNAL(started()), &pcapNetwork, SLOT(openLanPort()));
        thread.start();
    

    PCAPNetwork.cs

    void PCAPNetwork::openLanPort()
    {
        pcap_if_t *alldevs;
        pcap_if_t *d;
        int inum;
        int i=0;    
        char errbuf[PCAP_ERRBUF_SIZE];
        if (pcap_findalldevs( &alldevs, errbuf) == -1)
        {
            //Error in pcap_findalldevs
            return;
        }
    
        /* Print the list */
        for(d=alldevs; d; d=d->next)
        {
    //print name and description        
        }
    
        if(i==0)
        {
            //No interfaces found! Make sure WinPcap is installed
            return;
        }
    
        //Enter the interface number
        inum=1;//I do not know how can I select adapter with mac address
    
        if(inum < 1 || inum > i)
        {
            //Interface number out of range
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return;
        }
    
        /* Jump to the selected adapter */
        for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
        /* Open the device */
        if ( (adhandle= pcap_open_live(d->name,     
                                       65536, 
                                       1,              
                                       1000,  
                                       NULL  
                                       ) ) == NULL)
        {
            //Unable to open the adapter.
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return;
        }
        //listening
        pcap_freealldevs(alldevs);
         
        commandData();
    
        pcap_loop(adhandle, 0, packet_handler, NULL);
    }
    void PCAPNetwork::commandData(){   
        QByteArray data;
        data[0]=commands->COMMANDS_DATA;
        sendData(data);
    }
    void PCAPNetwork::sendData(const QByteArray &data)
    {
    const u_char* p;
    std::memcpy(&p,data.constData(),data.size());
    if(pcap_sendpacket(adhandle, 
                           p, 
                           sizeof(p)
                           ) != 0 )
        {
            //Error sending the packet
    qDebug()<<"Error: "<<pcap_geterr( adhandle );
            return;
        }
    
    }
    
    J 1 Reply Last reply 13 Jul 2020, 08:05
    0
    • N neda
      13 Jul 2020, 07:58

      Hi,
      I use "pcap".
      I wanna send and receive data.
      But I have an error while sending the packet and "pcap_sendpacket" function return "-1".

      myproject.pro

      INCLUDEPATH += D:/WpdPack/Include
      LIBS += -L"D:/WpdPack/Lib" -lwpcap -lPacket
      code_text
      

      main.cpp

      QThread thread;
          thread.setObjectName("PCAPNetworkThread");
          mySerialPort.moveToThread(&thread);
          QObject::connect(&thread, SIGNAL(started()), &pcapNetwork, SLOT(openLanPort()));
          thread.start();
      

      PCAPNetwork.cs

      void PCAPNetwork::openLanPort()
      {
          pcap_if_t *alldevs;
          pcap_if_t *d;
          int inum;
          int i=0;    
          char errbuf[PCAP_ERRBUF_SIZE];
          if (pcap_findalldevs( &alldevs, errbuf) == -1)
          {
              //Error in pcap_findalldevs
              return;
          }
      
          /* Print the list */
          for(d=alldevs; d; d=d->next)
          {
      //print name and description        
          }
      
          if(i==0)
          {
              //No interfaces found! Make sure WinPcap is installed
              return;
          }
      
          //Enter the interface number
          inum=1;//I do not know how can I select adapter with mac address
      
          if(inum < 1 || inum > i)
          {
              //Interface number out of range
              /* Free the device list */
              pcap_freealldevs(alldevs);
              return;
          }
      
          /* Jump to the selected adapter */
          for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
      
          /* Open the device */
          if ( (adhandle= pcap_open_live(d->name,     
                                         65536, 
                                         1,              
                                         1000,  
                                         NULL  
                                         ) ) == NULL)
          {
              //Unable to open the adapter.
              /* Free the device list */
              pcap_freealldevs(alldevs);
              return;
          }
          //listening
          pcap_freealldevs(alldevs);
           
          commandData();
      
          pcap_loop(adhandle, 0, packet_handler, NULL);
      }
      void PCAPNetwork::commandData(){   
          QByteArray data;
          data[0]=commands->COMMANDS_DATA;
          sendData(data);
      }
      void PCAPNetwork::sendData(const QByteArray &data)
      {
      const u_char* p;
      std::memcpy(&p,data.constData(),data.size());
      if(pcap_sendpacket(adhandle, 
                             p, 
                             sizeof(p)
                             ) != 0 )
          {
              //Error sending the packet
      qDebug()<<"Error: "<<pcap_geterr( adhandle );
              return;
          }
      
      }
      
      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 13 Jul 2020, 08:05 last edited by
      #2

      @neda said in Pacp - Send Data:

      fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adhandle ));

      What does this line print out?
      And I'm not sure what this has to do with Qt.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply 13 Jul 2020, 08:11
      1
      • J jsulm
        13 Jul 2020, 08:05

        @neda said in Pacp - Send Data:

        fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adhandle ));

        What does this line print out?
        And I'm not sure what this has to do with Qt.

        N Offline
        N Offline
        neda
        wrote on 13 Jul 2020, 08:11 last edited by neda
        #3

        @jsulm said in Pacp - Send Data:

        What does this line print out?

        PacketSendPacket failed

        And I'm not sure what this has to do with Qt.

        I did not find a document or a good sample to use "pcap" in Qt.

        mrjjM 1 Reply Last reply 13 Jul 2020, 15:34
        0
        • N neda
          13 Jul 2020, 08:11

          @jsulm said in Pacp - Send Data:

          What does this line print out?

          PacketSendPacket failed

          And I'm not sure what this has to do with Qt.

          I did not find a document or a good sample to use "pcap" in Qt.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 13 Jul 2020, 15:34 last edited by
          #4

          @neda
          Hi
          And you did install winpcap/Win10Pcap so the driver is running?
          Also, you are using visual studio compiler ?
          Im not sure it works with mingw.

          N 1 Reply Last reply 14 Jul 2020, 05:08
          0
          • mrjjM mrjj
            13 Jul 2020, 15:34

            @neda
            Hi
            And you did install winpcap/Win10Pcap so the driver is running?
            Also, you are using visual studio compiler ?
            Im not sure it works with mingw.

            N Offline
            N Offline
            neda
            wrote on 14 Jul 2020, 05:08 last edited by neda
            #5

            @mrjj said in Pacp - Send Data:

            Also, you are using visual studio compiler ?

            Qt 5.12.5 MSVC2017 32bit

            And you did install winpcap/Win10Pcap so the driver is running?

            I installed "http://www.win10pcap.org/download/Win10Pcap-v10.2-5002.msi".

            Now "pcap_sendpacket" return 0, It's OK.
            But I do not have a correct answer from the device.

            I have a "QByteArray" Data for send such as:

            "\x02\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
            

            I don't know this code is true or not.

            void PCAPNetwork::sendData(const QByteArray &data)
            {
            const u_char* p= reinterpret_cast<const uchar *>(data.constData());
            //std::memcpy(&p,data.constData(),data.size());
            pcap_sendpacket(adhandle, 
                                                    p, 
                                                    sizeof(p)
                                                    );
            
            mrjjM 1 Reply Last reply 14 Jul 2020, 05:43
            0
            • N neda
              14 Jul 2020, 05:08

              @mrjj said in Pacp - Send Data:

              Also, you are using visual studio compiler ?

              Qt 5.12.5 MSVC2017 32bit

              And you did install winpcap/Win10Pcap so the driver is running?

              I installed "http://www.win10pcap.org/download/Win10Pcap-v10.2-5002.msi".

              Now "pcap_sendpacket" return 0, It's OK.
              But I do not have a correct answer from the device.

              I have a "QByteArray" Data for send such as:

              "\x02\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
              

              I don't know this code is true or not.

              void PCAPNetwork::sendData(const QByteArray &data)
              {
              const u_char* p= reinterpret_cast<const uchar *>(data.constData());
              //std::memcpy(&p,data.constData(),data.size());
              pcap_sendpacket(adhandle, 
                                                      p, 
                                                      sizeof(p)
                                                      );
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 14 Jul 2020, 05:43 last edited by
              #6

              @neda
              Hi
              from a quick glance - it looks ok.
              Should not truncate values.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                neda
                wrote on 14 Jul 2020, 10:21 last edited by neda
                #7

                I think I was wrong in reading data.
                The device sends a lot of data in real-time.
                I read them by this code.

                void PCAPNetwork::packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
                {
                

                How can I convert "pkt_data" to decimal?

                mrjjM 1 Reply Last reply 14 Jul 2020, 10:23
                0
                • N neda
                  14 Jul 2020, 10:21

                  I think I was wrong in reading data.
                  The device sends a lot of data in real-time.
                  I read them by this code.

                  void PCAPNetwork::packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
                  {
                  

                  How can I convert "pkt_data" to decimal?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 14 Jul 2020, 10:23 last edited by
                  #8

                  @neda said in Pacp - Send Data:

                  u_char

                  But is this not a byte already ?

                  If you place cursor on u_char and press F2, what is it then defined as?

                  N 1 Reply Last reply 14 Jul 2020, 10:46
                  0
                  • mrjjM mrjj
                    14 Jul 2020, 10:23

                    @neda said in Pacp - Send Data:

                    u_char

                    But is this not a byte already ?

                    If you place cursor on u_char and press F2, what is it then defined as?

                    N Offline
                    N Offline
                    neda
                    wrote on 14 Jul 2020, 10:46 last edited by
                    #9

                    @mrjj

                    u_char = unsigned char

                    qDebug()<<"data: "<<pkt_data;
                    

                    output:

                    data:  0x46c16bc
                    

                    I don't know how can I convert it to decimal.

                    mrjjM 1 Reply Last reply 14 Jul 2020, 10:52
                    0
                    • N neda
                      14 Jul 2020, 10:46

                      @mrjj

                      u_char = unsigned char

                      qDebug()<<"data: "<<pkt_data;
                      

                      output:

                      data:  0x46c16bc
                      

                      I don't know how can I convert it to decimal.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 14 Jul 2020, 10:52 last edited by mrjj
                      #10

                      @neda
                      Is it zero terminated ?

                      That is the address it writes out. Not the actual data i think.

                      You can convert it to bytearray
                      QByteArray databuf = QByteArray(reinterpret_cast<char*>(pkt_data), header->len);
                      (i think but not 100% sure header->len is data len. check docs)

                      N 1 Reply Last reply 15 Jul 2020, 04:22
                      0
                      • mrjjM mrjj
                        14 Jul 2020, 10:52

                        @neda
                        Is it zero terminated ?

                        That is the address it writes out. Not the actual data i think.

                        You can convert it to bytearray
                        QByteArray databuf = QByteArray(reinterpret_cast<char*>(pkt_data), header->len);
                        (i think but not 100% sure header->len is data len. check docs)

                        N Offline
                        N Offline
                        neda
                        wrote on 15 Jul 2020, 04:22 last edited by
                        #11

                        @mrjj said in Pacp - Send Data:

                        Not the actual data i think.

                        You right.
                        The device sends a lot of data in real-time.
                        How can I read the actual data?

                        mrjjM 1 Reply Last reply 15 Jul 2020, 07:05
                        0
                        • N neda
                          15 Jul 2020, 04:22

                          @mrjj said in Pacp - Send Data:

                          Not the actual data i think.

                          You right.
                          The device sends a lot of data in real-time.
                          How can I read the actual data?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 15 Jul 2020, 07:05 last edited by
                          #12

                          @neda
                          The data is in pkt_data as far as i can see from docs
                          so try
                          QByteArray databuf = QByteArray(reinterpret_cast<char*>(pkt_data), header->len);
                          qDebug()<<"data: "<<databuf ;

                          1 Reply Last reply
                          3

                          1/12

                          13 Jul 2020, 07:58

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved