Obtaining packet timestamps on kernel level
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Obtaining packet timestamps on kernel level




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Obtaining packet timestamps on kernel level  
jdthebigj@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-13-07 06:21 PM

Can anyone demonstrate how to obtain kernel level timestamps of
packets by using the SO_TIMESTAMP option and/or the ioctl SIOCGSTAMP
option. I am desperately trying to get my code to work but I'm not
able to find enough guidance to complete my task. I'm pasting my
incomplete code. It will be a great help if someone can help me fix
it. I'm using SUSE 10.2. Forgive the poor coding style. Thanks

JD

 ________________________________________
_______________________

/*
** client.c -- a stream socket client demo
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
//#include <sys/time.h>
#include <time.h>
#define PORT 3490 // the port client will be connecting to

#define MAXDATASIZE 100 // max number of bytes we can get at once

int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[30];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address
information
struct msghdr msg;
struct cmsghdr *cmsg;
int on =1;
struct timeval tval;
struct timeval *tv=NULL;
struct timeval now;
time_t ts;
char *time;
char *buffer;
struct tm *tm_time;
unsigned char ctlbuf[128];
struct sockaddr_in address;
struct iovec iov;

//msg.msg_name = &address;
//msg.msg_namelen = sizeof(struct sockaddr_in);
//msg.msg_iov = &iov;
//msg.msg_iovlen = MAX_DATA_SIZE;
msg.msg_control = &ctlbuf;
msg.msg_controllen = sizeof(ctlbuf);
msg.msg_flags = 0;


if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}

if ((he=gethostbyname(argv[1])) == NULL) {  // get the host
info
perror("gethostbyname");
exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");

}

their_addr.sin_family = AF_INET;    // host byte order
their_addr.sin_port = htons(PORT);  // short, network byte
order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);

memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of
the struct

if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}


if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) != 0) {
perror("setsockopt");
return -1;
}

if (setsockopt(sockfd, SOL_SOCKET, SO_TIMESTAMP, &on,
sizeof(on)) != 0) {
perror("setsockopt");
return -1;
}

if ((numbytes=recvmsg(sockfd,&msg,0 )) == -1) {
perror("recvmsg");
exit(1);

}

for (cmsg = CMSG_FIRSTHDR(&msg); cmsg!=NULL ; cmsg =
CMSG_NXTHDR(&msg,cmsg))
{
printf("------------------Entering the for looop ------------\n
\n");
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type
== SO_TIMESTAMP) {
/* Copy to avoid alignment problems: */
memcpy(&now, CMSG_DATA(cmsg), sizeof(now));
tv= &now;
//              tv  = (struct timeval *) CMSG_DATA(cmsg);
ts = tv->tv_sec*1000000;
ts +=tv->tv_usec;
printf("Seconds: %ld Microseconds: %ld  \n
\n",tv->tv_sec, tv->tv_usec);
}

}
if (cmsg == NULL && ioctl(sockfd,SIOCGSTAMP,&tv)==-1)
perror("ioctl(SIOCGSTAMP)");
ts = tv->tv_sec*1000000;
ts +=tv->tv_usec;

printf("-----------------Output of gettimeofday()------------\n
\n");

gettimeofday(&tval,NULL);
strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&tval.tv_sec));
printf("%s%ld\n",buffer,tval.tv_usec);



printf("------------------Output of SO_TIMESTAMP/
SIOCGSTAMP------------\n\n");

time = ctime(&ts);
printf("Received: %s at time: %d = %s",&msg,ts,time);

close(sockfd);

return 0;
}






[ Post a follow-up to this message ]



    Re: Obtaining packet timestamps on kernel level  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-16-07 06:21 PM

On Apr 13, 4:58 pm, jdtheb...@gmail.com wrote:
> Can anyone demonstrate how to obtain kernel level timestamps of
> packets by using the SO_TIMESTAMP option and/or the ioctl SIOCGSTAMP
> option. I am desperately trying to get my code to work but I'm not
> able to find enough guidance to complete my task. I'm pasting my
> incomplete code. It will be a great help if someone can help me fix
> it. I'm using SUSE 10.2. Forgive the poor coding style. Thanks

A timestamp gets attached as a control message (also called ancillary
data). http://www.linux-m32r.org/lxr/http/...et/sock.h#L1307

Please see man cmsg(3) for how to handle it.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:03 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register