Getting TCP timestamp

395 views
Skip to first unread message

g.katsarov

unread,
Jan 16, 2015, 6:39:05 PM1/16/15
to lib...@googlegroups.com
Hello, 

I want to get the timestamp of a tcp packet, I had a look in the tcp.cpp file but my limited programming knowledge I am not able to get it right. I would be very grateful if somebody could help me whit this, thank you in advance.


Matias Fontanini

unread,
Jan 16, 2015, 6:53:17 PM1/16/15
to lib...@googlegroups.com
You can get the timestamp from a Packet object. Something like:

Sniffer sniffer = ...;
while (Packet pkt = sniffer.next_packet()) {
    Timestamp ts = pkt.timestamp();
}

See the documentation in case you are in doubt:

g.katsarov

unread,
Jan 17, 2015, 5:47:43 AM1/17/15
to lib...@googlegroups.com
Thank you Matias, I will try it on Monday because I am writing it in the University.
Message has been deleted

g.katsarov

unread,
Jan 19, 2015, 8:57:52 AM1/19/15
to lib...@googlegroups.com
I am trying to do it for Sniffer::sniff_loop in the callback function. That seems to work but haven't checked it properly yet.
         
            while (Packet pkt = pdu.rfind_pdu<RawPDU>()) {
                Timestamp ts = pkt.timestamp();
                std::cout << "Ts: " << pkt.timestamp().seconds()<< std::endl;

            }

On Saturday, 17 January 2015 00:53:17 UTC+1, Matias Fontanini wrote:

Matias Fontanini

unread,
Jan 19, 2015, 12:13:06 PM1/19/15
to lib...@googlegroups.com
You can't get a timestamp on the sniff_loop callback function. If you want a timestamp, you need to use Sniffer::next_packet.

g.katsarov

unread,
Jan 19, 2015, 3:54:41 PM1/19/15
to lib...@googlegroups.com
It is giving some possible output but I haven't checked it properly, so I guess you are right. So what can I do? This is basically the structure:

#include "Psnifferlibtins.h"

using namespace Tins;
using namespace std;

int CBHandler::sniffer(char* device)
{
Sniffer sniffer(device);
sniffer.sniff_loop(std::bind(
&CBHandler::sniffer_callback,
            this,
            std::placeholders::_1)
);
return 0;
}

bool CBHandler::sniffer_callback(const PDU &pdu)
{
        const RawPDU &raw = pdu.rfind_pdu<RawPDU>();      
const IP &ip = pdu.rfind_pdu<IP>();
const TCP &tcp = pdu.rfind_pdu<TCP>();

        //a lot of things that are not important here

return true;
}

Matias Fontanini

unread,
Jan 19, 2015, 4:22:01 PM1/19/15
to lib...@googlegroups.com
Just translate it so you use Packet:


int CBHandler::sniffer(char* device)
{
Sniffer sniffer(device);
while (Packet pkt = sniffer.next_packet()) {
             sniffer_callback(pkt);
        }
return 0;
}

bool CBHandler::sniffer_callback(const Packet &pkt)
{
     const auto timestamp = pkt.timestamp();
     const PDU& pdu = *pkt.packet();
     const RawPDU &raw = pdu.rfind_pdu<RawPDU>();
// same as before

g.katsarov

unread,
Jan 19, 2015, 6:54:51 PM1/19/15
to lib...@googlegroups.com
But in this case do I need the CBHandler struct(that's why I am doing the bind with the placeholders like one of your examples), I used in order to keep the values of some of the variables that I didn't want to get changed with every packet entering the loop. Or now with the next_packet I won't have this problem. There are few examples with the next_packet and I really don't understand, I am sorry. 

Matias Fontanini

unread,
Jan 19, 2015, 6:58:51 PM1/19/15
to lib...@googlegroups.com
That code is the exact equivalent of your previous code, except now you have the timestamp.

g.katsarov

unread,
Jan 19, 2015, 7:07:46 PM1/19/15
to lib...@googlegroups.com
Thank you for the prompt response, I will think on it and try it tomorrow in the university because it is 01:07 AM in Germany. 
Message has been deleted

g.katsarov

unread,
Jan 20, 2015, 9:57:25 AM1/20/15
to lib...@googlegroups.com
Shouldn't it be const PDU& pdu = *pkt.pdu(); instead of const PDU& pdu = *pkt.packet(); in order to get the pdu?

On Tuesday, 20 January 2015 00:58:51 UTC+1, Matias Fontanini wrote:

g.katsarov

unread,
Jan 20, 2015, 11:01:30 AM1/20/15
to lib...@googlegroups.com
If i have const PDU& pdu = *pkt.packet(); I get the following errors:
'const class Tins::Packet' has no member named 'packet'
Method 'packet' could not be resolved

If I have const PDU& pdu = *pkt.Packet(); I get the following error:
error: invalid use of 'Tins::Packet::Packet'

If I have const PDU& pdu = *pkt.pdu(); I get the following output:
terminate called after throwing an instance of 'Tins::pdu_not_found'
  what():  PDU not found
Aborted (core dumped)

Matias Fontanini

unread,
Jan 20, 2015, 11:05:38 AM1/20/15
to g.katsarov, lib...@googlegroups.com

It is the last choice, pdu(). You get that exception since you lookeed for a PDU which is not in that packet. Catch the exception and go on

--
You received this message because you are subscribed to the Google Groups "libtins" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libtins+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

g.katsarov

unread,
Jan 22, 2015, 9:37:37 AM1/22/15
to lib...@googlegroups.com, georgi.a...@gmail.com
Ok, so my question is how to declare "raw" out of the try function because afterwards it is not recognized and the only lame solution I am capable of is to put the rest in the try function.

    const auto timestamp = pkt.timestamp().seconds();                                        
    cout << "ts: " << timestamp;

    const PDU &pdu = *pkt.pdu();

    try
    {

        const RawPDU &raw = pdu.rfind_pdu<RawPDU>();
    }
    catch (Tins::pdu_not_found e)
    {
        cout << "An exception occurred. Exception Nr.'\n'";
        return true;

Matias Fontanini

unread,
Jan 22, 2015, 1:40:06 PM1/22/15
to lib...@googlegroups.com, georgi.a...@gmail.com
Although I'd like to help you, this question falls outside of the scope of the library, it's more like a basic programming question. Please use other resources to solve this, like stackoverflow, etc.

g.katsarov

unread,
Jan 22, 2015, 2:00:51 PM1/22/15
to lib...@googlegroups.com, georgi.a...@gmail.com
Ok, thank you, I just don't have enough knowledge in C++.

Georgi Katsarov

unread,
Jan 29, 2015, 5:27:45 PM1/29/15
to lib...@googlegroups.com
Everything is working perfectly, thank you for all your assistance.
Reply all
Reply to author
Forward
0 new messages