1

My code consists of two programs: a TCP server and a TCP client. The goal of the project is to get timestamping for TCP working. I consulted this piece of linux documentation, and I can't seem to find anything that would indicate that my code shouldn't work. It says SO_TIMESTAMPING works with stream sockets. I'm really lost here. Or am I misunderstanding how this should work? I have never worked with linux and never done any networking, so there might be an obvious error on my part, but I don't see it.

  • client.cpp:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h> 
#include <linux/errqueue.h>
#include <linux/net_tstamp.h>

int port = 8989;
const char *address = "127.0.0.1";

int main(int argc, char *argv[])
{
    int sockfd = 0, n = 0;
    char recvBuff[1024];
    struct sockaddr_in serv_addr; 

    memset(recvBuff, '0',sizeof(recvBuff));
    if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
        fprintf(stderr, "\n Error : Could not create socket \n");
        return 1;
    }

    // Enable timestamping:
    int timestampingFlags = SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_SOFTWARE;
    int optRet = setsockopt(sockfd, SOL_SOCKET, SO_TIMESTAMPING, &timestampingFlags, sizeof(timestampingFlags));
    if(optRet < 0)
    {
        printf("Unable to set socket option for timestamping");
    } // OK

    memset(&serv_addr, '0', sizeof(serv_addr)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port); 

    if(inet_pton(AF_INET, address, &serv_addr.sin_addr)<=0)
    {
        fprintf(stderr, "\n inet_pton error occured\n");
        return 1;
    } 

    if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       fprintf(stderr, "\n Error : Connect Failed \n");
       perror(0);
       return 1;
    } 

    // Receive responses
    while ((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
    {
        recvBuff[n] = 0;
        if(fputs(recvBuff, stdout) == EOF)
        {
            fprintf(stderr, "\n Error : Fputs error\n");
        }

        // Get and print the time stamp
        struct msghdr msgh;
        struct cmsghdr *cmsg;
        struct scm_timestamping *timeStamp;

        int flags = MSG_WAITALL | MSG_PEEK;
        int recvRet = recvmsg(sockfd, &msgh, flags);
        /* Receive auxiliary data in msgh */

        // There are no messages here
        for(cmsg = CMSG_FIRSTHDR(&msgh);
            cmsg != NULL;
            cmsg = CMSG_NXTHDR(&msgh, cmsg))
        {
            printf("A control message arrived!\n");
            if (cmsg->cmsg_level == SOL_SOCKET &&
                cmsg->cmsg_type == SCM_TIMESTAMPING)
            {
                timeStamp = (struct scm_timestamping *)CMSG_DATA(cmsg);
                printf("Timestamp received: %ld.09%ld\n", timeStamp->ts[0].tv_sec, timeStamp->ts[0].tv_nsec);
                break;
            }
        }
    }

    if(n < 0)
    {
        fprintf(stderr, "\n Read error \n");
    }
    return 0;
}

-server.cpp:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 
#include <signal.h>

// call this function to start a nanosecond-resolution timer
struct timespec timer_start()
{
    struct timespec start_time;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
    return start_time;
}

// call this function to end a timer, returning microseconds elapsed as a long
long timer_end(struct timespec start_time)
{
    struct timespec end_time;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
    long diffInNanos = (end_time.tv_sec - start_time.tv_sec) * (long)1e9 + (end_time.tv_nsec - start_time.tv_nsec);
    return diffInNanos / 1000;
}

int port = 8989;

int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr; 

    char sendBuff[1025];
    time_t ticks; 

    listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(port);

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    printf("Now listening for a connection!\n");
    listen(listenfd, 1);     
    
    // Wait for a connection from a client socket
    connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
    printf("Connected!\n");

    // Once connection is established, start sending messagess in a regular time interval
    
    long timeBetweenSendsUS = 1000*1000;
    for(struct timespec startTime = timer_start();
        true;
        startTime = timer_start())
    {
        memset(sendBuff, '0', sizeof(sendBuff));
        ticks = time(NULL);
        snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));

        long elapsedUS = timer_end(startTime);
        usleep(timeBetweenSendsUS - elapsedUS);
        printf("Sending message!\n");
        write(connfd, sendBuff, strlen(sendBuff));
    }

    close(connfd);
    return 0;
}

I then compile each file separately using g++ <filename> -o <filename> and run the server binary first and the client binary second while the server is running. So, to repeat my question: Why are there no control messages in the ancillary data?

wojtat
  • 11
  • 1
  • What specifically does "There are no messages here mean"? What is the value of `recvRet`? – Useless Aug 20 '20 at 16:58
  • Oh, it's -1. And perror() says "Invalid argument". I guess I still don't see where my mistake is. It might be that the sockfd is the invalid argument, is my guess. – wojtat Aug 20 '20 at 17:31

0 Answers0