Obtaining packet timestamps on kernel level - Unix

This is a discussion on Obtaining packet timestamps on kernel level - Unix ; 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 ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Obtaining packet timestamps on kernel level

  1. Obtaining packet timestamps on kernel level

    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
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    //#include
    #include
    #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;
    }


  2. Re: Obtaining packet timestamps on kernel level

    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/s...t/sock.h#L1307

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


+ Reply to Thread