Home > OS >  Get time stamp from cmsg can be NULL?
Get time stamp from cmsg can be NULL?

Time:07-05

I looking at this code https://elixir.bootlin.com/linux/v4.6.7/source/Documentation/networking/timestamping/timestamping.c#L181

This code try to print the timestamp of packet using struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg);

If this is UDP packet. Is there any way that CMSG_DATA(cmsg) will return NULL?

CodePudding user response:

If you look at the definition of CMSG_DATA you see

#define CMSG_DATA(cmsg) ((void *)((char *)(cmsg)   CMSG_ALIGN(sizeof(struct cmsghdr))))

This adds an offset to cmsg. Unless you manage to feed a cmsg into that macro that evaluates to NULL after adding that offset, the result can never be come NULL.

  • Related