| 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;
}
|