LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2011, 09:00 AM   #1
begroup
LQ Newbie
 
Registered: Jan 2011
Posts: 6

Rep: Reputation: 0
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!
 
Old 01-08-2011, 10:43 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 01-10-2011, 02:31 AM   #3
begroup
LQ Newbie
 
Registered: Jan 2011
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 01-10-2011, 02:44 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.

Last edited by dwhitney67; 01-10-2011 at 02:48 AM.
 
Old 01-17-2011, 04:53 AM   #5
begroup
LQ Newbie
 
Registered: Jan 2011
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 01-17-2011, 08:16 AM   #6
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by begroup View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can't extract file in a tar packet with timestamp error Richard.Yang Linux - Software 2 07-06-2009 03:01 AM
dmesg Invalid packet / INPUT packet died flood H_TeXMeX_H Slackware 5 11-12-2007 02:52 PM
A packet filter using libipq which uses ether type field to capture the packet can26_manish Programming 2 10-16-2007 05:35 AM
Change access.log timestamp,squid proxy gordonb Linux - Software 1 04-20-2006 12:28 AM
access IP header in packet linux_lover2005 Programming 3 04-11-2005 01:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration