LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to access packet timestamp (https://www.linuxquestions.org/questions/programming-9/how-to-access-packet-timestamp-854999/)

begroup 01-08-2011 09:00 AM

How to access packet timestamp
 
hello
I want to access the timestamp field of the packet being sent or received.
I am not getting clear idea as to which ioctl i should use, and how it should be used in the program.
Also it would be very helpful if somebody could explain rough flow of the program for accessing the timestamp.

Waiting for the help.

Thank u in advace!
:)

dwhitney67 01-08-2011 10:43 AM

Which time stamp are you referring to? One that is sent via a client (or server) application to a peer? Or the one that may be embedded in an ICMP formatted message? I'm not aware of a time stamp field available in either the IP header, or the TCP or UDP headers.

begroup 01-10-2011 02:31 AM

i want to retrieve the time when the packet is actually sent or received and that too in nanoseconds. Latest kernel 2.6.32.2 have a function ktime_get_real() which returns system time in nanoseconds. it is declared in ktime.h and defined in timekeeping.c We tried to call this funtion in our C program , but it gave errors. I am attaching the program....
please help...ld


#include<stdio.h>
#include<time.h>
#include<linux/ktime.h>
int main()
{
ktime_t tim;
tim=ktime_get_real();
return 0;
}

Is this a compatibility problem? Does linux kernel 2.6.32.2 supports nanosecond time? If not which kernel version supports time in ns format?
How should we proceed?

eagerly waiting for help.

Thank u.

dwhitney67 01-10-2011 02:44 AM

You can get the current system time (epoch) down to the nanosecond (if your system supports this), using the real-time clock:
Code:

/*
 *  Compile/Link this code using:  gcc -Wall nanotime.c -lrt
 */

#include <time.h>
#include <stdio.h>

int main(void)
{
  struct timespec resolution;
  struct timespec theTime;

  if (clock_getres(CLOCK_REALTIME, &resolution) == 0)
  {
      /* nanosecond resolution is available if resolution.tv_nsec == 1 */

      if (clock_gettime(CLOCK_REALTIME, &theTime) == 0)
      {
        printf("%ld, %ld\n", theTime.tv_sec, theTime.tv_nsec);
      }
  }

  return 0;
}

P.S. See the header of the code for instructions on how to compile/link it.

begroup 01-17-2011 04:53 AM

Packet timestamp
 
Hello
What i want to do is to timestamp the frame and send it. I tried using libpcap's pcap_sendpacket() function but it gave error. When libpcap captures packet, it constructs packet header and stores timestamp in the ts field of the packet header. SO is there any other library similar to libpcap which can timestamp and send the packet? If not then how should i get the accurate time at which the packet was sent.
Can i use libnet for sending the packets? and does libnet timestamps the outgoing packets ?If not then what is the solution?
I want to all the programming at the data link layer.

Eagerly waiting for the reply.

orgcandman 01-17-2011 08:16 AM

Quote:

Originally Posted by begroup (Post 4227126)
Hello
What i want to do is to timestamp the frame and send it. I tried using libpcap's pcap_sendpacket() function but it gave error. When libpcap captures packet, it constructs packet header and stores timestamp in the ts field of the packet header. SO is there any other library similar to libpcap which can timestamp and send the packet? If not then how should i get the accurate time at which the packet was sent.
Can i use libnet for sending the packets? and does libnet timestamps the outgoing packets ?If not then what is the solution?
I want to all the programming at the data link layer.

Eagerly waiting for the reply.

There's no way to accurately do what you want to do - there's no place (in many protocols) for a timestamp to be inserted. That's intentional, btw, since you don't know how timezone or latency or anything else, for that matter, will impact that measurement. And, frankly, most protocols don't care.

If you want to timestamp your packets, you'll have to come up with a clever way of doing so. For instance, if you're building the packets from the ground up (ie: you're building the IP header + message body) you might look into using a proprietary IP option to include the timestamp. Or, if you're writing your own IP-based protocol, you might look into adding a timestamp field. If this is on the local network, you might just use the arrival stamps to track the packet, and take a stamp just before you call send() (admittedly, all of these may not be terribly accurate).

In the end, there's no good way of doing what you want to do, afaik. Someone may know of a wrapper protocol (maybe something like IPIP or GRE) which contains timestamp information that you might implement. I'm not sure, though.


All times are GMT -5. The time now is 01:12 PM.