Unix Programming - Obtaining packet timestamps on kernel level

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2007 > Obtaining packet timestamps on kernel level





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Obtaining packet timestamps on kernel level
jdthebigj@gmail.com

2007-04-13, 1: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;
}

Maxim Yegorushkin

2007-04-16, 1: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.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com