Results 1 to 9 of 9

Thread: Data Structure help!

  1. #1
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Data Structure help!

    Hello all,

    I have a thread that calls a pcap loop function which is then called from a class to add items to a table.

    Thread class:

    Qt Code:
    1. void PcapThread::startCaptureOnInterface()
    2. {
    3. int r;
    4.  
    5. while(captureOn){
    6. struct pcap_pkthdr *header;
    7. const u_char *pkt_data;
    8.  
    9. r = pcap_next_ex(descr,&header,&pkt_data);
    10.  
    11. emit addToWidget(header,pkt_data);
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    PacketTable class:

    Qt Code:
    1. void PacketTable::addToWidget(struct pcap_pkthdr* pkthdr, const u_char* packet)
    To copy to clipboard, switch view to plain text mode 

    so far this method adds items to a table such as source IP, destination IP, packet length etc but there is no storage.

    However i would like to store this in a data structure by using const u_char *packet as this is how the packet headers are declared:

    Qt Code:
    1. struct ether_header *ethernet = (struct ether_header*)packet;
    2. struct iphdr *ip = (struct iphdr*)(packet + sizeof(struct ether_header));
    3. struct tcphdr *tcp = (struct tcphdr*)(packet + sizeof(struct ether_header) + sizeof(struct iphdr));
    4. payload = (u_char *)(packet + sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct tcphdr));
    To copy to clipboard, switch view to plain text mode 

    Im not sure how to go about this as i would to declare methods in a seperate class then iterate and call the methods to get data out.

    Darshan

  2. #2
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: Data Structure help!

    update:

    I have created a vector and added an object of class type Packet which contains the following:

    Qt Code:
    1. Packet::Packet(const u_char *packet)
    2. {
    3.  
    4. ethernet = (struct ether_header*)packet;
    5. ip = (struct iphdr*)(packet + sizeof(struct ether_header));
    6.  
    7. }
    8.  
    9.  
    10. QString Packet::getSourceIp()
    11. {
    12. struct in_addr saddr;
    13.  
    14. saddr.s_addr = ip->saddr;
    15. char *sourceip = inet_ntoa(saddr);
    16.  
    17. return QString::fromUtf8(sourceip);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    and in the Packet Table class i have added vector which adds the Packet class with the constructor const u_char *packet. So for every packet a new Packet object is created and added to the vector. Im not sure if this i right please correct me if its wrong

    Qt Code:
    1. void PacketTable::addToWidget(struct pcap_pkthdr* pkthdr, const u_char* packet)
    2. {
    3. vector.append(new Packet(packet));
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QVectorIterator<Packet*> i(vector);
    2. while (i.hasNext()){
    3. Packet *temp = i.next();
    4. QString s = temp->getSourceIp();
    5. qDebug() << s;
    To copy to clipboard, switch view to plain text mode 

    however when i iterate over the vector to get the sourceip out of each element i only get the ip address from the last packet not the others.

    Can anyone please help i don't know whats wrong

    Darshan

  3. #3
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: Data Structure help!

    anyone?

  4. #4
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: Data Structure help!

    Please anyone this is really urgent! I can't seem to figure out what the problem is or how to solve it.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Data Structure help!

    You suffer from a classical C problem of storing pointers.

    You assign something to some area of memory. Then you take the address of that area and store it somewhere. Then you assign some other data to the same area of memory and store a pointer to it somewhere else. Then suddenly all previously stored pointers point to the data stored the most recently. Why? Because all pointers point to the same area of memory! Check out values of those pointers (by casting them to int) - they will all be exactly the same!

    At some point you need to copy the data and store it somewhere. So far you are only copying pointers to data. I suggest you use QByteArray to store your packets. You can use it just like char * (i.e. using constDat() and data() methods) and it will take care of copying the data when you copy the byte array object.

  6. #6
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: Data Structure help!

    is there a way to convert the u_char for the QByteArray to be able to read the data?

    i have tried this QByteArray b = QByteArray((const char*)packet)
    but will need to convert it back to u_char to be able to read the data.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Data Structure help!

    If I understand correctly u_char is the same as "unsigned char" which in turn can safely be cast to char back and forth. Does that answer your question?

  8. #8
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: Data Structure help!

    i have tried this but the output i get consists of random numbers when getting the ip address

    QByteArray b;
    b.append((const char*)pkt);

    const u_char* packet = (unsigned char*)b.data(); is this the correct way to do it?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Data Structure help!

    It depends on the context, but if some conditions are met, the code looks ok.

Similar Threads

  1. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 23:46
  2. Replies: 4
    Last Post: 19th October 2007, 20:47
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 13:53
  4. Replies: 16
    Last Post: 7th March 2006, 16:57
  5. Replies: 1
    Last Post: 1st March 2006, 12:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.