Reputation: 2398
I would like to retrieve the reception timestamp for UDP packets received using boost asio.
I found out that the kernel provides the socket option SO_TIMESTAMP which should allow the generation of a timestamp when the packets is received by the NIC. I also found this old ticket which was proposing a patch to add support for SO_TIMESTAMP.
I'm using boost 1.60.0 and I couldn't enable this option:
ip::udp::socket sock;
...
sock.set_option(ip::unicast::timestamp(true));
How can I retrieve a UDP packet reception time with boost and compute the elapsed time since reception when I receive the packet with a synchronous or an asynchronous read?
Upvotes: 2
Views: 1537
Reputation: 7990
It seems boost has not implement this option yet, you can do it with native socket if your platform supports ancillary data:
int socket = sock.native();
int opt = 1;
setsockopt( sock, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt));
then access control messages from the native socket:
int received = recvmsg(socket, &msgh, 0);
struct msghdr msgh;
struct cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
if ((cmsg->cmsg_level == SOL_SOCKET ) &&(cmsg->cmsg_type == SO_TIMESTAMP ))
// read the timestamp
}
Upvotes: 1